-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: defmodule snippet infer module name #398
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
defmodule NextLS.Snippet do | ||
@moduledoc false | ||
|
||
def get("defmodule/2", nil) do | ||
def get(label, trigger_character, opts \\ []) | ||
|
||
def get("defmodule/2", nil, opts) do | ||
uri = Keyword.get(opts, :uri) | ||
|
||
modulename = | ||
if uri do | ||
infer_module_name(uri) | ||
else | ||
"ModuleName" | ||
end | ||
|
||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
insert_text: """ | ||
defmodule ${1:ModuleName} do | ||
defmodule ${1:#{modulename}} do | ||
$0 | ||
end | ||
""" | ||
} | ||
end | ||
|
||
def get("defstruct/1", nil) do | ||
def get("defstruct/1", nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -23,7 +34,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("defprotocol/2", nil) do | ||
def get("defprotocol/2", nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -35,7 +46,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("defimpl/2", nil) do | ||
def get("defimpl/2", nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -49,7 +60,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("defimpl/3", nil) do | ||
def get("defimpl/3", nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -63,7 +74,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("def/" <> _, nil) do | ||
def get("def/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -75,7 +86,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("defp/" <> _, nil) do | ||
def get("defp/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -87,7 +98,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("defmacro/" <> _, nil) do | ||
def get("defmacro/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -101,7 +112,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("defmacrop/" <> _, nil) do | ||
def get("defmacrop/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -115,7 +126,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("for/" <> _, nil) do | ||
def get("for/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -127,7 +138,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("with/" <> _, nil) do | ||
def get("with/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -139,7 +150,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("case/" <> _, nil) do | ||
def get("case/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -155,7 +166,7 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get("cond/" <> _, nil) do | ||
def get("cond/" <> _, nil, _opts) do | ||
%{ | ||
kind: GenLSP.Enumerations.CompletionItemKind.snippet(), | ||
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(), | ||
|
@@ -171,7 +182,47 @@ defmodule NextLS.Snippet do | |
} | ||
end | ||
|
||
def get(_label, _trigger_character) do | ||
def get(_label, _trigger_character, _opts) do | ||
nil | ||
end | ||
|
||
defp infer_module_name(uri) do | ||
result = | ||
uri | ||
|> Path.split() | ||
|> Enum.reduce(false, fn | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain what the first element of the accumulator signifies here? The clause with support is confusing me a tad. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually the module in
do you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant the tuple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's used here to check if there is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is the line that I am asking about. I'm not sure where you use that later, you said something about tests ending in "Test", but i'm not seeing that either in this code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh my bad, I'll try to explain. I'm looking for three folders
here are some tests that can help https://github.com/elixir-tools/next-ls/blob/9d65b759ac0cf86c943190342cf829097fc62346/test/next_ls/snippet_test.exs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, i understand now, thanks! basically you are just striping There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lucacervello When you use someone else's code, please mention the source. If possible, contacting the author would be best; otherwise, it appears quite unethical. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've removed the code on main. Sorry about that! |
||
"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 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule NextLS.SnippetTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias NextLS.Snippet | ||
|
||
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") | ||
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") | ||
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") | ||
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") | ||
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") | ||
end | ||
|
||
test "without uri" do | ||
assert %{insert_text: "defmodule ${1:ModuleName} do\n $0\nend\n", insert_text_format: 2, kind: 15} == | ||
Snippet.get("defmodule/2", nil) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this change necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
without it
don't trigger autocomplete because there is no previous node
line here: https://github.com/elixir-tools/next-ls/pull/398/files/9d65b759ac0cf86c943190342cf829097fc62346#diff-934a3387a31dbe6cdfbab7475b2f2c24722ff71a7c11b282ef363ac6952f1c9fR19