Skip to content
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

Format document raises an exception #7

Closed
malaire opened this issue Jul 19, 2020 · 15 comments
Closed

Format document raises an exception #7

malaire opened this issue Jul 19, 2020 · 15 comments

Comments

@malaire
Copy link

malaire commented Jul 19, 2020

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
@malaire malaire changed the title Doesn't work at all Format document raises an exception Jul 19, 2020
@malaire
Copy link
Author

malaire commented Jul 19, 2020

@rwols
Copy link
Member

rwols commented Jul 19, 2020

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.

@rwols
Copy link
Member

rwols commented Jul 19, 2020

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".

@malaire
Copy link
Author

malaire commented Jul 19, 2020

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.

@rwols
Copy link
Member

rwols commented Jul 19, 2020

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'

@malaire
Copy link
Author

malaire commented Jul 19, 2020

>>> window.project_file_name()
'/y/projects/A-Z/E/elixir-learning/elixir-learning.sublime-project'
>>> window.folders()
[]

@rchl
Copy link
Member

rchl commented Jul 19, 2020

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.

@malaire
Copy link
Author

malaire commented Jul 19, 2020

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.

@rchl
Copy link
Member

rchl commented Jul 19, 2020

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.

@malaire
Copy link
Author

malaire commented Jul 19, 2020

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.

@rwols
Copy link
Member

rwols commented Jul 19, 2020

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.

@rchl
Copy link
Member

rchl commented Jul 19, 2020

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...

@axelson
Copy link

axelson commented Jul 20, 2020

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.

@malaire
Copy link
Author

malaire commented Jul 20, 2020

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?

@princemaple
Copy link
Collaborator

Nowadays Elixir LS works on separate files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants