From c7edd8b060ddb707096797148657af90d742c53d Mon Sep 17 00:00:00 2001 From: Dhananjay Date: Wed, 17 Jul 2024 19:09:27 +0530 Subject: [PATCH] cscope: use lua port always (#45) --- README.md | 8 +++-- doc/cscope_maps.txt | 12 +++++-- lua/cscope/init.lua | 34 ++------------------ lua/cscope_maps/init.lua | 55 +++++++++++++------------------- lua/cscope_maps/utils/helper.lua | 28 ++-------------- 5 files changed, 41 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index 9aa9f01..2db53bf 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ` 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`. diff --git a/doc/cscope_maps.txt b/doc/cscope_maps.txt index 7c6d2f3..41d4bb9 100644 --- a/doc/cscope_maps.txt +++ b/doc/cscope_maps.txt @@ -1,9 +1,10 @@ -*cscope_maps.txt* For Neovim >= v0.10.0 Last change: 2024 July 16 +*cscope_maps.txt* For Neovim >= v0.10.0 Last change: 2024 July 17 ============================================================================== Table of Contents *cscope_maps-table-of-contents* 1. cscope_maps.nvim |cscope_maps-cscope_maps.nvim| + - Requirements |cscope_maps-cscope_maps.nvim-requirements| - Features |cscope_maps-cscope_maps.nvim-features| - Installation |cscope_maps-cscope_maps.nvim-installation| - Configuration |cscope_maps-cscope_maps.nvim-configuration| @@ -17,12 +18,18 @@ For old school code navigation :) Heavily inspired by emacs’ xcscope.el . -**Adds cscope support for Neovim 0.9+** +**Adds cscope support for Neovim** cscope_maps.nvim.v2.webm +REQUIREMENTS *cscope_maps-cscope_maps.nvim-requirements* + +- Neovim >= 0.10 +- cscope + + FEATURES *cscope_maps-cscope_maps.nvim-features* @@ -34,7 +41,6 @@ CSCOPE ~ - Keymaps can be disabled using `disable_maps` option. - Supports `cscope` and `gtags-cscope`. Use `cscope.exec` option to specify executable. - `:Cstag ` 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 hints. - See |cscope_maps-this-section| for `vim-gutentags`. diff --git a/lua/cscope/init.lua b/lua/cscope/init.lua index 76e7688..59d85b2 100644 --- a/lua/cscope/init.lua +++ b/lua/cscope/init.lua @@ -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) @@ -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 diff --git a/lua/cscope_maps/init.lua b/lua/cscope_maps/init.lua index fa163df..a193b11 100644 --- a/lua/cscope_maps/init.lua +++ b/lua/cscope_maps/init.lua @@ -1,5 +1,4 @@ local helper = require("cscope_maps.utils.helper") -local sv = require("cscope.stack_view") local M = {} ---@class CsMapsConfig @@ -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 diff --git a/lua/cscope_maps/utils/helper.lua b/lua/cscope_maps/utils/helper.lua index 563b456..a907b4a 100644 --- a/lua/cscope_maps/utils/helper.lua +++ b/lua/cscope_maps/utils/helper.lua @@ -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 = { @@ -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 @@ -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", "Cscope db build", { desc = sym_map.b }) -end - -M.init_keymaps = function(prefix) - if not M.legacy_cscope() then - map("n", "", - [[exe "Cstag" expand("")]], - { noremap = true, silent = true, desc = "ctag" }) - end - - M.default_keymaps(prefix) + map("n", "", [[exe "Cstag" expand("")]], { noremap = true, silent = true, desc = "ctag" }) end return M