Skip to content

Commit 2522bbc

Browse files
committed
Squashed commit of the following:
commit a39cd5398670443fb6eb1b5e644a348c0ed86662 Author: aceforeverd <[email protected]> Date: Sun Sep 1 10:53:07 2024 +0800 cd.lua commit 421f185 Author: aceforeverd <[email protected]> Date: Sat Aug 31 13:37:29 2024 +0800 Initial find root function from project.nvim
1 parent 92c3ba2 commit 2522bbc

File tree

6 files changed

+103
-23
lines changed

6 files changed

+103
-23
lines changed

lua/aceforeverd/cd.lua

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--- Set directory for curent buffer. By LSPs firstly, if non available, use fallback function
2+
--- @param fallback {fn: function, hint: string} Fallback function info returns root directory as string
3+
local function find_root(fallback)
4+
local ft = vim.api.nvim_get_option_value('filetype', { buf = 0 })
5+
local clients = vim.lsp.get_clients({ bufnr = 0 })
6+
7+
local dirs = vim
8+
.iter(clients)
9+
:filter(function(client)
10+
local filetypes = client.config.filetypes
11+
if filetypes and vim.tbl_contains(filetypes, ft) then
12+
return true
13+
end
14+
15+
return false
16+
end)
17+
:map(function(client)
18+
return { root = client.config.root_dir, client = client.name }
19+
end)
20+
:totable()
21+
22+
if type(fallback) == 'table' then
23+
local dir = fallback.fn()
24+
if dir ~= nil and dir ~= '' then
25+
table.insert(dirs, { root = dir, client = fallback.hint })
26+
end
27+
end
28+
29+
if #dirs == 0 then
30+
vim.notify('no rule to cd')
31+
return
32+
end
33+
34+
local verbose_status = vim.trim(vim.fn.execute('verbose pwd'))
35+
36+
vim.ui.select(dirs, {
37+
prompt = 'SELECT ROOT. ' .. verbose_status,
38+
format_item = function(info)
39+
return string.format('%s [%s]', info.root, info.client)
40+
end,
41+
}, function(choice)
42+
if choice ~= nil then
43+
if vim.fn.getcwd() ~= choice.root then
44+
vim.notify('setting root to ' .. choice.root, vim.log.levels.INFO, {})
45+
vim.fn.chdir(choice.root)
46+
else
47+
vim.notify('root already in ' .. choice.root, vim.log.levels.INFO, {})
48+
end
49+
end
50+
end)
51+
end
52+
53+
return {
54+
find_root = find_root,
55+
}

lua/aceforeverd/cmd.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ return {
1111
-- Sg <pattern> [<dir1> <dir2> ...]
1212
vim.api.nvim_create_user_command(
1313
'Sg',
14-
require('aceforeverd.keymap.init').ast_grep_search,
14+
require('aceforeverd.grep').ast_grep,
1515
-- TODO: custom complete function
1616
{ nargs = '+', desc = 'ast grep search', complete = 'dir' }
1717
)

lua/aceforeverd/grep.lua

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--[[--
2+
Copyright (c) 2024 Ace <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
--]]--
17+
18+
---@param opts table ast grep search pattern
19+
local function ast_grep_search(opts)
20+
local args = opts.fargs
21+
local pattern = args[1]
22+
23+
local cmds = { 'ast-grep', 'run', '--heading', 'never', '--pattern', pattern }
24+
for i = 2, #args, 1 do
25+
table.insert(cmds, args[i])
26+
end
27+
28+
local expr = string.format(
29+
'system([%s])',
30+
vim.iter(cmds):map(function(v)
31+
return vim.fn.shellescape(v)
32+
end):join(", ")
33+
)
34+
vim.cmd('lgetexpr ' .. expr)
35+
vim.cmd('lopen')
36+
end
37+
38+
return {
39+
ast_grep = ast_grep_search
40+
}

lua/aceforeverd/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function M.setup()
5959
})
6060

6161
require('aceforeverd.cmd').setup()
62+
require('aceforeverd.keymap').setup()
6263
require('aceforeverd.plugins').setup()
6364

6465
-- keymap guideline

lua/aceforeverd/keymap/init.lua

+5-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1-
---@param opts table ast grep search pattern
2-
local function ast_grep_search(opts)
3-
local args = opts.fargs
4-
local pattern = args[1]
5-
6-
local cmds = { 'ast-grep', 'run', '--heading', 'never', '--pattern', pattern }
7-
for i = 2, #args, 1 do
8-
table.insert(cmds, args[i])
9-
end
10-
11-
local expr = string.format(
12-
'system([%s])',
13-
vim.iter(cmds):map(function(v)
14-
return vim.fn.shellescape(v)
15-
end):join(", ")
16-
)
17-
vim.cmd('lgetexpr ' .. expr)
18-
vim.cmd('lopen')
19-
end
20-
211
return {
2+
setup = function()
3+
vim.keymap.set('n', '<leader>cd', function()
4+
require('aceforeverd.cd').find_root({ fn = vim.fn['FindRootDirectory'], hint = 'vim-rooter' })
5+
end, { desc = 'set root directory', remap = false })
6+
end,
227
select_browse_plugin = require('aceforeverd.keymap.plugin_browse').select_browse_plugin,
23-
ast_grep_search = ast_grep_search,
248
}

lua/aceforeverd/lsp/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function M.go()
6767
fieldalignment = true,
6868
shadow = true,
6969
},
70-
}
70+
},
7171
}
7272
go_cfg.capabilities.textDocument.completion.dynamicRegistration = true
7373

0 commit comments

Comments
 (0)