Home

Awesome

<!-- LTeX: enabled=false -->

nvim-rulebook 📖

<!-- LTeX: enabled=true -->

<a href="https://dotfyle.com/plugins/chrisgrieser/nvim-rulebook"><img alt ="badge" src="https://dotfyle.com/plugins/chrisgrieser/nvim-rulebook/shield"/></a>

Add inline-comments to ignore rules, or lookup rule documentation online.

Some LSPs provide code actions for that – this plugin adds commands for linters and LSPs that don't.

<!-- toc --> <!-- tocstop -->

Features

Supported Sources

You easily add a custom source via the plugin configuration. Though, please consider making a PR to add support for a source if it is missing.

Rule Data for built-in support of linters and formatters

<!-- INFO use `just update-readme` to automatically update this section --> <!-- auto-generated: start -->

Rule Lookup

Add Ignore Comment

<!-- auto-generated: end --> <!-- TODO: auto-generate this section as well -->

Suppress Formatting

Installation

This plugin requires diagnostics provided by a source that supports Neovim's built-in diagnostics system. (nvim's built-in LSP client, efm-langserver or nvim-lint are such sources.)

In addition, nvim 0.10 is required.

-- lazy.nvim
{
	"chrisgrieser/nvim-rulebook",
	keys = {
		{ "<leader>ri", function() require("rulebook").ignoreRule() end },
		{ "<leader>rl", function() require("rulebook").lookupRule() end },
		{ "<leader>ry", function() require("rulebook").yankDiagnosticCode() end },
		{ "<leader>sf", function() require("rulebook").suppressFormatter() end, mode = {"n", "x"} },
	}
},
-- packer
use { "chrisgrieser/nvim-rulebook" }

-- in your config
vim.keymap.set("n", "<leader>ri", function() require("rulebook").ignoreRule() end)
vim.keymap.set("n", "<leader>rl", function() require("rulebook").lookupRule() end)
vim.keymap.set("n", "<leader>ry", function() require("rulebook").yankDiagnosticCode() end)
vim.keymap.set({ "n", "x" }, "<leader>sf", function() require("rulebook").suppressFormatter() end)

Configuration

The configuration is optional. You only need to add a config when you want to add or customize sources.

When adding your own source, you must add the exact, case-sensitive source name, for example, clang-tidy, not clang.

require("rulebook").setup = ({
	-- if no diagnostic is found in current line, search this many lines forward
	forwSearchLines = 10,

	ignoreComments = {
		selene = {
			comment = "-- selene: allow(%s)",
			location = "prevLine",
		},
		-- ... (full list of supported sources can be found in the README)

		yourCustomSource = { -- exact, case-sensitive source-name
			comment = "// disabling-comment %s", -- %s will be replaced with rule-id
			location = "prevLine", -- "prevLine"|"sameLine"|"encloseLine"
		}

		-- if location is "encloseLine", needs to be a list of two strings
		anotherCustomSource = {
			comment = { "// disable-rule %s", "// enable-rule %s" },
			location = "encloseLine",
		}
	},
	ruleDocs = {
		selene = "https://kampfkarren.github.io/selene/lints/%s.html"
		-- ... (full list of supported sources can be found in the README)

		-- Search URL when no documentation definition is available for a
		-- diagnostic source. `%s` will be replaced with the diagnostic source & code.
		-- Default is the DDG "Ducky Search" (automatically opening first result).
		fallback = "https://duckduckgo.com/?q=%s+%%21ducky&kl=en-us",

		-- the value of the rule documentations accept either a string or a function
		-- if a string, `%s` will be replaced with rule-id
		-- if a function, takes a `:h diagnostic-structure` as argument and must 
		-- return a url
		yourCustomSource = "https://my-docs/%s.hthml",
		anotherCustomSource = function(diag)
			-- ...
			return url
		end,
	}
	suppressFormatter = {
		lua = {
			-- normal mode
			ignoreBlock = "-- stylua: ignore",
			location = "prevLine",

			-- visual mode
			ignoreRange = { "-- stylua: ignore start", "-- stylua: ignore start" },
		},
	}
})

The plugin uses vim.ui.select, so the appearance of the rule selection can be customized by using a UI-plugin like dressing.nvim.

Customize Built-in Sources

Built-in sources be customized by overwriting them in the configuration:

-- example: use `disable-line` instead of the default `disable-next-line` for eslint
require("rulebook").setup = {
	ignoreComments = {
		eslint = {
			comment = "// eslint-disable-line %s",
			location = "sameLine",
		},
	},
}

Limitations

API

Availability of Rule Lookup

The function require("rulebook").hasDocs(diag), expects a diagnostic object and returns a boolean whether nvim-rulebook documentation for the respective diagnostic available. One use case for this is to add a visual indicator if there is a rule lookup available for a diagnostic (see vim.diagnostic.config).

vim.diagnostic.config {
	virtual_text = {
		suffix = function(diag) return require("rulebook").hasDocs(diag) and " îȘ€ " or "" end,
	},
}
<!-- vale Google.FirstPerson = NO -->

Credits

In my day job, I am a sociologist studying the social mechanisms underlying the digital economy. For my PhD project, I investigate the governance of the app economy and how software ecosystems manage the tension between innovation and compatibility. If you are interested in this subject, feel free to get in touch.

I also occasionally blog about vim: Nano Tips for Vim

<a href='https://ko-fi.com/Y8Y86SQ91' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>