Class: RubyLsp::Requests::BaseRequest

Inherits:
SyntaxTree::Visitor
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/ruby_lsp/requests/base_request.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(document, **_kwargs) ⇒ BaseRequest

Returns a new instance of BaseRequest.



18
19
20
21
22
23
24
25
26
# File 'lib/ruby_lsp/requests/base_request.rb', line 18

def initialize(document, **_kwargs)
  @document = document

  # Parsing the document here means we're taking a lazy approach by only doing it when the first feature request
  # is received by the server. This happens because {Document#parse} remembers if there are new edits to be parsed
  @document.parse

  super()
end

Instance Method Details

#full_constant_name(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_lsp/requests/base_request.rb', line 55

def full_constant_name(node)
  name = +node.constant.value
  constant = T.let(node, SyntaxTree::Node)

  while constant.is_a?(SyntaxTree::ConstPathRef)
    constant = constant.parent

    case constant
    when SyntaxTree::ConstPathRef
      name.prepend("#{constant.constant.value}::")
    when SyntaxTree::VarRef
      name.prepend("#{constant.value.value}::")
    end
  end

  name
end

#locate(node, position, node_types: []) ⇒ Object



80
81
82
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
108
109
110
111
112
# File 'lib/ruby_lsp/requests/base_request.rb', line 80

def locate(node, position, node_types: [])
  queue = T.let(node.child_nodes.compact, T::Array[T.nilable(SyntaxTree::Node)])
  closest = node

  until queue.empty?
    candidate = queue.shift

    # Skip nil child nodes
    next if candidate.nil?

    # Add the next child_nodes to the queue to be processed
    queue.concat(candidate.child_nodes)

    # Skip if the current node doesn't cover the desired position
    loc = candidate.location
    next unless (loc.start_char...loc.end_char).cover?(position)

    # If the node's start character is already past the position, then we should've found the closest node already
    break if position < loc.start_char

    # If there are node types to filter by, and the current node is not one of those types, then skip it
    next if node_types.any? && node_types.none? { |type| candidate.is_a?(type) }

    # If the current node is narrower than or equal to the previous closest node, then it is more precise
    closest_loc = closest.location
    if loc.end_char - loc.start_char <= closest_loc.end_char - closest_loc.start_char
      parent = T.let(closest, SyntaxTree::Node)
      closest = candidate
    end
  end

  [closest, parent]
end

#range_from_syntax_tree_node(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_lsp/requests/base_request.rb', line 40

def range_from_syntax_tree_node(node)
  loc = node.location

  LanguageServer::Protocol::Interface::Range.new(
    start: LanguageServer::Protocol::Interface::Position.new(
      line: loc.start_line - 1,
      character: loc.start_column,
    ),
    end: LanguageServer::Protocol::Interface::Position.new(line: loc.end_line - 1, character: loc.end_column),
  )
end

#runObject



29
# File 'lib/ruby_lsp/requests/base_request.rb', line 29

def run; end

#visible?(node, range) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'lib/ruby_lsp/requests/base_request.rb', line 115

def visible?(node, range)
  return true if range.nil?
  return false if node.nil?

  loc = node.location
  range.cover?(loc.start_line - 1) && range.cover?(loc.end_line - 1)
end

#visit_all(nodes) ⇒ Object



35
36
37
# File 'lib/ruby_lsp/requests/base_request.rb', line 35

def visit_all(nodes)
  nodes.each { |node| visit(node) }
end