From dbc30b7122ebb4cb8829b898742e73144f6bebee Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Sun, 19 May 2024 23:14:26 +0200 Subject: [PATCH] refactor!: use updated nvim APIs. From now on requires nvim 0.10 --- lua/genghis/file-movement.lua | 34 ++++++++++++++-------------------- lua/genghis/utils.lua | 3 ++- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/lua/genghis/file-movement.lua b/lua/genghis/file-movement.lua index e05c123..2b2faae 100644 --- a/lua/genghis/file-movement.lua +++ b/lua/genghis/file-movement.lua @@ -4,20 +4,20 @@ local u = require("genghis.utils") -------------------------------------------------------------------------------- ---Requests a 'workspace/willRenameFiles' on any running LSP client, that supports it ----stolen from https://github.com/LazyVim/LazyVim/blob/fecc5faca25c209ed62e3658dd63731e26c0c643/lua/lazyvim/util/init.lua#L304 +---SOURCE https://github.com/LazyVim/LazyVim/blob/fecc5faca25c209ed62e3658dd63731e26c0c643/lua/lazyvim/util/init.lua#L304 ---@param fromName string ---@param toName string function M.sendWillRenameToLsp(fromName, toName) - local clients = vim.lsp.get_active_clients { bufnr = 0 } + local clients = vim.lsp.get_clients { bufnr = 0 } for _, client in ipairs(clients) do - if client:supports_method("workspace/willRenameFiles") then - local resp = client.request_sync("workspace/willRenameFiles", { + if client.supports_method("workspace/willRenameFiles") then + local response = client.request_sync("workspace/willRenameFiles", { files = { { oldUri = vim.uri_from_fname(fromName), newUri = vim.uri_from_fname(toName) }, }, - }, 1000) - if resp and resp.result ~= nil then - vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding) + }, 1000, 0) + if response and response.result ~= nil then + vim.lsp.util.apply_workspace_edit(response.result, client.offset_encoding) end end end @@ -26,16 +26,9 @@ end ---@nodiscard ---@return boolean function M.lspSupportsRenaming() - -- INFO `client:supports_method()` seems to always return true, whatever is - -- supplied as argument. This does not affect `onRename`, but here we need to - -- check for the server_capabilities to properly identify whether our LSP - -- supports renaming or not. - -- TODO investigate if `client:supports_method()` works in nvim 0.10 or later - local clients = vim.lsp.get_active_clients { bufnr = 0 } + local clients = vim.lsp.get_clients { bufnr = 0 } for _, client in ipairs(clients) do - local workspaceCap = client.server_capabilities.workspace - local supports = workspaceCap and workspaceCap.fileOperations and workspaceCap.fileOperations.willRename - if supports then return true end + if client.supports_method("workspace/willRenameFiles") then return true end end return false end @@ -44,9 +37,7 @@ end ---@param newFilePath string function M.moveFile(oldFilePath, newFilePath) local renamed, _ = vim.loop.fs_rename(oldFilePath, newFilePath) - if renamed then - return true - end + if renamed then return true end ---try `fs_copyfile` to support moving across partitions local copied, copiedError = vim.loop.fs_copyfile(oldFilePath, newFilePath) if copied then @@ -58,7 +49,10 @@ function M.moveFile(oldFilePath, newFilePath) return false end else - u.notify(("Failed to move %q to %q: %q"):format(oldFilePath, newFilePath, copiedError), "error") + u.notify( + ("Failed to move %q to %q: %q"):format(oldFilePath, newFilePath, copiedError), + "error" + ) return false end end diff --git a/lua/genghis/utils.lua b/lua/genghis/utils.lua index 106f091..85fe5be 100644 --- a/lua/genghis/utils.lua +++ b/lua/genghis/utils.lua @@ -3,7 +3,7 @@ local M = {} ---@param bufnr? number|"#"|"$" function M.bwipeout(bufnr) - bufnr = bufnr and vim.fn.bufnr(bufnr) or 0 ---@diagnostic disable-line: param-type-mismatch + bufnr = bufnr and vim.fn.bufnr(bufnr) or 0 vim.api.nvim_buf_delete(bufnr, { force = true }) end @@ -20,6 +20,7 @@ function M.notify(msg, level) vim.notify(msg, vim.log.levels[level:upper()], { title = "nvim-genghis" }) end +---@nodiscard ---@param filepath string ---@return boolean function M.fileExists(filepath) return vim.loop.fs_stat(filepath) ~= nil end