-
-
Save aghriss/90206887fef301febe6d644272bba367 to your computer and use it in GitHub Desktop.
lspconfig - Implement addToDictionary for ltex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local S = {} | |
-- define the files for each language | |
-- new words will be added to the last file in the language table | |
S.dictionaries = { | |
["en-US"] = { vim.fn.stdpath("config") .. "/spell/en.txt" }, | |
} | |
-- function to avoid interacting with the table directly | |
function S.getDictFiles(lang) | |
local files = S.dictionaries[lang] | |
if files then | |
return files | |
else | |
return nil | |
end | |
end | |
-- combine words from all the files. Each line should contain one word | |
function S.readDictFiles(lang) | |
local files = S.getDictFiles(lang) | |
local dict = {} | |
if files then | |
for _, file in ipairs(files) do | |
local f = io.open(file, "r") | |
if f then | |
for l in f:lines() do | |
table.insert(dict, l) | |
end | |
else | |
print("Can not read dict file %q", file) | |
end | |
end | |
else | |
print("Lang %q has no files", lang) | |
end | |
return dict | |
end | |
-- Append words to the last element of the language files | |
function S.addWordsToFiles(lang, words) | |
local files = S.getDictFiles(lang) | |
if not files then | |
return print("no dictionary file defined for lang %q", lang) | |
else | |
local file = io.open(files[#files - 0], "a+") | |
if file then | |
for _,word in ipairs(words) do | |
file:write(word .. "\n") | |
end | |
file:close() | |
else | |
return print("Failed insert %q", vim.inspect(words)) | |
end | |
end | |
end | |
-- The following part is a classic lspconfig config section | |
local lspconfig = require("lspconfig") | |
-- notifying wkspc will refresh the settings that contain the dictionary | |
local wkspc = "workspace/didChangeConfiguration" | |
-- instead of looping through the list of clients and check client.name == 'ltex' (which many solutions out there are doing) | |
-- We attach the command function to the bufer then ltex is loaded | |
local function on_attach(client, bufnr) | |
require("plugins.configs.lspconfig").on_attach(client, bufnr) | |
-- the second argumeng is named 'ctx', but we don't need it here | |
--- command = {argument={...}, command=..., title=...} | |
local addToDict = function(command, _) | |
for _, arg in ipairs(command.arguments) do | |
-- not the most efficent way, we could readDictFiles once per lang | |
for lang, words in pairs(arg.words) do | |
S.addWordsToFiles(lang, words) | |
client.config.settings.ltex.dictionary = { | |
[lang] = S.readDictFiles(lang), | |
} | |
end | |
end | |
-- notify the client of the new settings | |
return client.notify(wkspc, client.config.settings) | |
end | |
-- add the function to handle the command | |
-- then lsp.commands does not find the handler, it will look at opts.handler["workspace/executeCommand"] | |
vim.lsp.commands["_ltex.addToDictionary"] = addToDict | |
end | |
-- 'pluging.config.lspconfig' is from NvChad configuraion | |
lspconfig.ltex.setup({ | |
on_attach = on_attach, | |
capabilities = require("plugins.configs.lspconfig").capabilities, | |
filetypes = { "tex", "markdown" }, | |
settings = { | |
ltex = { | |
dictionary = { | |
["en-US"] = S.readDictFiles("en-US"), | |
}, | |
}, | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment