feat(nvim): cspell now uses condition, mason and a keybinding to add the word to a dictionary

This commit is contained in:
Sergio Laín 2024-01-07 01:05:54 +01:00
parent 4aae3b7069
commit 20b4515d0f
No known key found for this signature in database
GPG key ID: 14C9B8080681777B
5 changed files with 55 additions and 12 deletions

View file

@ -106,7 +106,7 @@
"persistent-breakpoints.nvim": { "branch": "main", "commit": "db2ad5974b0435cb806cd287e7525219d6ac4bd3" },
"plenary.nvim": { "branch": "master", "commit": "55d9fe89e33efd26f532ef20223e5f9430c8b0c0" },
"presence.nvim": { "branch": "main", "commit": "87c857a56b7703f976d3a5ef15967d80508df6e6" },
"rainbow-delimiters.nvim": { "branch": "master", "commit": "f7a55274a74053ccfafc24005b6f46303d543288" },
"rainbow-delimiters.nvim": { "branch": "master", "commit": "18d1350ec3897e7282b4cbf78ef6f50191ae78f8" },
"refactoring.nvim": { "branch": "master", "commit": "28c5a33a0c489eb559396d9424b042ccfdb03b14" },
"rustaceanvim": { "branch": "master", "commit": "f36e3a62b81b9a4ebd1771e6ee81fb5d85383af8" },
"satellite.nvim": { "branch": "main", "commit": "1a20861227eba8bf2d8282ab4ec5fc071e8b20e2" },

View file

@ -91,4 +91,5 @@
"NEWS.md": "2123"
},
"version": 2
}
}

View file

@ -1,14 +1,29 @@
return {
"mfussenegger/nvim-lint",
opts = {
linters_by_ft = {
["*"] = { "cspell" },
},
linters = {
cspell = {
condition = function(ctx)
return vim.fs.find({ "cspell.json" }, { path = ctx.filename, upward = true })[1]
end,
{
"williamboman/mason.nvim",
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, { "cspell" })
end,
},
{
"mfussenegger/nvim-lint",
opts = {
linters_by_ft = {
["*"] = { "cspell" },
},
linters = {
cspell = {
condition = function(ctx)
if vim.fs.find({ "cspell.json" }, { path = ctx.filename, upward = true })[1] then
-- stylua: ignore
vim.keymap.set("n", "<leader>!", function() require("util.cspell").addWordToDictionary() end, { desc = "Add Word to Dictionary", silent = true })
return true
else
return false
end
end,
},
},
},
},

View file

@ -0,0 +1,27 @@
local M = {}
function M.addWordToDictionary()
local word = vim.fn.expand("<cword>")
local dictionary_path = require("lazyvim.util.root").get() .. "/cspell-tool.txt"
-- Append the word to the dictionary file
local file = io.open(dictionary_path, "a")
if file then
-- Detect new line at the end of the file or not
local last_char = file:seek("end", -1)
if last_char ~= nil and last_char ~= "\n" then
word = "\n" .. word
end
file:write(word .. "")
file:close()
vim.notify("Added word to dictionary", "info", { title = "cSpell" })
-- Reload buffer to update the dictionary
vim.cmd("e!")
else
vim.notify("Could not open cSpell dictionary", "error", { title = "cSpell" })
end
end
return M

Binary file not shown.