Skip to content

Commit

Permalink
allow disabling preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed Mar 27, 2024
1 parent 74c1fe6 commit 40dec5b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 48 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
84 changes: 37 additions & 47 deletions lua/lsp-preview/init.lua
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions lua/lsp-preview/workspace_edit.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 40dec5b

Please sign in to comment.