neovim-trunk
neovim-trunk copied to clipboard
Trunk Check Neovim Plugin
The Trunk Check Neovim Plugin is in Beta with limited support. If you encounter any issues, feel free to reach out to us on Slack or make a PR directory. For more information, see CONTRIBUTING.
Overview
Trunk Check runs 100+ tools to format, lint, static-analyze, and security-check dozens of languages and config formats. It will autodetect the best tools to run for your repo, then run them and provide results inline in Neovim. Compare to the Trunk Check VSCode Extension. The Neovim plugin has the following capabilities:
- Render diagnostics and autofixes inline
- Format files on save
- Display the list of linters that run on each file
- View and run commands from Trunk Action notifications
Installation
neovim-trunk can be installed using your favorite Neovim plugin manager. We've included some
instructions below:
Note: Some plugin managers offer varying capabilities for configuration and pinning refs
Prerequisites
- Minimum Neovim version:
v0.9.2 - Minimum Trunk CLI version:
1.17.0 - Some commands require
sedandteeto be inPATH - Format on save timeout only works on UNIX and if coreutils
timeoutis inPATH
lazy.nvim
- Follow the lazy.nvim install instructions to
modify your
lua/init.luafile (on UNIX~/.config/nvim/lua/init.lua) - Add the line
lua require("init")to yourinit.vimfile (on UNIX~/.config/nvim/init.vim) - Add the following setup to your
lua/init.luafile:
require("lazy").setup({
{
"trunk-io/neovim-trunk",
lazy = false,
-- optionally pin the version
-- tag = "v0.1.1",
-- these are optional config arguments (defaults shown)
config = {
-- trunkPath = "trunk",
-- lspArgs = {},
-- formatOnSave = true,
-- formatOnSaveTimeout = 10, -- seconds
-- logLevel = "info"
},
main = "trunk",
dependencies = {"nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim"}
}
})
vim-plug
- Install vim-plug
- Add the following to your
init.vimfile (on UNIX~/.config/nvim/init.vim)
call plug#begin()
" Required dependencies
Plug 'nvim-telescope/telescope.nvim'
Plug 'nvim-lua/plenary.nvim'
Plug 'trunk-io/neovim-trunk', { 'tag': '*' }
call plug#end()
lua if not pcall(function() require'trunk'.setup({}) end) then print("Run :PlugInstall and then relaunch Neovim to setup Trunk") end
- Add settings to the
setupcommand as desired. - Call
:PlugInstallto install and:PlugStatusto verify - Close and relaunch Neovim to start running Trunk
packer.nvim
- Follow the
packer.nvim install instructions to
modify your
lua/plugins.luafile (on UNIX~/.config/nvim/lua/plugins.lua). - Add the line
lua require("plugins")to your init.vim file (on UNIX~/.config/nvim/init.vim) - Add the following setup to your
lua/plugins.luafile:
return require("packer").startup(function(use)
use {
"trunk-io/neovim-trunk",
tag = "*",
requires = {"nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim"},
-- these are optional config arguments (defaults shown)
config = function() require("trunk").setup({
-- trunkPath = "trunk",
-- formatOnSave = true,
-- lspArgs = {},
-- formatOnSaveTimeout = 10, -- seconds
-- logLevel = "info"
}) end
}
end)
- Call
:PackerSyncto install and:PackerStatusto verify - Close and relaunch Neovim to start running Trunk
paq-nvim
- Install paq-nvim
- Add the following setup to your
lua/init.lua(on UNIX~/.config/nvim/lua/init.lua) file:
require "paq" {
-- Required dependencies
"nvim-telescope/telescope.nvim",
"nvim-lua/plenary.nvim",
"trunk-io/neovim-trunk"
}
if not pcall(function() require'trunk'.setup({}) end) then
print("Run :PaqInstall and then relaunch Neovim to setup Trunk")
end
- Add settings to the
setupcommand as desired. - Add the line
lua require("init")to yourinit.vimfile (on UNIX~/.config/nvim/init.vim) - Run
:PaqInstall - Close and relaunch Neovim to start running Trunk
Usage
- Verify that trunk is in your PATH and you have run
trunk initin your repo - Open a file in neovim
nvim <file> - View inline diagnostic annotations
- Run
:lua vim.lsp.buf.code_action()on a highlighted section to view and apply autofixes - Format on save is enabled by default: make a change and write to buffer (
:w) to autoformat the file
Other commands:
:TrunkConfigto open the repo.trunk/trunk.yamlfile for editing:TrunkStatusto review any linter failures:TrunkQueryto view the list of linters that run on your current file:TrunkActionsto view any Trunk Actions that have generated notifications and run their commands as appropriate
Configuration
The neovim extension can be configured as follows:
| Option | Configures | Default |
|---|---|---|
| trunkPath | Where to find the Trunk CLI launcher of binary | "trunk" |
| lspArgs | Optional arguments to append the Trunk LSP Server | {} |
| formatOnSave | Whether or not to autoformat file buffers when written | true |
| formatOnSaveTimeout | The maximum amount of time to spend attempting to autoformat, in seconds | 10 |
| logLevel | Verbosity of logs from the Neovim extension | "info" |
(These settings can be changed after loading by calling require("neovim-trunk").setup({}))
Notes
Unlike for VSCode, the Trunk Check Neovim Plugin does not currently provide any summary views for diagnostics. If you'd like, you can use a plugin like Trouble to view aggregate code actions.
Please view our docs for any additional Trunk setup instructions, as well as our plugins repo for the up to date list of supported linters.
When lint diagnostics show up, they may cause the line number column increase in width. To fix this,
run :set signcolumn=yes to have a column for diagnostics always present.