From 40dec5b97c684df74f6e4f5bfe436bb76afc0a8b Mon Sep 17 00:00:00 2001 From: Jan Steinke Date: Wed, 27 Mar 2024 17:20:39 +0100 Subject: [PATCH] allow disabling preview --- README.md | 3 +- lua/lsp-preview/init.lua | 84 +++++++++++++----------------- lua/lsp-preview/workspace_edit.lua | 57 ++++++++++++++++++++ 3 files changed, 96 insertions(+), 48 deletions(-) create mode 100644 lua/lsp-preview/workspace_edit.lua diff --git a/README.md b/README.md index bab024d..59ffd43 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,9 @@ applying. * [ ] break-down by edit and not by file * [ ] one buffer for all changes in a file, view-port shifts on selection * [ ] buffer contents updates based on selected changes -* [ ] allow disabling the preview +* [x] allow disabling the preview ## Inspired by * github.com/aznhe21/actions-preview.nvim +* github.com/stevearc/dressing.nvim diff --git a/lua/lsp-preview/init.lua b/lua/lsp-preview/init.lua index 32c966a..01a65e7 100644 --- a/lua/lsp-preview/init.lua +++ b/lua/lsp-preview/init.lua @@ -1,63 +1,53 @@ -local lDiff = require("lsp-preview.diff") -local lTelescope = require("lsp-preview.telescope") +local lWorkspaceEdit = require("lsp-preview.workspace_edit") local util = require("vim.lsp.util") -local orig_apply_workspace_edits = util.apply_workspace_edit - ----Used as injection for the telescope picker to apply the selection. ----Filters the workspace edit for the selected hunks. ----@param workspace_edit WorkspaceEdit ----@param offset_encoding string ----@return fun(selected_indices: {value: Value}[]) -local make_apply_func = function(workspace_edit, offset_encoding) - return function(selected_indices) - local documentChanges = {} - local changes = {} - for _, selection in ipairs(selected_indices) do - if selection.value.type == "documentChanges" then - local index = selection.value.index - local edit = workspace_edit.documentChanges[index] - table.insert(documentChanges, edit) - elseif selection.value.type == "changes" then - local entry = selection.value.entry - ---@cast entry Edit - local edit = workspace_edit.changes[entry.uri] - changes[entry.uri] = edit - end - end - - if not vim.tbl_isempty(documentChanges) then - workspace_edit.documentChanges = documentChanges - end - if not vim.tbl_isempty(changes) then - workspace_edit.changes = changes - end - orig_apply_workspace_edits(workspace_edit, offset_encoding) - end +local M = {} + +local apply_workspace_edit = util.apply_workspace_edit + +function M.setup(_) + end +M.rename = function(new_name, opts) + opts = opts or {} ----Overwriting the built-in function with the selection and preview capabilities ----@param workspace_edit WorkspaceEdit ----@param offset_encoding string ----@diagnostic disable-next-line: duplicate-set-field -util.apply_workspace_edit = function(workspace_edit, offset_encoding) - local documentChanges, changes = lDiff.get_changes(workspace_edit, offset_encoding) - local opt = {} - opt.diff = { ctxlen = 20 } -- provide a large diff context view + -- Reset it to the original before every operation in case of a failure. + ---@diagnostic disable-next-line: duplicate-set-field + util.apply_workspace_edit = apply_workspace_edit + -- built-in behaviour if preview is disabled + if not opts.preview then + vim.lsp.buf.rename(new_name, opts) + return + end + + ---@diagnostic disable-next-line: duplicate-set-field + util.apply_workspace_edit = lWorkspaceEdit.make_apply_workspace_edit(apply_workspace_edit) - -- TODO: doesn't work with workspaceChanges - -- TODO: brittle when the indices get out of order - lTelescope.apply_action(opt, documentChanges, changes, make_apply_func(workspace_edit, offset_encoding)) + vim.lsp.buf.rename(new_name, opts) end +M.code_action = function(opts) + opts = opts or {} -local M = {} + -- Reset it to the original before every operation in case of a failure. + ---@diagnostic disable-next-line: duplicate-set-field + util.apply_workspace_edit = apply_workspace_edit -function M.setup(_) + -- built-in behaviour if preview is disabled + if not opts.preview then + vim.lsp.buf.code_action(opts) + return + end + + ---@diagnostic disable-next-line: duplicate-set-field + util.apply_workspace_edit = lWorkspaceEdit.make_apply_workspace_edit(apply_workspace_edit) + -- automatically trigger Telescope when there is only one action + opts.apply = true + vim.lsp.buf.code_action(opts) end return M diff --git a/lua/lsp-preview/workspace_edit.lua b/lua/lsp-preview/workspace_edit.lua new file mode 100644 index 0000000..d00e038 --- /dev/null +++ b/lua/lsp-preview/workspace_edit.lua @@ -0,0 +1,57 @@ +local lDiff = require("lsp-preview.diff") +local lTelescope = require("lsp-preview.telescope") + +local M = {} + +---Used as injection for the telescope picker to apply the selection. +---Filters the workspace edit for the selected hunks. +---@param workspace_edit WorkspaceEdit +---@param offset_encoding string +---@return fun(selected_indices: {value: Value}[]) +local make_apply_func = function(workspace_edit, offset_encoding, orig_apply_workspace_edits) + return function(selected_indices) + local documentChanges = {} + local changes = {} + for _, selection in ipairs(selected_indices) do + if selection.value.type == "documentChanges" then + local index = selection.value.index + local edit = workspace_edit.documentChanges[index] + table.insert(documentChanges, edit) + elseif selection.value.type == "changes" then + local entry = selection.value.entry + ---@cast entry Edit + local edit = workspace_edit.changes[entry.uri] + changes[entry.uri] = edit + end + end + + if not vim.tbl_isempty(documentChanges) then + workspace_edit.documentChanges = documentChanges + end + if not vim.tbl_isempty(changes) then + workspace_edit.changes = changes + end + orig_apply_workspace_edits(workspace_edit, offset_encoding) + end +end + +---Overwriting the built-in function with the selection and preview capabilities +---@param orig_apply_workspace_edits fun(workspace_edit: WorkspaceEdit, offset_encoding: string) +---@return fun(workspace_edit: WorkspaceEdit, offset_encoding: string) +M.make_apply_workspace_edit = function(orig_apply_workspace_edits) + return function(workspace_edit, offset_encoding) + local documentChanges, changes = lDiff.get_changes(workspace_edit, offset_encoding) + local opt = {} + opt.diff = { ctxlen = 20 } -- provide a large diff context view + + + lTelescope.apply_action( + opt, + documentChanges, + changes, + make_apply_func(workspace_edit, offset_encoding, orig_apply_workspace_edits) + ) + end +end + +return M