neoclide / coc-python

Python extension for coc.nvim, fork of vscode-python
574 stars 51 forks source link

Running python.sortImports multiple times breaks #225

Closed rbhanot4739 closed 3 years ago

rbhanot4739 commented 4 years ago

I have a simple script test.py with just bunch of import statements

import glob
import sys
import argparse
import collections
import dis

Running python.sortImports produces this output which is expected

import argparse
import collections
import dis
import glob
import sys

However if I run the same command again, it produces the below output which is weird and removes some of the import statements and duplicates others.

import dis
import glob
import sys
import glob
import sys

This is how my coc-settings.json looks like

{
    "coc.preferences.formatOnSaveFiletypes": [
        "css",
        "markdown",
        "html",
        "yaml",
        "json",
        "python"
    ],
    "suggest.noselect": false,
    "suggest.triggerAfterInsertEnter": true,
    "suggest.timeout": 1000,
    "suggest.snippetIndicator": "►",
    "diagnostic.errorSign": "✖",
    "diagnostic.warningSign": "⚠",
    "diagnostic.infoSign": "ℹ",
    "diagnostic.hintSign": "➤",
    "diagnostic.messageDelay": 5,
    "diagnostic.checkCurrentLine": false,
    "diagnostic.refreshOnInsertMode": true,
    "diagnostic.enableHighlightLineNumber": true,
    "codeLens.enable": true,
    "signature.enable": true,
    "python.pythonPath": "python",
    "python.linting.pylintEnabled": false,
    "python.jediEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.formatting.provider": "autopep8",
    "python.linting.flake8Path": "/Users/rbhanot/.pyenv/shims/flake8",
    "python.formatting.autopep8Path": "/Users/rbhanot/.pyenv/shims/autopep8",
    "python.sortImports.path": "/Users/rbhanot/.pyenv/shims/isort",
    "coc.preferences.formatOnType": true,
    "coc.preferences.previewAutoClose": true,
    "coc.preferences.formatOnInsertLeave": true,
    "coc.preferences.rootPatterns": [
        ".git",
        "product-spec.json",
        "setup.py",
        ".env",
        "venv"
    ]
}

Output from cocInfo

vim version: NVIM v0.4.3
node version: v12.16.2
coc.nvim version: 0.0.78-d48afceae9
coc.nvim directory: /Users/rbhanot/.config/nvim/plugged/coc.nvim
term: iTerm.app
platform: darwin

Let me know if more information is needed from myside.

rbhanot4739 commented 4 years ago

Any update on this issue ?

rbhanot4739 commented 4 years ago

Can anyone please update on the issue ?

rbhanot4739 commented 4 years ago

Looks like @chemzqm is either quite busy I suppose or this issue is at least priority since its more than 2 weeks and I haven't had any comment from him.

rbhanot4739 commented 4 years ago

Is there any update on this ? As of now I am using isort in combination with ALE which seems to work fine but love to have this working with coc by default.

ikornaselur commented 4 years ago

I've gotten used to always saving immediately after organising imports. It seems that if I run twice, without saving in between, this happens. Makes me wonder if it's some caching going on?

alexd2580 commented 4 years ago

It seems that the import-sorting is not performed on the buffer but on the actual file - adding imports without saving and running python.sortImports duplicates some imports in the current buffer.

If you remove enough imports so that the code is now where the imports have been, do not save, and run python.sortImports, then parts of the code will be interleaved by formatted imports.

Adding imports using another editor while having the same file open in a buffer in vim, and then (without reloading) running python.sortImports copies parts of the newly added imports into the vim buffer, which confirms that the actual file is read, not the buffer and not some cache.

Would love to have this changed to organizing the current buffer, as opposed to reading the file from disc.

alexd2580 commented 4 years ago

This function is worth looking at, it is the one which calls isort, and supplies the input-file. https://github.com/neoclide/coc-python/blob/62a54c0305fd7d93cb8d4bed47d8b8e7317b54e9/src/providers/importSortProvider.ts#L29

vjsingh commented 3 years ago

+1. I agree it seems to work fine if you always save before calling sortImports, but would be nice if it worked on the buffer instead of the file.

alexd2580 commented 3 years ago

@vjsingh You can try checking out my PR/branch above in your local setup. Works on the buffer. Awaiting review. Using it myself.

vjsingh commented 3 years ago

@alexd2580 cool thanks!