Class: RubyLsp::Ree::ParsedDocumentBuilder

Inherits:
Object
  • Object
show all
Extended by:
ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb

Constant Summary

Constants included from ReeLspUtils

ReeLspUtils::Entry

Class 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

Class Method Details

.build_bean_document(ast) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 96

def self.build_bean_document(ast)
  document = RubyLsp::Ree::ParsedClassDocument.new(ast)
  
  document.parse_class_node
  document.parse_bean_methods

  document
end

.build_class_document(ast, package_name) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 77

def self.build_class_document(ast, package_name)
  document = RubyLsp::Ree::ParsedClassDocument.new(ast, package_name)
  
  document.parse_links_container_node
  document.parse_class_includes
  document.parse_links

  document
end

.build_dao_document(ast) ⇒ Object



105
106
107
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 105

def self.build_dao_document(ast)
  RubyLsp::Ree::ParsedDaoDocument.new(ast)
end

.build_detected_document_type(ast, package_name = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 52

def self.build_detected_document_type(ast, package_name = nil)
  if has_root_class?(ast)
    build_class_document(ast, package_name)
  elsif has_root_rspec_call?(ast)
    build_rspec_document(ast)
  else 
    nil
  end
end

.build_document(ast, type, package_name = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 35

def self.build_document(ast, type, package_name = nil)
  case type
  when :enum
    build_enum_document(ast)
  when :dao
    build_dao_document(ast)
  when :bean
    build_bean_document(ast)
  when :route
    build_route_document(ast)
  when :entity
    build_entity_document(ast)
  else
    build_detected_document_type(ast, package_name)
  end
end

.build_entity_document(ast) ⇒ Object



113
114
115
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 113

def self.build_entity_document(ast)
  RubyLsp::Ree::ParsedEntityDocument.new(ast)
end

.build_enum_document(ast) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 87

def self.build_enum_document(ast)
  document = RubyLsp::Ree::ParsedClassDocument.new(ast)
  
  document.parse_class_node
  document.parse_values

  document
end

.build_from_ast(ast, uri, type = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 21

def self.build_from_ast(ast, uri, type = nil)
  return if uri && !is_ruby_file?(uri)

  document = build_document(ast, type, package_name_from_uri(uri))
  return unless document

  document
end

.build_from_source(source, type: nil, package_name: nil) ⇒ Object



30
31
32
33
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 30

def self.build_from_source(source, type: nil, package_name: nil)
  ast = Prism.parse(source).value
  build_document(ast, type, package_name)
end

.build_from_uri(uri, type = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 11

def self.build_from_uri(uri, type = nil)
  return unless is_ruby_file?(uri)
  
  ast = Prism.parse_file(uri.path).value
  document = build_document(ast, type, package_name_from_uri(uri))
  return unless document

  document
end

.build_route_document(ast) ⇒ Object



109
110
111
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 109

def self.build_route_document(ast)
  RubyLsp::Ree::ParsedRouteDocument.new(ast)
end

.build_rspec_document(ast) ⇒ Object



70
71
72
73
74
75
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 70

def self.build_rspec_document(ast)
  document = RubyLsp::Ree::ParsedRspecDocument.new(ast)
  document.parse_links
  
  document
end

.has_root_class?(ast) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 62

def self.has_root_class?(ast)
  !!ast.statements.body.detect{ |node| node.is_a?(Prism::ClassNode) }
end

.has_root_rspec_call?(ast) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 66

def self.has_root_rspec_call?(ast)
  ast.statements.body.detect{ |node| node.is_a?(Prism::CallNode) && node.name == :describe }
end

.is_ruby_file?(uri) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_document_builder.rb', line 117

def self.is_ruby_file?(uri)
  File.extname(uri.to_s) == '.rb'
end