Skip to content

Commit

Permalink
refactor: simplify infer module code (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg authored Mar 24, 2024
1 parent 4151895 commit cb3f10b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 57 deletions.
23 changes: 12 additions & 11 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -580,22 +580,21 @@ defmodule NextLS do
|> Enum.reverse()
|> Enum.join("\n")

results =
lsp.assigns.registry
|> dispatch(:runtimes, fn entries ->
[result] =
{root_path, entries} =
dispatch(lsp.assigns.registry, :runtimes, fn entries ->
[{wuri, result}] =
for {runtime, %{uri: wuri}} <- entries, String.starts_with?(uri, wuri) do
document_slice
|> String.to_charlist()
|> Enum.reverse()
|> NextLS.Autocomplete.expand(runtime, env)
{wuri, document_slice |> String.to_charlist() |> Enum.reverse() |> NextLS.Autocomplete.expand(runtime, env)}
end

case result do
{:yes, entries} -> entries
_ -> []
{:yes, entries} -> {wuri, entries}
_ -> {wuri, []}
end
end)

results =
entries
|> Enum.reduce([], fn %{name: name, kind: kind} = symbol, results ->
{label, kind, docs} =
case kind do
Expand All @@ -617,7 +616,9 @@ defmodule NextLS do
documentation: docs
}

case NextLS.Snippet.get(label, nil, uri: uri) do
root_path = root_path |> URI.parse() |> Map.get(:path)

case NextLS.Snippet.get(label, nil, uri: Path.relative_to(URI.parse(uri).path, root_path)) do
nil -> [completion_item | results]
%{} = snippet -> [Map.merge(completion_item, snippet) | results]
end
Expand Down
54 changes: 13 additions & 41 deletions lib/next_ls/snippet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ defmodule NextLS.Snippet do
def get(label, trigger_character, opts \\ [])

def get("defmodule/2", nil, opts) do
uri = Keyword.get(opts, :uri)
path = Keyword.get(opts, :uri)

modulename =
if uri do
infer_module_name(uri)
if path do
infer_module_name(path)
else
"ModuleName"
end
Expand Down Expand Up @@ -186,43 +186,15 @@ defmodule NextLS.Snippet do
nil
end

defp infer_module_name(uri) do
result =
uri
|> Path.split()
|> Enum.reduce(false, fn
"lib", _ ->
{:lib, []}

"test", _ ->
{:test, []}

"support", {:test, _} ->
{:lib, []}

_, false ->
false

element, {type, elements} ->
camelized =
element
|> Path.rootname()
|> Macro.camelize()

{type, [camelized | elements]}
end)

case result do
{_, parts} ->
parts
|> Enum.reverse()
|> Enum.join(".")

false ->
uri
|> Path.basename()
|> Path.rootname()
|> Macro.camelize()
end
defp infer_module_name(path) do
path
|> Path.rootname()
|> then(fn
"test/support/" <> rest -> rest
"test/" <> rest -> rest
"lib/" <> rest -> rest
path -> path
end)
|> Macro.camelize()
end
end
10 changes: 5 additions & 5 deletions test/next_ls/snippet_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ defmodule NextLS.SnippetTest do
describe "defmodule snippet" do
test "simple module" do
assert %{insert_text: "defmodule ${1:Foo} do\n $0\nend\n", insert_text_format: 2, kind: 15} ==
Snippet.get("defmodule/2", nil, uri: "file:///my_proj/lib/foo.ex")
Snippet.get("defmodule/2", nil, uri: "lib/foo.ex")
end

test "nested module" do
assert %{insert_text: "defmodule ${1:Foo.Bar.Baz} do\n $0\nend\n", insert_text_format: 2, kind: 15} ==
Snippet.get("defmodule/2", nil, uri: "file:///my_proj/lib/foo/bar/baz.ex")
Snippet.get("defmodule/2", nil, uri: "lib/foo/bar/baz.ex")
end

test "test module" do
assert %{insert_text: "defmodule ${1:FooTest} do\n $0\nend\n", insert_text_format: 2, kind: 15} ==
Snippet.get("defmodule/2", nil, uri: "file:///my_proj/test/foo_test.exs")
Snippet.get("defmodule/2", nil, uri: "test/foo_test.exs")
end

test "support test module" do
assert %{insert_text: "defmodule ${1:Foo} do\n $0\nend\n", insert_text_format: 2, kind: 15} ==
Snippet.get("defmodule/2", nil, uri: "file:///my_proj/test/support/foo.ex")
Snippet.get("defmodule/2", nil, uri: "test/support/foo.ex")
end

test "module outside canonical folders" do
assert %{insert_text: "defmodule ${1:Foo} do\n $0\nend\n", insert_text_format: 2, kind: 15} ==
Snippet.get("defmodule/2", nil, uri: "file:///my_proj/foo.ex")
Snippet.get("defmodule/2", nil, uri: "foo.ex")
end

test "without uri" do
Expand Down

0 comments on commit cb3f10b

Please sign in to comment.