Skip to content

Commit

Permalink
Consider arity in function ordering (#442)
Browse files Browse the repository at this point in the history
Functions with lower arity (argument count) will appear earlier in the suggestion list.
  • Loading branch information
yerguden authored and scohen committed Nov 8, 2023
1 parent 5d7c6e7 commit 32bfa1d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Builder do
global_boost = Integer.to_string(9 - global_boost)
local_boost = Integer.to_string(9 - local_boost)

sort_text = "0#{global_boost}#{local_boost}_#{item.label}"
stripped_sort_text =
item.sort_text
|> fallback(item.label)
|> strip_boost()

sort_text = "0#{global_boost}#{local_boost}_#{stripped_sort_text}"
%Completion.Item{item | sort_text: sort_text}
end

Expand Down Expand Up @@ -212,4 +217,9 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Builder do
{:ok, length(local_name)}
end
end

@boost_re ~r/^[0-9_]+/
defp strip_boost(sort_text) do
String.replace(sort_text, @boost_re, "")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callable do
|> Builder.snippet(insert_text,
kind: :function,
label: label(callable, env),
sort_text: sort_text(callable),
tags: tags
)
|> maybe_boost(callable)
Expand All @@ -57,7 +58,8 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callable do
|> Builder.plain_text(name_and_arity,
detail: "(Capture)",
kind: :function,
label: name_and_arity
label: name_and_arity,
sort_text: sort_text(callable)
)
|> maybe_boost(callable, 4)

Expand All @@ -66,7 +68,8 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callable do
|> Builder.snippet(callable_snippet(callable, env),
detail: "(Capture with arguments)",
kind: :function,
label: label(callable, env)
label: label(callable, env),
sort_text: sort_text(callable)
)
|> maybe_boost(callable, 4)

Expand Down Expand Up @@ -115,4 +118,13 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callable do
defp name_and_arity(%_{name: name, arity: arity}) do
"#{name}/#{arity}"
end

defp sort_text(%_callable{name: name, arity: arity}) do
normalized_arity =
arity
|> Integer.to_string()
|> String.pad_leading(3, "0")

"#{name}:#{normalized_arity}"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.FunctionTest d
|> Enum.filter(fn completion ->
sort_text = completion.sort_text
# arity 1 and is is_map
not String.contains?(sort_text, ",") and
not String.contains?(sort_text, "/2") and
String.contains?(sort_text, ":001") and
String.contains?(sort_text, "is_map")
end)

Expand Down Expand Up @@ -258,7 +257,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.FunctionTest d

low_priority_completion? = fn fun ->
String.starts_with?(fun.label, "__") or
Enum.any?(defaults, &String.contains?(fun.sort_text, &1))
Enum.any?(defaults, &String.contains?(fun.label, &1))
end

{low_priority_completions, normal_completions} =
Expand All @@ -272,5 +271,19 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.FunctionTest d
assert boosted?(completion)
end
end

test "functions with lower arity have higher completion priority", %{project: project} do
[arity_2, arity_3] =
project
|> complete("Enum.|")
|> fetch_completion("count_until")
|> then(fn {:ok, list} -> list end)
|> Enum.sort_by(& &1.sort_text)

assert apply_completion(arity_2) == "Enum.count_until(${1:enumerable}, ${2:limit})"

assert apply_completion(arity_3) ==
"Enum.count_until(${1:enumerable}, ${2:fun}, ${3:limit})"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,24 @@ defmodule Lexical.Server.CodeIntelligence.CompletionTest do
name: "#{name}-callback",
origin: full_name,
argument_names: [],
metadata: %{}
metadata: %{},
arity: 0
},
%Candidate.Exception{name: "#{name}-exception", full_name: full_name},
%Candidate.Function{name: "my_func", origin: full_name, argument_names: [], metadata: %{}},
%Candidate.Macro{name: "my_macro", origin: full_name, argument_names: [], metadata: %{}},
%Candidate.Function{
name: "my_func",
origin: full_name,
argument_names: [],
metadata: %{},
arity: 0
},
%Candidate.Macro{
name: "my_macro",
origin: full_name,
argument_names: [],
metadata: %{},
arity: 0
},
%Candidate.MixTask{name: "#{name}-mix-task", full_name: full_name},
%Candidate.Module{name: "#{name}-module", full_name: full_name},
%Candidate.Module{name: "#{name}-submodule", full_name: "#{full_name}.Bar"},
Expand Down

0 comments on commit 32bfa1d

Please sign in to comment.