-
Notifications
You must be signed in to change notification settings - Fork 203
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
No suggestions from autoimport #431
Comments
I have the same problem, though I get a couple of errors from rope itself:
I am on Ubuntu 22.04, using Mason to manage the venv (I installed rope with |
I think this ticket is confounding a number of things. The autoimport mechanism as currently implemented in pylsp is triggered by doing code completion, not by code action. When you finish autocompleting a symbol that wasn't in the current scope, pylsp would insert the import statement at the top of the file. rope does support adding import statement for unrecognized symbol under the cursor, but AFAIK this is not yet implemented in either pylsp's builtin rope support nor in pylsp-rope. Also, if you're using Neovim's builtin LSP client, please try again using a different LSP client. Neovim's builtin LSP is currently unusably broken when it comes to handling CodeAction. Neovim's builtin LSP sometimes just randomly and inexplicably didn't pass in the line numbers that the user has selected to the language server. If you're interested in getting this fixed, please reach out to Neovim's authors to fix their builtin LSP's CodeAction handling. Or use any of the other supported LSP clients. vim-lsp, coc, and ALE all works perfectly with pylsp and pylsp-rope on neovim. |
This doesn't fully make sense to me. In order to get So in order to get an autocompletion, I need to first import the symbol, but to import the symbol, I need to pick it from the autocompletion list. Doesn't this mean that the feature is impossible to use?
I think this is the feature that I'm really looking for. Would it be best for this to be implemented in pylsp's builtin rope support or in pylsp-rope?
Didn't you just say that autoimport doesn't operate via code-actions? How would using a different LSP client change the result here? |
@WhyNotHugo Hi, sorry for the late reply, I was in the shower thinking about other things when I suddenly remembered that I never responded to your question here. Basically, the way this works is that if you want to use rope_autoimport, you need to change the pylsp completion provider to rope. IOW, you need set When you accept the completion, for example, by typing You'd normally want to run rope organise import (this is available in pylsp-rope as codeaction) and/or isort after that.
Ah, sorry for the confusion, that wasn't about the autoimport feature itself as currently implemented. I was responding to because you and @magnuslarsen trying to use LSP code action. python-lsp-server have plugin hooks that allows external plugins like pylsp-rope to implement code actions but AFAIK none of its built-in plugins actually implements any code actions. Implementing autoimport into pylsp-rope is definitely on the list of what I want to have too, it just wasn't clear how actions like this is best implemented in terms of what LSP standard provides. In the trivial case, when there's only one unambiguous choice autoimport as code action is just inserting a single line to the file, which is fine, but in more complex case, there could be multiple aliases with the same name to import from (e.g. if you try to autoimport Ideally, there would be an additional interactive menu that pops up after selecting the code action to request for additional prompts for the user to pass in additional choices/arguments to the actions, but the code action protocol doesn't seem to make it obvious how or whether something like that is even possible. |
@WhyNotHugo not sure this actually addresses your issue, but I found there was a bug that we don't set LSP preferences when the server realizes an initialize message. My #459 changes that. Happy to also address your configuration file use case if you tell me what exactly you wanted to to. |
Update: I added a way to get autoimport suggestions as part of code actions #471 |
#459 should be not released yet, but i tried to set it via ws config. |
Just a note, when I wrote pylsp autoimport, I tested it against jedi-completion only. I never ran it with rope-completion, though they are from the same project. In fact, pylsp-autoimport shares components from Jedi (it uses parsers from there). These components are likely causing issues as they are AST parsers I wrote myself.
Note: this may not reflect the current status, just the initial development from a year ago. |
Well, I was diving a bit into the python rope code base and found a quick improvement that should speed up the autoimporting process by ~2x (python-rope/rope#722) |
There could be two performance bottlenecks: Parsing/Discovery:The vast majority of this time (when I profiled it) was AST parsing. The above solution will fix a discovery bug but it can't surpass this limitation. The options to meaningfully improve indexing time are limited:
CompletionThis does 3 things
We have a lot of room to improve here, especially on the first two. I'd look into the Jedi Environments or using async or something. The sqlite db can also implement a lot of the sorting/indexing/deduplicating - I just didn't know that when I wrote the original queries |
I just want to report back (a little late...), with the newest version of pylsp autoimport works, even using |
@WhyNotHugo pls let us know if the issue has been resolved for you. |
I seem to have the same issue with newer version. |
It may be a bug in should_insert |
I can't get autoimport to work. I've followed the instructions in the documentation and a added
rope_autoimport
in the relevant section of my LSP's configuration:pylsp = { cmd = vim.lsp.rpc.connect("127.0.0.1", 9977), settings = { pylsp = { plugins = { -- These functionalities are handled by ruff: pyflakes = { enabled = false }, pycodestyle = { enabled = false }, -- I use black for formatting: yapf = { enabled = false }, autopep8 = { enabled = false }, black = { enabled = true }, -- NOTE: Requires `py3-lsp-black`. -- Others: mypy = { enabled = true }, + rope_autoimport = { enabled = true }, }, }, }, },
I'm opening a file in a project with the following content:
The LSP logs:
After putting the mouse cursor over
Decimal
(note that its import is missing), I request any available actions from the LSP. Neovim reports that no actions are available (I don't see any additional logs while this happens, which is odd).Any hints as to what might be wrong? I'm not sure what other information can be of use here.
The text was updated successfully, but these errors were encountered: