Skip to content

Commit

Permalink
feat: document symbols
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
mhanberg committed Jun 27, 2023
1 parent 80d0679 commit 7352dd8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
50 changes: 48 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule NextLS do
}

alias GenLSP.Requests.{
TextDocumentDocumentSymbol,
Initialize,
Shutdown,
TextDocumentFormatting,
Expand All @@ -38,7 +39,8 @@ defmodule NextLS do
TextEdit,
WorkDoneProgressBegin,
WorkDoneProgressEnd,
SymbolInformation
SymbolInformation,
DocumentSymbol
}

alias NextLS.Runtime
Expand Down Expand Up @@ -98,12 +100,56 @@ defmodule NextLS do
change: TextDocumentSyncKind.full()
},
document_formatting_provider: true,
workspace_symbol_provider: true
workspace_symbol_provider: true,
document_symbol_provider: true
},
server_info: %{name: "NextLS"}
}, assign(lsp, root_uri: root_uri)}
end

def handle_request(%TextDocumentDocumentSymbol{params: %{text_document: %{uri: uri}}}, lsp) do
file = URI.parse(uri).path

{mod_symbol, children} =
for %SymbolTable.Symbol{} = symbol <- SymbolTable.symbols(lsp.assigns.symbol_table, file), reduce: {nil, []} do
{mod, children} ->
name =
if symbol.type != :defstruct do
"#{symbol.type} #{symbol.name}"
else
"#{symbol.name}"
end

range = %Range{
start: %Position{
line: symbol.line - 1,
character: symbol.col - 1
},
end: %Position{
line: symbol.line - 1,
character: symbol.col - 1
}
}

sym = %DocumentSymbol{
name: name,
kind: elixir_kind_to_lsp_kind(symbol.type),
range: range,
selection_range: range
}

if symbol.type == :defmodule do
{sym, children}
else
{mod, [sym | children]}
end
end

symbols = %DocumentSymbol{mod_symbol | children: children}

{:reply, [symbols], lsp}
end

def handle_request(%WorkspaceSymbol{params: %{query: query}}, lsp) do
filter = fn sym ->
if query == "" do
Expand Down
33 changes: 29 additions & 4 deletions lib/next_ls/symbol_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ defmodule NextLS.SymbolTable do
@spec symbols(pid() | atom()) :: list(struct())
def symbols(server), do: GenServer.call(server, :symbols)

@spec symbols(pid() | atom(), String.t()) :: list(struct())
def symbols(server, file), do: GenServer.call(server, {:symbols, file})

def close(server), do: GenServer.call(server, :close)

def init(args) do
Expand All @@ -36,10 +39,34 @@ defmodule NextLS.SymbolTable do
{:ok, %{table: name}}
end

def handle_call({:symbols, file}, _, state) do
symbols =
:dets.foldl(
fn {_key, symbol}, acc ->
if file == symbol.file do
[symbol | acc]
else
acc
end
end,
[],
state.table
)
|> Enum.reverse()

{:reply, symbols, state}
end

def handle_call(:symbols, _, state) do
symbols =
:dets.foldl(
fn {_key, symbol}, acc -> [symbol | acc] end,
fn {_key, symbol}, acc ->
if String.match?(to_string(symbol.name), ~r/__.*__/) do
acc
else
[symbol | acc]
end
end,
[],
state.table
)
Expand Down Expand Up @@ -94,9 +121,7 @@ defmodule NextLS.SymbolTable do
)
end

for {name, {:v1, type, _meta, clauses}} <- defs,
not String.match?(to_string(name), ~r/__.*__/),
{meta, _, _, _} <- clauses do
for {name, {:v1, type, _meta, clauses}} <- defs, {meta, _, _, _} <- clauses do
:dets.insert(
state.table,
{mod,
Expand Down

0 comments on commit 7352dd8

Please sign in to comment.