mirror of
https://github.com/tomru/nvim.git
synced 2026-03-03 06:27:14 +01:00
add my first (broken) rooter plugin
This commit is contained in:
4
after/ftplugin/lua.lua
Normal file
4
after/ftplugin/lua.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
-- Run selection (simpler, but requires visual mode to be active)
|
||||||
|
vim.keymap.set('v', '<leader>x', ':lua <C-R>=getline("\'<", "\'>")<CR><CR>', { desc = 'execute selection' })
|
||||||
|
-- Run whole buffer
|
||||||
|
vim.keymap.set('n', '<leader>xx', ':luafile %<CR>', { desc = 'execute buffer' })
|
||||||
9
lua/plugins/local.lua
Normal file
9
lua/plugins/local.lua
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
return {
|
||||||
|
dir = '~/.config/nvim/lua/plugins/local/rooter.lua',
|
||||||
|
name = 'rooter',
|
||||||
|
config = function()
|
||||||
|
require('rooter').setup {
|
||||||
|
greeting = 'Loaded via lazy.nvim!',
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
}
|
||||||
93
lua/plugins/local/rooter.lua
Normal file
93
lua/plugins/local/rooter.lua
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
local log_level = vim.log.levels.ERROR
|
||||||
|
|
||||||
|
local function log(msg, level)
|
||||||
|
if level == vim.log.levels.ERROR then
|
||||||
|
vim.notify(msg, level)
|
||||||
|
else
|
||||||
|
print(msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_root()
|
||||||
|
local clients = vim.lsp.get_clients { bufnr = 0 }
|
||||||
|
if #clients == 0 then return vim.fs.root(0, { '.git' }) end
|
||||||
|
|
||||||
|
-- Define priority order (higher priority first)
|
||||||
|
local priority_order = {
|
||||||
|
'vtsls',
|
||||||
|
'eslint', -- formatting/linting servers last
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Find highest priority client
|
||||||
|
for _, priority_name in ipairs(priority_order) do
|
||||||
|
for _, client in ipairs(clients) do
|
||||||
|
if client.name == priority_name then return client.config.root_dir end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Fallback to first client with a root_dir
|
||||||
|
for _, client in ipairs(clients) do
|
||||||
|
if client.config.root_dir then return client.config.root_dir end
|
||||||
|
end
|
||||||
|
|
||||||
|
local git_root = vim.fs.root(0, { '.git' })
|
||||||
|
if git_root then return git_root end
|
||||||
|
|
||||||
|
log 'No LSP client found with root_dir'
|
||||||
|
end
|
||||||
|
|
||||||
|
local function debounce(fn, ms)
|
||||||
|
local timer = nil
|
||||||
|
return function(...)
|
||||||
|
local args = { ... }
|
||||||
|
if timer then
|
||||||
|
timer:stop()
|
||||||
|
timer:close()
|
||||||
|
end
|
||||||
|
timer = vim.uv.new_timer()
|
||||||
|
timer:start(ms, 0, function()
|
||||||
|
vim.schedule(function() fn(unpack(args)) end)
|
||||||
|
timer:close()
|
||||||
|
timer = nil
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function on_lsp_attach()
|
||||||
|
local root = get_root()
|
||||||
|
if root then
|
||||||
|
vim.notify('root_dir detected: ' .. root, log_level)
|
||||||
|
vim.cmd('lcd ' .. root)
|
||||||
|
else
|
||||||
|
vim.notify('No project root found', log_level)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local debounced_on_attacched = debounce(function()
|
||||||
|
on_lsp_attach()
|
||||||
|
vim.notify 'on_attach_done'
|
||||||
|
end, 1000)
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
-- Plugin configuration
|
||||||
|
M.config = {
|
||||||
|
greeting = 'Hello from rooter!',
|
||||||
|
some = 'option',
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Main plugin function
|
||||||
|
function M.setup(opts)
|
||||||
|
M.config = vim.tbl_deep_extend('force', M.config, opts or {})
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd('LspAttach', {
|
||||||
|
group = vim.api.nvim_create_augroup('my-rooter', { clear = true }),
|
||||||
|
callback = debounced_on_attacched,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create user commands
|
||||||
|
-- vim.api.nvim_create_user_command('MyPluginGreet', function() print(M.config.greeting) end, {})
|
||||||
|
-- vim.api.nvim_create_user_command('MyPluginStatus', function() print('Plugin loaded with config:', vim.inspect(M.config)) end, {})
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
Reference in New Issue
Block a user