-
-
Save lbiaggi/a3eb761ac2fdbff774b29c88844355b8 to your computer and use it in GitHub Desktop.
--- BEFORE USING, change language entries to fit your needs. | |
local lspconfig = require'lspconfig' | |
local configs = require'lspconfig/configs' | |
local util = require 'lspconfig/util' | |
local Dictionary_file = { | |
["pt-BR"] = {vim.fn.getenv("NVIM_HOME") .. "spell/dictionary.txt"} -- is there another way to find ~/.config/nvim ? | |
} | |
local DisabledRules_file = { | |
["pt-BR"] = {vim.fn.getenv("NVIM_HOME") .. "spell/disable.txt"} -- is there another way to find ~/.config/nvim ? | |
} | |
local FalsePositives_file = { | |
["pt-BR"] = {vim.fn.getenv("NVIM_HOME") .. "spell/false.txt"} -- is there another way to find ~/.config/nvim ? | |
} | |
local function readFiles(files) | |
local dict = {} | |
for _,file in ipairs(files) do | |
local f = io.open(file, "r") | |
for l in f:lines() do | |
table.insert(dict, l) | |
end | |
end | |
return dict | |
end | |
local function findLtexLang() | |
local buf_clients = vim.lsp.buf_get_clients() | |
for _, client in ipairs(buf_clients) do | |
if client.name == "ltex" then | |
return client.config.settings.ltex.language | |
end | |
end | |
end | |
local function findLtexFiles(filetype, value) | |
local files = nil | |
if filetype == 'dictionary' then | |
files = Dictionary_file[value or findLtexLang()] | |
elseif filetype == 'disable' then | |
files = DisabledRules_file[value or findLtexLang()] | |
elseif filetype == 'falsePositive' then | |
files = FalsePositives_file[value or findLtexLang()] | |
end | |
if files then | |
return files | |
else | |
return nil | |
end | |
end | |
local function updateConfig(lang, configtype) | |
local buf_clients = vim.lsp.buf_get_clients() | |
local client = nil | |
for _, lsp in ipairs(buf_clients) do | |
if lsp.name == "ltex" then | |
client = lsp | |
end | |
end | |
if client then | |
if configtype == 'dictionary' then | |
if client.config.settings.ltex.dictionary then | |
client.config.settings.ltex.dictionary = { | |
[lang] = readFiles(Dictionary_file[lang]) | |
}; | |
return client.notify('workspace/didChangeConfiguration', client.config.settings) | |
else | |
return vim.notify("Error when reading dictionary config, check it") | |
end | |
elseif configtype == 'disable' then | |
if client.config.settings.ltex.disabledRules then | |
client.config.settings.ltex.disabledRules = { | |
[lang] = readFiles(DisabledRules_file[lang]) | |
}; | |
return client.notify('workspace/didChangeConfiguration', client.config.settings) | |
else | |
return vim.notify("Error when reading disabledRules config, check it") | |
end | |
elseif configtype == 'falsePositive' then | |
if client.config.settings.ltex.hiddenFalsePositives then | |
client.config.settings.ltex.hiddenFalsePositives = { | |
[lang] = readFiles(FalsePositives_file[lang]) | |
}; | |
return client.notify('workspace/didChangeConfiguration', client.config.settings) | |
else | |
return vim.notify("Error when reading hiddenFalsePositives config, check it") | |
end | |
end | |
else | |
return nil | |
end | |
end | |
local function addToFile(filetype, lang, file, value) | |
file = io.open(file[#file-0], "a+") -- add only to last file defined. | |
if file then | |
file:write(value .. "\n") | |
file:close() | |
else | |
return print("Failed insert %q", value) | |
end | |
if filetype == 'dictionary' then | |
return updateConfig(lang, "dictionary") | |
elseif filetype == 'disable' then | |
return updateConfig(lang, "disable") | |
elseif filetype == 'falsePositive' then | |
return updateConfig(lang, "disable") | |
end | |
end | |
local function addTo(filetype, lang, file, value) | |
local dict = readFiles(file) | |
for _, v in ipairs(dict) do | |
if v == value then | |
return nil | |
end | |
end | |
return addToFile(filetype, lang, file, value) | |
end | |
if not lspconfig.ltex then | |
configs.ltex = { | |
default_config = { | |
cmd = {"ltex-ls"}; | |
filetypes = {'tex', 'bib', 'md'}; | |
root_dir = function(filename) | |
return util.path.dirname(filename) | |
end; | |
settings = { | |
ltex = { | |
enabled= {"latex", "tex", "bib", "md"}, | |
checkFrequency="save", | |
language="pt-BR", | |
diagnosticSeverity="information", | |
setenceCacheSize=5000, | |
additionalRules = { | |
enablePickyRules = true, | |
motherTongue= "pt-BR", | |
}; | |
-- trace = { server = "verbose"}; | |
-- ['ltex-ls'] = { | |
-- logLevel = "finest", | |
-- }, | |
dictionary = { | |
["pt-BR"] = readFiles(Dictionary_file["pt-BR"] or {}), | |
}; | |
disabledRules = { | |
["pt-BR"] = readFiles(DisabledRules_file["pt-BR"] or {}), | |
}; | |
hiddenFalsePositives = { | |
["pt-BR"] = readFiles(FalsePositives_file["pt-BR"] or {}), | |
}; | |
}, | |
}; | |
}; | |
}; | |
end | |
lspconfig.ltex.setup{} | |
lspconfig.ltex.dictionary_file = Dictionary_file | |
lspconfig.ltex.disabledrules_file = DisabledRules_file | |
lspconfig.ltex.falsepostivies_file = FalsePositives_file | |
-- https://github.com/neovim/nvim-lspconfig/issues/858 can't intercept, | |
-- override it then. | |
local orig_execute_command = vim.lsp.buf.execute_command | |
vim.lsp.buf.execute_command = function(command) | |
if command.command == '_ltex.addToDictionary' then | |
local arg = command.arguments[1].words -- can I really access like this? | |
for lang, words in pairs(arg) do | |
for _, word in ipairs(words) do | |
local filetype = "dictionary" | |
addTo(filetype,lang, findLtexFiles(filetype,lang), word) | |
end | |
end | |
elseif command.command == '_ltex.disableRules' then | |
local arg = command.arguments[1].ruleIds -- can I really access like this? | |
for lang, rules in pairs(arg) do | |
for _, rule in ipairs(rules) do | |
local filetype = "disable" | |
addTo(filetype,lang,findLtexFiles(filetype,lang), rule) | |
end | |
end | |
elseif command.command == '_ltex.hideFalsePositives' then | |
local arg = command.arguments[1].falsePositives -- can I really access like this? | |
for lang, rules in pairs(arg) do | |
for _, rule in ipairs(rules) do | |
local filetype = "falsePositive" | |
addTo(filetype,lang,findLtexFiles(filetype,lang), rule) | |
end | |
end | |
else | |
orig_execute_command(command) | |
end | |
end |
Ok :)
I used this file instead of yours from your guide and now nvim works without errors, but lsp-server still not starting. LspInfo says: filetype: No filetypes defined, please define filetypes in setup().
Where I should to define filetypes, can you share the example?
My init.vim
code for your lsp is:
lua << EOF
require('ltex')
EOF
Ok :)
I used this file instead of yours from your guide and now nvim works without errors, but lsp-server still not starting. LspInfo says:
filetype: No filetypes defined, please define filetypes in setup().
Where I should to define filetypes, can you share the example?My
init.vim
cod for your lsp is:lua << EOF require('ltex') EOF
the mentioned file is my PR which still a WIP
try this:
let g:tex_flavor = "latex"
silent! au FileType tex\
silent luafile ~/.config/nvim/lua/ltex.lua
you can use vim.fn.stdpath "config"
to get neovim's config directory
I'm trying to disable a few rules in my config. Could you share what the content of spell/disable.txt
looks like? I tried to create a file with the following content to disable English spelling:
MORFOLOGIK_RULE_EN
I got it working. The problem is on my side, as I forgot the settings
key and was only using ltex
.
Hello there!
I've made a plugin to support code actions (and some extras) in neovim with native LSP.
Handle dictionaries, false positives, and hidden rules in external files like the vscode-ltex plugin. The files are compatible between these plugins (same naming and content).
In the repository I drop some demos: barreiroleo/ltex_extra.nvim. Better name suggestions are welcome.
Thanks in advance to the people who want to try it and give me their opinion. Thanks also @lbiaggi for some ideas that i took from you.
A minimal script to implement _ltex.addToDictionary
:
https://gist.github.com/aghriss/90206887fef301febe6d644272bba367
No, but yours can be. I define it