From a7b56f8cf4bd93346a4ebc9ed9a9ec61bd6ee00f Mon Sep 17 00:00:00 2001 From: alexpasmantier Date: Mon, 22 Jul 2024 00:40:19 +0200 Subject: [PATCH] feat: automatically setup neo-tree hooks --- lua/pymple/api.lua | 1 + lua/pymple/config.lua | 23 +++++++++++++++++++ lua/pymple/health.lua | 14 ++++++++++-- lua/pymple/init.lua | 51 +++++++++++++++++++++++++++++++++++++++++- lua/pymple/keymaps.lua | 20 +++++++++++++++++ plugin/pymple.lua | 41 ++++++++++++++++++++++++--------- 6 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 lua/pymple/keymaps.lua diff --git a/lua/pymple/api.lua b/lua/pymple/api.lua index ba5989f..2c5bb92 100644 --- a/lua/pymple/api.lua +++ b/lua/pymple/api.lua @@ -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 diff --git a/lua/pymple/config.lua b/lua/pymple/config.lua index a2efcd1..d921d47 100644 --- a/lua/pymple/config.lua +++ b/lua/pymple/config.lua @@ -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 = "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 diff --git a/lua/pymple/health.lua b/lua/pymple/health.lua index b191101..069e5f8 100644 --- a/lua/pymple/health.lua +++ b/lua/pymple/health.lua @@ -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 = { @@ -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 ) ) diff --git a/lua/pymple/init.lua b/lua/pymple/init.lua index 9114bde..dc58d18 100644 --- a/lua/pymple/init.lua +++ b/lua/pymple/init.lua @@ -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) diff --git a/lua/pymple/keymaps.lua b/lua/pymple/keymaps.lua new file mode 100644 index 0000000..84ba8e2 --- /dev/null +++ b/lua/pymple/keymaps.lua @@ -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, + "lua require('pymple.api')." .. k .. "()", + { + desc = v.desc, + noremap = true, + silent = true, + } + ) + end +end + +return M diff --git a/plugin/pymple.lua b/plugin/pymple.lua index 49944ba..15d826e 100644 --- a/plugin/pymple.lua +++ b/plugin/pymple.lua @@ -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