Skip to content

Commit

Permalink
WorkspaceSymbols index after successful build (elixir-editors#263)
Browse files Browse the repository at this point in the history
* WorkspaceSymbols index after successful build

Previously WorkspaceSymbols depended on a side-effect of running Dialyzer:
elixir-lsp/elixir-ls#110 (comment)

Now that the Build step loads all modules (added in elixir-editors#227) we need to change the
WorkspaceSymbols index to be rebuilt after a successful compile.

* Fix tests

Allow WorkspaceSymbol to have it's own instance of the GenServer for the test by
being parameterized by the server's registered name

* Give Server some more time to respond

This is probably necessary now because workspace symbols is running earlier

* Bump up the timeout
  • Loading branch information
axelson authored May 29, 2020
1 parent 269e78d commit fb6241c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,36 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
## Client API

@spec symbols(String.t()) :: {:ok, [symbol_information_t]}
def symbols(query) do
def symbols(query, server \\ __MODULE__) do
results =
case query do
"f " <> fun_query ->
query(:functions, fun_query)
query(:functions, fun_query, server)

"t " <> type_query ->
query(:types, type_query)
query(:types, type_query, server)

"c " <> callback_query ->
query(:callbacks, callback_query)
query(:callbacks, callback_query, server)

module_query ->
query(:modules, module_query)
query(:modules, module_query, server)
end

{:ok, results}
end

def start_link(opts) do
GenServer.start_link(__MODULE__, :ok, opts |> Keyword.put(:name, __MODULE__))
GenServer.start_link(__MODULE__, :ok, opts |> Keyword.put_new(:name, __MODULE__))
end

def notify_build_complete do
GenServer.cast(__MODULE__, :build_complete)
def notify_build_complete(server \\ __MODULE__) do
GenServer.cast(server, :build_complete)
end

@spec notify_uris_modified([String.t()]) :: :ok
def notify_uris_modified(uris) do
GenServer.cast(__MODULE__, {:uris_modified, uris})
def notify_uris_modified(uris, server \\ __MODULE__) do
GenServer.cast(server, {:uris_modified, uris})
end

## Server Callbacks
Expand Down Expand Up @@ -351,13 +351,13 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
|> elem(0)
end

defp query(kind, query) do
defp query(kind, query, server) do
case String.trim(query) do
"" ->
[]

trimmed ->
GenServer.call(__MODULE__, {:query, kind, trimmed})
GenServer.call(server, {:query, kind, trimmed})
end
end

Expand Down
6 changes: 4 additions & 2 deletions apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ defmodule ElixirLS.LanguageServer.Server do
_ -> handle_build_result(:error, [Build.exception_to_diagnostic(reason)], state)
end

if reason == :normal do
WorkspaceSymbols.notify_build_complete()
end

state = if state.needs_build?, do: trigger_build(state), else: state
{:noreply, state}
end
Expand Down Expand Up @@ -670,8 +674,6 @@ defmodule ElixirLS.LanguageServer.Server do
GenServer.reply(from, contracts)
end

WorkspaceSymbols.notify_build_complete()

%{state | analysis_ready?: true, awaiting_contracts: dirty}
else
state
Expand Down
32 changes: 13 additions & 19 deletions apps/language_server/test/providers/workspace_symbols_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
use ExUnit.Case, async: false
alias ElixirLS.LanguageServer.Providers.WorkspaceSymbols

@server_name WorkspaceSymbolsTestServer

setup do
alias ElixirLS.Utils.PacketCapture
packet_capture = start_supervised!({PacketCapture, self()})
Process.register(packet_capture, :elixir_ls_test_process)

pid =
case WorkspaceSymbols.start_link([]) do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
{:ok, pid} = WorkspaceSymbols.start_link(name: @server_name)

state = :sys.get_state(pid)

Expand All @@ -31,19 +29,15 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
}
end)

WorkspaceSymbols.notify_build_complete()
WorkspaceSymbols.notify_build_complete(@server_name)

wait_until_indexed(pid)

on_exit(fn ->
:sys.replace_state(pid, fn _ -> state end)
end)

{:ok, %{}}
end

test "empty query" do
assert {:ok, []} == WorkspaceSymbols.symbols("")
assert {:ok, []} == WorkspaceSymbols.symbols("", @server_name)

assert_receive %{
"method" => "window/logMessage",
Expand All @@ -62,7 +56,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
},
name: "ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols"
}
]} = WorkspaceSymbols.symbols("ElixirLS.LanguageServer.Fixtures.")
]} = WorkspaceSymbols.symbols("ElixirLS.LanguageServer.Fixtures.", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

Expand All @@ -76,7 +70,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
},
name: "ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols"
}
]} = WorkspaceSymbols.symbols("work")
]} = WorkspaceSymbols.symbols("work", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")
end
Expand Down Expand Up @@ -129,7 +123,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.__info__/1"
}
]
} = WorkspaceSymbols.symbols("f ElixirLS.LanguageServer.Fixtures.")
} = WorkspaceSymbols.symbols("f ElixirLS.LanguageServer.Fixtures.", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

Expand All @@ -143,7 +137,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_function/1"
}
]} = WorkspaceSymbols.symbols("f fun")
]} = WorkspaceSymbols.symbols("f fun", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")
end
Expand All @@ -168,7 +162,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
name: "t ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_opaque_type/0"
}
]
} = WorkspaceSymbols.symbols("t ElixirLS.LanguageServer.Fixtures.")
} = WorkspaceSymbols.symbols("t ElixirLS.LanguageServer.Fixtures.", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

Expand All @@ -184,7 +178,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
name: "t ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_opaque_type/0"
}
]
} = WorkspaceSymbols.symbols("t opa")
} = WorkspaceSymbols.symbols("t opa", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")
end
Expand All @@ -209,7 +203,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
name: "c ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_macrocallback/1"
}
]
} = WorkspaceSymbols.symbols("c ElixirLS.LanguageServer.Fixtures.")
} = WorkspaceSymbols.symbols("c ElixirLS.LanguageServer.Fixtures.", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

Expand All @@ -223,7 +217,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
},
name: "c ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_macrocallback/1"
}
]} = WorkspaceSymbols.symbols("c macr")
]} = WorkspaceSymbols.symbols("c macr", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")
end
Expand Down
14 changes: 6 additions & 8 deletions apps/language_server/test/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,13 @@ defmodule ElixirLS.LanguageServer.ServerTest do
"uri" => ^error_file,
"diagnostics" => [
%{
"message" =>
"(CompileError) undefined function does_not_exist" <>
_,
"message" => "(CompileError) undefined function does_not_exist" <> _,
"range" => %{"end" => %{"line" => 3}, "start" => %{"line" => 3}},
"severity" => 1
}
]
})
}),
1000
end)
end

Expand All @@ -288,14 +287,13 @@ defmodule ElixirLS.LanguageServer.ServerTest do
"uri" => ^error_file,
"diagnostics" => [
%{
"message" =>
"(SyntaxError) syntax error before: ','" <>
_,
"message" => "(SyntaxError) syntax error before: ','" <> _,
"range" => %{"end" => %{"line" => 1}, "start" => %{"line" => 1}},
"severity" => 1
}
]
})
}),
1000
end)
end

Expand Down

0 comments on commit fb6241c

Please sign in to comment.