From e0c5c0eb5fa160162354637579674406f564a489 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Thu, 25 Jul 2024 18:50:49 +0300 Subject: [PATCH] WIP format --- lua/yazi/lsp/embedded/lsp-file-operations.lua | 68 +++++++++++-------- .../lsp-file-operations/did-create.lua | 14 ++-- .../lsp-file-operations/did-delete.lua | 14 ++-- .../lsp-file-operations/did-rename.lua | 19 ++++-- .../lsp/embedded/lsp-file-operations/log.lua | 5 +- .../embedded/lsp-file-operations/utils.lua | 29 ++++---- .../lsp-file-operations/will-create.lua | 30 +++++--- .../lsp-file-operations/will-delete.lua | 30 +++++--- .../lsp-file-operations/will-rename.lua | 30 +++++--- 9 files changed, 142 insertions(+), 97 deletions(-) diff --git a/lua/yazi/lsp/embedded/lsp-file-operations.lua b/lua/yazi/lsp/embedded/lsp-file-operations.lua index 67c0af76..2f23649d 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations.lua @@ -1,6 +1,6 @@ local M = {} -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local default_config = { debug = false, @@ -16,21 +16,21 @@ local default_config = { } local modules = { - willRenameFiles = "yazi.lsp.embedded.lsp-file-operations.will-rename", - didRenameFiles = "yazi.lsp.embedded.lsp-file-operations.did-rename", - willCreateFiles = "yazi.lsp.embedded.lsp-file-operations.will-create", - didCreateFiles = "yazi.lsp.embedded.lsp-file-operations.did-create", - willDeleteFiles = "yazi.lsp.embedded.lsp-file-operations.will-delete", - didDeleteFiles = "yazi.lsp.embedded.lsp-file-operations.did-delete", + willRenameFiles = 'yazi.lsp.embedded.lsp-file-operations.will-rename', + didRenameFiles = 'yazi.lsp.embedded.lsp-file-operations.did-rename', + willCreateFiles = 'yazi.lsp.embedded.lsp-file-operations.will-create', + didCreateFiles = 'yazi.lsp.embedded.lsp-file-operations.did-create', + willDeleteFiles = 'yazi.lsp.embedded.lsp-file-operations.will-delete', + didDeleteFiles = 'yazi.lsp.embedded.lsp-file-operations.did-delete', } local capabilities = { - willRenameFiles = "willRename", - didRenameFiles = "didRename", - willCreateFiles = "willCreate", - didCreateFiles = "didCreate", - willDeleteFiles = "willDelete", - didDeleteFiles = "didDelete", + willRenameFiles = 'willRename', + didRenameFiles = 'didRename', + willCreateFiles = 'willCreate', + didCreateFiles = 'didCreate', + willDeleteFiles = 'willDelete', + didDeleteFiles = 'didDelete', } ---@alias HandlerMap table a mapping from modules to events that trigger it @@ -52,15 +52,15 @@ local function setup_events(op_events, subscribe) end M.setup = function(opts) - M.config = vim.tbl_deep_extend("force", default_config, opts or {}) + M.config = vim.tbl_deep_extend('force', default_config, opts or {}) if M.config.debug then - log.level = "debug" + log.level = 'debug' end -- nvim-tree integration - local ok_nvim_tree, nvim_tree_api = pcall(require, "nvim-tree.api") + local ok_nvim_tree, nvim_tree_api = pcall(require, 'nvim-tree.api') if ok_nvim_tree then - log.debug("Setting up nvim-tree integration") + log.debug('Setting up nvim-tree integration') ---@type HandlerMap local nvim_tree_event = nvim_tree_api.events.Event @@ -68,9 +68,15 @@ M.setup = function(opts) willRenameFiles = { nvim_tree_event.WillRenameNode }, didRenameFiles = { nvim_tree_event.NodeRenamed }, willCreateFiles = { nvim_tree_event.WillCreateFile }, - didCreateFiles = { nvim_tree_event.FileCreated, nvim_tree_event.FolderCreated }, + didCreateFiles = { + nvim_tree_event.FileCreated, + nvim_tree_event.FolderCreated, + }, willDeleteFiles = { nvim_tree_event.WillRemoveFile }, - didDeleteFiles = { nvim_tree_event.FileRemoved, nvim_tree_event.FolderRemoved }, + didDeleteFiles = { + nvim_tree_event.FileRemoved, + nvim_tree_event.FolderRemoved, + }, } setup_events(events, function(module, event) nvim_tree_api.events.subscribe(event, function(args) @@ -80,14 +86,20 @@ M.setup = function(opts) end -- neo-tree integration - local ok_neo_tree, neo_tree_events = pcall(require, "neo-tree.events") + local ok_neo_tree, neo_tree_events = pcall(require, 'neo-tree.events') if ok_neo_tree then - log.debug("Setting up neo-tree integration") + log.debug('Setting up neo-tree integration') ---@type HandlerMap local events = { - willRenameFiles = { neo_tree_events.BEFORE_FILE_RENAME, neo_tree_events.BEFORE_FILE_MOVE }, - didRenameFiles = { neo_tree_events.FILE_RENAMED, neo_tree_events.FILE_MOVED }, + willRenameFiles = { + neo_tree_events.BEFORE_FILE_RENAME, + neo_tree_events.BEFORE_FILE_MOVE, + }, + didRenameFiles = { + neo_tree_events.FILE_RENAMED, + neo_tree_events.FILE_MOVED, + }, didCreateFiles = { neo_tree_events.FILE_ADDED }, didDeleteFiles = { neo_tree_events.FILE_DELETED }, -- currently no events in neo-tree for before creating or deleting, so unable to support those file operations @@ -95,7 +107,7 @@ M.setup = function(opts) } setup_events(events, function(module, event) -- create an event name based on the module and the event - local id = ("%s.%s"):format(module, event) + local id = ('%s.%s'):format(module, event) -- just in case setup is called twice, unsubscribe from event neo_tree_events.unsubscribe({ id = id }) neo_tree_events.subscribe({ @@ -103,7 +115,7 @@ M.setup = function(opts) event = event, handler = function(args) -- translate neo-tree arguemnts to the same format as nvim-tree - if type(args) == "table" then + if type(args) == 'table' then args = { old_name = args.source, new_name = args.destination } else args = { fname = args } @@ -113,7 +125,7 @@ M.setup = function(opts) end, }) end) - log.debug("Neo-tree integration setup complete") + log.debug('Neo-tree integration setup complete') end end @@ -123,8 +135,8 @@ M.default_capabilities = function() local config = M.config or default_config local result = { workspace = { - fileOperations = {} - } + fileOperations = {}, + }, } for operation, capability in pairs(capabilities) do result.workspace.fileOperations[capability] = config.operations[operation] diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/did-create.lua b/lua/yazi/lsp/embedded/lsp-file-operations/did-create.lua index 8207fc29..78fc50c7 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/did-create.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/did-create.lua @@ -1,12 +1,14 @@ -local utils = require("yazi.lsp.embedded.lsp-file-operations.utils") -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local utils = require('yazi.lsp.embedded.lsp-file-operations.utils') +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local M = {} M.callback = function(data) for _, client in pairs(vim.lsp.get_active_clients()) do - local did_create = - utils.get_nested_path(client, { "server_capabilities", "workspace", "fileOperations", "didCreate" }) + local did_create = utils.get_nested_path( + client, + { 'server_capabilities', 'workspace', 'fileOperations', 'didCreate' } + ) if did_create ~= nil then local filters = did_create.filters or {} if utils.matches_filters(filters, data.fname) then @@ -15,8 +17,8 @@ M.callback = function(data) { uri = vim.uri_from_fname(data.fname) }, }, } - client.notify("workspace/didCreateFiles", params) - log.debug("Sending workspace/didCreateFiles notification", params) + client.notify('workspace/didCreateFiles', params) + log.debug('Sending workspace/didCreateFiles notification', params) end end end diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/did-delete.lua b/lua/yazi/lsp/embedded/lsp-file-operations/did-delete.lua index 1db62a72..1d78fce1 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/did-delete.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/did-delete.lua @@ -1,12 +1,14 @@ -local utils = require("yazi.lsp.embedded.lsp-file-operations.utils") -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local utils = require('yazi.lsp.embedded.lsp-file-operations.utils') +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local M = {} M.callback = function(data) for _, client in pairs(vim.lsp.get_active_clients()) do - local did_delete = - utils.get_nested_path(client, { "server_capabilities", "workspace", "fileOperations", "didDelete" }) + local did_delete = utils.get_nested_path( + client, + { 'server_capabilities', 'workspace', 'fileOperations', 'didDelete' } + ) if did_delete ~= nil then local filters = did_delete.filters or {} if utils.matches_filters(filters, data.fname) then @@ -15,8 +17,8 @@ M.callback = function(data) { uri = vim.uri_from_fname(data.fname) }, }, } - client.notify("workspace/didDeleteFiles", params) - log.debug("Sending workspace/didDeleteFiles notification", params) + client.notify('workspace/didDeleteFiles', params) + log.debug('Sending workspace/didDeleteFiles notification', params) end end end diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/did-rename.lua b/lua/yazi/lsp/embedded/lsp-file-operations/did-rename.lua index d008fb83..3d02a420 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/did-rename.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/did-rename.lua @@ -1,22 +1,27 @@ -local utils = require("yazi.lsp.embedded.lsp-file-operations.utils") -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local utils = require('yazi.lsp.embedded.lsp-file-operations.utils') +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local M = {} M.callback = function(data) for _, client in pairs(vim.lsp.get_active_clients()) do - local did_rename = - utils.get_nested_path(client, { "server_capabilities", "workspace", "fileOperations", "didRename" }) + local did_rename = utils.get_nested_path( + client, + { 'server_capabilities', 'workspace', 'fileOperations', 'didRename' } + ) if did_rename ~= nil then local filters = did_rename.filters or {} if utils.matches_filters(filters, data.old_name) then local params = { files = { - { oldUri = vim.uri_from_fname(data.old_name), newUri = vim.uri_from_fname(data.new_name) }, + { + oldUri = vim.uri_from_fname(data.old_name), + newUri = vim.uri_from_fname(data.new_name), + }, }, } - client.notify("workspace/didRenameFiles", params) - log.debug("Sending workspace/didRenameFiles notification", params) + client.notify('workspace/didRenameFiles', params) + log.debug('Sending workspace/didRenameFiles notification', params) end end end diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/log.lua b/lua/yazi/lsp/embedded/lsp-file-operations/log.lua index 11955267..ef7a6071 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/log.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/log.lua @@ -1,6 +1,5 @@ -local log = require("plenary.log") +local log = require('plenary.log') return log.new({ - plugin = "nvim-yazi.lsp.embedded.lsp-file-operations", + plugin = 'nvim-yazi.lsp.embedded.lsp-file-operations', }, false) - diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/utils.lua b/lua/yazi/lsp/embedded/lsp-file-operations/utils.lua index 1b0afb11..c8d9dfa7 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/utils.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/utils.lua @@ -1,6 +1,6 @@ -local Path = require("plenary").path +local Path = require('plenary').path -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local M = {} @@ -17,7 +17,7 @@ end -- needed for globs like `**/` local ensure_dir_trailing_slash = function(path, is_dir) - if is_dir and not path:match("/$") then + if is_dir and not path:match('/$') then return path .. '/' end return path @@ -30,24 +30,25 @@ local get_absolute_path = function(name) return absolute_path, is_dir end -local get_regex = function (pattern) - local regex = vim.fn.glob2regpat(pattern.glob) - if pattern.options and pattern.options.ignorecase then - return "\\c" .. regex - end - return regex +local get_regex = function(pattern) + local regex = vim.fn.glob2regpat(pattern.glob) + if pattern.options and pattern.options.ignorecase then + return '\\c' .. regex + end + return regex end -- filter: FileOperationFilter local match_filter = function(filter, name, is_dir) local pattern = filter.pattern local match_type = pattern.matches - if not match_type or - (match_type == "folder" and is_dir) or - (match_type == "file" and not is_dir) + if + not match_type + or (match_type == 'folder' and is_dir) + or (match_type == 'file' and not is_dir) then local regex = get_regex(pattern) - log.debug("Matching name", name, "to pattern", regex) + log.debug('Matching name', name, 'to pattern', regex) local previous_ignorecase = vim.o.ignorecase vim.o.ignorecase = false local matched = vim.fn.match(name, regex) ~= -1 @@ -63,7 +64,7 @@ M.matches_filters = function(filters, name) local absolute_path, is_dir = get_absolute_path(name) for _, filter in pairs(filters) do if match_filter(filter, absolute_path, is_dir) then - log.debug("Path did match the filter", absolute_path, filter) + log.debug('Path did match the filter', absolute_path, filter) return true end end diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/will-create.lua b/lua/yazi/lsp/embedded/lsp-file-operations/will-create.lua index f0f47ffe..2caaef62 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/will-create.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/will-create.lua @@ -1,5 +1,5 @@ -local utils = require("yazi.lsp.embedded.lsp-file-operations.utils") -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local utils = require('yazi.lsp.embedded.lsp-file-operations.utils') +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local M = {} @@ -11,16 +11,22 @@ local function getWorkspaceEdit(client, fname) }, }, } - log.debug("Sending workspace/willCreateFiles request", will_create_params) - local timeout_ms = require("yazi.lsp.embedded.lsp-file-operations").config.timeout_ms - local success, resp = pcall(client.request_sync, "workspace/willCreateFiles", will_create_params, timeout_ms) - log.debug("Got workspace/willCreateFiles response", resp) + log.debug('Sending workspace/willCreateFiles request', will_create_params) + local timeout_ms = + require('yazi.lsp.embedded.lsp-file-operations').config.timeout_ms + local success, resp = pcall( + client.request_sync, + 'workspace/willCreateFiles', + will_create_params, + timeout_ms + ) + log.debug('Got workspace/willCreateFiles response', resp) if not success then - log.error("Error while sending workspace/willCreateFiles request", resp) + log.error('Error while sending workspace/willCreateFiles request', resp) return nil end if resp == nil or resp.result == nil then - log.warn("Got empty workspace/willCreateFiles response, maybe a timeout?") + log.warn('Got empty workspace/willCreateFiles response, maybe a timeout?') return nil end return resp.result @@ -28,14 +34,16 @@ end M.callback = function(data) for _, client in pairs(vim.lsp.get_active_clients()) do - local will_create = - utils.get_nested_path(client, { "server_capabilities", "workspace", "fileOperations", "willCreate" }) + local will_create = utils.get_nested_path( + client, + { 'server_capabilities', 'workspace', 'fileOperations', 'willCreate' } + ) if will_create ~= nil then local filters = will_create.filters or {} if utils.matches_filters(filters, data.fname) then local edit = getWorkspaceEdit(client, data.fname) if edit ~= nil then - log.debug("Going to apply workspace/willCreateFiles edit", edit) + log.debug('Going to apply workspace/willCreateFiles edit', edit) vim.lsp.util.apply_workspace_edit(edit, client.offset_encoding) end end diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/will-delete.lua b/lua/yazi/lsp/embedded/lsp-file-operations/will-delete.lua index b8ba9561..94196f02 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/will-delete.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/will-delete.lua @@ -1,5 +1,5 @@ -local utils = require("yazi.lsp.embedded.lsp-file-operations.utils") -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local utils = require('yazi.lsp.embedded.lsp-file-operations.utils') +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local M = {} @@ -11,16 +11,22 @@ local function getWorkspaceEdit(client, fname) }, }, } - log.debug("Sending workspace/willDeleteFiles request", will_delete_params) - local timeout_ms = require("yazi.lsp.embedded.lsp-file-operations").config.timeout_ms - local success, resp = pcall(client.request_sync, "workspace/willDeleteFiles", will_delete_params, timeout_ms) - log.debug("Got workspace/willDeleteFiles response", resp) + log.debug('Sending workspace/willDeleteFiles request', will_delete_params) + local timeout_ms = + require('yazi.lsp.embedded.lsp-file-operations').config.timeout_ms + local success, resp = pcall( + client.request_sync, + 'workspace/willDeleteFiles', + will_delete_params, + timeout_ms + ) + log.debug('Got workspace/willDeleteFiles response', resp) if not success then - log.error("Error while sending workspace/willDeleteFiles request", resp) + log.error('Error while sending workspace/willDeleteFiles request', resp) return nil end if resp == nil or resp.result == nil then - log.warn("Got empty workspace/willDeleteFiles response, maybe a timeout?") + log.warn('Got empty workspace/willDeleteFiles response, maybe a timeout?') return nil end return resp.result @@ -28,14 +34,16 @@ end M.callback = function(data) for _, client in pairs(vim.lsp.get_active_clients()) do - local will_delete = - utils.get_nested_path(client, { "server_capabilities", "workspace", "fileOperations", "willDelete" }) + local will_delete = utils.get_nested_path( + client, + { 'server_capabilities', 'workspace', 'fileOperations', 'willDelete' } + ) if will_delete ~= nil then local filters = will_delete.filters or {} if utils.matches_filters(filters, data.fname) then local edit = getWorkspaceEdit(client, data.fname) if edit ~= nil then - log.debug("Going to apply workspace/willDelete edit", edit) + log.debug('Going to apply workspace/willDelete edit', edit) vim.lsp.util.apply_workspace_edit(edit, client.offset_encoding) end end diff --git a/lua/yazi/lsp/embedded/lsp-file-operations/will-rename.lua b/lua/yazi/lsp/embedded/lsp-file-operations/will-rename.lua index 93943ffb..31b29474 100644 --- a/lua/yazi/lsp/embedded/lsp-file-operations/will-rename.lua +++ b/lua/yazi/lsp/embedded/lsp-file-operations/will-rename.lua @@ -1,5 +1,5 @@ -local utils = require("yazi.lsp.embedded.lsp-file-operations.utils") -local log = require("yazi.lsp.embedded.lsp-file-operations.log") +local utils = require('yazi.lsp.embedded.lsp-file-operations.utils') +local log = require('yazi.lsp.embedded.lsp-file-operations.log') local M = {} @@ -12,16 +12,22 @@ local function getWorkspaceEdit(client, old_name, new_name) }, }, } - log.debug("Sending workspace/willRenameFiles request", will_rename_params) - local timeout_ms = require("yazi.lsp.embedded.lsp-file-operations").config.timeout_ms - local success, resp = pcall(client.request_sync, "workspace/willRenameFiles", will_rename_params, timeout_ms) - log.debug("Got workspace/willRenameFiles response", resp) + log.debug('Sending workspace/willRenameFiles request', will_rename_params) + local timeout_ms = + require('yazi.lsp.embedded.lsp-file-operations').config.timeout_ms + local success, resp = pcall( + client.request_sync, + 'workspace/willRenameFiles', + will_rename_params, + timeout_ms + ) + log.debug('Got workspace/willRenameFiles response', resp) if not success then - log.error("Error while sending workspace/willRenameFiles request", resp) + log.error('Error while sending workspace/willRenameFiles request', resp) return nil end if resp == nil or resp.result == nil then - log.warn("Got empty workspace/willRenameFiles response, maybe a timeout?") + log.warn('Got empty workspace/willRenameFiles response, maybe a timeout?') return nil end return resp.result @@ -29,14 +35,16 @@ end M.callback = function(data) for _, client in pairs(vim.lsp.get_active_clients()) do - local will_rename = - utils.get_nested_path(client, { "server_capabilities", "workspace", "fileOperations", "willRename" }) + local will_rename = utils.get_nested_path( + client, + { 'server_capabilities', 'workspace', 'fileOperations', 'willRename' } + ) if will_rename ~= nil then local filters = will_rename.filters or {} if utils.matches_filters(filters, data.old_name) then local edit = getWorkspaceEdit(client, data.old_name, data.new_name) if edit ~= nil then - log.debug("Going to apply workspace/willRename edit", edit) + log.debug('Going to apply workspace/willRename edit', edit) vim.lsp.util.apply_workspace_edit(edit, client.offset_encoding) end end