-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
92c3ba2
commit 2522bbc
Showing
6 changed files
with
103 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--[[-- | ||
Copyright (c) 2024 Ace <[email protected]> | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters