-
Notifications
You must be signed in to change notification settings - Fork 204
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
Suggest an appropriate module name with the 'defmodule' snippet #684
Changes from all commits
37ffbef
3935a16
036901c
dd6c054
d3b624f
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 |
---|---|---|
|
@@ -565,7 +565,9 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do | |
completion | ||
end | ||
|
||
if snippet = snippet_for({origin, name}, context) do | ||
file_path = Keyword.get(options, :file_path) | ||
|
||
if snippet = snippet_for({origin, name}, Map.put(context, :file_path, file_path)) do | ||
%{completion | insert_text: snippet, kind: :snippet, label: name} | ||
else | ||
completion | ||
|
@@ -576,6 +578,12 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do | |
nil | ||
end | ||
|
||
defp snippet_for({"Kernel", "defmodule"}, %{file_path: file_path}) when is_binary(file_path) do | ||
# In a mix project the file_path can be something like "/some/code/path/project/lib/project/sub_path/my_file.ex" | ||
# so we'll try to guess the appropriate module name from the path | ||
"defmodule #{suggest_module_name(file_path)}$1 do\n\t$0\nend" | ||
end | ||
|
||
defp snippet_for(key, %{pipe_before?: true}) do | ||
# Get pipe-friendly version of snippet if available, otherwise fallback to standard | ||
Map.get(@pipe_func_snippets, key) || Map.get(@func_snippets, key) | ||
|
@@ -593,6 +601,75 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do | |
end | ||
end | ||
|
||
def suggest_module_name(file_path) when is_binary(file_path) do | ||
file_path | ||
|> Path.split() | ||
|> Enum.reverse() | ||
|> do_suggest_module_name() | ||
end | ||
|
||
defp do_suggest_module_name([]), do: nil | ||
|
||
defp do_suggest_module_name([filename | reversed_path]) do | ||
filename | ||
|> String.split(".") | ||
|> case do | ||
[file, "ex"] -> | ||
do_suggest_module_name(reversed_path, [file], topmost_parent: "lib") | ||
|
||
[file, "exs"] -> | ||
if String.ends_with?(file, "_test") do | ||
do_suggest_module_name(reversed_path, [file], topmost_parent: "test") | ||
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 seen code with tests in lib folder, it was assumed, that it gives better test coverage. 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. @sharpfun Thanks for the suggestions, but I'm hesitant to go any further at this point before anyone of the maintainers chimes in because it might be the wrong way to go about it 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. We can't make this work for everyone. I'd aim for covering basic mix project and phoenix app as those are most commonly used. |
||
else | ||
nil | ||
end | ||
|
||
_otherwise -> | ||
nil | ||
end | ||
end | ||
|
||
defp do_suggest_module_name([dir | _rest], module_name_acc, topmost_parent: topmost) | ||
when dir == topmost do | ||
module_name_acc | ||
|> Enum.map(&Macro.camelize/1) | ||
|> Enum.join(".") | ||
end | ||
|
||
defp do_suggest_module_name( | ||
[probable_phoenix_dir | [project_web_dir | _] = rest], | ||
module_name_acc, | ||
opts | ||
) | ||
when probable_phoenix_dir in [ | ||
"controllers", | ||
"views", | ||
"channels", | ||
"plugs", | ||
"endpoints", | ||
"sockets" | ||
] do | ||
if String.ends_with?(project_web_dir, "_web") do | ||
# by convention Phoenix doesn't use these folders as part of the module names | ||
# for modules located inside them, so we'll try to do the same | ||
do_suggest_module_name(rest, module_name_acc, opts) | ||
else | ||
# when not directly under the *_web folder however then we should make the folder | ||
# part of the module's name | ||
do_suggest_module_name(rest, [probable_phoenix_dir | module_name_acc], opts) | ||
end | ||
end | ||
|
||
defp do_suggest_module_name([dir_name | rest], module_name_acc, opts) do | ||
do_suggest_module_name(rest, [dir_name | module_name_acc], opts) | ||
end | ||
|
||
defp do_suggest_module_name([], _module_name_acc, _opts) do | ||
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. is it optimal to go all the way up to |
||
# we went all the way up without ever encountering a 'lib' or a 'test' folder | ||
# so we ignore the accumulated module name because it's probably wrong/useless | ||
nil | ||
end | ||
|
||
def function_snippet(name, args, arity, opts) do | ||
snippets_supported? = Keyword.get(opts, :snippets_supported, false) | ||
trigger_signature? = Keyword.get(opts, :trigger_signature?, false) | ||
|
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.
Usually phoenix projects have test/support folder that have these files:
Factories or other helpers are created in that folder.
Based on file extension topmost_parent will be "lib" which would be incorrect