Suggest an appropriate module name with the 'defmodule' snippet#684
Suggest an appropriate module name with the 'defmodule' snippet#684lukaszsamson merged 5 commits intoelixir-lsp:masterfrom
Conversation
| |> String.split(".") | ||
| |> case do | ||
| [file, "ex"] -> | ||
| do_suggest_module_name(reversed_path, [file], topmost_parent: "lib") |
There was a problem hiding this comment.
Usually phoenix projects have test/support folder that have these files:
channel_case.ex
conn_case.ex
data_case.ex
Factories or other helpers are created in that folder.
Based on file extension topmost_parent will be "lib" which would be incorrect
|
|
||
| [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.
I've seen code with tests in lib folder, it was assumed, that it gives better test coverage.
Maybe there is a way to get root folder of the project?
Umbrella projects will definitely complicate root folder solution :/
Or both "lib" and "test" could be used as topmost_parent.
What do you think?
There was a problem hiding this comment.
@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.
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.
| defp snippet_for({"Kernel", "defmodule"}, %{uri: uri}) when is_binary(uri) do | ||
| # In a mix project the uri can be something like "/some/code/path/project/lib/project/sub_path/myfile.ex" | ||
| # so we'll try to guess the appropriate module name from the path | ||
| "defmodule #{suggest_module_name(uri)}$1 do\n\t$0\nend" |
There was a problem hiding this comment.
convert URI to path. SourceFile has function for that
| 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.
is it optimal to go all the way up to / or C:\? We could stop at workdir level.
|
Nice feature @bottlenecked. I'd be happy to marge it. Can you verify if it works with umbrella apps and on windows? Those two are often tricky. |
|
Thanks @lukaszsamson for the encouragement. So I had some time to move forward with this to basically implement some of the suggestions here
What I still haven't done is
|
|
Some more thoughts on stopping at the project_dir level. It seems to me that the way the code is written, most of the times the traversal would stop at either the |
|
I managed to test this on Windows (virtualbox VM on mac) and as windows_suggest.mov |
Some plugin or setting in my editor trimmed extra spaces from the lines but in this case it cause a test to break by changing the cursor position of the auto-completion trigger
|
In d3b624f the broken test is fixed (extra space at end of line was previously trimmed out and now restored) and also removed the debug |
|
Much needed. Now i can get rid of the "Insert Elixir Module name here" extension. Genius! Any idea when a new release will come out that contains this? |
|
After having used this for a long while now, I just wanted to chime in and say thanks again for this. It is such a great addition! 🙏🏻 ❤️ |
Hi all, this is a feature that I always wanted to have and now took some time to implement.
Basically when creating a new file and writing a
defmod...prompt for auto-completion I wanted a likely module_name to be suggested along with completing thedefmodule do .. endsnippet.This is what my attempt looks like
suggest_module_name.mov
There are some limitations I'm aware of (and probably tens of others that I'm not aware of!)
MyProjectWeb.Controllers.PageControllerinstead ofMyProjectWeb.PageControllerStill, do let me know if this PR is on the right track and if so also let me know if these limitations (and possibly others) should be fixed before attempting to ship something like this to the users.
Thanks for taking the time to review!