Class: RubyLsp::Ree::ParsedClassDocument

Inherits:
ParsedBaseDocument show all
Includes:
ReeConstants, ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb

Constant Summary

Constants included from ReeConstants

ReeConstants::CONTRACT_CALL_NODE_NAMES, ReeConstants::DAO_DSL_MODULE, ReeConstants::ERROR_DEFINITION_NAMES, ReeConstants::LINKS_CONTAINER_TYPES, ReeConstants::LINK_DSL_MODULE, ReeConstants::MAPPER_DSL_MODULE, ReeConstants::ROUTES_DSL_MODULE

Constants included from ReeLspUtils

ReeLspUtils::Entry

Instance Attribute Summary collapse

Attributes inherited from ParsedBaseDocument

#ast, #link_nodes, #package_name

Instance Method Summary collapse

Methods included from ReeLspUtils

#camelize, #find_local_file_path, #get_range_for_fn_insert, #get_ree_type, #get_uri_path, #package_name_from_spec_uri, #package_name_from_uri, #package_path_from_uri, #parameter_name, #path_from_package_folder, #signature_params_from_node, #spec_relative_file_path_from_uri, #underscore

Methods inherited from ParsedBaseDocument

#find_import_for_package, #find_link_by_usage_name, #find_link_node, #includes_linked_object?, #node_name, #set_package_name

Constructor Details

#initialize(ast, package_name = nil) ⇒ ParsedClassDocument

Returns a new instance of ParsedClassDocument.



20
21
22
23
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 20

def initialize(ast, package_name = nil)
  super
  parse_class_node    
end

Instance Attribute Details

#bean_methodsObject (readonly)

Returns the value of attribute bean_methods.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def bean_methods
  @bean_methods
end

#class_includesObject (readonly)

Returns the value of attribute class_includes.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def class_includes
  @class_includes
end

#class_nodeObject (readonly)

Returns the value of attribute class_node.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def class_node
  @class_node
end

#defined_classesObject (readonly)

Returns the value of attribute defined_classes.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def defined_classes
  @defined_classes
end

#defined_constsObject (readonly)

Returns the value of attribute defined_consts.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def defined_consts
  @defined_consts
end

#doc_instance_methodsObject (readonly)

Returns the value of attribute doc_instance_methods.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def doc_instance_methods
  @doc_instance_methods
end

#error_definition_namesObject (readonly)

Returns the value of attribute error_definition_names.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def error_definition_names
  @error_definition_names
end

#error_definitionsObject (readonly)

Returns the value of attribute error_definitions.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def error_definitions
  @error_definitions
end

Returns the value of attribute links_container_block_node.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def links_container_block_node
  @links_container_block_node
end

Returns the value of attribute links_container_node.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def links_container_node
  @links_container_node
end

#valuesObject (readonly)

Returns the value of attribute values.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 15

def values
  @values
end

Instance Method Details

#allows_root_links?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 29

def allows_root_links?
  false
end

#class_nameObject



201
202
203
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 201

def class_name
  class_node.constant_path.name.to_s
end


61
62
63
64
65
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 61

def find_link_with_imported_object(name)
  @link_nodes.detect do |link_node|
    link_node.imports.include?(name)
  end
end

#full_class_nameObject



209
210
211
212
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 209

def full_class_name
  name_parts = [class_node.constant_path&.parent&.name, class_node.constant_path.name]
  name_parts.compact.map(&:to_s).join('::')
end

Returns:

  • (Boolean)


67
68
69
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 67

def has_blank_links_container?
  links_container_node && !@links_container_block_node
end

#has_body?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 33

def has_body?
  class_node && class_node.body && class_node.body.body
end

#has_root_class?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 25

def has_root_class?
  true
end

#imported_constantsObject



218
219
220
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 218

def imported_constants
  @link_nodes.map(&:imports).flatten.map(&:to_sym)
end

#includes_dao_dsl?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 41

def includes_dao_dsl?
  @class_includes.any?{ node_name(_1) == DAO_DSL_MODULE }
end

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 37

def includes_link_dsl?
  @class_includes.any?{ node_name(_1) == LINK_DSL_MODULE }
end

#includes_linked_constant?(const_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 57

def includes_linked_constant?(const_name)
  @link_nodes.map(&:imports).flatten.include?(const_name)
end

#includes_mapper_dsl?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 49

def includes_mapper_dsl?
  @class_includes.any?{ node_name(_1) == MAPPER_DSL_MODULE }
end

#includes_ree_dsl?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 53

def includes_ree_dsl?
  ree_dsls.size > 0
end

#includes_routes_dsl?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 45

def includes_routes_dsl?
  @class_includes.any?{ node_name(_1) == ROUTES_DSL_MODULE }
end


214
215
216
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 214

def links_container_node_name
  links_container_node.arguments.arguments.first.unescaped
end

