From 17d166ceb81daa49c24a415a9bfe235388bb8a32 Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Tue, 17 Oct 2023 20:55:46 +0800 Subject: [PATCH] docs: add usage with neovim --- README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- cspell.lua | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 cspell.lua diff --git a/README.md b/README.md index 2954642..4b8d0a5 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ - [Usage](#usage) - [Installation](#installation) - [Features](#features) + - [Usage with Neovim and null-ls](#usage-with-neovim-and-null-ls) - [Contributing](#contributing) - [License](#license) - [Acknowledgements](#acknowledgements) @@ -40,10 +41,61 @@ npm install -g cspell-tool ## Features -- Supports multiple file formats like `.md`, `.ts`, and `.json`. +- Supports multiple file formats like `.md`, `.ts`, `.lua` and `.json`. - Easily customizable via `cspell.json`. - Extends your project-specific dictionary. +### Usage with Neovim and null-ls + +This assumes you have [`mason.nvim`](https://github.com/williamboman/mason.nvim) and [`null-ls.nvim`](https://github.com/nvimtools/none-ls.nvim) installed. + +1. **Installing cSpell with Mason** + + Make sure your `mason.nvim` configuration in your `init.lua` includes `cspell` under `ensure_installed`: + + ```lua + ensure_installed = { + -- code spell + "codespell", + "misspell", + "cspell", + }, + ``` + +2. **Setting Up null-ls** + + Add the following code to your `init.lua` to set up `null-ls` for spell checking: + + ```lua + local cspell = require("cspell") + local ok, none_ls = pcall(require, "null-ls") + if not ok then + return + end + + local b = none_ls.builtins + local sources = { + -- spell check + b.diagnostics.codespell, + b.diagnostics.misspell, + -- cspell + cspell.diagnostics.with({ + diagnostics_postprocess = function(diagnostic) + diagnostic.severity = vim.diagnostic.severity["HINT"] + end, + }), + cspell.code_actions, + } + + return { + sources = sources, + debounce = 200, + debug = true, + } + ``` + +More details can be found in [`cspell example config with lazyvim`](./cspell.lua). + ## Contributing Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. diff --git a/cspell.lua b/cspell.lua new file mode 100644 index 0000000..45cf79b --- /dev/null +++ b/cspell.lua @@ -0,0 +1,53 @@ +-- Example config with lazyvim +return { + -- Auto install those tools with mason + { + "williamboman/mason.nvim", + opts = { + ensure_installed = { + -- code spell + "codespell", + "misspell", + "cspell", + }, + }, + }, + -- Set up null-ls to check spelling + { + "nvimtools/none-ls.nvim", + keys = { + { "cn", "NullLsInfo", desc = "NullLs Info" }, + }, + dependencies = { "mason.nvim", "davidmh/cspell.nvim" }, + event = { "BufReadPre", "BufNewFile" }, + opts = function() + local cspell = require("cspell") + local ok, none_ls = pcall(require, "null-ls") + if not ok then + return + end + + local b = none_ls.builtins + + local sources = { + + -- spell check + b.diagnostics.codespell, + b.diagnostics.misspell, + -- cspell + cspell.diagnostics.with({ + -- Set the severity to HINT for unknown words + diagnostics_postprocess = function(diagnostic) + diagnostic.severity = vim.diagnostic.severity["HINT"] + end, + }), + cspell.code_actions, + } + return { + sources = sources, + debounce = 200, + debug = true, + } + end, + }, +}