feat(core)!: move a bunch of LazyVim features to snacks.nvim (#4706)

## Description

LazyVim comes with a bunch of smaller QoL plugin like features, but it's
not easy for non LazyVim users to use them.

That's why I started working on
[snacks.nvim](https://github.com/folke/snacks.nvim), a collection of
small QoL plugins for Neovim.

Snacks also includes a bunch of new improvements to these features.

This PR fully integrates with snacks.

## Todo

- [ ] add proper deprecations where needed
- [ ] create snacks docs
- [ ] document all the new improvements relevant to LazyVim users

## Closes

- [ ] #4492 
- [ ] #4333
- [ ] #4687

## Screenshots

<!-- Add screenshots of the changes if applicable. -->

## Checklist

- [ ] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
This commit is contained in:
Folke Lemaitre
2024-11-07 15:54:47 +01:00
committed by GitHub
parent 75750be1c0
commit 2f4697443c
29 changed files with 277 additions and 1066 deletions

View File

@ -118,63 +118,6 @@ function M.on_supports_method(method, fn)
})
end
function M.rename_file()
local buf = vim.api.nvim_get_current_buf()
local old = assert(LazyVim.root.realpath(vim.api.nvim_buf_get_name(buf)))
local root = assert(LazyVim.root.realpath(LazyVim.root.get({ normalize = true })))
assert(old:find(root, 1, true) == 1, "File not in project root")
local extra = old:sub(#root + 2)
vim.ui.input({
prompt = "New File Name: ",
default = extra,
completion = "file",
}, function(new)
if not new or new == "" or new == extra then
return
end
new = LazyVim.norm(root .. "/" .. new)
vim.fn.mkdir(vim.fs.dirname(new), "p")
M.on_rename(old, new, function()
vim.fn.rename(old, new)
vim.cmd.edit(new)
vim.api.nvim_buf_delete(buf, { force = true })
vim.fn.delete(old)
end)
end)
end
---@param from string
---@param to string
---@param rename? fun()
function M.on_rename(from, to, rename)
local changes = { files = { {
oldUri = vim.uri_from_fname(from),
newUri = vim.uri_from_fname(to),
} } }
local clients = M.get_clients()
for _, client in ipairs(clients) do
if client.supports_method("workspace/willRenameFiles") then
local resp = client.request_sync("workspace/willRenameFiles", changes, 1000, 0)
if resp and resp.result ~= nil then
vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
end
end
end
if rename then
rename()
end
for _, client in ipairs(clients) do
if client.supports_method("workspace/didRenameFiles") then
client.notify("workspace/didRenameFiles", changes)
end
end
end
---@return _.lspconfig.options
function M.get_config(server)
local configs = require("lspconfig.configs")
@ -260,82 +203,6 @@ function M.format(opts)
end
end
---@alias LspWord {from:{[1]:number, [2]:number}, to:{[1]:number, [2]:number}} 1-0 indexed
M.words = {}
M.words.enabled = false
M.words.ns = vim.api.nvim_create_namespace("vim_lsp_references")
---@param opts? {enabled?: boolean}
function M.words.setup(opts)
opts = opts or {}
if not opts.enabled then
return
end
M.words.enabled = true
local handler = vim.lsp.handlers["textDocument/documentHighlight"]
vim.lsp.handlers["textDocument/documentHighlight"] = function(err, result, ctx, config)
if not vim.api.nvim_buf_is_loaded(ctx.bufnr) then
return
end
vim.lsp.buf.clear_references()
return handler(err, result, ctx, config)
end
M.on_supports_method("textDocument/documentHighlight", function(_, buf)
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", "CursorMoved", "CursorMovedI" }, {
group = vim.api.nvim_create_augroup("lsp_word_" .. buf, { clear = true }),
buffer = buf,
callback = function(ev)
if not require("lazyvim.plugins.lsp.keymaps").has(buf, "documentHighlight") then
return false
end
if not ({ M.words.get() })[2] then
if ev.event:find("CursorMoved") then
vim.lsp.buf.clear_references()
elseif not LazyVim.cmp.visible() then
vim.lsp.buf.document_highlight()
end
end
end,
})
end)
end
---@return LspWord[] words, number? current
function M.words.get()
local cursor = vim.api.nvim_win_get_cursor(0)
local current, ret = nil, {} ---@type number?, LspWord[]
for _, extmark in ipairs(vim.api.nvim_buf_get_extmarks(0, M.words.ns, 0, -1, { details = true })) do
local w = {
from = { extmark[2] + 1, extmark[3] },
to = { extmark[4].end_row + 1, extmark[4].end_col },
}
ret[#ret + 1] = w
if cursor[1] >= w.from[1] and cursor[1] <= w.to[1] and cursor[2] >= w.from[2] and cursor[2] <= w.to[2] then
current = #ret
end
end
return ret, current
end
---@param count number
---@param cycle? boolean
function M.words.jump(count, cycle)
local words, idx = M.words.get()
if not idx then
return
end
idx = idx + count
if cycle then
idx = (idx - 1) % #words + 1
end
local target = words[idx]
if target then
vim.api.nvim_win_set_cursor(0, target.from)
end
end
M.action = setmetatable({}, {
__index = function(_, action)
return function()