Class: RubyLsp::Document
- Inherits:
-
Object
- Object
- RubyLsp::Document
- 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
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cache_fetch(request_name, &block) ⇒ Object
- #cache_get(request_name) ⇒ Object
- #cache_set(request_name, value) ⇒ Object
- #create_scanner ⇒ Object
-
#initialize(source:, version:, uri:, encoding: Constant::PositionEncodingKind::UTF8) ⇒ Document
constructor
A new instance of Document.
- #locate(node, char_position, node_types: []) ⇒ Object
- #locate_node(position, node_types: []) ⇒ Object
- #parse ⇒ Object
- #parsed? ⇒ Boolean
- #push_edits(edits, version:) ⇒ Object
- #syntax_error? ⇒ Boolean
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, URI::Generic) @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
#source ⇒ Object (readonly)
Returns the value of attribute source.
16 17 18 |
# File 'lib/ruby_lsp/document.rb', line 16 def source @source end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
13 14 15 |
# File 'lib/ruby_lsp/document.rb', line 13 def tree @tree end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
22 23 24 |
# File 'lib/ruby_lsp/document.rb', line 22 def uri @uri end |
#version ⇒ Object (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_scanner ⇒ Object
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# 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)) nesting = T.let([], T::Array[T.any(SyntaxTree::ClassDeclaration, SyntaxTree::ModuleDeclaration)]) 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. The order here is important! We want to move in the # same order as the visiting mechanism, which means searching the child nodes before moving on to the next # sibling queue.unshift(*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 the candidate starts after the end of the previous nesting level, then we've exited that nesting level and # need to pop the stack previous_level = nesting.last nesting.pop if previous_level && candidate.start_char > previous_level.end_char # Keep track of the nesting where we found the target. This is used to determine the fully qualified name of the # target when it is a constant if candidate.is_a?(SyntaxTree::ClassDeclaration) || candidate.is_a?(SyntaxTree::ModuleDeclaration) nesting << candidate end # 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, nesting.map { |n| n.constant.constant.value }] 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), node_types: node_types) end |
#parse ⇒ Object
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
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
99 100 101 |
# File 'lib/ruby_lsp/document.rb', line 99 def syntax_error? @syntax_error end |