From 2522bbc424c947307e1da951cb163c7aa58b160a Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Sun, 1 Sep 2024 10:54:11 +0800 Subject: [PATCH] Squashed commit of the following: commit a39cd5398670443fb6eb1b5e644a348c0ed86662 Author: aceforeverd Date: Sun Sep 1 10:53:07 2024 +0800 cd.lua commit 421f185a07033080680f70219ab15a119bde7982 Author: aceforeverd Date: Sat Aug 31 13:37:29 2024 +0800 Initial find root function from project.nvim --- lua/aceforeverd/cd.lua | 55 +++++++++++++++++++++++++++++++++ lua/aceforeverd/cmd.lua | 2 +- lua/aceforeverd/grep.lua | 40 ++++++++++++++++++++++++ lua/aceforeverd/init.lua | 1 + lua/aceforeverd/keymap/init.lua | 26 +++------------- lua/aceforeverd/lsp/init.lua | 2 +- 6 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 lua/aceforeverd/cd.lua create mode 100644 lua/aceforeverd/grep.lua diff --git a/lua/aceforeverd/cd.lua b/lua/aceforeverd/cd.lua new file mode 100644 index 0000000..6d1947c --- /dev/null +++ b/lua/aceforeverd/cd.lua @@ -0,0 +1,55 @@ +--- Set directory for curent buffer. By LSPs firstly, if non available, use fallback function +--- @param fallback {fn: function, hint: string} Fallback function info returns root directory as string +local function find_root(fallback) + local ft = vim.api.nvim_get_option_value('filetype', { buf = 0 }) + local clients = vim.lsp.get_clients({ bufnr = 0 }) + + local dirs = vim + .iter(clients) + :filter(function(client) + local filetypes = client.config.filetypes + if filetypes and vim.tbl_contains(filetypes, ft) then + return true + end + + return false + end) + :map(function(client) + return { root = client.config.root_dir, client = client.name } + end) + :totable() + + if type(fallback) == 'table' then + local dir = fallback.fn() + if dir ~= nil and dir ~= '' then + table.insert(dirs, { root = dir, client = fallback.hint }) + end + end + + if #dirs == 0 then + vim.notify('no rule to cd') + return + end + + local verbose_status = vim.trim(vim.fn.execute('verbose pwd')) + + vim.ui.select(dirs, { + prompt = 'SELECT ROOT. ' .. verbose_status, + format_item = function(info) + return string.format('%s [%s]', info.root, info.client) + end, + }, function(choice) + if choice ~= nil then + if vim.fn.getcwd() ~= choice.root then + vim.notify('setting root to ' .. choice.root, vim.log.levels.INFO, {}) + vim.fn.chdir(choice.root) + else + vim.notify('root already in ' .. choice.root, vim.log.levels.INFO, {}) + end + end + end) +end + +return { + find_root = find_root, +} diff --git a/lua/aceforeverd/cmd.lua b/lua/aceforeverd/cmd.lua index e1bd455..fe828e1 100644 --- a/lua/aceforeverd/cmd.lua +++ b/lua/aceforeverd/cmd.lua @@ -11,7 +11,7 @@ return { -- Sg [ ...] vim.api.nvim_create_user_command( 'Sg', - require('aceforeverd.keymap.init').ast_grep_search, + require('aceforeverd.grep').ast_grep, -- TODO: custom complete function { nargs = '+', desc = 'ast grep search', complete = 'dir' } ) diff --git a/lua/aceforeverd/grep.lua b/lua/aceforeverd/grep.lua new file mode 100644 index 0000000..7940e1c --- /dev/null +++ b/lua/aceforeverd/grep.lua @@ -0,0 +1,40 @@ +--[[-- +Copyright (c) 2024 Ace + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +--]]-- + +---@param opts table ast grep search pattern +local function ast_grep_search(opts) + local args = opts.fargs + local pattern = args[1] + + local cmds = { 'ast-grep', 'run', '--heading', 'never', '--pattern', pattern } + for i = 2, #args, 1 do + table.insert(cmds, args[i]) + end + + local expr = string.format( + 'system([%s])', + vim.iter(cmds):map(function(v) + return vim.fn.shellescape(v) + end):join(", ") + ) + vim.cmd('lgetexpr ' .. expr) + vim.cmd('lopen') +end + +return { + ast_grep = ast_grep_search +} diff --git a/lua/aceforeverd/init.lua b/lua/aceforeverd/init.lua index 85396ed..7966c76 100644 --- a/lua/aceforeverd/init.lua +++ b/lua/aceforeverd/init.lua @@ -59,6 +59,7 @@ function M.setup() }) require('aceforeverd.cmd').setup() + require('aceforeverd.keymap').setup() require('aceforeverd.plugins').setup() -- keymap guideline diff --git a/lua/aceforeverd/keymap/init.lua b/lua/aceforeverd/keymap/init.lua index 4077d28..f7c07e0 100644 --- a/lua/aceforeverd/keymap/init.lua +++ b/lua/aceforeverd/keymap/init.lua @@ -1,24 +1,8 @@ ----@param opts table ast grep search pattern -local function ast_grep_search(opts) - local args = opts.fargs - local pattern = args[1] - - local cmds = { 'ast-grep', 'run', '--heading', 'never', '--pattern', pattern } - for i = 2, #args, 1 do - table.insert(cmds, args[i]) - end - - local expr = string.format( - 'system([%s])', - vim.iter(cmds):map(function(v) - return vim.fn.shellescape(v) - end):join(", ") - ) - vim.cmd('lgetexpr ' .. expr) - vim.cmd('lopen') -end - return { + setup = function() + vim.keymap.set('n', 'cd', function() + require('aceforeverd.cd').find_root({ fn = vim.fn['FindRootDirectory'], hint = 'vim-rooter' }) + end, { desc = 'set root directory', remap = false }) + end, select_browse_plugin = require('aceforeverd.keymap.plugin_browse').select_browse_plugin, - ast_grep_search = ast_grep_search, } diff --git a/lua/aceforeverd/lsp/init.lua b/lua/aceforeverd/lsp/init.lua index 9bfb445..eb74b76 100644 --- a/lua/aceforeverd/lsp/init.lua +++ b/lua/aceforeverd/lsp/init.lua @@ -67,7 +67,7 @@ function M.go() fieldalignment = true, shadow = true, }, - } + }, } go_cfg.capabilities.textDocument.completion.dynamicRegistration = true