#module_nameObject



205
206
207
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 205

def module_name
  class_node.constant_path&.parent&.name.to_s
end

#parse_bean_methodsObject



132
133
134
135
136
137
138
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 132

def parse_bean_methods
  return unless has_body?

  @bean_methods ||= class_node.body.body
    .select{ _1.is_a?(Prism::DefNode) }
    .map{ OpenStruct.new(name: node_name(_1).to_s, signatures: parse_signatures_from_params(_1.parameters)) }
end

#parse_class_includesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 83

def parse_class_includes
  return @class_includes if @class_includes
  @class_includes = []
  return @class_includes unless has_body?

  @class_includes = class_node.body.body.select{ node_name(_1) == :include }.map do |class_include|
    first_arg = class_include.arguments.arguments.first

    include_name = case first_arg
    when Prism::ConstantPathNode
      parent_name = class_include.arguments.arguments.first.parent.name.to_s
      module_name = class_include.arguments.arguments.first.name

      [parent_name, module_name].compact.join('::')
    when Prism::ConstantReadNode
      first_arg.name.to_s
    else
      ''
    end
  
    OpenStruct.new(
      name: include_name, location: class_include.location
    )          
  end
end

#parse_class_nodeObject



71
72
73
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 71

def parse_class_node
  @class_node ||= ast.statements.body.detect{ |node| node.is_a?(Prism::ClassNode) }
end

#parse_const_objectsObject



197
198
199
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 197

def parse_const_objects
  RubyLsp::Ree::ConstObjectsParser.new(self).class_const_objects
end

#parse_defined_classesObject



175
176
177
178
179
180
181
182
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 175

def parse_defined_classes
  @defined_classes = []
  return unless has_body?

  @defined_classes = class_node.body.body
    .select{ _1.is_a?(Prism::ClassNode) }
    .map(&:name)
end

#parse_defined_constsObject



184
185
186
187
188
189
190
191
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 184

def parse_defined_consts
  @defined_consts = []
  return unless has_body?

  @defined_consts += class_node.body.body
    .select{ _1.is_a?(Prism::ConstantWriteNode) }
    .map(&:name)
end

#parse_error_definitionsObject



165
166
167
168
169
170
171
172
173
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 165

def parse_error_definitions
  return unless has_body?

  @error_definitions = class_node.body.body
    .select{ _1.is_a?(Prism::ConstantWriteNode) }
    .select{ ERROR_DEFINITION_NAMES.include?(node_name(_1.value)) }

  @error_definition_names = @error_definitions.map(&:name)
end

#parse_instance_methodsObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 140

def parse_instance_methods
  return if @doc_instance_methods
  @doc_instance_methods = []

  current_contract_node = nil
  class_node.body.body.each do |node|
    if node.is_a?(Prism::CallNode) && CONTRACT_CALL_NODE_NAMES.include?(node_name(node))
      current_contract_node = node
    else
      if node.is_a?(Prism::DefNode)
        @doc_instance_methods << RubyLsp::Ree::ParsedMethodNode.new(node, current_contract_node)
      end

      current_contract_node = nil
    end
  end

  @doc_instance_methods
end


109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 109

def parse_links
  @link_nodes = []
  return [] unless has_body?

  links_container_body = if links_container_node && @links_container_block_node && @links_container_block_node.body
    @links_container_block_node.body.body 
  elsif class_includes.any?{ node_name(_1) == LINK_DSL_MODULE }
    class_node.body.body
  else
    return []
  end

  @link_nodes = RubyLsp::Ree::LinksParser.new(links_container_body, package_name).parse_links
end


75
76
77
78
79
80
81
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 75

def parse_links_container_node
  return unless has_body?

  @links_container_node ||= class_node.body.body.detect{ |node| LINKS_CONTAINER_TYPES.include?(node_name(node)) }
  @links_container_node_type = node_name(@links_container_node)
  @links_container_block_node ||= @links_container_node&.block
end

#parse_method_callsObject



193
194
195
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 193

def parse_method_calls
  RubyLsp::Ree::CallObjectsParser.new(self).class_call_objects
end

#parse_signatures_from_params(parameters) ⇒ Object



160
161
162
163
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 160

def parse_signatures_from_params(parameters)
  signature_params = signature_params_from_node(parameters)
  [RubyIndexer::Entry::Signature.new(signature_params)]
end

#parse_valuesObject



124
125
126
127
128
129
130
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 124

def parse_values
  return unless has_body?
  
  @values ||= class_node.body.body
    .select{ node_name(_1) == :val }
    .map{ OpenStruct.new(name: _1.arguments.arguments.first.unescaped) }
end

#ree_dslsObject



222
223
224
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_class_document.rb', line 222

def ree_dsls
  @class_includes.select{ node_name(_1).downcase.match?(/ree/) && node_name(_1).downcase.match?(/dsl/)}
end