Class: RubyLsp::Document

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/document.rb

Defined Under Namespace

Classes: Scanner

Constant Summary collapse

PositionShape =
T.type_alias { { line: Integer, character: Integer } }
RangeShape =
T.type_alias { { start: PositionShape, end: PositionShape } }
EditShape =
T.type_alias { { range: RangeShape, text: String } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, version:, uri:, encoding: Constant::PositionEncodingKind::UTF8) ⇒ Document

Returns a new instance of Document.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_lsp/document.rb', line 25

def initialize(source:, version:, uri:, encoding: Constant::PositionEncodingKind::UTF8)
  @cache = T.let({}, T::Hash[String, T.untyped])
  @encoding = T.let(encoding, String)
  @source = T.let(source, String)
  @version = T.let(version, Integer)
  @uri = T.let(uri, String)
  @unparsed_edits = T.let([], T::Array[EditShape])
  @syntax_error = T.let(false, T::Boolean)
  @tree = T.let(SyntaxTree.parse(@source), T.nilable(SyntaxTree::Node))
rescue SyntaxTree::Parser::ParseError
  @syntax_error = true
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/ruby_lsp/document.rb', line 16

def source
  @source
end

#treeObject (readonly)

Returns the value of attribute tree.



13
14
15
# File 'lib/ruby_lsp/document.rb', line 13

def tree
  @tree
end

#uriObject (readonly)

Returns the value of attribute uri.



22
23
24
# File 'lib/ruby_lsp/document.rb', line 22

def uri
  @uri
end

#versionObject (readonly)

Returns the value of attribute version.



19
20
21
# File 'lib/ruby_lsp/document.rb', line 19

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
# File 'lib/ruby_lsp/document.rb', line 39

def ==(other)
  @source == other.source
end

#cache_fetch(request_name, &block) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/ruby_lsp/document.rb', line 51

def cache_fetch(request_name, &block)
  cached = @cache[request_name]
  return cached if cached

  result = block.call(self)
  @cache[request_name] = result
  result
end

#cache_get(request_name) ⇒ Object



66
67
68
# File 'lib/ruby_lsp/document.rb', line 66

def cache_get(request_name)
  @cache[request_name]
end

#cache_set(request_name, value) ⇒ Object



61
62
63
# File 'lib/ruby_lsp/document.rb', line 61

def cache_set(request_name, value)
  @cache[request_name] = value
end

#create_scannerObject



109
110
111
# File 'lib/ruby_lsp/document.rb', line 109

def create_scanner
  Scanner.new(@source, @encoding)
end

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ruby_lsp/document.rb', line 132

def locate(node, char_position, node_types: [])
  queue = T.let(node.child_nodes.compact, T::Array[T.nilable(SyntaxTree::Node)])
  closest = node
  parent = T.let(nil, T.nilable(SyntaxTree::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?(char_position)

    # If the node's start character is already past the position, then we should've found the closest node
    # already
    break if char_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.class == 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 = closest
      closest = candidate
    end
  end

  [closest, parent]
end

#locate_node(position, node_types: []) ⇒ Object



119
120
121
122
123
# File 'lib/ruby_lsp/document.rb', line 119

def locate_node(position, node_types: [])
  return [nil, nil] unless parsed?

  locate(T.must(@tree), create_scanner.find_char_position(position))
end

#parseObject



88
89
90
91
92
93
94
95
96
# File 'lib/ruby_lsp/document.rb', line 88

def parse
  return if @unparsed_edits.empty?

  @unparsed_edits.clear
  @tree = SyntaxTree.parse(@source)
  @syntax_error = false
rescue SyntaxTree::Parser::ParseError
  @syntax_error = true
end

#parsed?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/ruby_lsp/document.rb', line 104

def parsed?
  !@tree.nil?
end

#push_edits(edits, version:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby_lsp/document.rb', line 71

def push_edits(edits, version:)
  edits.each do |edit|
    range = edit[:range]
    scanner = create_scanner

    start_position = scanner.find_char_position(range[:start])
    end_position = scanner.find_char_position(range[:end])

    @source[start_position...end_position] = edit[:text]
  end

  @version = version
  @unparsed_edits.concat(edits)
  @cache.clear
end

#syntax_error?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/ruby_lsp/document.rb', line 99

def syntax_error?
  @syntax_error
end