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

Are we managed to get organize import and auto import working #16

Open
f279801 opened this issue Aug 18, 2020 · 32 comments
Open

Are we managed to get organize import and auto import working #16

f279801 opened this issue Aug 18, 2020 · 32 comments

Comments

@f279801
Copy link

f279801 commented Aug 18, 2020

Just a question on if we are able to get organize import and auto import working?
If we are then would you mind to give me a hint.
s = socket.socket()

  1. I expect the auto import to work but it does not. Instead I got '"socket" is not defined'.
  2. Even when I do it with lsp-organize-imports, it also does not help.
    LSP :: source.organizeImports action not available

[-]/home/xtaidin/workspace/dev/lsp-pyright
-[-]pyright:31970
|-[-]Buffers
| -[+]test.py
-[-]Capabilities
|-[X]callHierarchyProvider: t
|-[-]executeCommandProvider:
| |-[X]workDoneProgress: t
| -[X]commands: []
|-[-]codeActionProvider:
| |-[X]workDoneProgress: t
| -[X]codeActionKinds: [quickfix source.organizeImports]
|-[-]signatureHelpProvider:
| |-[X]workDoneProgress: t
| -[X]triggerCharacters: [( , )]
|-[-]completionProvider:
| |-[X]workDoneProgress: t
| |-[X]resolveProvider: t
| -[X]triggerCharacters: [. []
|-[-]renameProvider:
| -[X]workDoneProgress: t
|-[-]documentHighlightProvider:
| -[X]workDoneProgress: t
|-[-]hoverProvider:
| -[X]workDoneProgress: t
|-[-]workspaceSymbolProvider:
| -[X]workDoneProgress: t
|-[-]documentSymbolProvider:
| -[X]workDoneProgress: t
|-[-]referencesProvider:
| -[X]workDoneProgress: t
|-[-]definitionProvider:
| -[X]workDoneProgress: t
-[X]textDocumentSync: 1

  1. Maybe it's a server problem?

[Trace - 09:33:23 ] Sending request 'textDocument/codeAction - (259)'.
Params: {
"textDocument": {
"uri": "file:///home/xtaidin/workspace/dev/lsp-pyright/testpython/test.py"
},
"range": {
"start": {
"line": 1,
"character": 11
},
"end": {
"line": 1,
"character": 11
}
},
"context": {
"diagnostics": [
{
"source": "Pyright (reportUndefinedVariable)",
"severity": 1,
"message": ""socket" is not defined",
"range": {
"end": {
"character": 16,
"line": 1
},
"start": {
"character": 10,
"line": 1
}
}
}
],
"only": [
"source.organizeImports"
]
}
}
[Trace - 09:33:23 ] Received response 'textDocument/codeAction - (259)' in 2ms.
Result: []

@seagle0128
Copy link
Collaborator

seagle0128 commented Aug 19, 2020

auto-import works well in my env.

emacs

organize-imports doesn't work, and raises this message

LSP :: source.organizeImports action not available

@f279801
Copy link
Author

f279801 commented Aug 19, 2020

Great to hear that.
Do you have any addtional setup like configuration file or similar thing like that?
Besides, with version of pyright you are using, I'm using pyright 1.1.63.

Would it be possible if you paste your io log as well. I'm curious about what the server replied to us.

@seagle0128
Copy link
Collaborator

seagle0128 commented Aug 19, 2020

Great to hear that.
Do you have any addtional setup like configuration file or similar thing like that?
Besides, with version of pyright you are using, I'm using pyright 1.1.63.

Would it be possible if you paste your io log as well. I'm curious about what the server replied to us.

No additional settings. I am using pyright 1.1.64.

I think I know what's the problem. If the module name or keyword was not input in the buffer, it won't be listed in the completion list. The issue needs lsp-mode core to take a look.

@f279801
Copy link
Author

f279801 commented Aug 19, 2020

I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server.
I get this to work for some use cases now, below are some observations:

  • A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
  • The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
  • organizeImports seems to be removed now after the commit 2d0e3eb.

@f279801
Copy link
Author

f279801 commented Aug 19, 2020

Ref: microsoft/pyright#926 (comment)

@seagle0128
Copy link
Collaborator

seagle0128 commented Aug 20, 2020

I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server.
I get this to work for some use cases now, below are some observations:

  • A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
  • The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
  • organizeImports seems to be removed now after the commit 2d0e3eb.

@yyoncho can u pls take a look at the issue about auto import and organize imports?

@f279801 The commit 2d0e3eb seems just removing organize-imports on saving. I tried in VSCode, the command works manually.

@yyoncho
Copy link
Member

yyoncho commented Aug 20, 2020

Organize import in lsp protocol is code action. If the server supports it we will call it.

@repelliuss
Copy link

I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server.
I get this to work for some use cases now, below are some observations:

  • A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
  • The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
  • organizeImports seems to be removed now after the commit 2d0e3eb.

I'm having the same behavior and the workaround works for me. Also, organize imports doesn't work for me either. It returns the same LSP :: source.organizeImports action not available message.

@seagle0128
Copy link
Collaborator

Organize import in lsp protocol is code action. If the server supports it we will call it.

It works with vscode but doesn't with lsp-mode. Wired.

@yyoncho
Copy link
Member

yyoncho commented Aug 20, 2020

It works with vscode but doesn't with lsp-mode. Wired.

Can someone provide client/server log from vscode?

@f279801
Copy link
Author

f279801 commented Aug 21, 2020

It works with vscode but doesn't with lsp-mode. Wired.

Can someone provide client/server log from vscode?

@seagle0128 would you mind to help with this request? I don't use vscode.
I still suspect that the server somehow manage to have a different result during it import resolution phase.

@seagle0128
Copy link
Collaborator

It works with vscode but doesn't with lsp-mode. Wired.

Can someone provide client/server log from vscode?

@seagle0128 would you mind to help with this request? I don't use vscode.
I still suspect that the server somehow manage to have a different result during it import resolution phase.

I tried a several times but failed to get the verbose logs in vscode 😭️

@yyoncho
Copy link
Member

yyoncho commented Aug 22, 2020

Can someone point me to vscode client? Seems like the server has code action pyright.organizeimports but I am not sure if it is invoked explicitly or it is returned as code action.

@seagle0128
Copy link
Collaborator

seagle0128 commented Aug 22, 2020

Can someone point me to vscode client? Seems like the server has code action pyright.organizeimports but I am not sure if it is invoked explicitly or it is returned as code action.

C-S-P to select Pyright: Organize Imports. If choosing Organize Imports, the same error message appears: "no organize imports action available". I think another action is implemented in pyright extension.

image

@yyoncho
Copy link
Member

yyoncho commented Aug 22, 2020

So, generally it should work either via this:

(lsp-make-interactive-code-action pyright-organize-imports "pyright.organizeimports")

Or by doing something like:

(lsp--send-execute-command "pyright.organizeimports" [])

@seagle0128
Copy link
Collaborator

seagle0128 commented Aug 22, 2020

@yyoncho I supposed it should work, but still got error: LSP :: pyright.organizeimports action not available. The command exists in pyright indeed. Please refer to https://github.com/microsoft/pyright/blob/1d56d6765f3cb7f579081d5b3c363a3df7d83337/server/src/commands/commands.ts and https://github.com/microsoft/pyright/blob/fbe2eb4da7d4fcfbce0cd6ef3c5bb1cb4f10df87/client/package.json.

Please advise?

@f279801
Copy link
Author

f279801 commented Aug 24, 2020

I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server.
I get this to work for some use cases now, below are some observations:

  • A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
  • The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
  • organizeImports seems to be removed now after the commit 2d0e3eb.

I'm having the same behavior and the workaround works for me. Also, organize imports doesn't work for me either. It returns the same LSP :: source.organizeImports action not available message.

I suspect that the first problem is actually an expected behavior and opened a question to pyright here:
microsoft/pyright#967

Let's wait for their answer.

@f279801
Copy link
Author

f279801 commented Aug 25, 2020

  • A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).

