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