Skip to content

Commit fdae11d

Browse files
author
Jan Steinke
committed
provide setup options for defaults
1 parent a8a1d6e commit fdae11d

File tree

4 files changed

+75
-12
lines changed

4 files changed

+75
-12
lines changed

README.md

+37-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
11
# LSP-Preview.nvim
22

3-
A plugin to that allows previewing of all changes done to a workspace before
3+
A plugin that allows previewing of all changes done to a workspace before
44
applying.
55

6+
Note: This plugin affects all workspace edits coming from the language server.
7+
Not all are currently supported, if you run into problems with other methods,
8+
please let me know.
9+
10+
## Installation
11+
12+
lazy.nvim:
13+
14+
```lua
15+
{
16+
'jan-xyz/lsp-preview.nvim',
17+
opts = {},
18+
}
19+
```
20+
21+
## Configuration
22+
23+
If you're fine with the defaults, you're good to go after installation. If you
24+
want to tweak, call this function:
25+
26+
```lua
27+
require("dressing").setup({
28+
--Automatically apply code-actions if there is only 1 available.
29+
apply = true,
30+
--Configuration provided to vim.diff (see `:h vim.diff()`)
31+
diff = {
32+
ctxlen = 5,
33+
},
34+
})
35+
```
36+
637
## TODO
738

839
* [x] implement selection
940
* [x] auto-select all changes
1041
* [x] implement rename
1142
* [x] make it work with normal workspace edits
1243
* [x] don't rely on picker order selection
13-
* [ ] break-down by edit and not by file
14-
* [ ] one buffer for all changes in a file, view-port shifts on selection
15-
* [ ] buffer contents updates based on selected changes
16-
* [ ] Allow sorting list by token type that changes (e.g. var, class, comment)
1744
* [x] allow disabling the preview
45+
* [x] provide configuration options
46+
* [ ] break-down by edit and not by file, one buffer for all changes in a file,
47+
view-port shifts on selection, buffer contents updates based on selected
48+
changes
49+
* [ ] Allow sorting list by token type that changes (e.g. var, class, comment)
1850

1951
## Inspired by
2052

lua/lsp-preview/config.lua

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
local M = {}
2+
3+
---@class Options
4+
---Automatically apply code-actions if there is only 1 available.
5+
---@field apply boolean
6+
---Configuration provided to vim.diff (see `:h vim.diff()`)
7+
---@field diff table
8+
local default_config = {
9+
apply = true,
10+
diff = {
11+
ctxlen = 5,
12+
},
13+
}
14+
15+
16+
local user_config = default_config
17+
18+
function M.setup(config)
19+
user_config = vim.tbl_deep_extend("force", default_config, config)
20+
end
21+
22+
setmetatable(M, {
23+
__index = function(_, key)
24+
return user_config[key]
25+
end,
26+
})
27+
28+
return M

lua/lsp-preview/init.lua

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local lWorkspaceEdit = require("lsp-preview.workspace_edit")
2+
local config = require("lsp-preview.config")
23

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

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

10-
function M.setup(_)
11-
11+
---Setup the default behaviour of the plugin
12+
---@param opts Options
13+
function M.setup(opts)
14+
config.setup(opts)
1215
end
1316

1417
M.rename = function(new_name, opts)
15-
opts = opts or {}
18+
opts = vim.tbl_deep_extend("force", config, opts or {})
1619

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

3942
M.code_action = function(opts)
40-
opts = opts or {}
43+
opts = vim.tbl_deep_extend("force", config, opts or {})
4144

4245
-- Reset it to the original before every operation in case of a failure.
4346
---@diagnostic disable-next-line: duplicate-set-field

lua/lsp-preview/workspace_edit.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local lDiff = require("lsp-preview.diff")
22
local lTelescope = require("lsp-preview.telescope")
3+
local config = require("lsp-preview.config")
34

45
local M = {}
56

@@ -41,12 +42,11 @@ end
4142
M.make_apply_workspace_edit = function(orig_apply_workspace_edits)
4243
return function(workspace_edit, offset_encoding)
4344
local documentChanges, changes = lDiff.get_changes(workspace_edit, offset_encoding)
44-
local opt = {}
45-
opt.diff = { ctxlen = 20 } -- provide a large diff context view
4645

46+
local opts = config
4747

4848
lTelescope.apply_action(
49-
opt,
49+
opts,
5050
documentChanges,
5151
changes,
5252
make_apply_func(workspace_edit, offset_encoding, orig_apply_workspace_edits)

0 commit comments

Comments
 (0)