Class: ThemeCheck::LanguageServer::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/language_server/handler.rb

Constant Summary collapse

CAPABILITIES =
{
  completionProvider: {
    triggerCharacters: ['.', '{{ ', '{% '],
    context: true,
  },
  documentLinkProvider: true,
  textDocumentSync: {
    openClose: true,
    change: TextDocumentSyncKind::FULL,
    willSave: false,
    save: true,
  },
}

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Handler

Returns a new instance of Handler.



21
22
23
24
# File 'lib/theme_check/language_server/handler.rb', line 21

def initialize(server)
  @server = server
  @diagnostics_tracker = DiagnosticsTracker.new
end

Instance Method Details

#on_exit(_id, _params) ⇒ Object Also known as: on_shutdown



40
41
42
# File 'lib/theme_check/language_server/handler.rb', line 40

def on_exit(_id, _params)
  close!
end

#on_initialize(id, params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/theme_check/language_server/handler.rb', line 26

def on_initialize(id, params)
  @root_path = path_from_uri(params["rootUri"]) || params["rootPath"]

  # Tell the client we don't support anything if there's no rootPath
  return send_response(id, { capabilities: {} }) if @root_path.nil?
  @storage = in_memory_storage(@root_path)
  @completion_engine = CompletionEngine.new(@storage)
  @document_link_engine = DocumentLinkEngine.new(@storage)
  # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#responseMessage
  send_response(id, {
    capabilities: CAPABILITIES,
  })
end

#on_text_document_completion(id, params) ⇒ Object



71
72
73
74
75
76
# File 'lib/theme_check/language_server/handler.rb', line 71

def on_text_document_completion(id, params)
  relative_path = relative_path_from_text_document_uri(params)
  line = params.dig('position', 'line')
  col = params.dig('position', 'character')
  send_response(id, completions(relative_path, line, col))
end

#on_text_document_did_change(_id, params) ⇒ Object



45
46
47
48
# File 'lib/theme_check/language_server/handler.rb', line 45

def on_text_document_did_change(_id, params)
  relative_path = relative_path_from_text_document_uri(params)
  @storage.write(relative_path, content_changes_text(params))
end

#on_text_document_did_close(_id, params) ⇒ Object



50
51
52
53
# File 'lib/theme_check/language_server/handler.rb', line 50

def on_text_document_did_close(_id, params)
  relative_path = relative_path_from_text_document_uri(params)
  @storage.write(relative_path, "")
end

#on_text_document_did_open(_id, params) ⇒ Object



55
56
57
58
59
60
# File 'lib/theme_check/language_server/handler.rb', line 55

def on_text_document_did_open(_id, params)
  return unless @diagnostics_tracker.first_run?
  relative_path = relative_path_from_text_document_uri(params)
  @storage.write(relative_path, text_document_text(params))
  analyze_and_send_offenses(text_document_uri(params))
end

#on_text_document_did_save(_id, params) ⇒ Object



62
63
64
# File 'lib/theme_check/language_server/handler.rb', line 62

def on_text_document_did_save(_id, params)
  analyze_and_send_offenses(text_document_uri(params))
end


66
67
68
69
# File 'lib/theme_check/language_server/handler.rb', line 66

def on_text_document_document_link(id, params)
  relative_path = relative_path_from_text_document_uri(params)
  send_response(id, document_links(relative_path))
end