Class: RubyLsp::Document
- Inherits:
-
Object
- Object
- RubyLsp::Document
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/ruby_lsp/document.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Scanner
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#parse_result ⇒ Object
readonly
Returns the value of attribute parse_result.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#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
- #comments ⇒ 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
- #push_edits(edits, version:) ⇒ Object
- #sorbet_sigil_is_true_or_higher ⇒ Object
- #syntax_error? ⇒ Boolean
- #tree ⇒ Object
- #typechecker_enabled? ⇒ Boolean
Constructor Details
#initialize(source:, version:, uri:, encoding: Constant::PositionEncodingKind::UTF8) ⇒ Document
Returns a new instance of Document.
27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby_lsp/document.rb', line 27 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) @needs_parsing = T.let(true, T::Boolean) @parse_result = T.let(parse, Prism::ParseResult) end |
Instance Attribute Details
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
24 25 26 |
# File 'lib/ruby_lsp/document.rb', line 24 def encoding @encoding end |
#parse_result ⇒ Object (readonly)
Returns the value of attribute parse_result.
12 13 14 |
# File 'lib/ruby_lsp/document.rb', line 12 def parse_result @parse_result end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
15 16 17 |
# File 'lib/ruby_lsp/document.rb', line 15 def source @source end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
21 22 23 |
# File 'lib/ruby_lsp/document.rb', line 21 def uri @uri end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
18 19 20 |
# File 'lib/ruby_lsp/document.rb', line 18 def version @version end |
Instance Method Details
#==(other) ⇒ Object
48 49 50 |
# File 'lib/ruby_lsp/document.rb', line 48 def ==(other) @source == other.source end |
#cache_fetch(request_name, &block) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/ruby_lsp/document.rb', line 60 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
75 76 77 |
# File 'lib/ruby_lsp/document.rb', line 75 def cache_get(request_name) @cache[request_name] end |
#cache_set(request_name, value) ⇒ Object
70 71 72 |
# File 'lib/ruby_lsp/document.rb', line 70 def cache_set(request_name, value) @cache[request_name] = value end |
#comments ⇒ Object
43 44 45 |
# File 'lib/ruby_lsp/document.rb', line 43 def comments @parse_result.comments end |
#create_scanner ⇒ Object
105 106 107 |
# File 'lib/ruby_lsp/document.rb', line 105 def create_scanner Scanner.new(@source, @encoding) end |
#locate(node, char_position, node_types: []) ⇒ Object
126 127 128 129 130 131 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 |
# File 'lib/ruby_lsp/document.rb', line 126 def locate(node, char_position, node_types: []) queue = T.let(node.child_nodes.compact, T::Array[T.nilable(Prism::Node)]) closest = node parent = T.let(nil, T.nilable(Prism::Node)) nesting = T.let([], T::Array[T.any(Prism::ClassNode, Prism::ModuleNode)]) 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 T.unsafe(queue).unshift(*candidate.child_nodes) # Skip if the current node doesn't cover the desired position loc = candidate.location next unless (loc.start_offset...loc.end_offset).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_offset # 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 && loc.start_offset > previous_level.location.end_offset # 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?(Prism::ClassNode) || candidate.is_a?(Prism::ModuleNode) 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_offset - loc.start_offset <= closest_loc.end_offset - closest_loc.start_offset parent = closest closest = candidate end end [closest, parent, nesting.map { |n| n.constant_path.location.slice }] end |
#locate_node(position, node_types: []) ⇒ Object
115 116 117 |
# File 'lib/ruby_lsp/document.rb', line 115 def locate_node(position, node_types: []) locate(@parse_result.value, create_scanner.find_char_position(position), node_types: node_types) end |
#parse ⇒ Object
97 |
# File 'lib/ruby_lsp/document.rb', line 97 def parse; end |
#push_edits(edits, version:) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ruby_lsp/document.rb', line 80 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 @needs_parsing = true @cache.clear end |
#sorbet_sigil_is_true_or_higher ⇒ Object
177 178 179 180 181 |
# File 'lib/ruby_lsp/document.rb', line 177 def sorbet_sigil_is_true_or_higher parse_result.magic_comments.any? do |comment| comment.key == "typed" && ["true", "strict", "strong"].include?(comment.value) end end |
#syntax_error? ⇒ Boolean
100 101 102 |
# File 'lib/ruby_lsp/document.rb', line 100 def syntax_error? @parse_result.failure? end |
#tree ⇒ Object
38 39 40 |
# File 'lib/ruby_lsp/document.rb', line 38 def tree @parse_result.value end |
#typechecker_enabled? ⇒ Boolean
184 185 186 |
# File 'lib/ruby_lsp/document.rb', line 184 def typechecker_enabled? DependencyDetector.instance.typechecker && sorbet_sigil_is_true_or_higher end |