Skip to content

Commit 79865d8

Browse files
committed
fix: respect language settings of imported files
cspell only respects the language of the main config, thus making it impossible to add new languages exclusively by importing them. This commit appends all the languages found in imported files to the main config automatically.
1 parent be04e47 commit 79865d8

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

lua/cspell/helpers.lua

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,29 @@ M.create_merged_cspell_json = function(params, cspell_config_mapping)
7979
return CONFIG_INFO_BY_PATH[merged_config_path]
8080
end
8181

82+
local cspell_json = {
83+
version = "0.2",
84+
language = "en",
85+
words = {},
86+
flagWords = {},
87+
import = cspell_config_paths,
88+
}
89+
8290
for _, cspell_config_path in pairs(cspell_config_mapping) do
8391
local path_exists = cspell_config_path ~= nil
8492
and cspell_config_path ~= ""
8593
and Path:new(cspell_config_path):exists()
8694
if path_exists then
95+
pcall(function()
96+
local cspell_config = vim.json.decode(Path:new(cspell_config_path):read())
97+
if
98+
cspell_config.language ~= nil
99+
and type(cspell_config.language) == "string"
100+
and cspell_config.language ~= ''
101+
then
102+
cspell_json.language = cspell_json.language .. "," .. cspell_config.language
103+
end
104+
end)
87105
table.insert(cspell_config_paths, cspell_config_path)
88106
else
89107
local debug_message = M.format(
@@ -94,14 +112,6 @@ M.create_merged_cspell_json = function(params, cspell_config_mapping)
94112
end
95113
end
96114

97-
local cspell_json = {
98-
version = "0.2",
99-
language = "en",
100-
words = {},
101-
flagWords = {},
102-
import = cspell_config_paths,
103-
}
104-
105115
local existing_config = M.get_cspell_config(params, merged_config_path)
106116

107117
if existing_config ~= nil then
@@ -219,8 +229,8 @@ M.generate_cspell_config_path = function(params, directory)
219229
if not vim.tbl_contains(CSPELL_CONFIG_FILES, config_file_preferred_name) then
220230
vim.notify(
221231
"Invalid config_file_preferred_name for cspell json file: "
222-
.. config_file_preferred_name
223-
.. '. The name "cspell.json" will be used instead',
232+
.. config_file_preferred_name
233+
.. '. The name "cspell.json" will be used instead',
224234
vim.log.levels.WARN,
225235
{ title = "cspell.nvim" }
226236
)

0 commit comments

Comments
 (0)