Skip to content

Commit

Permalink
cscope: make symbol arg optional in user commands (#48)
Browse files Browse the repository at this point in the history
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 <op>` user command
  • Loading branch information
dhananjaylatkar authored Aug 1, 2024
1 parent 30925c5 commit de1c238
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 82 deletions.
30 changes: 10 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sym>`
- `:Cstag <sym>` does `tags` search if no results are found in `cscope`.
- Empty `<sym>` can be used in `:Cs` and `:Cstag` to pick `<cword>` as sym.
- Supports `cscope` and `gtags-cscope`. Use `cscope.exec` option to specify executable.
- `:Cstag <symbol>` does `tags` search if no results are found in `cscope`.
- Keymaps can be disabled using `disable_maps` option.
- `:CsPrompt <op>` 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`.
Expand Down Expand Up @@ -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 <op>` is user command to invoke prompt.
This command provides prompt which asks for input
before running `:Cscope` command.

e.g. Following snippet maps <kbd>C-c C-g</kbd> to find global def of symbol
under cursor

```lua
vim.keymap.set(
"n",
"<C-c><C-g>",
[[<cmd>lua require('cscope_maps').cscope_prompt('g', vim.fn.expand("<cword>"))<cr>]],
{ noremap = true, silent = true }
)
vim.keymap.set("n", "<C-c><C-g>", ":CsPrompt g")
```

#### Using `:Cscope` command

Use `vim.api.nvim_set_keymap()` to set keymap for cscope.

e.g. Following snippet maps <kbd>C-c C-g</kbd> to find global def of symbol
under cursor

```lua
vim.keymap.set(
"n",
"<C-c><C-g>",
[[<cmd>exe "Cscope find g" expand("<cword>")<cr>]],
{ noremap = true, silent = true }
)
vim.keymap.set("n", "<C-c><C-g>", ":Cs f g")
```
33 changes: 11 additions & 22 deletions doc/cscope_maps.txt
Original file line number Diff line number Diff line change
@@ -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*
Expand Down Expand Up @@ -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 <sym>`
- `:Cstag <sym>` does `tags` search if no results are found in `cscope`.
- Empty `<sym>` can be used in `:Cs` and `:Cstag` to pick `<cword>` as sym.
- Supports `cscope` and `gtags-cscope`. Use `cscope.exec` option to specify executable.
- `:Cstag <symbol>` does `tags` search if no results are found in `cscope`.
- Keymaps can be disabled using `disable_maps` option.
- `:CsPrompt <op>` 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 |cscope_maps-this-section| for `vim-gutentags`.
Expand Down Expand Up @@ -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 <op>` 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",
"<C-c><C-g>",
[[<cmd>lua require('cscope_maps').cscope_prompt('g', vim.fn.expand("<cword>"))<cr>]],
{ noremap = true, silent = true }
)
vim.keymap.set("n", "<C-c><C-g>", ":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",
"<C-c><C-g>",
[[<cmd>exe "Cscope find g" expand("<cword>")<cr>]],
{ noremap = true, silent = true }
)
vim.keymap.set("n", "<C-c><C-g>", ":Cs f g")
<

Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
Expand Down
23 changes: 13 additions & 10 deletions lua/cscope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -343,6 +342,14 @@ M.db_update = function(op, files)
end
end

M.default_sym = function(op)
local arg = "<cword>"
if vim.tbl_contains({ "f", "i", "7", "8" }, op) then
arg = "<cfile>"
end
return vim.fn.expand(arg)
end

M.run = function(args)
-- Parse top level input and call appropriate functions
local args_num = #args
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lua/cscope/stack_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions lua/cscope_maps/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local helper = require("cscope_maps.utils.helper")
local cs = require("cscope")
local M = {}

---@class CsMapsConfig
Expand All @@ -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
Expand All @@ -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

Expand Down
35 changes: 11 additions & 24 deletions lua/cscope_maps/utils/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[[<cmd>lua require('cscope_maps').cscope_prompt('%s', vim.fn.expand("<%s>"))<cr>]],
operation,
sel
)
end

M.default_keymaps = function(prefix)
local map = vim.keymap.set
local sym_map = M.sym_map
Expand All @@ -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", "<cmd>Cscope db build<cr>", { desc = sym_map.b })
map("n", "<C-]>", [[<cmd>exe "Cstag" expand("<cword>")<cr>]], { noremap = true, silent = true, desc = "ctag" })
map("n", prefix .. "s", ":CsPrompt s<cr>", { desc = sym_map.s })
map("n", prefix .. "g", ":CsPrompt g<cr>", { desc = sym_map.g })
map("n", prefix .. "c", ":CsPrompt c<cr>", { desc = sym_map.c })
map("n", prefix .. "t", ":CsPrompt t<cr>", { desc = sym_map.t })
map("n", prefix .. "e", ":CsPrompt e<cr>", { desc = sym_map.e })
map("n", prefix .. "f", ":CsPrompt f<cr>", { desc = sym_map.f })
map("n", prefix .. "i", ":CsPrompt i<cr>", { desc = sym_map.i })
map("n", prefix .. "d", ":CsPrompt d<cr>", { desc = sym_map.d })
map("n", prefix .. "a", ":CsPrompt a<cr>", { desc = sym_map.a })
map("n", prefix .. "b", ":Cs db build<cr>", { desc = sym_map.b })
map("n", "<C-]>", ":Cstag<cr>", { noremap = true, silent = true, desc = "ctag" })
end

return M

0 comments on commit de1c238

Please sign in to comment.