-
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.
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 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,32 @@ | ||
---@class dotvim.core | ||
local M = {} | ||
|
||
---@type dotvim.core.action | ||
M.action = require("dotvim.core.action") | ||
|
||
---@type dotvim.core.lsp | ||
M.lsp = require("dotvim.core.lsp") | ||
|
||
---@param cmd string|fun(string):any | ||
---@return fun():any | ||
function M.input_then_exec(cmd) | ||
local function callback(input) | ||
if type(cmd) == "string" then | ||
vim.api.nvim_command(cmd .. input) | ||
else | ||
cmd(input) | ||
end | ||
end | ||
|
||
return function() | ||
vim.ui.input({ | ||
prompt = "Arguments, " .. cmd, | ||
}, function(input) | ||
if input then | ||
callback(input) | ||
end | ||
end) | ||
end | ||
end | ||
|
||
return M |
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,42 @@ | ||
---@class dotvim.core.lsp | ||
local M = {} | ||
|
||
---@param callback fun(client?: vim.lsp.Client, buffer: number): boolean? | ||
function M.on_lsp_attach(callback) | ||
vim.api.nvim_create_autocmd("LspAttach", { | ||
callback = function(args) | ||
local buffer = args.buf ---@type number | ||
---@type vim.lsp.Client? | ||
local client = vim.lsp.get_client_by_id(args.data.client_id) | ||
return callback(client, buffer) | ||
end, | ||
}) | ||
end | ||
|
||
---Try to get the root of the current LSP client | ||
---@param bufnr? number | ||
---@return string?, string? -- root_dir,client_name | ||
function M.get_lsp_root(bufnr) | ||
bufnr = vim.F.if_nil(bufnr, 0) | ||
local buf_ft = vim.api.nvim_get_option_value("filetype", { | ||
buf = bufnr, | ||
}) | ||
---@type vim.lsp.Client[] | ||
local clients = vim.lsp.get_clients { | ||
bufnr = bufnr, | ||
} | ||
if #clients == 0 then | ||
return nil | ||
end | ||
|
||
for _, value in ipairs(clients) do | ||
local filetypes = vim.F.if_nil(vim.tbl_get(value.config, "filetypes"), {}) | ||
if vim.tbl_contains(filetypes, buf_ft) then | ||
return value.config.root_dir, value.name | ||
end | ||
end | ||
|
||
return nil | ||
end | ||
|
||
return M |
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,12 @@ | ||
---@class dotvim.core.registry | ||
local M = {} | ||
|
||
---@type table<string, dotvim.core.action.Action> | ||
M.actions = {} | ||
|
||
---@param action dotvim.core.action.Action | ||
M.register_action = function(action) | ||
M.actions[action.id] = action | ||
end | ||
|
||
return M |
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,61 @@ | ||
---@class dotvim.core.vim | ||
local M = {} | ||
|
||
local uncount_lsp = { | ||
["null-ls"] = true, | ||
["copilot"] = true, | ||
["none-ls"] = true, | ||
} | ||
|
||
---@class dotvim.vim.BufferWrapper Buffer relative properties | ||
---@field bufnr number | ||
---@field filetype string | ||
---@field full_file_name string | ||
---@field lsp_servers vim.lsp.Client[] | ||
---@field ts_highlighter vim.treesitter.highlighter[] | ||
local Buffer = {} | ||
|
||
---@return string | ||
function Buffer:as_cache_key() | ||
local arr = { | ||
self.filetype, | ||
self.full_file_name, | ||
#self.lsp_servers, | ||
#self.ts_highlighter, | ||
} | ||
for _, v in ipairs(self.lsp_servers) do | ||
arr[#arr + 1] = v.name | ||
end | ||
return vim.json.encode(arr) | ||
end | ||
|
||
---Returns the number of language servers attached to the buffer except | ||
---`null-ls`, `copilot` and `none-ls`. | ||
---@return number | ||
function Buffer:lang_server_count() | ||
local count = 0 | ||
for _, v in ipairs(self.lsp_servers) do | ||
if not uncount_lsp[v.name] then | ||
count = count + 1 | ||
end | ||
end | ||
return count | ||
end | ||
|
||
---@param bufnr number | ||
---@return dotvim.vim.BufferWrapper | ||
function M.wrap_buffer(bufnr) | ||
return setmetatable({ | ||
bufnr = bufnr, | ||
filetype = vim.api.nvim_get_option_value("filetype", { buf = bufnr }), | ||
full_file_name = vim.api.nvim_buf_get_name(bufnr), | ||
lsp_servers = vim.lsp.get_clients { | ||
bufnr = bufnr, | ||
}, | ||
ts_highlighter = { vim.treesitter.highlighter.active[bufnr] }, | ||
}, { | ||
__index = Buffer, | ||
}) | ||
end | ||
|
||
return M |