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.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cache_fetch(request_name, &block) ⇒ Object
- #create_scanner ⇒ Object
-
#initialize(source, encoding = "utf-8") ⇒ Document
constructor
A new instance of Document.
- #parse ⇒ Object
- #parsed? ⇒ Boolean
- #push_edits(edits) ⇒ Object
- #syntax_error? ⇒ Boolean
Constructor Details
#initialize(source, encoding = "utf-8") ⇒ Document
Returns a new instance of Document.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ruby_lsp/document.rb', line 19 def initialize(source, encoding = "utf-8") @cache = T.let({}, T::Hash[Symbol, T.untyped]) @encoding = T.let(encoding, String) @source = T.let(source, 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
#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 |
Instance Method Details
#==(other) ⇒ Object
31 32 33 |
# File 'lib/ruby_lsp/document.rb', line 31 def ==(other) @source == other.source end |
#cache_fetch(request_name, &block) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/ruby_lsp/document.rb', line 42 def cache_fetch(request_name, &block) cached = @cache[request_name] return cached if cached result = block.call(self) @cache[request_name] = result result end |
#create_scanner ⇒ Object
89 90 91 |
# File 'lib/ruby_lsp/document.rb', line 89 def create_scanner Scanner.new(@source, @encoding) end |
#parse ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/ruby_lsp/document.rb', line 68 def parse return if @unparsed_edits.empty? @tree = SyntaxTree.parse(@source) @syntax_error = false @unparsed_edits.clear rescue SyntaxTree::Parser::ParseError @syntax_error = true end |
#parsed? ⇒ Boolean
84 85 86 |
# File 'lib/ruby_lsp/document.rb', line 84 def parsed? !@tree.nil? end |
#push_edits(edits) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ruby_lsp/document.rb', line 52 def push_edits(edits) 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 @unparsed_edits.concat(edits) @cache.clear end |
#syntax_error? ⇒ Boolean
79 80 81 |
# File 'lib/ruby_lsp/document.rb', line 79 def syntax_error? @syntax_error end |