Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

feat(cspell): Option to run cspell diagnostics with a specific cspell config #1329

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions lua/null-ls/builtins/code_actions/cspell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,6 @@ local cspell_diagnostics = function(bufnr, lnum, cursor_col)
return diagnostics
end

local CSPELL_CONFIG_FILES = {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to reuse these between diagnostics and code_actions so I moved them to a shared cspell helper

"cspell.json",
".cspell.json",
"cSpell.json",
".Sspell.json",
".cspell.config.json",
}

-- find the first cspell.json file in the directory tree
local find_cspell_config = function(cwd)
local cspell_json_file = nil
for _, file in ipairs(CSPELL_CONFIG_FILES) do
local path = vim.fn.findfile(file, (cwd or vim.loop.cwd()) .. ";")
if path ~= "" then
cspell_json_file = path
break
end
end
return cspell_json_file
end

-- create a bare minimum cspell.json file
local create_cspell_json = function(cwd, file_name)
local cspell_json = {
Expand Down Expand Up @@ -65,13 +44,13 @@ return h.make_builtin({
fn = function(params)
local actions = {}
local config = params:get_config()
local find_json = config.find_json or find_cspell_config
local find_json = config.find_json or h.cspell.find_cspell_config

-- create_config_file if nil defaults to true
local create_config_file = config.create_config_file ~= false

local create_config_file_name = config.create_config_file_name or "cspell.json"
if not vim.tbl_contains(CSPELL_CONFIG_FILES, create_config_file_name) then
if not vim.tbl_contains(h.cspell.CSPELL_CONFIG_FILES, create_config_file_name) then
vim.notify(
"Invalid default file name for cspell json file: "
.. create_config_file_name
Expand Down
10 changes: 10 additions & 0 deletions lua/null-ls/builtins/diagnostics/cspell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ return h.make_builtin({
generator_opts = {
command = "cspell",
args = function(params)
local config = params:get_config()
local find_json = config.find_json or h.cspell.find_cspell_config
local cspell_json_file = find_json(params.cwd)

if cspell_json_file == nil or cspell_json_file == "" then
cspell_json_file = "cspell.json" -- default for the -c option
end

local cspell_args = {
"lint",
"-c",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, cspell can run without a config file. What will happen when this flag is used but the config file doesn't exist?

cspell_json_file,
"--language-id",
params.ft,
"stdin",
Expand Down
25 changes: 25 additions & 0 deletions lua/null-ls/helpers/cspell.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local CSPELL_CONFIG_FILES = {
Copy link
Author

@juanpprieto juanpprieto Jan 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it might be the wrong place for this logic as it's not a global null-ls helper. I just couldn't find a more suitable folder. Maybe adding a common or shared folder within builtins/ would be better?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree that if we're going to go with this approach we should put this elsewhere.

"cspell.json",
".cspell.json",
"cSpell.json",
".Sspell.json",
".cspell.config.json",
}

-- find the first cspell.json file in the directory tree
local find_cspell_config = function(cwd)
local cspell_json_file = nil
for _, file in ipairs(CSPELL_CONFIG_FILES) do
local path = vim.fn.findfile(file, (cwd or vim.loop.cwd()) .. ";")
if path ~= "" then
cspell_json_file = path
break
end
end
return cspell_json_file
end

return {
CSPELL_CONFIG_FILES = CSPELL_CONFIG_FILES,
find_cspell_config = find_cspell_config,
}
1 change: 1 addition & 0 deletions lua/null-ls/helpers/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {
cache = require("null-ls.helpers.cache"),
cspell = require("null-ls.helpers.cspell"),
diagnostics = require("null-ls.helpers.diagnostics"),
formatter_factory = require("null-ls.helpers.formatter_factory"),
generator_factory = require("null-ls.helpers.generator_factory"),
Expand Down