Class: PlatformosCheck::LanguageServer::CompletionContext

Inherits:
Object
  • Object
show all
Includes:
PositionHelper
Defined in:
lib/platformos_check/language_server/completion_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PositionHelper

#bounded, #from_index_to_row_column, #from_row_column_to_index

Constructor Details

#initialize(storage, relative_path, line, col) ⇒ CompletionContext

Returns a new instance of CompletionContext.



10
11
12
13
14
15
# File 'lib/platformos_check/language_server/completion_context.rb', line 10

def initialize(storage, relative_path, line, col)
  @storage = storage
  @relative_path = relative_path
  @line = line
  @col = col
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



8
9
10
# File 'lib/platformos_check/language_server/completion_context.rb', line 8

def col
  @col
end

#lineObject (readonly)

Returns the value of attribute line.



8
9
10
# File 'lib/platformos_check/language_server/completion_context.rb', line 8

def line
  @line
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



8
9
10
# File 'lib/platformos_check/language_server/completion_context.rb', line 8

def relative_path
  @relative_path
end

#storageObject (readonly)

Returns the value of attribute storage.



8
9
10
# File 'lib/platformos_check/language_server/completion_context.rb', line 8

def storage
  @storage
end

Instance Method Details

#absolute_cursorObject



25
26
27
# File 'lib/platformos_check/language_server/completion_context.rb', line 25

def absolute_cursor
  @absolute_cursor ||= from_row_column_to_index(buffer, line, col)
end

#bufferObject



17
18
19
# File 'lib/platformos_check/language_server/completion_context.rb', line 17

def buffer
  @buffer ||= storage.read(relative_path)
end

#buffer_until_previous_rowObject



21
22
23
# File 'lib/platformos_check/language_server/completion_context.rb', line 21

def buffer_until_previous_row
  @buffer_without_current_row ||= buffer[0..absolute_cursor].lines[0...-1].join
end

#clone_and_overwrite(col:) ⇒ Object



49
50
51
# File 'lib/platformos_check/language_server/completion_context.rb', line 49

def clone_and_overwrite(col:)
  CompletionContext.new(storage, relative_path, line, col)
end

#contentObject



35
36
37
# File 'lib/platformos_check/language_server/completion_context.rb', line 35

def content
  @content ||= token&.content
end

#cursorObject



29
30
31
32
33
# File 'lib/platformos_check/language_server/completion_context.rb', line 29

def cursor
  @cursor ||= (absolute_cursor - token&.start) || 0
rescue StandardError
  0
end

#tokenObject



39
40
41
42
43
44
45
46
47
# File 'lib/platformos_check/language_server/completion_context.rb', line 39

def token
  @token ||= Tokens.new(buffer).find do |t|
    # 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.
    t.start < absolute_cursor && absolute_cursor <= t.end
  end
end