Class: Solargraph::LanguageServer::Message::TextDocument::Completion

Inherits:
Base
  • Object
show all
Defined in:
lib/solargraph/language_server/message/text_document/completion.rb

Instance Attribute Summary

Attributes inherited from Base

#filename

Attributes inherited from Base

#error, #host, #id, #method, #params, #request, #result

Instance Method Summary collapse

Methods inherited from Base

#post_initialize

Methods included from UriHelpers

file_to_uri, uri_to_file

Methods inherited from Base

#initialize, #post_initialize, #send_response, #set_error, #set_result

Constructor Details

This class inherits a constructor from Solargraph::LanguageServer::Message::Base

Instance Method Details

#empty_result(incomplete = false) ⇒ Hash


45
46
47
48
49
50
# File 'lib/solargraph/language_server/message/text_document/completion.rb', line 45

def empty_result incomplete = false
  {
    isIncomplete: incomplete,
    items: []
  }
end

#processObject


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/solargraph/language_server/message/text_document/completion.rb', line 6

def process
  line = params['position']['line']
  col = params['position']['character']
  begin
    completion = host.completions_at(params['textDocument']['uri'], line, col)
    if host.cancel?(id)
      return set_result(empty_result) if host.cancel?(id)
    end
    items = []
    last_context = nil
    idx = -1
    completion.pins.each do |pin|
      idx += 1 if last_context != pin.context
      items.push pin.completion_item.merge({
        textEdit: {
          range: completion.range.to_hash,
          newText: pin.name.sub(/=$/, ' = ')
        },
        sortText: "#{idx.to_s.rjust(4, '0')}#{pin.name}"
      })
      items.last[:data][:uri] = params['textDocument']['uri']
      last_context = pin.context
    end
    set_result(
      isIncomplete: false,
      items: items
    )
  rescue InvalidOffsetError => e
    Logging.logger.info "Completion ignored invalid offset: #{params['textDocument']['uri']}, line #{line}, character #{col}"
    set_result empty_result
  end
rescue FileNotFoundError => e
  Logging.logger.warn "[#{e.class}] #{e.message}"
  Logging.logger.warn e.backtrace.join("\n")
  set_result empty_result
end