jupyterlab-lsp icon indicating copy to clipboard operation
jupyterlab-lsp copied to clipboard

Use yapf fixer

Open quantum-booty opened this issue 3 years ago • 9 comments

Is it possible to implement syntax fixers like yapf? It's annoying to see the all the linter warnings and having to fix them one by one.

quantum-booty avatar Sep 22 '20 20:09 quantum-booty

@quantum-booty thanks for raising this, have been meaning to for a while!

Is it possible to implement syntax fixers like yapf?

It certainly is possible! PRs welcome, but as I said, this is also feature in which I am very interested, and so maybe once we can clear some outstanding stuff, I might be able to take that on.

In the meantime, here's a sketch of how such a PR might be built, with the caveat that I haven't actually landed a whole feature myself, just refactored and reviewed, so maybe @krassowski and @trajamsmith can weigh in on how this agrees with what they did:

  • research the formatting spec from the Language Server Protocol
  • determine if we need to register the capability with the server
  • add a new client feature.
    • some of the features are single files, while others are folders
      • I would think formatting could be a single file, unless it has a lot of complicated options

    • decide on the UI
      • some features only integrate with CodeMirror
      • some have dedicated widgets (e.g. diagnostics)
      • some have keyboard shortcuts like Other Popular Editors (e.g. vscode)
        • we might want to set some as defaults
        • Ctrl+Shift+I according to my muscle memory, but that triggers the browser inspector (in Chrome and FF), and we shouldn't be That Extension

  • write some acceptance tests against one or more of the tested language servers
    • on the project we use black, so might prefer a pyls-black example, but yapf is fine, too, as it's already a dependency of pyls (as packaged by conda-forge)

  • :tada:

bollwyvl avatar Sep 23 '20 01:09 bollwyvl

Is there completely no YAPF support in jupyterlab-lsp? I understand that there's no easy way to fix formatting in jupyterlab-lsp now. But is there a way to configure jupyterlab-lsp to complain about bad formatted code (using yapf )? I have the following settings but it doesn't make jupyteralb-lsp complain about bad formatted code.

image

dclong avatar Feb 18 '21 01:02 dclong

It appears it only implements the format provider, not diagnostics:

https://github.com/palantir/python-language-server/blob/91a13687dbd5247374253b245124befb8d9c60c9/pyls/plugins/yapf_format.py

bollwyvl avatar Feb 18 '21 02:02 bollwyvl

I'm confused, why is this issue here and not in python-lsp-server? I thought jupyterlab-lsp is language agnostic?

michaelaye avatar Mar 17 '22 02:03 michaelaye

as described above, the client needs to implement the feature. Making it work for the general case, including notebooks, is non-trivial. PRs welcome!

bollwyvl avatar Mar 17 '22 02:03 bollwyvl

I would, but the whole code formatting story inside jlab has become a completely obfuscated story with too many settings locations, clients-upon-servers, knobs to turn, some formatter's options supported, some not, it's just mayhem, to me anyway, so without understanding anything, I can't even begin to think where to start. For example, python-lsp-server has an internal black and there's a plugin for it also providing black, so plugin needed or not? But there's no yapf support, but jupyterlab-code-formatter supports yapf, however, it doesn't seem to support providing options to yapf. I guess jupyterlab-code-formatter will become obsolete soon, as it's not using the lsp architecture?

michaelaye avatar Mar 17 '22 02:03 michaelaye

yes, the language server spec is complicated. for reference, LSP has approximately 5x the messages of the jupyter kernel spec. this is why it hasn't been implemented on this repo yet.

yes, jupyterlab is also rather complicated, as its trying to serve a lot of different use cases. we've been slowly improving the configuration process upstream, such as with the newer configuration UI: yes, it has warts, but we're trying. and mostly on a volunteer basis.

once the complexity of jupyterlab, codemirror and the formatting LSP have been implemented, once, in this repo it would work "for free" for all the language servers that have implemented formatting. the configuration of those servers would not be free, but at least would be configurable. the configuration is also often very complicated.

as we often try out features first for python, we would likely look at that server you mention first. the one you mention can use yapf internally, but also has a plugin. but has nothing to do with this repo, other than that we would use it in our integration tests.

i am not familiar with other lab extensions + serverextensions that do formatting, but no, presumably they do not implement the LSP formatting spec, and might only support python.

bollwyvl avatar Mar 17 '22 02:03 bollwyvl

I'm only now realizing (please correct me if I'm wrong), that ANY code formatting via LSP is not implemented yet, is that what you meant with it ?:

LSP has approximately 5x the messages of the jupyter kernel spec. this is why it hasn't been implemented on this repo yet.

So, the fact that python-lsp-server (btw, it's a server, but also a client for jupyterlab-lsp?) does support code formatting via yapf, does not apply (yet) to jupyterlab yet, right? (Works, I guess, only in Spyder, for which that lsp-server is being maintained).

So, the only way to currently get code-formatting happening in jlab then is https://github.com/ryantam626/jupyterlab_code_formatter , it seems.

michaelaye avatar Mar 17 '22 03:03 michaelaye

ANY code formatting via LSP is not implemented yet,

that is correct. it would appear as a file/folder under the features.

python-lsp-server (btw, it's a server, but also a client for jupyterlab-lsp?)

yes, it is a Language Server. No, it is not a client. Spyder, VSCode, neovim, and jupyterlab (with jupyterlab-lsp installed) are clients.

does support code formatting via yapf, does not apply (yet) to jupyterlab

yep, because we do not advertise that we support the formatting message, the servers are not sending it.

only way to currently get code-formatting happening in jlab

that could be!

bollwyvl avatar Mar 17 '22 03:03 bollwyvl