Class: RubyLsp::Document

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Document

Returns a new instance of Document.



22
23
24
25
26
27
28
# File 'lib/ruby_lsp/document.rb', line 22

def initialize(source)
  @tree = T.let(SyntaxTree.parse(source), SyntaxTree::Node)
  @cache = T.let({}, T::Hash[Symbol, T.untyped])
  @syntax_error_edits = T.let([], T::Array[EditShape])
  @source = source
  @parsable_source = T.let(source.dup, String)
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/ruby_lsp/document.rb', line 16

def source
  @source
end

#syntax_error_editsObject (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

#treeObject (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

#push_edits(edits) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_lsp/document.rb', line 52

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/ruby_lsp/document.rb', line 66

def syntax_errors?
  @syntax_error_edits.any?
end