From a32684ac6a3605f436e67879545ade660281d464 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Sun, 25 Jun 2023 15:01:37 -0400 Subject: [PATCH] feat: filter workspace symbols using query --- lib/next_ls.ex | 12 +++++++-- test/next_ls_test.exs | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 9cfeadf4..b81c5ed4 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -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), diff --git a/test/next_ls_test.exs b/test/next_ls_test.exs index 25ef6970..b00db59a 100644 --- a/test/next_ls_test.exs +++ b/test/next_ls_test.exs @@ -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