Skip to content

Commit

Permalink
Add commands for custom code action callbacks (#15)
Browse files Browse the repository at this point in the history
In recent CLI versions, LSP will suggest code actions for
enabling/disabling linters and opening the Trunk config. By registering
commands to the
[`commands`](https://neovim.io/doc/user/lsp.html#vim.lsp.commands)
table, we can provide callbacks for these.

Atm we don't have a non-ANSI coloring mode so we strip the output again
with `sed`.

This approach will likely not be feasible for `emacs` since if the
commands don't exist it will attempt to execute them on the command line
IIRC.
  • Loading branch information
TylerJang27 authored Jan 5, 2024
1 parent 228ba33 commit 1a14c42
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions .trunk/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
plugins
user_trunk.yaml
user.yaml
tmp
8 changes: 4 additions & 4 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
version: 0.1
cli:
version: 1.17.2-beta.5
version: 1.18.2-beta.14
options:
- commands: [upgrade]
args: -y --no-progress
plugins:
sources:
- id: trunk
ref: v1.2.5
ref: v1.4.1
uri: https://github.com/trunk-io/plugins
- id: configs
uri: https://github.com/trunk-io/configs
ref: v0.0.7
ref: v1.0.1
lint:
enabled:
- stylua@0.18.2
- stylua@0.19.1
ignore:
- linters: [ALL]
paths: [lsp*]
Expand Down
26 changes: 26 additions & 0 deletions lua/trunk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,32 @@ local function connect()
-- TODO(Tyler): Conditionally add a progress bar pane?
end,
},
-- custom callbacks for commands from code actions
commands = {
["trunk.checkEnable"] = function(command, _client_info, _command_str, _args)
-- TODO(Tyler): Use non-ANSI mode
vim.cmd(
"!"
.. table.concat(executionTrunkPath(), " ")
.. " check enable "
.. table.concat(command["arguments"])
.. [[ | sed -e 's/\x1b\[[0-9;]*m//g']]
)
end,
["trunk.checkDisable"] = function(command, _client_info, _command_str, _args)
-- TODO(Tyler): Use non-ANSI mode
vim.cmd(
"!"
.. table.concat(executionTrunkPath(), " ")
.. " check disable "
.. table.concat(command["arguments"])
.. [[ | sed -e 's/\x1b\[[0-9;]*m//g']]
)
end,
["trunk.openConfigFile"] = function(_command, _client_info, _command_str, _args)
vim.cmd(":edit " .. findConfig())
end,
},
})
end
end
Expand Down

0 comments on commit 1a14c42

Please sign in to comment.