Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit a39cd5398670443fb6eb1b5e644a348c0ed86662
Author: aceforeverd <teapot@aceforeverd.com>
Date:   Sun Sep 1 10:53:07 2024 +0800

    cd.lua

commit 421f185
Author: aceforeverd <teapot@aceforeverd.com>
Date:   Sat Aug 31 13:37:29 2024 +0800

    Initial find root function from project.nvim
  • Loading branch information
aceforeverd committed Sep 1, 2024
1 parent 92c3ba2 commit 2522bbc
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 23 deletions.
55 changes: 55 additions & 0 deletions lua/aceforeverd/cd.lua
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 1 addition & 1 deletion lua/aceforeverd/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ return {
-- Sg <pattern> [<dir1> <dir2> ...]
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' }
)
Expand Down
40 changes: 40 additions & 0 deletions lua/aceforeverd/grep.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--[[--
Copyright (c) 2024 Ace <teapot@aceforeverd.com>
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 <http://www.gnu.org/licenses/>.
--]]--

---@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
}
1 change: 1 addition & 0 deletions lua/aceforeverd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function M.setup()
})

require('aceforeverd.cmd').setup()
require('aceforeverd.keymap').setup()
require('aceforeverd.plugins').setup()

-- keymap guideline
Expand Down
26 changes: 5 additions & 21 deletions lua/aceforeverd/keymap/init.lua
Original file line number Diff line number Diff line change
@@ -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', '<leader>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,
}
2 changes: 1 addition & 1 deletion lua/aceforeverd/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function M.go()
fieldalignment = true,
shadow = true,
},
}
},
}
go_cfg.capabilities.textDocument.completion.dynamicRegistration = true

Expand Down

0 comments on commit 2522bbc

Please sign in to comment.