Skip to content

Commit

Permalink
feat: filter workspace symbols using query (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Jun 25, 2023
1 parent c1aa20c commit 65f4ee4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,17 @@ defmodule NextLS do
}, assign(lsp, root_uri: root_uri)}
end

def handle_request(%WorkspaceSymbol{params: %{query: _query}}, lsp) do
def handle_request(%WorkspaceSymbol{params: %{query: query}}, lsp) do
filter = fn sym ->
if query == "" do
true
else
to_string(sym) =~ query
end
end

symbols =
for %SymbolTable.Symbol{} = symbol <- SymbolTable.symbols(lsp.assigns.symbol_table) do
for %SymbolTable.Symbol{} = symbol <- SymbolTable.symbols(lsp.assigns.symbol_table), filter.(symbol.name) do
%SymbolInformation{
name: to_string(symbol.name),
kind: elixir_kind_to_lsp_kind(symbol.type),
Expand Down
60 changes: 60 additions & 0 deletions test/next_ls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,64 @@ defmodule NextLSTest do
"name" => "Foo.CodeAction.NestedMod"
} in symbols
end

test "workspace symbols with query", %{client: client, cwd: cwd} do
assert :ok ==
notify(client, %{
method: "initialized",
jsonrpc: "2.0",
params: %{}
})

assert_notification "window/logMessage", %{"message" => "[NextLS] Runtime ready..."}
assert_notification "window/logMessage", %{"message" => "[NextLS] Compiled!"}

request client, %{
method: "workspace/symbol",
id: 2,
jsonrpc: "2.0",
params: %{
query: "fo"
}
}

assert_result 2, symbols

assert [
%{
"kind" => 12,
"location" => %{
"range" => %{
"start" => %{
"line" => 3,
"character" => 0
},
"end" => %{
"line" => 3,
"character" => 0
}
},
"uri" => "file://#{cwd}/lib/bar.ex"
},
"name" => "foo"
},
%{
"kind" => 12,
"location" => %{
"range" => %{
"start" => %{
"line" => 4,
"character" => 0
},
"end" => %{
"line" => 4,
"character" => 0
}
},
"uri" => "file://#{cwd}/lib/code_action.ex"
},
"name" => "foo"
}
] == symbols
end
end

0 comments on commit 65f4ee4

Please sign in to comment.