Class: Go::Merge::NodeWrapper
- Inherits:
-
Object
- Object
- Go::Merge::NodeWrapper
- Defined in:
- lib/go/merge/node_wrapper.rb,
sig/go/merge.rbs
Overview
Structural identity and source-range adapter for a top-level Go AST node. rubocop:disable Metrics/AbcSize, Metrics/ClassLength, Metrics/CyclomaticComplexity, Metrics/MethodLength -- Go declaration forms share one structural identity boundary
Constant Summary collapse
- DECLARATION_TYPES =
%w[ const_declaration function_declaration import_declaration method_declaration package_clause type_declaration var_declaration ].freeze
- GROUP_SPEC_TYPES =
%w[const_spec import_spec type_spec var_spec].freeze
Instance Attribute Summary collapse
-
#leading_comments ⇒ Array[untyped]
readonly
Returns the value of attribute leading_comments.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#source ⇒ String
readonly
Returns the value of attribute source.
-
#trailing_comments ⇒ Array[untyped]
readonly
Returns the value of attribute trailing_comments.
Instance Method Summary collapse
- #attach_trailing_comment!(comment) ⇒ Object
- #end_byte ⇒ Integer
- #end_line ⇒ Integer
- #grouped? ⇒ Boolean
-
#initialize(node, source:, leading_comments: []) ⇒ NodeWrapper
constructor
A new instance of NodeWrapper.
- #semantic_tree ⇒ Array[untyped]
- #signature ⇒ Array[untyped]
- #source_text ⇒ String
- #start_byte ⇒ Integer
- #start_line ⇒ Integer
Constructor Details
#initialize(node, source:, leading_comments: []) ⇒ NodeWrapper
Returns a new instance of NodeWrapper.
21 22 23 24 25 26 |
# File 'lib/go/merge/node_wrapper.rb', line 21 def initialize(node, source:, leading_comments: []) @node = node @source = source @leading_comments = leading_comments.freeze @trailing_comments = [] end |
Instance Attribute Details
#leading_comments ⇒ Array[untyped] (readonly)
Returns the value of attribute leading_comments.
19 20 21 |
# File 'lib/go/merge/node_wrapper.rb', line 19 def leading_comments @leading_comments end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
19 20 21 |
# File 'lib/go/merge/node_wrapper.rb', line 19 def node @node end |
#source ⇒ String (readonly)
Returns the value of attribute source.
19 20 21 |
# File 'lib/go/merge/node_wrapper.rb', line 19 def source @source end |
#trailing_comments ⇒ Array[untyped] (readonly)
Returns the value of attribute trailing_comments.
19 20 21 |
# File 'lib/go/merge/node_wrapper.rb', line 19 def trailing_comments @trailing_comments end |
Instance Method Details
#attach_trailing_comment!(comment) ⇒ Object
66 67 68 |
# File 'lib/go/merge/node_wrapper.rb', line 66 def attach_trailing_comment!(comment) trailing_comments << comment end |
#end_byte ⇒ Integer
70 |
# File 'lib/go/merge/node_wrapper.rb', line 70 def end_byte = trailing_comments.last&.end_byte || node.end_byte |
#end_line ⇒ Integer
72 |
# File 'lib/go/merge/node_wrapper.rb', line 72 def end_line = line_for_byte(end_byte.zero? ? 0 : end_byte - 1) |
#grouped? ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/go/merge/node_wrapper.rb', line 49 def grouped? case node.type when 'import_declaration' !find_descendant(node, ['import_spec_list']).nil? when 'type_declaration', 'const_declaration' node.children.any? { |child| child.type == '(' } when 'var_declaration' !find_descendant(node, ['var_spec_list']).nil? else false end end |
#semantic_tree ⇒ Array[untyped]
75 76 77 |
# File 'lib/go/merge/node_wrapper.rb', line 75 def semantic_tree semantic_node(node) end |
#signature ⇒ Array[untyped]
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/go/merge/node_wrapper.rb', line 28 def signature case node.type when 'package_clause' [:package, required_descendant_text(node, 'package_identifier')] when 'import_declaration' import_signature when 'function_declaration' [:function, required_direct_child_text(node, 'identifier')] when 'method_declaration' [:method, receiver_type_name, required_direct_child_text(node, 'field_identifier')] when 'type_declaration' declaration_signature(:type, 'type_spec', 'type_identifier') when 'const_declaration' declaration_signature(:const, 'const_spec', 'identifier') when 'var_declaration' declaration_signature(:var, 'var_spec', 'identifier') else raise ArgumentError, "Unsupported top-level Go node #{node.type.inspect}" end.freeze end |
#source_text ⇒ String
73 |
# File 'lib/go/merge/node_wrapper.rb', line 73 def source_text = source.byteslice(start_byte...end_byte).to_s |
#start_byte ⇒ Integer
62 63 64 |
# File 'lib/go/merge/node_wrapper.rb', line 62 def start_byte leading_comments.first&.start_byte || node.start_byte end |
#start_line ⇒ Integer
71 |
# File 'lib/go/merge/node_wrapper.rb', line 71 def start_line = line_for_byte(start_byte) |