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
29
30
31
# 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)
  @unparsed_edits = T.let([], T::Array[EditShape])
  @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

#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



34
35
36
# File 'lib/ruby_lsp/document.rb', line 34

def ==(other)
  @source == other.source
end

#cache_fetch(request_name, &block) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/ruby_lsp/document.rb', line 45

def cache_fetch(request_name, &block)
  cached = @cache[request_name]
  return cached if cached

  result = block.call(self)
  @cache[request_name] = result
  result
end

#parseObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_lsp/document.rb', line 64

def parse
  return if @unparsed_edits.empty?

  @tree = SyntaxTree.parse(@source)
  @syntax_error_edits.clear
  @unparsed_edits.clear
  @parsable_source = @source.dup
rescue SyntaxTree::Parser::ParseError
  @syntax_error_edits = @unparsed_edits
  update_parsable_source(@unparsed_edits)
end

#parsed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ruby_lsp/document.rb', line 82

def parsed?
  !@tree.nil?
end

#push_edits(edits) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/ruby_lsp/document.rb', line 55

def push_edits(edits)
  # Apply the edits on the real source
  edits.each { |edit| apply_edit(@source, edit[:range], edit[:text]) }

  @unparsed_edits.concat(edits)
  @cache.clear
end

#syntax_errors?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/ruby_lsp/document.rb', line 77

def syntax_errors?
  @syntax_error_edits.any?
end