alexpasmantier / pymple.nvim

Refactor Python imports on file move/rename in Neovim
Apache License 2.0
69 stars 2 forks source link

No confirmation prompt - no code refactoring #38

Closed drusmanbashir closed 1 month ago

drusmanbashir commented 1 month ago

Hi Thanks for the much needed plugin! I am setting it up with nvim-tree. I have installed all dependencies. if i go inside nvim-tree and rename a file, a rename box appears as shown in your gif. But after renaming the file nothing happens at all. No confirmation box appears. I just get the message that the file has been renamed.

Neovim: NVIM v0.10.1
Build type: Release
LuaJIT 2.1.1713484068

Checkhealth:

============================================================================== pymple: health#pymple#check

Checking for required plugins ~

Checking external dependencies ~

For example, I just renamed a file common.py to common2.py. See the Pymplelog:

[DEBUG Sun 08 Sep 2024 02:31:00 AM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:118: Updating imports from /home/ub/code/fran/fran/utils/common.py to /home/ub/code/fran/fran/utils/common2.py in filetypes: { "python", "markdown" }
[DEBUG Sun 08 Sep 2024 02:31:00 AM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:66: Operating on a file
[DEBUG Sun 08 Sep 2024 02:31:00 AM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:16: split gg args: --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.utils\s+import\s+(?:\([\sa-zA-Z0-9_,]*common[\sa-zA-Z0-9_,]*\)\s*|[\w,]*common[\w,]*)' .
[DEBUG Sun 08 Sep 2024 02:31:00 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.utils\s+import\s+(?:\([\sa-zA-Z0-9_,]*common[\sa-zA-Z0-9_,]*\)\s*|[\w,]*common[\w,]*)' .
[DEBUG Sun 08 Sep 2024 02:31:00 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[DEBUG Sun 08 Sep 2024 02:31:00 AM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:36: monolithic gg args: --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.utils\.common\b' .
[DEBUG Sun 08 Sep 2024 02:31:00 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.utils\.common\b' .
[DEBUG Sun 08 Sep 2024 02:31:01 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[INFO  Sun 08 Sep 2024 02:31:01 AM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:129: No jobs to run.
[INFO  Sun 08 Sep 2024 02:31:10 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:33: --------------------------------------------------------------
[INFO  Sun 08 Sep 2024 02:31:10 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:34: ---                   NEW PYMPLE SESSION                   ---
[INFO  Sun 08 Sep 2024 02:31:10 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:35: --------------------------------------------------------------
[DEBUG Sun 08 Sep 2024 02:31:10 AM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:24: Created PympleUpdateImports user command
[DEBUG Sun 08 Sep 2024 02:31:10 AM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:35: Created PympleResolveImport user command
[DEBUG Sun 08 Sep 2024 02:31:10 AM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:42: Created PympleBuild user command
[DEBUG Sun 08 Sep 2024 02:31:10 AM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:49: Created PympleLogs user command
[INFO  Sun 08 Sep 2024 02:31:10 AM BST] .../.local/share/nvim/lazy/pymple.nvim/lua/pymple/hooks.lua:62: Found nvim-tree installation, hooking up events
[INFO  Sun 08 Sep 2024 02:31:10 AM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:59: Pymple setup complete
alexpasmantier commented 1 month ago

Hi, looking at the logs, it looks like pymple is trying to resolve imports from root in your case.

Could you show me your project tree, and try running

:lua print(vim.fn.getcwd())

and paste me the output of that?

Cheers

drusmanbashir commented 1 month ago

The project is called fran. It resides under /home/ub/code/fran/ ... Project structure in screenshot Screenshot from 2024-09-09 00-01-58

I use "ahmedkhalf/project.nvim" to change cwd() dynamically. If I am inside any .py file buffer in this project and called :lua print(vim.fn.getcwd()) I get :

/home/ub/code/fran/fran

if i am inside nvim-tree sidebar and call the command, i get:

/home/ub/code

Thanks!

alexpasmantier commented 1 month ago

Could you try adding the following root python marker to your config?

  python = {
    -- the names of root markers to look out for when discovering a project
    root_markers = { "pyproject.toml", "setup.py", ".git", "manage.py", ".project_root" },
    ...                                                                  ---------------
  }
drusmanbashir commented 1 month ago

I made changes as suggested. My configuration looks as follows:

    {
        "alexpasmantier/pymple.nvim",
        dependencies = {
            "nvim-lua/plenary.nvim",
            "MunifTanjim/nui.nvim",
            -- optional (nicer ui)
            "stevearc/dressing.nvim",
            "nvim-tree/nvim-web-devicons",
        },
        build = ":PympleBuild",
        config = function()
            require("pymple").setup({
                update_imports = {
                    -- the filetypes on which to run the update imports command
                    -- NOTE: this should at least include "python" for the plugin to
                    -- actually do anything useful
                    filetypes = { "python", "markdown" },
                },
                -- options for the add import for symbol under cursor feature
                add_import_to_buf = {
                    -- whether to autosave the buffer after adding the import (which will
                    -- automatically format/sort the imports if you have on-save autocommands)
                    autosave = true,
                },
                python = {
                    -- the names of root markers to look out for when discovering a project
                    root_markers = { "pyproject.toml", "setup.py", ".git", "manage.py", ".project_root" },
                },
                keymaps = {
                    -- Resolves import for symbol under cursor.
                    -- This will automatically find and add the corresponding import to
                    -- the top of the file (below any existing doctsring)
                    resolve_import_under_cursor = {
                        desc = "Resolve import under cursor",
                        keys = "<leader>lq", -- feel free to change this to whatever you like
                    },
                },
                logging = {
                    file = {
                        enabled = true,
                        path = vim.fn.stdpath("data") .. "/pymple.vlog",
                        max_lines = 1000, -- feel free to increase this number
                    },
                    -- this might help in some scenarios
                    console = {
                        enabled = false,
                    },
                    level = "debug",
                },
            }
)
        end,
    },
}

Log files:

[DEBUG Mon 09 Sep 2024 01:59:05 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:24: Created PympleUpdateImports user command
[DEBUG Mon 09 Sep 2024 01:59:05 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:35: Created PympleResolveImport user command
[DEBUG Mon 09 Sep 2024 01:59:05 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:42: Created PympleBuild user command
[DEBUG Mon 09 Sep 2024 01:59:05 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:49: Created PympleLogs user command
[INFO  Mon 09 Sep 2024 01:59:05 PM BST] .../.local/share/nvim/lazy/pymple.nvim/lua/pymple/hooks.lua:62: Found nvim-tree installation, hooking up events
[INFO  Mon 09 Sep 2024 01:59:05 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:59: Pymple setup complete
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:118: Updating imports from /home/ub/code/fran/fran/utils/common2.py to /home/ub/code/fran/fran/utils/common.py in filetypes: { "python", "markdown" }
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:66: Operating on a file
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:16: split gg args: --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.utils\s+import\s+(?:\([\sa-zA-Z0-9_,]*common2[\sa-zA-Z0-9_,]*\)\s*|[\w,]*common2[\w,]*)' .
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.utils\s+import\s+(?:\([\sa-zA-Z0-9_,]*common2[\sa-zA-Z0-9_,]*\)\s*|[\w,]*common2[\w,]*)' .
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:36: monolithic gg args: --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.utils\.common2\b' .
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.utils\.common2\b' .
[DEBUG Mon 09 Sep 2024 02:00:13 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[INFO  Mon 09 Sep 2024 02:00:13 PM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:129: No jobs to run.
alexpasmantier commented 1 month ago

Thanks,

I'm pretty sure the project root discovery is starting too high in your case and never finds your project markers.

Could you try updating to the latest version (I added a couple of log messages to try and debug your case), re-running your command and printing the logs?

drusmanbashir commented 1 month ago
[INFO  Mon 09 Sep 2024 05:14:19 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:34: ---                   NEW PYMPLE SESSION                   ---
[INFO  Mon 09 Sep 2024 05:14:19 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:35: --------------------------------------------------------------
[DEBUG Mon 09 Sep 2024 05:14:19 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:40: Current directory:  /home/ub
[DEBUG Mon 09 Sep 2024 05:14:19 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:24: Created PympleUpdateImports user command
[DEBUG Mon 09 Sep 2024 05:14:19 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:35: Created PympleResolveImport user command
[DEBUG Mon 09 Sep 2024 05:14:19 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:42: Created PympleBuild user command
[DEBUG Mon 09 Sep 2024 05:14:19 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:49: Created PympleLogs user command
[INFO  Mon 09 Sep 2024 05:14:19 PM BST] .../.local/share/nvim/lazy/pymple.nvim/lua/pymple/hooks.lua:62: Found nvim-tree installation, hooking up events
[INFO  Mon 09 Sep 2024 05:14:19 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:61: Pymple setup complete
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:118: Updating imports from /home/ub/code/fran/fran/utils/common.py to /home/ub/code/fran/fran/utils/common2.py in filetypes: { "python", "markdown" }
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...local/share/nvim/lazy/pymple.nvim/lua/pymple/project.lua:36: Project root:  /home/ub/code/fran:/home/ub/code/label_analysis:/home/ub/code/dicom_utils:/home/ub/code/xnat
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:66: Operating on a file
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:16: split gg args: --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.utils\s+import\s+(?:\([\sa-zA-Z0-9_,]*common[\sa-zA-Z0-9_,]*\)\s*|[\w,]*common[\w,]*)' .
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.utils\s+import\s+(?:\([\sa-zA-Z0-9_,]*common[\sa-zA-Z0-9_,]*\)\s*|[\w,]*common[\w,]*)' .
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:36: monolithic gg args: --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.utils\.common\b' .
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.utils\.common\b' .
[DEBUG Mon 09 Sep 2024 05:14:34 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[INFO  Mon 09 Sep 2024 05:14:34 PM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:129: No jobs to run.
[INFO  Mon 09 Sep 2024 05:15:07 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:33: --------------------------------------------------------------
drusmanbashir commented 1 month ago

See above. Thanks for helping. I dont mind it looking into other projects, e.g., code/xnat , code/dicom_utils as they import from this project (fran), but it does not refactor the import statements as far as i can see inside fran.

alexpasmantier commented 1 month ago

Ok, got it. I'm pretty sure opening neovim directly in your project folder will make it work.

I'll try adding additional logic to properly discover project root in workflows like yours in the coming days.

drusmanbashir commented 1 month ago

Just fyi: I now get a brief error when I start nvim: sed cant read 1459d: no such file or directory :D

alexpasmantier commented 1 month ago

Just pushed a fix for your project discovery problem.

Pymple should now be able to automatically infer which project you're working on when renaming files.

It works on my machine. Can you confirm that's the case for yours?

Thanks in advance.

PS: the sed problem is probably linked to interaction with another plugin since pymple doesn't run anything related to sed on startup. Fixed in #42

drusmanbashir commented 1 month ago

Didn't work. Logs:

[INFO  Sat 14 Sep 2024 03:09:00 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:33: --------------------------------------------------------------
[INFO  Sat 14 Sep 2024 03:09:00 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:34: ---                   NEW PYMPLE SESSION                   ---
[INFO  Sat 14 Sep 2024 03:09:00 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:35: --------------------------------------------------------------
[DEBUG Sat 14 Sep 2024 03:09:00 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:40: Current directory:  /home/ub
[DEBUG Sat 14 Sep 2024 03:09:00 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:24: Created PympleUpdateImports user command
[DEBUG Sat 14 Sep 2024 03:09:00 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:35: Created PympleResolveImport user command
[DEBUG Sat 14 Sep 2024 03:09:00 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:42: Created PympleBuild user command
[DEBUG Sat 14 Sep 2024 03:09:00 PM BST] ...share/nvim/lazy/pymple.nvim/lua/pymple/user_commands.lua:49: Created PympleLogs user command
[INFO  Sat 14 Sep 2024 03:09:00 PM BST] .../.local/share/nvim/lazy/pymple.nvim/lua/pymple/hooks.lua:62: Found nvim-tree installation, hooking up events
[INFO  Sat 14 Sep 2024 03:09:00 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/init.lua:61: Pymple setup complete
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:118: Updating imports from /home/ub/code/fran/fran/transforms/imageio.py to /home/ub/code/fran/fran/transforms/imageio2.py in filetypes: { "python", "markdown" }
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...local/share/nvim/lazy/pymple.nvim/lua/pymple/project.lua:36: Project root:  /home/ub/code/fran:/home/ub/code/label_analysis:/home/ub/code/dicom_utils:/home/ub/code/xnat:/home/ub/code/registration
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:66: Operating on a file
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:16: split gg args: --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.transforms\s+import\s+(?:\([\sa-zA-Z0-9_,]*imageio[\sa-zA-Z0-9_,]*\)\s*|[\w,]*imageio[\w,]*)' .
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -U -t python -t markdown 'from\s+\.home\.ub\.code\.fran\.fran\.transforms\s+import\s+(?:\([\sa-zA-Z0-9_,]*imageio[\sa-zA-Z0-9_,]*\)\s*|[\w,]*imageio[\w,]*)' .
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...nvim/lazy/pymple.nvim/lua/pymple/update_imports/init.lua:36: monolithic gg args: --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.transforms\.imageio\b' .
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:97: Starting gg job: gg -C --json -t python -t markdown '[^\.]\b\.home\.ub\.code\.fran\.fran\.transforms\.imageio\b' .
[DEBUG Sat 14 Sep 2024 03:09:38 PM BST] ...b/.local/share/nvim/lazy/pymple.nvim/lua/pymple/jobs.lua:119: 0 results found
[INFO  Sat 14 Sep 2024 03:09:38 PM BST] ...ub/.local/share/nvim/lazy/pymple.nvim/lua/pymple/api.lua:130: No jobs to run.
[
alexpasmantier commented 1 month ago

My bad, didn't spot that pythonpath in the logs. You should be good to go now 😉

drusmanbashir commented 1 month ago

Thanks to you, Now i have a powerful IDE. Still use pycharm for debugging as the debugger is a bit meh. that an interesting nextt project for you ? lol

alexpasmantier commented 1 month ago

I mostly debug python using ipdb tbh. Never really felt the need for a full-fledged debugger :)

alexpasmantier commented 1 month ago

Cheers !

You're welcome to contribute to pymple in the future if you find any interesting improvement you feel is worth integrating! 👍🏻