Files
dotfiles/.config/nvim/fnl/lspconfig/ltex.fnl
2022-08-23 20:44:19 +02:00

76 lines
4.9 KiB
Fennel

(import-macros {: exec : map!} :hibiscus.vim)
(local M {})
(local files {:dictionary (.. (vim.fn.stdpath "config") "/spell/en.utf-8.add")
:disabledRules (.. (vim.fn.stdpath "config") "/spell/disable.txt")
:hiddenFalsePositives (.. (vim.fn.stdpath "config") "/spell/false.txt")})
(fn file_exists [file] (let [f (io.open file "rb")]
(match f nil (do (f.close f) false)
_ true)))
(fn lines_from [file] (match (file_exists file) false []
true (let [lines []]
(do (each [line (io.lines file)]
(table.insert lines line))
lines))))
(fn get_client_by_name [name] (let [buf_clients (vim.lsp.buf_get_clients)]
(each [_ client (ipairs buf_clients)]
(match client.name name (lua "return client")))))
(fn update_config [lang configtype] (let [client (get_client_by_name "ltex")]
(if (not= client nil)
(if (not= (. client.config.settings.ltex configtype) nil)
(do (tset client.config.settings.ltex configtype {lang (lines_from (. files configtype))})
(client.notify "workspace/didChangeConfiguration" client.config.settings))
(vim.notify "Error when reading dictionary config, check it")))))
(fn add_to_file [configtype lang file value] (let [dict (lines_from file)]
(do (each [_ v (ipairs dict)]
(if (= v value) (lua "return nil")))
(let [file (io.open file "a+")]
(if (not= file nil)
(do (file.write file (.. value "\n"))
(file.close file)
(update_config lang configtype))
(vim.notify (string.format "Failed insert %s" value)))))))
(fn do_command [arg configtype] (each [lang words (pairs arg)]
(each [_ word (ipairs words)]
(add_to_file configtype lang (. files configtype) word))))
(tset vim.lsp.commands "_ltex.addToDictionary"
(fn [command _] (do_command (. (. command.arguments 1) :words) "dictionary")))
(tset vim.lsp.commands "_ltex.disableRules"
(fn [command _] (do_command (. (. command.arguments 1) :ruleIds) "disabledRules")))
(tset vim.lsp.commands "_ltex.hideFalsePositives"
(fn [command _] (do_command (. (. command.arguments 1) :falsePositives) "hiddenFalsePositives")))
(fn post_attach [] (do (exec [[:setlocal "spell"] [:setlocal "nospell"]])
(update_config "en-US" "dictionary")
(update_config "en-US" "disabledRules")
(update_config "en-US" "hiddenFalsePositives")
(map! [n :buffer :verbose] :zug (fn [] (do (exec [[:normal! "zug"]])
(update_config "en-US" "dictionary")
nil))
"Remove word from spellfile and update ltex")
(map! [n :buffer :verbose] :zg (fn [] (do (exec [[:normal! "zg"]])
(update_config "en-US" "dictionary")
nil))
"Add word to spellfile and update ltex")
nil))
(set M.setup (fn [opts]
((. (. (require "lspconfig") "ltex") :setup) {:on_attach (fn [client]
(do (opts.on_attach client) (post_attach) nil))
:capabilities opts.capabilities
:flags opts.flags
:settings {:ltex {:dictionary []
:disabledRules []
:hiddenFalsePositives []}
:additionalRules {:enablePickyRules true}}})))
M