Skip to content

Commit

Permalink
provide setup options for defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed Mar 27, 2024
1 parent a8a1d6e commit fdae11d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
# LSP-Preview.nvim

A plugin to that allows previewing of all changes done to a workspace before
A plugin that allows previewing of all changes done to a workspace before
applying.

Note: This plugin affects all workspace edits coming from the language server.
Not all are currently supported, if you run into problems with other methods,
please let me know.

## Installation

lazy.nvim:

```lua
{
'jan-xyz/lsp-preview.nvim',
opts = {},
}
```

## Configuration

If you're fine with the defaults, you're good to go after installation. If you
want to tweak, call this function:

```lua
require("dressing").setup({
--Automatically apply code-actions if there is only 1 available.
apply = true,
--Configuration provided to vim.diff (see `:h vim.diff()`)
diff = {
ctxlen = 5,
},
})
```

## TODO

* [x] implement selection
* [x] auto-select all changes
* [x] implement rename
* [x] make it work with normal workspace edits
* [x] don't rely on picker order selection
* [ ] 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 sorting list by token type that changes (e.g. var, class, comment)
* [x] allow disabling the preview
* [x] provide configuration options
* [ ] 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 sorting list by token type that changes (e.g. var, class, comment)

## Inspired by

Expand Down
28 changes: 28 additions & 0 deletions lua/lsp-preview/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local M = {}

---@class Options
---Automatically apply code-actions if there is only 1 available.
---@field apply boolean
---Configuration provided to vim.diff (see `:h vim.diff()`)
---@field diff table
local default_config = {
apply = true,
diff = {
ctxlen = 5,
},
}


local user_config = default_config

function M.setup(config)
user_config = vim.tbl_deep_extend("force", default_config, config)
end

setmetatable(M, {
__index = function(_, key)
return user_config[key]
end,
})

return M
11 changes: 7 additions & 4 deletions lua/lsp-preview/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local lWorkspaceEdit = require("lsp-preview.workspace_edit")
local config = require("lsp-preview.config")

local util = require("vim.lsp.util")

Expand All @@ -7,12 +8,14 @@ local M = {}
-- The original workspace_edit from the std library of vim for backup and resets.
local apply_workspace_edit = util.apply_workspace_edit

function M.setup(_)

---Setup the default behaviour of the plugin
---@param opts Options
function M.setup(opts)
config.setup(opts)
end

M.rename = function(new_name, opts)
opts = opts or {}
opts = vim.tbl_deep_extend("force", config, opts or {})

-- Reset it to the original before every operation in case of a failure.
---@diagnostic disable-next-line: duplicate-set-field
Expand All @@ -37,7 +40,7 @@ M.rename_preview = function(new_name, opts)
end

M.code_action = function(opts)
opts = opts or {}
opts = vim.tbl_deep_extend("force", config, opts or {})

-- Reset it to the original before every operation in case of a failure.
---@diagnostic disable-next-line: duplicate-set-field
Expand Down
6 changes: 3 additions & 3 deletions lua/lsp-preview/workspace_edit.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local lDiff = require("lsp-preview.diff")
local lTelescope = require("lsp-preview.telescope")
local config = require("lsp-preview.config")

local M = {}

Expand Down Expand Up @@ -41,12 +42,11 @@ end
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

local opts = config

lTelescope.apply_action(
opt,
opts,
documentChanges,
changes,
make_apply_func(workspace_edit, offset_encoding, orig_apply_workspace_edits)
Expand Down

0 comments on commit fdae11d

Please sign in to comment.