-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5c9c0a
commit 9a4860a
Showing
7 changed files
with
348 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
defmodule NextLS.SignatureHelp do | ||
@moduledoc false | ||
|
||
import NextLS.DB.Query | ||
|
||
alias GenLSP.Structures.ParameterInformation | ||
alias GenLSP.Structures.SignatureHelp | ||
alias GenLSP.Structures.SignatureInformation | ||
alias NextLS.ASTHelpers | ||
alias NextLS.DB | ||
|
||
def fetch(file, {line, col}, db, _logger) do | ||
code = File.read!(file) | ||
|
||
{mod, func} = | ||
ASTHelpers.Functions.get_function_name_from_params(code, line, col) | ||
|
||
query = | ||
~Q""" | ||
SELECT | ||
* | ||
FROM | ||
symbols | ||
WHERE | ||
symbols.module = ? | ||
AND symbols.name = ?; | ||
""" | ||
|
||
args = [Enum.map_join(mod, ".", &Atom.to_string/1), Atom.to_string(func)] | ||
|
||
symbol = DB.query(db, query, args) | ||
|
||
result = | ||
case symbol do | ||
nil -> | ||
nil | ||
|
||
[] -> | ||
nil | ||
|
||
[[_, _mod, file, type, label, line, col | _] | _] = _definition -> | ||
if type in ["def", "defp"] do | ||
code = File.read!(file) | ||
|
||
params = | ||
code | ||
|> ASTHelpers.Functions.get_function_params(label, line, col) | ||
|> Enum.map(fn {name, _, _} -> | ||
%ParameterInformation{ | ||
label: Atom.to_string(name) | ||
} | ||
end) | ||
|
||
%SignatureHelp{ | ||
signatures: [ | ||
%SignatureInformation{ | ||
label: label, | ||
documentation: "need help", | ||
parameters: params | ||
# active_parameter: 0 | ||
} | ||
], | ||
# active_signature: 1, | ||
active_parameter: 0 | ||
} | ||
else | ||
nil | ||
end | ||
end | ||
|
||
result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
defmodule NextLS.SignatureHelpTest do | ||
use ExUnit.Case, async: true | ||
|
||
import GenLSP.Test | ||
import NextLS.Support.Utils | ||
|
||
@moduletag :tmp_dir | ||
|
||
describe "function" do | ||
@describetag root_paths: ["my_proj"] | ||
setup %{tmp_dir: tmp_dir} do | ||
File.mkdir_p!(Path.join(tmp_dir, "my_proj/lib")) | ||
File.write!(Path.join(tmp_dir, "my_proj/mix.exs"), mix_exs()) | ||
[cwd: tmp_dir] | ||
end | ||
|
||
setup %{cwd: cwd} do | ||
remote = Path.join(cwd, "my_proj/lib/remote.ex") | ||
|
||
File.write!(remote, """ | ||
defmodule Remote do | ||
def bang!(bang) do | ||
bang | ||
end | ||
end | ||
""") | ||
|
||
imported = Path.join(cwd, "my_proj/lib/imported.ex") | ||
|
||
File.write!(imported, """ | ||
defmodule Imported do | ||
def boom(boom1, _boom2) do | ||
boom1 | ||
end | ||
end | ||
""") | ||
|
||
bar = Path.join(cwd, "my_proj/lib/bar.ex") | ||
|
||
File.write!(bar, """ | ||
defmodule Bar do | ||
def run() do | ||
Remote.bang!() | ||
end | ||
end | ||
""") | ||
|
||
[bar: bar, imported: imported, remote: remote] | ||
end | ||
|
||
setup :with_lsp | ||
|
||
test "get signature help", %{client: client, bar: bar} = context do | ||
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}}) | ||
|
||
assert_is_ready(context, "my_proj") | ||
assert_notification "$/progress", %{"value" => %{"kind" => "end", "message" => "Finished indexing!"}} | ||
|
||
uri = uri(bar) | ||
|
||
request(client, %{ | ||
method: "textDocument/signatureHelp", | ||
id: 4, | ||
jsonrpc: "2.0", | ||
params: %{ | ||
position: %{line: 3, character: 16}, | ||
textDocument: %{uri: uri} | ||
} | ||
}) | ||
|
||
assert_result 4, %{ | ||
"activeParameter" => 0, | ||
"activeSignature" => 0, | ||
"signatures" => [ | ||
%{ | ||
"activeParameter" => 0, | ||
"parameters" => [ | ||
%{"label" => "bang"} | ||
], | ||
"documentation" => "need help", | ||
"label" => "bang!" | ||
} | ||
] | ||
} | ||
end | ||
|
||
test "get signature help 2", %{client: client, bar: bar} = context do | ||
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}}) | ||
|
||
assert_is_ready(context, "my_proj") | ||
assert_notification "$/progress", %{"value" => %{"kind" => "end", "message" => "Finished indexing!"}} | ||
|
||
uri = uri(bar) | ||
|
||
request(client, %{ | ||
method: "textDocument/signatureHelp", | ||
id: 4, | ||
jsonrpc: "2.0", | ||
params: %{ | ||
position: %{line: 8, character: 10}, | ||
textDocument: %{uri: uri} | ||
} | ||
}) | ||
|
||
assert_result 4, %{ | ||
"activeParameter" => 0, | ||
"activeSignature" => 0, | ||
"signatures" => [ | ||
%{ | ||
"activeParameter" => 0, | ||
"parameters" => [ | ||
%{"label" => "bang"} | ||
], | ||
"documentation" => "need help", | ||
"label" => "bang!" | ||
} | ||
] | ||
} | ||
end | ||
end | ||
end |
Oops, something went wrong.