Skip to content

Commit

Permalink
Suggest behavior callbacks (lexical-lsp#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
doughsay authored Jun 5, 2023
1 parent 30b96d9 commit 1cc39ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callable do
alias Lexical.RemoteControl.Completion.Result
alias Lexical.Server.CodeIntelligence.Completion.Env

@callables [Result.Function, Result.Macro]
@callables [Result.Function, Result.Macro, Result.Callback]

def completion(%callable_module{arity: 0} = callable, %Env{} = env)
when callable_module in @callables do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callback do
alias Lexical.RemoteControl.Completion.Result
alias Lexical.Server.CodeIntelligence.Completion.Env
alias Lexical.Server.CodeIntelligence.Completion.Translatable
alias Lexical.Server.CodeIntelligence.Completion.Translations.Callable

use Translatable.Impl, for: Result.Callback

def translate(%Result.Callback{} = callback, _builder, %Env{} = env) do
Callable.completion(callback, env)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule Lexical.Server.CodeIntelligence.Completion.Translations.CallbackTest do
use Lexical.Test.Server.CompletionCase

describe "callback completions" do
test "suggest callbacks", %{project: project} do
source = ~q[
defmodule MyServer do
use GenServer
def handle_inf|
end
]

{:ok, completion} =
project
|> complete(source)
|> fetch_completion(kind: :function)

assert completion.insert_text == "handle_info(${1:msg}, ${2:state})"
end

test "do not add parens if they're already present", %{project: project} do
source = ~q[
defmodule MyServer do
use GenServer
def handle_inf|(msg, state)
end
]

{:ok, completion} =
project
|> complete(source)
|> fetch_completion(kind: :function)

assert completion.insert_text == "handle_info"
end
end
end

0 comments on commit 1cc39ed

Please sign in to comment.