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.
-
#syntax_error_edits ⇒ Object
readonly
Returns the value of attribute syntax_error_edits.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cache_fetch(request_name, &block) ⇒ Object
-
#initialize(source) ⇒ Document
constructor
A new instance of Document.
- #parsed? ⇒ Boolean
- #push_edits(edits) ⇒ Object
- #syntax_errors? ⇒ Boolean
Constructor Details
#initialize(source) ⇒ Document
Returns a new instance of Document.
22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby_lsp/document.rb', line 22 def initialize(source) @cache = T.let({}, T::Hash[Symbol, T.untyped]) @syntax_error_edits = T.let([], T::Array[EditShape]) @source = T.let(source, String) @parsable_source = T.let(source.dup, String) @tree = T.let(SyntaxTree.parse(@source), T.nilable(SyntaxTree::Node)) rescue SyntaxTree::Parser::ParseError # Do not raise if we failed to parse 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 |
#syntax_error_edits ⇒ Object (readonly)
Returns the value of attribute syntax_error_edits.
19 20 21 |
# File 'lib/ruby_lsp/document.rb', line 19 def syntax_error_edits @syntax_error_edits 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 |
Instance Method Details
#==(other) ⇒ Object
33 34 35 |
# File 'lib/ruby_lsp/document.rb', line 33 def ==(other) @source == other.source end |
#cache_fetch(request_name, &block) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/ruby_lsp/document.rb', line 44 def cache_fetch(request_name, &block) cached = @cache[request_name] return cached if cached result = block.call(self) @cache[request_name] = result result end |
#parsed? ⇒ Boolean
73 74 75 |
# File 'lib/ruby_lsp/document.rb', line 73 def parsed? !@tree.nil? end |
#push_edits(edits) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ruby_lsp/document.rb', line 54 def push_edits(edits) # Apply the edits on the real source edits.each { |edit| apply_edit(@source, edit[:range], edit[:text]) } @cache.clear @tree = SyntaxTree.parse(@source) @syntax_error_edits.clear @parsable_source = @source.dup nil rescue SyntaxTree::Parser::ParseError update_parsable_source(edits) end |
#syntax_errors? ⇒ Boolean
68 69 70 |
# File 'lib/ruby_lsp/document.rb', line 68 def syntax_errors? @syntax_error_edits.any? end |