Skip to content

Commit

Permalink
feat: snippets (#385)
Browse files Browse the repository at this point in the history
Closes #59

Co-authored-by: Mitchell Hanberg <[email protected]>
  • Loading branch information
lucacervello and mhanberg committed Mar 8, 2024
1 parent 8ab28a1 commit 92248b6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
21 changes: 14 additions & 7 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ defmodule NextLS do
_ -> []
end
end)
|> Enum.map(fn %{name: name, kind: kind} = symbol ->
|> Enum.reduce([], fn %{name: name, kind: kind} = symbol, results ->
{label, kind, docs} =
case kind do
:struct -> {name, GenLSP.Enumerations.CompletionItemKind.struct(), ""}
Expand All @@ -592,13 +592,20 @@ defmodule NextLS do
_ -> {name, GenLSP.Enumerations.CompletionItemKind.text(), ""}
end

%GenLSP.Structures.CompletionItem{
label: label,
kind: kind,
insert_text: name,
documentation: docs
}
completion_item =
%GenLSP.Structures.CompletionItem{
label: label,
kind: kind,
insert_text: name,
documentation: docs
}

case NextLS.Snippet.get(label, nil) do
nil -> [completion_item | results]
%{} = snippet -> [Map.merge(completion_item, snippet) | results]
end
end)
|> Enum.reverse()

{:reply, results, lsp}
rescue
Expand Down
104 changes: 46 additions & 58 deletions lib/next_ls/snippet.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defmodule NextLS.Snippet do
@moduledoc false

def get("defmodule" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("defmodule/2", nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -14,9 +13,8 @@ defmodule NextLS.Snippet do
}
end

def get("defstruct" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("defstruct/1", nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -25,9 +23,8 @@ defmodule NextLS.Snippet do
}
end

def get("defprotocol" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("defprotocol/2", nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -38,38 +35,36 @@ defmodule NextLS.Snippet do
}
end

def get("defimpl" = label, nil) do
[
%GenLSP.Structures.CompletionItem{
label: label,
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
defimpl ${1:ProtocolName} do
def ${2:function_name}(${3:parameter_name}) do
$0
end
def get("defimpl/2", nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
defimpl ${1:ProtocolName} do
def ${2:function_name}(${3:parameter_name}) do
$0
end
"""
},
%GenLSP.Structures.CompletionItem{
label: label <> "f",
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
defimpl ${1:ProtocolName}, for: ${2:StructName} do
def ${3:function_name}(${4:parameter_name}) do
$0
end
end
"""
}
end

def get("defimpl/3", nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
defimpl ${1:ProtocolName}, for: ${2:StructName} do
def ${3:function_name}(${4:parameter_name}) do
$0
end
"""
}
]
end
"""
}
end

def get("def" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("def/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -80,9 +75,8 @@ defmodule NextLS.Snippet do
}
end

def get("defp" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("defp/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -93,9 +87,8 @@ defmodule NextLS.Snippet do
}
end

def get("defmacro" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("defmacro/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -108,9 +101,8 @@ defmodule NextLS.Snippet do
}
end

def get("defmacrop" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("defmacrop/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -123,9 +115,8 @@ defmodule NextLS.Snippet do
}
end

def get("for" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("for/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -136,9 +127,8 @@ defmodule NextLS.Snippet do
}
end

def get("with" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("with/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -149,9 +139,8 @@ defmodule NextLS.Snippet do
}
end

def get("case" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("case/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand All @@ -166,9 +155,8 @@ defmodule NextLS.Snippet do
}
end

def get("cond" = label, nil) do
%GenLSP.Structures.CompletionItem{
label: label,
def get("cond/" <> _, nil) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
Expand Down

0 comments on commit 92248b6

Please sign in to comment.