Skip to content

Commit

Permalink
feat: automatically setup neo-tree hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Jul 21, 2024
1 parent c24e7bc commit a7b56f8
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 14 deletions.
1 change: 1 addition & 0 deletions lua/pymple/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ end
---@param source string: The path to the source file/dir (before renaming/moving)
---@param destination string: The path to the destination file/dir (after renaming/moving)
M.update_imports = function(source, destination)
P("update imports")
update_imports.update_imports(source, destination)
end

Expand Down
23 changes: 23 additions & 0 deletions lua/pymple/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,27 @@ M.HL_GROUPS = {
Mode = "ModeMsg",
}

---@alias Keymaps { resolve_import_under_cursor: {keys: string, desc: string} }

---@type Keymaps
local default_keymaps = {
resolve_import = { keys = "<leader>li", desc = "Resolve import under cursor" },
}

---@alias UserCommandOptions {update_imports: boolean, resolve_imports: boolean}

---@type UserCommandOptions
local default_user_command_options = {
update_imports = true,
resolve_imports = true,
}

---@alias Config { keymaps: Keymaps, create_user_commands: UserCommandOptions }

---@type Config
M.default_config = {
keymaps = default_keymaps,
create_user_commands = default_user_command_options,
}

return M
14 changes: 12 additions & 2 deletions lua/pymple/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ local optional_dependencies = {
},
},
},
{
functionality = "resolve_imports",
package = {
{
name = "gg",
url = "[alexpasmantier/grip-grab](https://github.com/alexpasmantier/grip-grab)",
optional = false,
},
},
},
{
functionality = "update_imports",
package = {
Expand Down Expand Up @@ -95,8 +105,8 @@ M.check = function()
error(
("%s %s"):format(
err_msg,
("`%s` finder will not function without %s installed."):format(
opt_dep.finder_name,
("Functionality `%s` will not work without %s installed."):format(
opt_dep.functionality,
package.url
)
)
Expand Down
51 changes: 50 additions & 1 deletion lua/pymple/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
local M = {}

function M.setup(_) end
local config = require("pymple.config")
local keymaps = require("pymple.keymaps")
local api = require("pymple.api")

---@param opts Config
local function setup(opts)
opts = opts or {}

setmetatable(opts, { __index = config.default_config })

if opts.create_user_commands.update_imports then
vim.api.nvim_create_user_command("UpdatePythonImports", function(args)
require("pymple.api").update_imports(args.fargs[1], args.fargs[2])
end, {
desc = "Update all imports in workspace after renaming `source` to `destination`",
nargs = "+",
})
end

if opts.create_user_commands.resolve_imports then
vim.api.nvim_create_user_command("ResolvePythonImport", function(_)
require("pymple.api").resolve_import()
end, {
desc = "Resolves import for symbol under cursor",
})
end

keymaps.setup_keymaps(opts.keymaps)

local neotree_installed, events = pcall(require, "neo-tree.events")
if neotree_installed then
events.subscribe({
event = events.FILE_MOVED,
handler = function(args)
api.update_imports(args.source, args.destination)
end,
})
events.subscribe({
event = events.FILE_RENAMED,
handler = function(args)
api.update_imports(args.source, args.destination)
end,
})
end
end

function M.setup(opts)
opts = opts or config.default_config
setup(opts)
end

return setmetatable(M, {
__index = function(_, k)
Expand Down
20 changes: 20 additions & 0 deletions lua/pymple/keymaps.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local M = {}

---Setup pymple keymaps
---@param keymaps Keymaps
M.setup_keymaps = function(keymaps)
for k, v in pairs(keymaps) do
vim.api.nvim_set_keymap(
"n",
v.keys,
"<cmd>lua require('pymple.api')." .. k .. "()<CR>",
{
desc = v.desc,
noremap = true,
silent = true,
}
)
end
end

return M
41 changes: 30 additions & 11 deletions plugin/pymple.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
vim.api.nvim_create_user_command("UpdatePythonImports", function(opts)
require("pymple.api").update_imports(opts.fargs[1], opts.fargs[2])
end, {
desc = "Update all imports in workspace after renaming `source` to `destination`",
nargs = "+",
})
local utils = require("pymple.utils")

vim.api.nvim_create_user_command("ResolvePythonImportUnderCursor", function(_)
require("pymple.api").resolve_import()
end, {
desc = "Resolves import for symbol under cursor",
})
local function handle_error(err)
-- Extract the actual error message without traceback
local error_message = err:match(":%d+: (.+)")
vim.api.nvim_echo({ { error_message or err, "ErrorMsg" } }, true, {})
end

local required_binaries = {
{
name = "sed",
binary = "sed",
help = "https://www.gnu.org/software/sed/manual/sed.html",
},
{
name = "gg",
binary = "gg",
help = "https://github.com/alexpasmantier/grip-grab",
},
}

for _, binary in ipairs(required_binaries) do
if not utils.check_binary_installed(binary.binary) then
handle_error(
"[pymple.nvim]: Binary "
.. binary.name
.. " is not installed. Please install it to use pymple.nvim.\nFor more information, see "
.. binary.help
)
end
end

0 comments on commit a7b56f8

Please sign in to comment.