Skip to content

Commit

Permalink
Explicitly request file watchers from client (#35)
Browse files Browse the repository at this point in the history
* Add explicit file watcher request

It is recommended that servers register for the capability to watch
files. Notably, in emacs, clients such as lsp-mode will act
conservatively and not send file change notifications without the added
message.

Added a generic capability registration request and specify file
watchers after initialization.

* Mix format

* Add workspace capabilities, conditionally send file watcher message
  • Loading branch information
daniel-koudouna authored and axelson committed Jun 24, 2019
1 parent 6cc58bd commit 95c021f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
12 changes: 12 additions & 0 deletions apps/language_server/lib/language_server/json_rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ defmodule ElixirLS.LanguageServer.JsonRpc do
notify("window/logMessage", %{type: message_type_code(type), message: to_string(message)})
end

def register_capability_request(server \\ __MODULE__, method, options) do
send_request(server, "client/registerCapability", %{
"registrations" => [
%{
"id" => :crypto.hash(:sha, method) |> Base.encode16(),
"method" => method,
"registerOptions" => options
}
]
})
end

def show_message_request(server \\ __MODULE__, type, message, actions) do
send_request(server, "window/showMessageRequest", %{
"type" => message_type_code(type),
Expand Down
24 changes: 23 additions & 1 deletion apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ defmodule ElixirLS.LanguageServer.Server do
super(msg, state)
end

def handle_info(:send_file_watchers, state) do
JsonRpc.register_capability_request("workspace/didChangeWatchedFiles", %{
"watchers" => [
%{"globPattern" => "**/*.ex"},
%{"globPattern" => "**/*.exs"}
]
})

{:noreply, state}
end

def handle_info(:default_config, state) do
state =
case state do
Expand Down Expand Up @@ -322,6 +333,16 @@ defmodule ElixirLS.LanguageServer.Server do
# If we don't receive workspace/didChangeConfiguration for 5 seconds, use default settings
Process.send_after(self(), :default_config, 5000)

# Explicitly request file watchers from the client if supported
supports_dynamic = get_in(client_capabilities, [
"textDocument",
"codeAction",
"dynamicRegistration"
])
if supports_dynamic do
Process.send_after(self(), :send_file_watchers, 100)
end

{:ok, %{"capabilities" => server_capabilities()}, state}
end

Expand Down Expand Up @@ -445,7 +466,8 @@ defmodule ElixirLS.LanguageServer.Server do
"documentSymbolProvider" => true,
"documentOnTypeFormattingProvider" => %{"firstTriggerCharacter" => "\n"},
"codeLensProvider" => %{"resolveProvider" => false},
"executeCommandProvider" => %{"commands" => ["spec"]}
"executeCommandProvider" => %{"commands" => ["spec"]},
"workspace" => %{ "workspaceFolders" => %{"supported" => true, "changeNotifications" => true}}
}
end

Expand Down

0 comments on commit 95c021f

Please sign in to comment.