Class: RubyLsp::ERBDocument

Inherits:
Document show all
Defined in:
lib/ruby_lsp/erb_document.rb

Overview

: [ParseResultType = Prism::ParseLexResult]

Defined Under Namespace

Classes: ERBScanner

Constant Summary

Constants inherited from Document

Document::EMPTY_CACHE, Document::MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES

Instance Attribute Summary collapse

Attributes inherited from Document

#encoding, #last_edit, #parse_result, #semantic_tokens, #source, #uri, #version

Instance Method Summary collapse

Methods inherited from Document

#==, #cache_fetch, #cache_get, #cache_set, #clear_cache, #find_index_by_position, #past_expensive_limit?, #push_edits

Constructor Details

#initialize(source:, version:, uri:, global_state:) ⇒ ERBDocument

: (source: String, version: Integer, uri: URI::Generic, global_state: GlobalState) -> void



14
15
16
17
18
19
20
21
# File 'lib/ruby_lsp/erb_document.rb', line 14

def initialize(source:, version:, uri:, global_state:)
  # This has to be initialized before calling super because we call `parse` in the parent constructor, which
  # overrides this with the proper virtual host language source
  @host_language_source = "" #: String
  super
  @code_units_cache =
    @parse_result.code_units_cache(@encoding) #: (^(Integer arg0) -> Integer | Prism::CodeUnitsCache)
end

Instance Attribute Details

#code_units_cacheObject (readonly)

: (^(Integer arg0) -> Integer | Prism::CodeUnitsCache)



11
12
13
# File 'lib/ruby_lsp/erb_document.rb', line 11

def code_units_cache
  @code_units_cache
end

#host_language_sourceObject (readonly)

: String



8
9
10
# File 'lib/ruby_lsp/erb_document.rb', line 8

def host_language_source
  @host_language_source
end

Instance Method Details

#astObject

: -> Prism::ProgramNode



40
41
42
# File 'lib/ruby_lsp/erb_document.rb', line 40

def ast
  @parse_result.value.first
end

#inside_host_language?(char_position) ⇒ Boolean

: (Integer char_position) -> bool?

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/ruby_lsp/erb_document.rb', line 69

def inside_host_language?(char_position)
  char = @host_language_source[char_position]
  char && char != " "
end

#language_idObject

: -> Symbol



52
53
54
# File 'lib/ruby_lsp/erb_document.rb', line 52

def language_id
  :erb
end

#locate_node(position, node_types: []) ⇒ Object

: (Hash[Symbol, untyped] position, ?node_types: Array) -> NodeContext



57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_lsp/erb_document.rb', line 57

def locate_node(position, node_types: [])
  char_position, _ = find_index_by_position(position)

  RubyDocument.locate(
    ast,
    char_position,
    code_units_cache: @code_units_cache,
    node_types: node_types,
  )
end

#parse!Object

: -> bool



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_lsp/erb_document.rb', line 25

def parse!
  return false unless @needs_parsing

  @needs_parsing = false
  scanner = ERBScanner.new(@source)
  scanner.scan
  @host_language_source = scanner.host_language
  # Use partial script to avoid syntax errors in ERB files where keywords may be used without the full context in
  # which they will be evaluated
  @parse_result = Prism.parse_lex(scanner.ruby, partial_script: true)
  @code_units_cache = @parse_result.code_units_cache(@encoding)
  true
end

#syntax_error?Boolean

: -> bool

Returns:

  • (Boolean)


46
47
48
# File 'lib/ruby_lsp/erb_document.rb', line 46

def syntax_error?
  @parse_result.failure?
end