Class: ThemeCheck::LanguageServer::CompletionEngine

Inherits:
Object
  • Object
show all
Includes:
PositionHelper
Defined in:
lib/theme_check/language_server/completion_engine.rb

Instance Method Summary collapse

Methods included from PositionHelper

#bounded, #from_index_to_row_column, #from_row_column_to_index

Constructor Details

#initialize(storage) ⇒ CompletionEngine

Returns a new instance of CompletionEngine.



8
9
10
11
# File 'lib/theme_check/language_server/completion_engine.rb', line 8

def initialize(storage)
  @storage = storage
  @providers = CompletionProvider.all.map { |x| x.new(storage) }
end

Instance Method Details

#completions(relative_path, line, col) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/theme_check/language_server/completion_engine.rb', line 13

def completions(relative_path, line, col)
  buffer = @storage.read(relative_path)
  cursor = from_row_column_to_index(buffer, line, col)
  token = find_token(buffer, cursor)
  return [] if token.nil?

  @providers.flat_map do |p|
    p.completions(
      token.content,
      cursor - token.start
    )
  end
end

#find_token(buffer, cursor) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/theme_check/language_server/completion_engine.rb', line 27

def find_token(buffer, cursor)
  Tokens.new(buffer).find do |token|
    # Here we include the next character and exclude the first
    # one becase when we want to autocomplete inside a token
    # and at most 1 outside it since the cursor could be placed
    # at the end of the token.
    token.start < cursor && cursor <= token.end
  end
end