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,
  },
  codeActionProvider: {
    codeActionKinds: CodeActionProvider.all.map(&:kind),
    resolveProvider: false,
    workDoneProgress: false,
  },
  documentLinkProvider: true,
  executeCommandProvider: {
    workDoneProgress: false,
    commands: ExecuteCommandProvider.all.map(&:command),
  },
  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.



38
39
40
# File 'lib/theme_check/language_server/handler.rb', line 38

def initialize(bridge)
  @bridge = bridge
end

Instance Method Details

#on_exit(_id, _params) ⇒ Object



76
77
78
# File 'lib/theme_check/language_server/handler.rb', line 76

def on_exit(_id, _params)
  close!
end

#on_initialize(id, params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/theme_check/language_server/handler.rb', line 42

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?

  @client_capabilities = ClientCapabilities.new(params.dig(:capabilities) || {})
  @configuration = Configuration.new(@bridge, @client_capabilities)
  @bridge.supports_work_done_progress = @client_capabilities.supports_work_done_progress?
  @storage = in_memory_storage(@root_path)
  @diagnostics_manager = DiagnosticsManager.new
  @completion_engine = CompletionEngine.new(@storage)
  @document_link_engine = DocumentLinkEngine.new(@storage)
  @diagnostics_engine = DiagnosticsEngine.new(@storage, @bridge, @diagnostics_manager)
  @execute_command_engine = ExecuteCommandEngine.new
  @execute_command_engine << CorrectionExecuteCommandProvider.new(@storage, @bridge, @diagnostics_manager)
  @execute_command_engine << RunChecksExecuteCommandProvider.new(@diagnostics_engine, @root_path, config_for_path(@root_path))
  @code_action_engine = CodeActionEngine.new(@storage, @diagnostics_manager)
  @bridge.send_response(id, {
    capabilities: CAPABILITIES,
    serverInfo: SERVER_INFO,
  })
end

#on_initialized(_id, _params) ⇒ Object



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

def on_initialized(_id, _params)
  return unless @configuration
  @configuration.fetch
  @configuration.register_did_change_capability
end

#on_shutdown(id, _params) ⇒ Object



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

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

#on_text_document_code_action(id, params) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/theme_check/language_server/handler.rb', line 115

def on_text_document_code_action(id, params)
  absolute_path = text_document_uri(params)
  start_position = range_element(params, :start)
  end_position = range_element(params, :end)
  only_code_action_kinds = params.dig(:context, :only) || []
  @bridge.send_response(id, @code_action_engine.code_actions(
    absolute_path,
    start_position,
    end_position,
    only_code_action_kinds,
  ))
end

#on_text_document_completion(id, params) ⇒ Object



108
109
110
111
112
113
# File 'lib/theme_check/language_server/handler.rb', line 108

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, @completion_engine.completions(relative_path, line, col))
end

#on_text_document_did_change(_id, params) ⇒ Object



86
87
88
89
90
# File 'lib/theme_check/language_server/handler.rb', line 86

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), text_document_version(params))
  analyze_and_send_offenses(text_document_uri(params), only_single_file: true) if @configuration.check_on_change?
end

#on_text_document_did_close(_id, params) ⇒ Object



92
93
94
95
96
97
# File 'lib/theme_check/language_server/handler.rb', line 92

def on_text_document_did_close(_id, params)
  relative_path = relative_path_from_text_document_uri(params)
  file_system_content = Pathname.new(text_document_uri(params)).read(mode: 'rb', encoding: 'UTF-8')
  # On close, the file system becomes the source of truth
  @storage.write(relative_path, file_system_content, nil)
end

#on_text_document_did_open(_id, params) ⇒ Object



80
81
82
83
84
# File 'lib/theme_check/language_server/handler.rb', line 80

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), text_document_version(params))
  analyze_and_send_offenses(text_document_uri(params)) if @configuration.check_on_open?
end

#on_text_document_did_save(_id, params) ⇒ Object



99
100
101
# File 'lib/theme_check/language_server/handler.rb', line 99

def on_text_document_did_save(_id, params)
  analyze_and_send_offenses(text_document_uri(params)) if @configuration.check_on_save?
end


103
104
105
106
# File 'lib/theme_check/language_server/handler.rb', line 103

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

#on_workspace_did_change_configuration(_id, _params) ⇒ Object



135
136
137
# File 'lib/theme_check/language_server/handler.rb', line 135

def on_workspace_did_change_configuration(_id, _params)
  @configuration.fetch(force: true)
end

#on_workspace_execute_command(id, params) ⇒ Object



128
129
130
131
132
133
# File 'lib/theme_check/language_server/handler.rb', line 128

def on_workspace_execute_command(id, params)
  @bridge.send_response(id, @execute_command_engine.execute(
    params[:command],
    params[:arguments],
  ))
end