I suspect that the first problem is actually an expected behavior and opened a question to pyright here:
microsoft/pyright#967

Let's wait for their answer.

So it's confirmed that it's an expected behavior.

@padraic-mcatee
Copy link

Anyone else still receiving LSP :: source.organizeImports action not available?

@seagle0128
Copy link
Collaborator

@padraic-mcatee According to microsoft/pyright#967, it's as designed in pyright.

@ardrabczyk
Copy link

@seagle0128 I also get LSP :: pyright.organizeimports action not available and (lsp--send-execute-command "pyright.organizeimports" []) does nothing for me. Have you figured out how to use it?

@ardrabczyk
Copy link

pyright.organizeimports command has be invoked like that: (lsp-send-execute-command "pyright.organizeimports" ["file:///home/ja/python/test.py"]) (the file:// parameter is obligatory).

I checked that this._generateSortedImportText(sortedStatements) in pyright/packages/pyright-internal/src/languageService/importSorter.ts contains a properly sorted list of import statements but the changes are not reflected in Emacs buffer. I guess this is because Emacs does not listen for the reply returned by server or cannot parse it?

@tshu-w
Copy link

tshu-w commented Jun 10, 2021

@seagle0128 cannot we import after typing via something like code action?
Screen Shot 2021-06-10 at 16 49 33

@wztdream
Copy link

Since pyright will not support code action, if we still use pyright, is there any other package we can use for code action? For example type np auto add import numpy as np in the file?

@padraic-mcatee
Copy link

@ardrabczyk I see that this works for me as well.

@indigoviolet
Copy link

@seagle0128, any comments on @ardrabczyk 's note above that even though pyright.organizeimports is supposed to work, it doesn't reflect in emacs?

@seagle0128
Copy link
Collaborator

@indigoviolet What's the behavior of pyright extension of VSCode? Can you confirm if there are same?

@C-alcium
Copy link

C-alcium commented Jul 8, 2022

Seems like to me that it behaves exactly the same way in VsCode, and the only way to get imports in a sane way is by adding an import alias or importing a module directly.

e.g.

Works

import django.urls as urls 


urls.include(... # this will autocomplete

Does not work

from django.urls import include

include( ... # This will not, and it will also complain that it is an unknown symbol)

Edit: I also just noticed that if you have something like this:

include( ... # This will not, and it will also complain that it is an unknown symbol)

and you delete the include and retype it, it can and does suggest the correct auto-import. But it is not smart enough to see that include can / should be imported from django.urls. What's going on there, I'm not sure, but it is quite frustrating I have to say.

@seagle0128
Copy link
Collaborator

If the behaviors are same, it seems like an issue of the server side.

@C-alcium
Copy link

C-alcium commented Jul 8, 2022

That seems to be the case to me - I think the only way to fix this is to get the pyright server to fix it.

@mpereira
Copy link

mpereira commented Oct 4, 2023

@ardrabczyk thanks for your workaround, it works great!

I overrode the default gn LSP mode binding for lsp-organize-imports to run it, if someone finds it useful until this is addressed upstream.

(defun mpereira/lsp-pyright-buffer-file-organize-imports ()
  (interactive)
  (lsp-send-execute-command "pyright.organizeimports"
                            (vector (concat "file://" (buffer-file-name)))))

(use-package lsp-pyright
  :after lsp-mode
  :hook ((python-mode-hook . (lambda ()
                               (general-define-key
                                :keymaps '(local)
                                :states '(normal visual)
                                "gn" #'mpereira/lsp-pyright-buffer-file-organize-imports))))
  :init
  (require 'lsp-pyright))

@seagle0128
Copy link
Collaborator

seagle0128 commented Oct 6, 2023

@mpereira I added this new command. See 2f2631a.

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