Skip to content

trunk-io/neovim-trunk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trunk Check Neovim Plugin

docs slack

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
neovim

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 sed and tee to be in PATH
  • Format on save timeout only works on UNIX and if coreutils timeout is in PATH
  1. Follow the lazy.nvim install instructions to modify your lua/init.lua file (on UNIX ~/.config/nvim/lua/init.lua)
  2. Add the line lua require("init") to your init.vim file (on UNIX ~/.config/nvim/init.vim)
  3. Add the following setup to your lua/init.lua file:
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"}
	}
})
  1. Install vim-plug
  2. Add the following to your init.vim file (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
  1. Add settings to the setup command as desired.
  2. Call :PlugInstall to install and :PlugStatus to verify
  3. Close and relaunch Neovim to start running Trunk
  1. Follow the packer.nvim install instructions to modify your lua/plugins.lua file (on UNIX ~/.config/nvim/lua/plugins.lua).
  2. Add the line lua require("plugins") to your init.vim file (on UNIX ~/.config/nvim/init.vim)
  3. Add the following setup to your lua/plugins.lua file:
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)
  1. Call :PackerSync to install and :PackerStatus to verify
  2. Close and relaunch Neovim to start running Trunk
  1. Install paq-nvim
  2. 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
  1. Add settings to the setup command as desired.
  2. Add the line lua require("init") to your init.vim file (on UNIX ~/.config/nvim/init.vim)
  3. Run :PaqInstall
  4. Close and relaunch Neovim to start running Trunk

Usage

  1. Verify that trunk is in your PATH and you have run trunk init in your repo
  2. Open a file in neovim nvim <file>
  3. View inline diagnostic annotations
  4. Run :lua vim.lsp.buf.code_action() on a highlighted section to view and apply autofixes
  5. Format on save is enabled by default: make a change and write to buffer (:w) to autoformat the file

Other commands:

  1. :TrunkConfig to open the repo .trunk/trunk.yaml file for editing
  2. :TrunkStatus to review any linter failures
  3. :TrunkQuery to view the list of linters that run on your current file
  4. :TrunkActions to 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.