Class: Rust::Merge::NodeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rust/merge/node_wrapper.rb,
sig/rust/merge.rbs

Overview

Structural identity and source-range adapter for one top-level Rust item. rubocop:disable Metrics/AbcSize, Metrics/ClassLength, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity -- Rust item forms share one structural identity boundary

Constant Summary collapse

DECLARATION_TYPES =

Returns:

  • (Array[String])
%w[
  const_item
  enum_item
  expression_statement
  extern_crate_declaration
  function_item
  impl_item
  macro_definition
  mod_item
  static_item
  struct_item
  trait_item
  type_item
  union_item
  use_declaration
].freeze
NAMED_ITEMS =

Returns:

  • (Hash[String, [Symbol, Array[String]]])
{
  'const_item' => [:const, %w[identifier]],
  'enum_item' => [:enum, %w[type_identifier]],
  'function_item' => [:function, %w[identifier]],
  'macro_definition' => [:macro, %w[identifier]],
  'mod_item' => [:module, %w[identifier]],
  'static_item' => [:static, %w[identifier]],
  'struct_item' => [:struct, %w[type_identifier]],
  'trait_item' => [:trait, %w[type_identifier]],
  'type_item' => [:type, %w[type_identifier]],
  'union_item' => [:union, %w[type_identifier]]
}.freeze
COMMENT_TYPES =

Returns:

  • (Array[String])
%w[block_comment line_comment].freeze
ATTRIBUTE_TYPES =

Returns:

  • (Array[String])
%w[attribute_item].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, source:, leading_comments: []) ⇒ NodeWrapper

Returns a new instance of NodeWrapper.

Parameters:

  • node (Object)
  • source: (String)
  • leading_comments: (Array[untyped]) (defaults to: [])


41
42
43
44
45
46
# File 'lib/rust/merge/node_wrapper.rb', line 41

def initialize(node, source:, leading_comments: [])
  @node = node
  @source = source
  @leading_comments = leading_comments.freeze
  @trailing_comments = []
end

Instance Attribute Details

#leading_commentsArray[untyped] (readonly)

Returns the value of attribute leading_comments.

Returns:

  • (Array[untyped])


39
40
41
# File 'lib/rust/merge/node_wrapper.rb', line 39

def leading_comments
  @leading_comments
end

#nodeObject (readonly)

Returns the value of attribute node.

Returns:

  • (Object)


39
40
41
# File 'lib/rust/merge/node_wrapper.rb', line 39

def node
  @node
end

#sourceString (readonly)

Returns the value of attribute source.

Returns:

  • (String)


39
40
41
# File 'lib/rust/merge/node_wrapper.rb', line 39

def source
  @source
end

#trailing_commentsArray[untyped] (readonly)

Returns the value of attribute trailing_comments.

Returns:

  • (Array[untyped])


39
40
41
# File 'lib/rust/merge/node_wrapper.rb', line 39

def trailing_comments
  @trailing_comments
end

Instance Method Details

#attach_trailing_comment!(comment) ⇒ Object

Parameters:

  • comment (Object)

Returns:

  • (Object)


71
# File 'lib/rust/merge/node_wrapper.rb', line 71

def attach_trailing_comment!(comment) = trailing_comments << comment

#compound?Boolean

Returns:

  • (Boolean)


65
# File 'lib/rust/merge/node_wrapper.rb', line 65

def compound? = compound_use?

#end_byteInteger

Returns:

  • (Integer)


67
# File 'lib/rust/merge/node_wrapper.rb', line 67

def end_byte = trailing_comments.last&.end_byte || node.end_byte

#end_lineInteger

Returns:

  • (Integer)


69
# File 'lib/rust/merge/node_wrapper.rb', line 69

def end_line = line_for_byte(end_byte.zero? ? 0 : end_byte - 1)

#membership_signatureArray[untyped]

Returns:

  • (Array[untyped])


59
60
61
62
63
# File 'lib/rust/merge/node_wrapper.rb', line 59

def membership_signature
  return semantic_node(use_body) if compound_use?

  signature
end

#semantic_treeArray[untyped]

Returns:

  • (Array[untyped])


72
# File 'lib/rust/merge/node_wrapper.rb', line 72

def semantic_tree = semantic_node(node)

#signatureArray[untyped]

Returns:

  • (Array[untyped])


48
49
50
51
52
53
54
55
56
57
# File 'lib/rust/merge/node_wrapper.rb', line 48

def signature
  value = case node.type
          when 'use_declaration' then use_signature
          when 'extern_crate_declaration' then extern_crate_signature
          when 'impl_item' then impl_signature
          when 'expression_statement' then macro_invocation_signature
          else named_signature(node)
          end
  value.freeze
end

#source_textString

Returns:

  • (String)


70
# File 'lib/rust/merge/node_wrapper.rb', line 70

def source_text = source.byteslice(start_byte...end_byte).to_s

#start_byteInteger

Returns:

  • (Integer)


66
# File 'lib/rust/merge/node_wrapper.rb', line 66

def start_byte = leading_comments.first&.start_byte || node.start_byte

#start_lineInteger

Returns:

  • (Integer)


68
# File 'lib/rust/merge/node_wrapper.rb', line 68

def start_line = line_for_byte(start_byte)