Skip to content

Commit

Permalink
cscope: use lua port always
Browse files Browse the repository at this point in the history
Remove <0.10 support
  • Loading branch information
dhananjaylatkar committed Jul 17, 2024
1 parent ebb3941 commit 2635d7f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 93 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ For old school code navigation :)

Heavily inspired by emacs' [xcscope.el](https://github.com/dkogan/xcscope.el).

**Adds `cscope` support for [Neovim](https://neovim.io/) 0.9+**
**Adds `cscope` support for [Neovim](https://neovim.io/)**

[cscope_maps.nvim.v2.webm](https://github.com/dhananjaylatkar/cscope_maps.nvim/assets/27724944/7ab4d902-fe6d-4914-bff6-353136c72803)

## Requirements

- Neovim >= 0.10
- [cscope](https://sourceforge.net/projects/cscope/files/)

## Features

### Cscope
Expand All @@ -18,7 +23,6 @@ Heavily inspired by emacs' [xcscope.el](https://github.com/dkogan/xcscope.el).
- Keymaps can be disabled using `disable_maps` option.
- Supports `cscope` and `gtags-cscope`. Use `cscope.exec` option to specify executable.
- `:Cstag <symbol>` does `tags` search if no results are found in `cscope`.
- For `nvim < 0.9`, legacy cscope will be used. It will support keymaps. It won't have all the niceties of lua port.
- 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
34 changes: 2 additions & 32 deletions lua/cscope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -447,32 +447,6 @@ M.user_command = function()
})
end

---Initialization API for inbuilt cscope
---Used for neovim < 0.9
M.legacy_setup = function()
-- use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
vim.opt.cscopetag = true
-- check cscope for definition of a symbol before checking ctags: set to 1
-- if you want the reverse search order.
vim.opt.csto = 0
-- show msg when cscope db added
vim.opt.cscopeverbose = true
-- results in quickfix window
vim.opt.cscopequickfix = "s-,g-,c-,t-,e-,f-,i-,d-,a-"

if type(M.opts.db_file) == "table" then
for _, db in ipairs(M.opts.db_file) do
if vim.loop.fs_stat(db) ~= nil then
vim.api.nvim_command("cs add " .. db)
end
end
else -- string
if vim.loop.fs_stat(M.opts.db_file) ~= nil then
vim.api.nvim_command("cs add " .. M.opts.db_file)
end
end
end

---Initialization api
---@param opts CsConfig
M.setup = function(opts)
Expand Down Expand Up @@ -506,12 +480,8 @@ M.setup = function(opts)
end
end

if helper.legacy_cscope() then
M.legacy_setup()
else
cscope_picker = require("cscope.pickers." .. M.opts.picker)
M.user_command()
end
cscope_picker = require("cscope.pickers." .. M.opts.picker)
M.user_command()
end

return M
55 changes: 22 additions & 33 deletions lua/cscope_maps/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local helper = require("cscope_maps.utils.helper")
local sv = require("cscope.stack_view")
local M = {}

---@class CsMapsConfig
Expand All @@ -14,48 +13,38 @@ M.opts = {
cscope = {}, -- defaults are in cscope.lua
}

-- function to print xcscpoe.el like prompts
M.cscope_prompt = function(operation, default_symbol)
if M.opts.skip_input_prompt then
vim.cmd.Cscope({ args = { "find", operation, default_symbol } })
else
local prompt = string.format("%s (default: '%s'): ", helper.sym_map[operation], default_symbol)
vim.ui.input({ prompt = prompt }, function(new_symbol)
if new_symbol == nil then
return
end
if new_symbol ~= "" then
vim.cmd.Cscope({ args = { "find", operation, new_symbol } })
else
vim.cmd.Cscope({ args = { "find", operation, default_symbol } })
end
end)
end
end

---Initialization api
---@param opts CsMapsConfig
M.setup = function(opts)
opts = opts or {}
M.opts = vim.tbl_deep_extend("force", M.opts, opts)

local cscope = "Cscope"

if helper.legacy_cscope() then
cscope = "cscope"
end

require("cscope").setup(M.opts.cscope)

-- function to print xcscpoe.el like prompts
M.cscope_prompt = function(operation, default_symbol)
local cmd = cscope .. " find " .. operation
if M.opts.skip_input_prompt then
cmd = cmd .. " " .. default_symbol
helper.run_cscope_command(cmd)
else
local prompt = helper.sym_map[operation] .. " (default: '" .. default_symbol .. "'): "
vim.ui.input({ prompt = prompt }, function(new_symbol)
if new_symbol == nil then
return
end
if new_symbol ~= "" then
cmd = cmd .. " " .. new_symbol
else
cmd = cmd .. " " .. default_symbol
end
helper.run_cscope_command(cmd)
end)
end
end

if not M.opts.disable_maps then
-- Mappings
helper.init_keymaps(M.opts.prefix)
helper.default_keymaps(M.opts.prefix)
end

sv.setup()
require("cscope").setup(M.opts.cscope)
require("cscope.stack_view").setup()
end

return M
28 changes: 2 additions & 26 deletions lua/cscope_maps/utils/helper.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
local M = {}
local map = vim.keymap.set

M.ver = vim.version()

M.legacy_cscope = function()
-- cscope is removed from nvim 0.9+
return M.ver.major == 0 and M.ver.minor < 9
end

M.run_cscope_command = function(cmd)
vim.api.nvim_command(cmd)
if M.legacy_cscope() then
print(cmd)
vim.api.nvim_command("copen")
end
end

-- define key table for input strings
M.sym_map = {
Expand Down Expand Up @@ -44,6 +28,7 @@ M.get_cscope_prompt_cmd = function(operation, selection)
end

M.default_keymaps = function(prefix)
local map = vim.keymap.set
local sym_map = M.sym_map
local ok, wk = pcall(require, "which-key")
if ok then
Expand All @@ -63,16 +48,7 @@ M.default_keymaps = function(prefix)
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 })
end

M.init_keymaps = function(prefix)
if not M.legacy_cscope() then
map("n", "<C-]>",
[[<cmd>exe "Cstag" expand("<cword>")<cr>]],
{ noremap = true, silent = true, desc = "ctag" })
end

M.default_keymaps(prefix)
map("n", "<C-]>", [[<cmd>exe "Cstag" expand("<cword>")<cr>]], { noremap = true, silent = true, desc = "ctag" })
end

return M

0 comments on commit 2635d7f

Please sign in to comment.