Skip to content

add additional extensions, clean #569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule ElixirLS.LanguageServer.Server do
end
end

@watched_extensions [
@default_watched_extensions [
".ex",
".exs",
".erl",
Expand Down Expand Up @@ -266,23 +266,7 @@ defmodule ElixirLS.LanguageServer.Server do
Process.send_after(self(), :default_config, 5000)

if state.supports_dynamic do
watchers = for ext <- @watched_extensions, do: %{"globPattern" => "**/*" <> ext}

register_capability_result =
JsonRpc.register_capability_request("workspace/didChangeWatchedFiles", %{
"watchers" => watchers
})

case register_capability_result do
{:ok, nil} ->
:ok

other ->
JsonRpc.log_message(
:error,
"client/registerCapability returned: #{inspect(other)}"
)
end
add_watched_extensions(state, @default_watched_extensions)
end

state
Expand Down Expand Up @@ -313,8 +297,8 @@ defmodule ElixirLS.LanguageServer.Server do

new_settings =
case changed_settings do
%{"elixirLS" => changed_settings} when is_map(changed_settings) ->
Map.merge(prev_settings, changed_settings)
%{"elixirLS" => settings} when is_map(settings) ->
Map.merge(prev_settings, settings)

_ ->
prev_settings
Expand Down Expand Up @@ -415,11 +399,15 @@ defmodule ElixirLS.LanguageServer.Server do
defp handle_notification(did_change_watched_files(changes), state = %__MODULE__{}) do
changes = Enum.filter(changes, &match?(%{"uri" => "file:" <> _}, &1))

# `settings` may not always be available here, like during testing
additional_watched_extensions =
Map.get(state.settings || %{}, "additionalWatchedExtensions", [])

needs_build =
Enum.any?(changes, fn %{"uri" => uri = "file:" <> _, "type" => type} ->
path = SourceFile.path_from_uri(uri)

Path.extname(path) in @watched_extensions and
Path.extname(path) in (additional_watched_extensions ++ @default_watched_extensions) and
(type in [1, 3] or not Map.has_key?(state.source_files, uri) or
state.source_files[uri].dirty?)
end)
Expand Down Expand Up @@ -1101,6 +1089,7 @@ defmodule ElixirLS.LanguageServer.Server do
mix_env = Map.get(settings, "mixEnv", "test")
mix_target = Map.get(settings, "mixTarget")
project_dir = Map.get(settings, "projectDir")
additional_watched_extensions = Map.get(settings, "additionalWatchedExtensions", [])

state =
state
Expand All @@ -1109,11 +1098,36 @@ defmodule ElixirLS.LanguageServer.Server do
|> maybe_set_mix_target(mix_target)
|> set_project_dir(project_dir)
|> set_dialyzer_enabled(enable_dialyzer)
|> add_watched_extensions(additional_watched_extensions)

state = create_gitignore(state)
trigger_build(%{state | settings: settings})
end

defp add_watched_extensions(state = %__MODULE__{}, []) do
state
end

defp add_watched_extensions(state = %__MODULE__{}, exts) when is_list(exts) do
case JsonRpc.register_capability_request(
"workspace/didChangeWatchedFiles",
%{
"watchers" => Enum.map(exts, &%{"globPattern" => "**/*" <> &1})
}
) do
{:ok, nil} ->
:ok

other ->
JsonRpc.log_message(
:error,
"client/registerCapability returned: #{inspect(other)}"
)
end

state
end

defp set_dialyzer_enabled(state = %__MODULE__{}, enable_dialyzer) do
cond do
enable_dialyzer and state.dialyzer_sup == nil and is_binary(state.project_dir) ->
Expand Down
18 changes: 18 additions & 0 deletions apps/language_server/test/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,24 @@ defmodule ElixirLS.LanguageServer.ServerTest do
assert state.needs_build? || state.build_running?
end

# https://github.com/elixir-lsp/elixir-ls/pull/569
@tag :additional_extension
test "watched file updated outside, non-default extension", %{server: server} do
uri = "file:///file.veex"
fake_initialize(server)

# Simulate settings related to this test
:sys.replace_state(server, fn state ->
%{state | settings: %{"additionalWatchedExtensions" => [".veex"]}}
end)

# Check if *.veex file triggers build
Server.receive_packet(server, did_change_watched_files([%{"uri" => uri, "type" => 2}]))

state = :sys.get_state(server)
assert state.needs_build? || state.build_running?
end

test "gracefully skip not supported URI scheme", %{server: server} do
uri = "git://github.com/user/repo.git"
fake_initialize(server)
Expand Down