From de1c2389c2b9b8cd90a07476c3d5c1191d1a1249 Mon Sep 17 00:00:00 2001 From: Dhananjay Date: Thu, 1 Aug 2024 18:09:30 +0530 Subject: [PATCH] cscope: make symbol arg optional in user commands (#48) cscope and stack_view - use `cword` when symbol is not provided in cmd cscope_maps - No need to provide symbol in `cscope_prompt()` - Introduce `:CsPrompt ` user command --- README.md | 30 +++++++++------------------ doc/cscope_maps.txt | 33 ++++++++++-------------------- lua/cscope/init.lua | 23 ++++++++++++--------- lua/cscope/stack_view/init.lua | 5 +---- lua/cscope_maps/init.lua | 24 ++++++++++++++++++++-- lua/cscope_maps/utils/helper.lua | 35 ++++++++++---------------------- 6 files changed, 68 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 2db53bf..994968a 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,12 @@ Heavily inspired by emacs' [xcscope.el](https://github.com/dkogan/xcscope.el). - Tries to mimic vim's builtin cscope functionality. - Provides user command, `:Cscope` which acts same as good old `:cscope`. -- Short commands are supported. e.g. `:Cs f g main` -- Keymaps can be disabled using `disable_maps` option. +- Short commands are supported. e.g. `:Cs f g ` +- `:Cstag ` does `tags` search if no results are found in `cscope`. +- Empty `` can be used in `:Cs` and `:Cstag` to pick `` as sym. - Supports `cscope` and `gtags-cscope`. Use `cscope.exec` option to specify executable. -- `:Cstag ` does `tags` search if no results are found in `cscope`. +- Keymaps can be disabled using `disable_maps` option. +- `:CsPrompt ` can be used to invoke cscope prompt. - Display results in quickfix, **telescope**, **fzf-lua** or **mini.pick**. - Has [which-key.nvim](https://github.com/folke/which-key.nvim) hints. - See [this section](#vim-gutentags) for `vim-gutentags`. @@ -184,36 +186,24 @@ Disable default keymaps by setting `disable_maps = true`. There are 2 ways to add keymaps for `Cscope`. -#### Using `cscope_prompt()` function +#### Using `:CsPrompt` command -`cscope_prompt(operation, default_symbol)` is exposed to user. -This function provides prompt which asks for input (see screenshots below) +`:CsPrompt ` is user command to invoke prompt. +This command provides prompt which asks for input before running `:Cscope` command. e.g. Following snippet maps C-c C-g to find global def of symbol under cursor ```lua -vim.keymap.set( - "n", - "", - [[lua require('cscope_maps').cscope_prompt('g', vim.fn.expand(""))]], - { noremap = true, silent = true } -) +vim.keymap.set("n", "", ":CsPrompt g") ``` #### Using `:Cscope` command -Use `vim.api.nvim_set_keymap()` to set keymap for cscope. - e.g. Following snippet maps C-c C-g to find global def of symbol under cursor ```lua -vim.keymap.set( - "n", - "", - [[exe "Cscope find g" expand("")]], - { noremap = true, silent = true } -) +vim.keymap.set("n", "", ":Cs f g") ``` diff --git a/doc/cscope_maps.txt b/doc/cscope_maps.txt index 41d4bb9..227844c 100644 --- a/doc/cscope_maps.txt +++ b/doc/cscope_maps.txt @@ -1,4 +1,4 @@ -*cscope_maps.txt* For Neovim >= v0.10.0 Last change: 2024 July 17 +*cscope_maps.txt* For Neovim >= v0.10.0 Last change: 2024 August 01 ============================================================================== Table of Contents *cscope_maps-table-of-contents* @@ -37,10 +37,12 @@ CSCOPE ~ - Tries to mimic vim’s builtin cscope functionality. - Provides user command, `:Cscope` which acts same as good old `:cscope`. -- Short commands are supported. e.g. `:Cs f g main` -- Keymaps can be disabled using `disable_maps` option. +- Short commands are supported. e.g. `:Cs f g ` +- `:Cstag ` does `tags` search if no results are found in `cscope`. +- Empty `` can be used in `:Cs` and `:Cstag` to pick `` as sym. - Supports `cscope` and `gtags-cscope`. Use `cscope.exec` option to specify executable. -- `:Cstag ` does `tags` search if no results are found in `cscope`. +- Keymaps can be disabled using `disable_maps` option. +- `:CsPrompt ` can be used to invoke cscope prompt. - Display results in quickfix, **telescope**, **fzf-lua** or **mini.pick**. - Has which-key.nvim hints. - See |cscope_maps-this-section| for `vim-gutentags`. @@ -226,37 +228,24 @@ Disable default keymaps by setting `disable_maps = true`. There are 2 ways to add keymaps for `Cscope`. -USING CSCOPE_PROMPT() FUNCTION +USING :CSPROMPT COMMAND -`cscope_prompt(operation, default_symbol)` is exposed to user. This function -provides prompt which asks for input (see screenshots below) before running -`:Cscope` command. +`:CsPrompt ` is user command to invoke prompt. This command provides prompt +which asks for input before running `:Cscope` command. e.g. Following snippet maps C-c C-g to find global def of symbol under cursor >lua - vim.keymap.set( - "n", - "", - [[lua require('cscope_maps').cscope_prompt('g', vim.fn.expand(""))]], - { noremap = true, silent = true } - ) + vim.keymap.set("n", "", ":CsPrompt g") < USING :CSCOPE COMMAND -Use `vim.api.nvim_set_keymap()` to set keymap for cscope. - e.g. Following snippet maps C-c C-g to find global def of symbol under cursor >lua - vim.keymap.set( - "n", - "", - [[exe "Cscope find g" expand("")]], - { noremap = true, silent = true } - ) + vim.keymap.set("n", "", ":Cs f g") < Generated by panvimdoc diff --git a/lua/cscope/init.lua b/lua/cscope/init.lua index 9092862..bf21224 100644 --- a/lua/cscope/init.lua +++ b/lua/cscope/init.lua @@ -257,11 +257,10 @@ M.find = function(op, symbol) end M.cstag = function(symbol) - if symbol == nil then - return RC.INVALID_SYMBOL - end - local op = "g" + -- if symbol is not provided use cword + symbol = symbol or M.default_sym(op) + local ok, res = M.get_result(M.op_s_n[op], op, symbol, true) if ok == RC.SUCCESS then return M.open_picker(op, symbol, res) @@ -343,6 +342,14 @@ M.db_update = function(op, files) end end +M.default_sym = function(op) + local arg = "" + if vim.tbl_contains({ "f", "i", "7", "8" }, op) then + arg = "" + end + return vim.fn.expand(arg) +end + M.run = function(args) -- Parse top level input and call appropriate functions local args_num = #args @@ -355,13 +362,9 @@ M.run = function(args) local cmd = args[1] if cmd:sub(1, 1) == "f" then - if args_num < 3 then - log.warn("find command expects atleast 3 arguments") - return - end - local op = args[2] - local symbol = args[3] + -- if symbol is not provided use cword or cfile + local symbol = args[3] or M.default_sym(op) -- collect all args for i = 4, args_num do diff --git a/lua/cscope/stack_view/init.lua b/lua/cscope/stack_view/init.lua index 05b1acb..2e1a679 100644 --- a/lua/cscope/stack_view/init.lua +++ b/lua/cscope/stack_view/init.lua @@ -335,11 +335,8 @@ M.run_cmd = function(args) local cmd = args[1] if vim.startswith(cmd, "o") then - if #args ~= 3 then - return - end local stk_dir = args[2] - local symbol = args[3] + local symbol = args[3] or cs.default_sym("s") if vim.startswith(stk_dir, "d") then stk_dir = "down" elseif vim.startswith(stk_dir, "u") then diff --git a/lua/cscope_maps/init.lua b/lua/cscope_maps/init.lua index a193b11..f6bf4c3 100644 --- a/lua/cscope_maps/init.lua +++ b/lua/cscope_maps/init.lua @@ -1,4 +1,5 @@ local helper = require("cscope_maps.utils.helper") +local cs = require("cscope") local M = {} ---@class CsMapsConfig @@ -14,7 +15,17 @@ M.opts = { } -- function to print xcscpoe.el like prompts -M.cscope_prompt = function(operation, default_symbol) +M.cscope_prompt = function(operation) + if + not vim.tbl_contains(vim.tbl_keys(cs.op_s_n), operation) + and not vim.tbl_contains(vim.tbl_values(cs.op_s_n), operation) + then + return + end + if vim.tbl_contains(vim.tbl_values(cs.op_s_n), operation) then + operation = cs.op_n_s[operation] + end + local default_symbol = cs.default_sym(operation) if M.opts.skip_input_prompt then vim.cmd.Cscope({ args = { "find", operation, default_symbol } }) else @@ -38,12 +49,21 @@ M.setup = function(opts) opts = opts or {} M.opts = vim.tbl_deep_extend("force", M.opts, opts) + vim.api.nvim_create_user_command("CsPrompt", function(opts) + M.cscope_prompt(opts.fargs[1]) + end, { + nargs = "*", + complete = function() + return vim.tbl_keys(cs.op_s_n) + end, + }) + if not M.opts.disable_maps then -- Mappings helper.default_keymaps(M.opts.prefix) end - require("cscope").setup(M.opts.cscope) + cs.setup(M.opts.cscope) require("cscope.stack_view").setup() end diff --git a/lua/cscope_maps/utils/helper.lua b/lua/cscope_maps/utils/helper.lua index a907b4a..479021a 100644 --- a/lua/cscope_maps/utils/helper.lua +++ b/lua/cscope_maps/utils/helper.lua @@ -14,19 +14,6 @@ M.sym_map = { b = "Build database", } -M.get_cscope_prompt_cmd = function(operation, selection) - local sel = "cword" -- word under cursor - if selection == "f" then -- file under cursor - sel = "cfile" - end - - return string.format( - [[lua require('cscope_maps').cscope_prompt('%s', vim.fn.expand("<%s>"))]], - operation, - sel - ) -end - M.default_keymaps = function(prefix) local map = vim.keymap.set local sym_map = M.sym_map @@ -38,17 +25,17 @@ M.default_keymaps = function(prefix) wk.register({ [prefix] = { name = "+cscope" } }) end end - map("n", prefix .. "s", M.get_cscope_prompt_cmd("s", "w"), { desc = sym_map.s }) - map("n", prefix .. "g", M.get_cscope_prompt_cmd("g", "w"), { desc = sym_map.g }) - map("n", prefix .. "c", M.get_cscope_prompt_cmd("c", "w"), { desc = sym_map.c }) - map("n", prefix .. "t", M.get_cscope_prompt_cmd("t", "w"), { desc = sym_map.t }) - map("n", prefix .. "e", M.get_cscope_prompt_cmd("e", "w"), { desc = sym_map.e }) - map("n", prefix .. "f", M.get_cscope_prompt_cmd("f", "f"), { desc = sym_map.f }) - map("n", prefix .. "i", M.get_cscope_prompt_cmd("i", "f"), { desc = sym_map.i }) - map("n", prefix .. "d", M.get_cscope_prompt_cmd("d", "w"), { desc = sym_map.d }) - map("n", prefix .. "a", M.get_cscope_prompt_cmd("a", "w"), { desc = sym_map.a }) - map("n", prefix .. "b", "Cscope db build", { desc = sym_map.b }) - map("n", "", [[exe "Cstag" expand("")]], { noremap = true, silent = true, desc = "ctag" }) + map("n", prefix .. "s", ":CsPrompt s", { desc = sym_map.s }) + map("n", prefix .. "g", ":CsPrompt g", { desc = sym_map.g }) + map("n", prefix .. "c", ":CsPrompt c", { desc = sym_map.c }) + map("n", prefix .. "t", ":CsPrompt t", { desc = sym_map.t }) + map("n", prefix .. "e", ":CsPrompt e", { desc = sym_map.e }) + map("n", prefix .. "f", ":CsPrompt f", { desc = sym_map.f }) + map("n", prefix .. "i", ":CsPrompt i", { desc = sym_map.i }) + map("n", prefix .. "d", ":CsPrompt d", { desc = sym_map.d }) + map("n", prefix .. "a", ":CsPrompt a", { desc = sym_map.a }) + map("n", prefix .. "b", ":Cs db build", { desc = sym_map.b }) + map("n", "", ":Cstag", { noremap = true, silent = true, desc = "ctag" }) end return M