Skip to content

Commit

Permalink
feat(oil.nvim): add support for oil.nvim (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier authored Sep 4, 2024
1 parent cb060c0 commit aef024a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand Down
63 changes: 50 additions & 13 deletions lua/pymple/hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,32 +22,69 @@ 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)
api.update_imports(data.old_name, data.new_name, 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

Expand Down
2 changes: 1 addition & 1 deletion lua/pymple/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit aef024a

Please # to comment.