Skip to content
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

feat: completions #289

Merged
merged 11 commits into from
Oct 19, 2023
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Still in heavy development, currently supporting the following features:
- Extensions
- Credo
- Hover
- Completions †‡

† - denotes an experimental feature, which can be toggled on/off.
‡ - denotes a partially implemented feature.

## Supported Elixir Versions

Expand Down
79 changes: 77 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule NextLS do
alias GenLSP.Notifications.WorkspaceDidChangeWorkspaceFolders
alias GenLSP.Requests.Initialize
alias GenLSP.Requests.Shutdown
alias GenLSP.Requests.TextDocumentCompletion
alias GenLSP.Requests.TextDocumentDefinition
alias GenLSP.Requests.TextDocumentDocumentSymbol
alias GenLSP.Requests.TextDocumentFormatting
Expand Down Expand Up @@ -117,6 +118,14 @@ defmodule NextLS do
save: %SaveOptions{include_text: true},
change: TextDocumentSyncKind.full()
},
completion_provider:
if init_opts.experimental.completions.enable do
%GenLSP.Structures.CompletionOptions{
trigger_characters: [".", "@", "&", "%", "^", ":", "!", "-", "~", "/", "{"]
}
else
nil
end,
document_formatting_provider: true,
hover_provider: true,
workspace_symbol_provider: true,
Expand Down Expand Up @@ -504,6 +513,60 @@ defmodule NextLS do
resp
end

def handle_request(%TextDocumentCompletion{params: %{text_document: %{uri: uri}, position: position}}, lsp) do
document = lsp.assigns.documents[uri]

document_slice =
document
|> Enum.take(position.line + 1)
|> Enum.reverse()
|> then(fn [last_line | rest] ->
{line, _forget} = String.split_at(last_line, position.character)
[line | rest]
end)
|> Enum.reverse()
|> Enum.join("\n")

results =
lsp.assigns.registry
|> dispatch(:runtimes, fn entries ->
[result] =
for {runtime, %{uri: wuri}} <- entries, String.starts_with?(uri, wuri) do
NextLS.Autocomplete.expand(document_slice |> String.to_charlist() |> Enum.reverse(), runtime)
end

case result do
{:yes, entries} -> entries
_ -> []
end
end)
|> Enum.map(fn %{name: name, kind: kind} = symbol ->
{label, kind, docs} =
case kind do
:struct -> {name, GenLSP.Enumerations.CompletionItemKind.struct(), ""}
:function -> {"#{name}/#{symbol.arity}", GenLSP.Enumerations.CompletionItemKind.function(), symbol.docs}
:module -> {name, GenLSP.Enumerations.CompletionItemKind.module(), ""}
:variable -> {name, GenLSP.Enumerations.CompletionItemKind.variable(), ""}
:dir -> {name, GenLSP.Enumerations.CompletionItemKind.folder(), ""}
:file -> {name, GenLSP.Enumerations.CompletionItemKind.file(), ""}
:keyword -> {name, GenLSP.Enumerations.CompletionItemKind.field(), ""}
_ -> {name, GenLSP.Enumerations.CompletionItemKind.text(), ""}
end

%GenLSP.Structures.CompletionItem{
label: label,
kind: kind,
insert_text: name,
documentation: docs
}
end)

{:reply, results, lsp}
rescue
_ ->
{:reply, [], lsp}
end

def handle_request(%Shutdown{}, lsp) do
{:reply, nil, assign(lsp, exit_code: 0)}
end
Expand Down Expand Up @@ -1019,18 +1082,30 @@ defmodule NextLS do
# penalty for unmatched letter
defp calc_unmatched_penalty(score, _traits), do: score - 1

defmodule InitOpts.Experimental do
@moduledoc false
defstruct completions: %{enable: false}
end

defmodule InitOpts do
@moduledoc false
import Schematic

defstruct mix_target: "host", mix_env: "dev"
defstruct mix_target: "host", mix_env: "dev", experimental: %NextLS.InitOpts.Experimental{}

def validate(opts) do
schematic =
nullable(
schema(__MODULE__, %{
optional(:mix_target) => str(),
optional(:mix_env) => str()
optional(:mix_env) => str(),
optional(:experimental) =>
schema(NextLS.InitOpts.Experimental, %{
optional(:completions) =>
map(%{
{"enable", :enable} => bool()
})
})
})
)

Expand Down
Loading
Loading