sublimelsp / LSP-elixir

Elixir support for Sublime LSP plugin

Home Page:https://lsp.sublimetext.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Format document raises an exception

malaire opened this issue · comments

When I right-click in Elixir file and select "LSP > Format Document", status bar shows "an exception was raised:" and file is not formatted. I get this error in LSP Log Panel:

lsp-elixir: 
22:18:21.256 [error] Process #PID<0.125.0> raised an exception
** (FunctionClauseError) no function clause matching in String.downcase/2
    (elixir 1.10.4) lib/string.ex:813: String.downcase(nil, :default)
    (language_server 0.5.0) lib/language_server/providers/formatting.ex:35: ElixirLS.LanguageServer.Providers.Formatting.can_format?/2
    (language_server 0.5.0) lib/language_server/providers/formatting.ex:10: ElixirLS.LanguageServer.Providers.Formatting.format/3
    (language_server 0.5.0) lib/language_server/server.ex:564: anonymous fn/3 in ElixirLS.LanguageServer.Server.handle_request_async/2

If that's the case, perhaps you opened an elixir file by itself -- without a folder.

It is perhaps possible that this language server assumes you will open a folder. For instance, open an elixir project with subl path/to/elixir/project or set up a .sublime-project file.

Can you open the SublimeText console and run the following?

window.folders()

It should print the folders of your window. If it's empty, that means that sublime-window is operating in "single file mode".

I have set up a .sublime-project file. window.folders() returns [].

I don't understand what you mean by opening a folder. I have a Sublime Text project, within which I have few files opened, including this Elixir file.

I have a Sublime Text project, within which I have few files opened, including this Elixir file.

That is apparently not the case when window.folders() returns an empty list ([]). You can verify whether you opened the sublime-project file by running

window.project_file_name()

In the ST console. It should print the file path of the .sublime-project, or print nothing when the window is not attached to a project file. For instance for me it prints

>>> window.project_file_name()
'/home/raoul/.config/sublime-text-3/Packages/LSP/LSP.sublime-project'
>>> window.project_file_name()
'/y/projects/A-Z/E/elixir-learning/elixir-learning.sublime-project'
>>> window.folders()
[]

Setting up a project doesn't imply that there are workspace folders. To add a folder just drag some folder into the ST window. Or use Project -> Add Folder to Project... menu item.

I have never used "Add Folder to Project" as I don't see any use for that feature.

I did that now but that doesn't help at all: window.folders() returns ['/y/projects/A-Z/E/elixir-learning/hello'] but "LSP > Format Document" still gives the same exception as before.

Is the file you are trying to format within that folder? LSP treats files within the workspace folders differently than files outside.
Also make sure you restart ST after adding the folder.

Ok, it's working now:

After adding folder to project I closed Sublime Text and re-opened project. Then I got prompt to install rebar3, after which I got few minutes of 100% CPU consumption, and then code formatting started working.

If LSP-elixir only works with files opened from project-folders, then that limitation should be mentioned in documentation.

Even better would be if the plugin would tell you automatically that the server can’t be started. But that is only possible with the ST4 api, which this helper package doesn’t yet implement.

Even better would be if the plugin would tell you automatically that the server can’t be started. But that is only possible with the ST4 api, which this helper package doesn’t yet implement.

I think the server is started in this case, just the formatting command is broken.

Langauge servers generally are more useful in projects (so when you add the whole folder of your project) as then they provide features like project-wide symbols and go-to-definition. If you are opening just single files then you might be loosing out on features. As far as formatting not working on individual files, it would probably be something that the underlying server is responsible for. You might want to make an issue on https://github.com/elixir-lsp/elixir-ls/ and see what maintainers say. Maybe it's possible to support it for all cases...

Langauge servers generally are more useful in projects (so when you add the whole folder of your project) as then they provide features like project-wide symbols and go-to-definition. If you are opening just single files then you might be loosing out on features.

As a contributor to ElixirLS I'd like to echo this, and ElixirLS doesn't support the single-file use-case. It is being tracked in elixir-lsp/elixir-ls#307 but there aren't any current plans to work on it.

What about adding alternative ways to configure project directory so I don't need to use "Add Folder to Project" which just wastes space in sidebar?

For example using directory of window.project_file_name() as project directory?

Nowadays Elixir LS works on separate files.