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
permalink #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
permalink #source ⇒ Object (readonly)
Returns the value of attribute source.
16 17 18 |
# File 'lib/ruby_lsp/document.rb', line 16 def source @source end |
permalink #tree ⇒ Object (readonly)
Returns the value of attribute tree.
13 14 15 |
# File 'lib/ruby_lsp/document.rb', line 13 def tree @tree end |
permalink #uri ⇒ Object (readonly)
Returns the value of attribute uri.
22 23 24 |
# File 'lib/ruby_lsp/document.rb', line 22 def uri @uri end |
permalink #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
permalink #==(other) ⇒ Object
[View source]
39 40 41 |
# File 'lib/ruby_lsp/document.rb', line 39 def ==(other) @source == other.source end |
permalink #cache_fetch(request_name, &block) ⇒ Object
[View source]
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 |
permalink #cache_get(request_name) ⇒ Object
[View source]
66 67 68 |
# File 'lib/ruby_lsp/document.rb', line 66 def cache_get(request_name) @cache[request_name] end |
permalink #cache_set(request_name, value) ⇒ Object
[View source]
61 62 63 |
# File 'lib/ruby_lsp/document.rb', line 61 def cache_set(request_name, value) @cache[request_name] = value end |
permalink #create_scanner ⇒ Object
[View source]
109 110 111 |
# File 'lib/ruby_lsp/document.rb', line 109 def create_scanner Scanner.new(@source, @encoding) end |
permalink #locate(node, char_position, node_types: []) ⇒ Object
[View source]
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 |
permalink #locate_node(position, node_types: []) ⇒ Object
[View source]
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 |
permalink #parse ⇒ Object
[View source]
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 |
permalink #parsed? ⇒ Boolean
104 105 106 |
# File 'lib/ruby_lsp/document.rb', line 104 def parsed? !@tree.nil? end |
permalink #push_edits(edits, version:) ⇒ Object
[View source]
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 |
permalink #syntax_error? ⇒ Boolean
99 100 101 |
# File 'lib/ruby_lsp/document.rb', line 99 def syntax_error? @syntax_error end |