Skip to content

Commit

Permalink
Improve macro completions
Browse files Browse the repository at this point in the history
- Removes unnecessary `$0` from end of some snippets, as it introduces
  trailing whitespace
- Use more consistent capitalization for labels
- Improved snippets for `defdelegate`, `defguard`, `defguardp`, `with`,
  `quote`
  • Loading branch information
zachallaun committed Oct 13, 2023
1 parent bb28384 commit 4df4961
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
label = "#{macro.name} (Define a protocol)"

snippet = """
defprotocol ${1:protocol name} do
defprotocol ${1:protocol_name} do
$0
end\
"""
Expand All @@ -133,7 +133,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
label = "#{macro.name} (Define a protocol implementation)"

snippet = """
defimpl ${1:protocol name}, for: ${2:type} do
defimpl ${1:protocol_name}, for: ${2:struct_name} do
$0
end\
"""
Expand All @@ -150,7 +150,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
def translate(%Candidate.Macro{name: "defoverridable"} = macro, builder, env) do
label = "#{macro.name} (Mark a function as overridable)"

snippet = "defoverridable ${1:keyword or behaviour} $0"
snippet = "defoverridable ${1:fun_arities_or_behaviour}"

env
|> builder.snippet(snippet,
Expand All @@ -164,7 +164,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
def translate(%Candidate.Macro{name: "defdelegate", arity: 2} = macro, builder, env) do
label = "#{macro.name} (Define a delegate function)"

snippet = "defdelegate ${1:call}, to: ${2:module} $0"
snippet = "defdelegate ${1:call}(${2:args}), to: ${3:module}"

env
|> builder.snippet(snippet,
Expand All @@ -178,7 +178,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
def translate(%Candidate.Macro{name: "defguard", arity: 1} = macro, builder, env) do
label = "#{macro.name} (Define a guard macro)"

snippet = "defguard ${1:call} $0"
snippet = "defguard ${1:guard}(${2:args}) when $0"

env
|> builder.snippet(snippet,
Expand All @@ -192,7 +192,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
def translate(%Candidate.Macro{name: "defguardp", arity: 1} = macro, builder, env) do
label = "#{macro.name} (Define a private guard macro)"

snippet = "defguardp ${1:call} $0"
snippet = "defguardp ${1:guard}(${2:args}) when $0"

env
|> builder.snippet(snippet,
Expand All @@ -206,7 +206,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
def translate(%Candidate.Macro{name: "defexception", arity: 1} = macro, builder, env) do
label = "#{macro.name} (Define an exception)"

snippet = "defexception [${1:fields}] $0"
snippet = "defexception [${1::message}]"

env
|> builder.snippet(snippet,
Expand All @@ -220,7 +220,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
def translate(%Candidate.Macro{name: "defstruct", arity: 1} = macro, builder, env) do
label = "#{macro.name} (Define a struct)"

snippet = "defstruct [${1:fields}] $0"
snippet = "defstruct [${1:fields}]"

env
|> builder.snippet(snippet,
Expand All @@ -232,7 +232,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

def translate(%Candidate.Macro{name: "alias", arity: 2} = macro, builder, env) do
label = "#{macro.name} (alias a module's name)"
label = "#{macro.name} (Alias a module's name)"

snippet = "alias $0"

Expand All @@ -246,7 +246,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

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

env
Expand All @@ -258,7 +258,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

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

snippet = "require $0"

Expand All @@ -275,7 +275,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
label = "#{macro.name} (quote block)"

snippet = """
quote ${1:options} do
quote $1 do
$0
end\
"""
Expand All @@ -294,7 +294,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do

snippet = """
receive do
${1:message shape} -> $0
${1:pattern} -> $0
end\
"""

Expand Down Expand Up @@ -326,10 +326,10 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

def translate(%Candidate.Macro{name: "with" <> _, arity: 1} = macro, builder, env) do
label = "with block"
label = "#{macro.name} (with statement)"

snippet = """
with ${1:match} do
with ${1:pattern} <- ${2:expression} do
$0
end\
"""
Expand All @@ -344,11 +344,11 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

def translate(%Candidate.Macro{name: "case", arity: 2} = macro, builder, env) do
label = "#{macro.name} (Case statement)"
label = "#{macro.name} (case statement)"

snippet = """
case ${1:test} do
${2:match} -> $0
case $1 do
${2:pattern} -> $0
end\
"""

Expand All @@ -362,10 +362,10 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

def translate(%Candidate.Macro{name: "if", arity: 2} = macro, builder, env) do
label = "#{macro.name} (If statement)"
label = "#{macro.name} (if statement)"

snippet = """
if ${1:test} do
if $1 do
$0
end\
"""
Expand All @@ -380,7 +380,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

def translate(%Candidate.Macro{name: "import", arity: 2} = macro, builder, env) do
label = "#{macro.name} (import a module's functions)"
label = "#{macro.name} (Import a module's functions)"

snippet = "import $0"

Expand All @@ -394,10 +394,10 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

def translate(%Candidate.Macro{name: "unless", arity: 2} = macro, builder, env) do
label = "#{macro.name} (Unless statement)"
label = "#{macro.name} (unless statement)"

snippet = """
unless ${1:test} do
unless $1 do
$0
end\
"""
Expand All @@ -412,7 +412,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
end

def translate(%Candidate.Macro{name: "cond"} = macro, builder, env) do
label = "#{macro.name} (Cond statement)"
label = "#{macro.name} (cond statement)"

snippet = """
cond do
Expand All @@ -434,7 +434,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Macro do
label = "#{macro.name} (comprehension)"

snippet = """
for ${1:match} <- ${2:enumerable} do
for ${1:pattern} <- ${2:enumerable} do
$0
end\
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
defprotocol ${1:protocol name} do
defprotocol ${1:protocol_name} do
$0
end\
"""
Expand All @@ -222,7 +222,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
defimpl ${1:protocol name}, for: ${2:type} do
defimpl ${1:protocol_name}, for: ${2:struct_name} do
$0
end\
"""
Expand All @@ -238,7 +238,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.label == "defoverridable (Mark a function as overridable)"
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == "defoverridable ${1:keyword or behaviour} $0"
assert apply_completion(completion) == "defoverridable ${1:fun_arities_or_behaviour}"
end

test "defdelegate returns a snippet", %{project: project} do
Expand All @@ -250,7 +250,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.detail
assert completion.label == "defdelegate (Define a delegate function)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "defdelegate ${1:call}, to: ${2:module} $0"
assert apply_completion(completion) == "defdelegate ${1:call}(${2:args}), to: ${3:module}"
end

test "defguard returns a snippet", %{project: project} do
Expand All @@ -262,7 +262,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.detail
assert completion.label == "defguard (Define a guard macro)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "defguard ${1:call} $0"
assert apply_completion(completion) == "defguard ${1:guard}(${2:args}) when $0"
end

test "defguardp returns a snippet", %{project: project} do
Expand All @@ -274,7 +274,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.detail
assert completion.label == "defguardp (Define a private guard macro)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "defguardp ${1:call} $0"
assert apply_completion(completion) == "defguardp ${1:guard}(${2:args}) when $0"
end

test "defexception returns a snippet", %{project: project} do
Expand All @@ -286,7 +286,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.detail
assert completion.label == "defexception (Define an exception)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "defexception [${1:fields}] $0"
assert apply_completion(completion) == "defexception [${1::message}]"
end

test "defstruct returns a snippet", %{project: project} do
Expand All @@ -298,7 +298,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.detail
assert completion.label == "defstruct (Define a struct)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "defstruct [${1:fields}] $0"
assert apply_completion(completion) == "defstruct [${1:fields}]"
end

test "alias returns a snippet", %{project: project} do
Expand All @@ -308,7 +308,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("alias ")

assert completion.detail
assert completion.label == "alias (alias a module's name)"
assert completion.label == "alias (Alias a module's name)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "alias $0"
end
Expand All @@ -319,7 +319,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> complete("us|")
|> fetch_completion("use ")

assert completion.label == "use (invoke another module's __using__ macro)"
assert completion.label == "use (Invoke another module's __using__ macro)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "use $0"
end
Expand All @@ -331,7 +331,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("import")

assert completion.detail
assert completion.label == "import (import a module's functions)"
assert completion.label == "import (Import a module's functions)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "import $0"
end
Expand All @@ -343,7 +343,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("require")

assert completion.detail
assert completion.label == "require (require a module's macros)"
assert completion.label == "require (Require a module's macros)"
assert completion.insert_text_format == :snippet
assert apply_completion(completion) == "require $0"
end
Expand All @@ -359,7 +359,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
quote ${1:options} do
quote $1 do
$0
end\
"""
Expand All @@ -377,7 +377,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do

assert apply_completion(completion) == """
receive do
${1:message shape} -> $0
${1:pattern} -> $0
end\
"""
end
Expand Down Expand Up @@ -406,11 +406,11 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("with")

assert completion.detail
assert completion.label == "with block"
assert completion.label == "with (with statement)"
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
with ${1:match} do
with ${1:pattern} <- ${2:expression} do
$0
end\
"""
Expand All @@ -423,11 +423,11 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("if")

assert completion.detail
assert completion.label == "if (If statement)"
assert completion.label == "if (if statement)"
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
if ${1:test} do
if $1 do
$0
end\
"""
Expand All @@ -440,11 +440,11 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("unless")

assert completion.detail
assert completion.label == "unless (Unless statement)"
assert completion.label == "unless (unless statement)"
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
unless ${1:test} do
unless $1 do
$0
end\
"""
Expand All @@ -457,12 +457,12 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("case")

assert completion.detail
assert completion.label == "case (Case statement)"
assert completion.label == "case (case statement)"
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
case ${1:test} do
${2:match} -> $0
case $1 do
${2:pattern} -> $0
end\
"""
end
Expand All @@ -474,7 +474,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
|> fetch_completion("cond")

assert completion.detail
assert completion.label == "cond (Cond statement)"
assert completion.label == "cond (cond statement)"
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
Expand All @@ -496,7 +496,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.MacroTest do
assert completion.insert_text_format == :snippet

assert apply_completion(completion) == """
for ${1:match} <- ${2:enumerable} do
for ${1:pattern} <- ${2:enumerable} do
$0
end\
"""
Expand Down

0 comments on commit 4df4961

Please sign in to comment.