Class: ThemeCheck::LanguageServer::Handler

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

Constant Summary collapse

SERVER_INFO =
{
  name: $PROGRAM_NAME,
  version: ThemeCheck::VERSION,
}
CAPABILITIES =
{
  completionProvider: {
    triggerCharacters: ['.', '{{ ', '{% '],
    context: true,
  },
  documentLinkProvider: true,
  textDocumentSync: {
    openClose: true,
    change: TextDocumentSyncKind::FULL,
    willSave: false,
    save: true,
  },
}

Instance Method Summary collapse

Methods included from URIHelper

#file_path, #file_uri

Constructor Details

#initialize(bridge) ⇒ Handler

Returns a new instance of Handler.



29
30
31
# File 'lib/theme_check/language_server/handler.rb', line 29

def initialize(bridge)
  @bridge = bridge
end

Instance Method Details

#on_exit(_id, _params) ⇒ Object



54
55
56
# File 'lib/theme_check/language_server/handler.rb', line 54

def on_exit(_id, _params)
  close!
end

#on_initialize(id, params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/theme_check/language_server/handler.rb', line 33

def on_initialize(id, params)
  @root_path = root_path_from_params(params)

  # Tell the client we don't support anything if there's no rootPath
  return @bridge.send_response(id, { capabilities: {} }) if @root_path.nil?

  @bridge.supports_work_done_progress = params.dig('capabilities', 'window', 'workDoneProgress') || false
  @storage = in_memory_storage(@root_path)
  @completion_engine = CompletionEngine.new(@storage)
  @document_link_engine = DocumentLinkEngine.new(@storage)
  @diagnostics_engine = DiagnosticsEngine.new(@bridge)
  @bridge.send_response(id, {
    capabilities: CAPABILITIES,
    serverInfo: SERVER_INFO,
  })
end

#on_shutdown(id, _params) ⇒ Object



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

def on_shutdown(id, _params)
  @bridge.send_response(id, nil)
end

#on_text_document_completion(id, params) ⇒ Object



83
84
85
86
87
88
# File 'lib/theme_check/language_server/handler.rb', line 83

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')
  @bridge.send_response(id, completions(relative_path, line, col))
end

#on_text_document_did_change(_id, params) ⇒ Object



58
59
60
61
# File 'lib/theme_check/language_server/handler.rb', line 58

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



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

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



68
69
70
71
72
# File 'lib/theme_check/language_server/handler.rb', line 68

def on_text_document_did_open(_id, params)
  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)) if @diagnostics_engine.first_run?
end

#on_text_document_did_save(_id, params) ⇒ Object



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

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


78
79
80
81
# File 'lib/theme_check/language_server/handler.rb', line 78

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