diff --git a/README.md b/README.md index 5a754b5..4cc0914 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,10 @@ The following features are currently available: - ✅ confirmation prompt - ✅ preview window - ✅ optionally ignore individual changes - - ✅ currently supports `neo-tree` and `nvim-tree` + - currently supports: + - ✅ `neo-tree` + - ✅ `nvim-tree` + - ✅ `oil.nvim` - Automatic missing import resolution for symbol under cursor: - ✅ searches in current workspace - ✅ searches in virtual environments @@ -74,7 +77,7 @@ The following features are currently available: ## 🚀 Usage ### 🦀 Import updates on file move/rename -If you're using a file explorer such as `neo-tree` or `nvim-tree`, pymple will automatically detect it and setup the appropriate hooks. +If you're using a file explorer such as `neo-tree`, `nvim-tree` or `oil.nvim`, pymple will automatically detect it and setup the appropriate hooks. When you rename or move a file or directory, you'll be prompted with a confirmation window and be able to preview the pending changes while discarding the ones you don't want. diff --git a/lua/pymple/hooks.lua b/lua/pymple/hooks.lua index 0c2640e..4dafd34 100644 --- a/lua/pymple/hooks.lua +++ b/lua/pymple/hooks.lua @@ -7,7 +7,7 @@ local M = {} ---@param events table ---@param opts UpdateImportsOptions -local subscribe_to_neotree_events = function(events, opts) +local setup_neotree_hooks = function(events, opts) events.subscribe({ event = events.FILE_MOVED, handler = function(args) @@ -22,7 +22,9 @@ local subscribe_to_neotree_events = function(events, opts) }) end -local subscribe_to_nvimtree_events = function(nvimtree_api, opts) +---@param nvimtree_api table +---@param opts UpdateImportsOptions +local setup_nvimtree_hooks = function(nvimtree_api, opts) local Event = nvimtree_api.events.Event nvimtree_api.events.subscribe(Event.NodeRenamed, function(data) @@ -30,24 +32,59 @@ local subscribe_to_nvimtree_events = function(nvimtree_api, opts) end) end +--[[ + OilActionsPost { + buf = 14, + data = { + actions = { { + dest_url = "oil:///Users/alex/code/python/data-doctrine/scripts/QAC_dataset/toto.py", + entry_type = "file", + src_url = "oil:///Users/alex/code/python/data-doctrine/scripts/QAC_dataset/utils.py", + type = "move" + } } + }, + event = "User", + file = "OilActionsPost", + id = 99, + match = "OilActionsPost" +} +--]] +local setup_oil_hooks = function(opts) + vim.api.nvim_create_autocmd("User", { + pattern = "OilActionsPost", + callback = function(args) + local parse_url = function(url) + return url:match("^.*://(.*)$") + end + local data = args.data + for _, action in ipairs(data.actions) do + if action.type == "move" then + local src_url = parse_url(action.src_url) + local dest_url = parse_url(action.dest_url) + api.update_imports(src_url, dest_url, opts) + end + end + end, + }) +end + M.setup = function() local neotree_installed, events = pcall(require, "neo-tree.events") if neotree_installed then - log.info( - "Found neo-tree installation, hooking up FILE_MOVED and FILE_RENAMED events" - ) - subscribe_to_neotree_events(events, config.user_config.update_imports) + log.info("Found neo-tree installation, hooking up events") + setup_neotree_hooks(events, config.user_config.update_imports) end local nvimtree_installed, nvimtree_api = pcall(require, "nvim-tree.api") if nvimtree_installed then - log.info( - "Found nvim-tree installation, hooking up FILE_MOVED and FILE_RENAMED events" - ) - subscribe_to_nvimtree_events( - nvimtree_api, - config.user_config.update_imports - ) + log.info("Found nvim-tree installation, hooking up events") + setup_nvimtree_hooks(nvimtree_api, config.user_config.update_imports) + end + + local oil_installed, _ = pcall(require, "oil") + if oil_installed then + log.info("Found oil installation, hooking up events") + setup_oil_hooks(config.user_config.update_imports) end end diff --git a/lua/pymple/init.lua b/lua/pymple/init.lua index 3bda31e..054a07e 100644 --- a/lua/pymple/init.lua +++ b/lua/pymple/init.lua @@ -56,7 +56,7 @@ end ---@param opts Config function M.setup(opts) setup(opts) - log.debug("Pymple setup complete") + log.info("Pymple setup complete") end function M.install()