From cb3f10b755530fb8a03b0a8518169f26e14b6fe9 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Sun, 24 Mar 2024 18:35:59 -0400 Subject: [PATCH] refactor: simplify infer module code (#399) --- lib/next_ls.ex | 23 ++++++++------- lib/next_ls/snippet.ex | 54 +++++++++-------------------------- test/next_ls/snippet_test.exs | 10 +++---- 3 files changed, 30 insertions(+), 57 deletions(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index c51b8e11..b5a2b257 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -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 @@ -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 diff --git a/lib/next_ls/snippet.ex b/lib/next_ls/snippet.ex index 447160cb..22ac13fe 100644 --- a/lib/next_ls/snippet.ex +++ b/lib/next_ls/snippet.ex @@ -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 @@ -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 diff --git a/test/next_ls/snippet_test.exs b/test/next_ls/snippet_test.exs index 6898480b..3f2ffd56 100644 --- a/test/next_ls/snippet_test.exs +++ b/test/next_ls/snippet_test.exs @@ -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