Skip to content

Commit

Permalink
Only emit a single snippet for 'use'
Browse files Browse the repository at this point in the history
  • Loading branch information
scohen committed Aug 22, 2023
1 parent 427cade commit 23d544d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do

use Translatable.Impl, for: Candidate.Macro

@snippet_macros ~w(def defp defmacro defmacrop defimpl defmodule defprotocol defguard defguardp defexception test)
@snippet_macros ~w(def defp defmacro defmacrop defimpl defmodule defprotocol defguard defguardp defexception test use)

def translate(%Candidate.Macro{name: name}, _builder, _env)
when name in ["__before_compile__", "__using__", "__after_compile__"] do
Expand Down Expand Up @@ -248,6 +248,18 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
|> builder.boost()
end

def translate(%Candidate.Macro{name: "use", arity: 1}, builder, env) do
label = "use (invoke another module's __using__ macro)"
snippet = "use $0"

env
|> builder.snippet(snippet,
kind: :class,
label: label
)
|> builder.boost()
end

def translate(%Candidate.Macro{name: "require" <> _, arity: 2} = macro, builder, env) do
label = "#{macro.name} (require a module's macros)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.insert_text == "alias $0"
end

test "use returns a snippet", %{project: project} do
assert {:ok, completion} =
project
|> complete("us|")
|> fetch_completion("use ")

assert completion.label == "use (invoke another module's __using__ macro)"
assert completion.insert_text_format == :snippet
assert completion.insert_text == "use $0"
end

test "import returns a snippet", %{project: project} do
assert {:ok, completion} =
project
Expand Down

0 comments on commit 23d544d

Please sign in to comment.