Skip to content

Commit

Permalink
WIP format
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Jul 25, 2024
1 parent c53ad9d commit e0c5c0e
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 97 deletions.
68 changes: 40 additions & 28 deletions lua/yazi/lsp/embedded/lsp-file-operations.lua
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<string, string[]> a mapping from modules to events that trigger it
Expand All @@ -52,25 +52,31 @@ 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
local events = {
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)
Expand All @@ -80,30 +86,36 @@ 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
-- Issue to add the missing events: https://github.com/nvim-neo-tree/neo-tree.nvim/issues/1276
}
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({
id = id,
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 }
Expand All @@ -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

Expand All @@ -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]
Expand Down
14 changes: 8 additions & 6 deletions lua/yazi/lsp/embedded/lsp-file-operations/did-create.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions lua/yazi/lsp/embedded/lsp-file-operations/did-delete.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
19 changes: 12 additions & 7 deletions lua/yazi/lsp/embedded/lsp-file-operations/did-rename.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 2 additions & 3 deletions lua/yazi/lsp/embedded/lsp-file-operations/log.lua
Original file line number Diff line number Diff line change
@@ -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)

29 changes: 15 additions & 14 deletions lua/yazi/lsp/embedded/lsp-file-operations/utils.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
30 changes: 19 additions & 11 deletions lua/yazi/lsp/embedded/lsp-file-operations/will-create.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -11,31 +11,39 @@ 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
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
Expand Down
Loading

0 comments on commit e0c5c0e

Please sign in to comment.