From 44bb3db121b891dbb6bb46b8d698dcddf0e09757 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 17 Mar 2024 09:51:01 -0400 Subject: [PATCH] optional ltex languagetool secret --- lua/custom/configs/lspsettings.lua | 20 ++++-- lua/custom/configs/overrides.lua | 19 +---- lua/custom/utils.lua | 112 ++++++++++++++++------------- 3 files changed, 81 insertions(+), 70 deletions(-) diff --git a/lua/custom/configs/lspsettings.lua b/lua/custom/configs/lspsettings.lua index e8da2c4..0a26aee 100644 --- a/lua/custom/configs/lspsettings.lua +++ b/lua/custom/configs/lspsettings.lua @@ -52,12 +52,7 @@ M.ltex = { "html", "xhtml", }, - languageToolOrg = { - apiKey = require "custom.secret.languagetool_token", - username = "accounts@cscherr.de", - languageToolHttpServerUrl = "https://api.languagetoolplus.com/v2/", - }, - languageToolHttpServerUrl = "https://api.languagetoolplus.com/v2/", + -- load token and additional languagetool items later }, }, } @@ -170,4 +165,17 @@ M.textlsp = { }, }, } + +-- load secrets +-- the secret module should just return a string with the token +local available, token = require("custom.utils").try_require "custom.secret.languagetool_token" +if available then + M.ltex.languageToolOrg = { + apiKey = token, + username = "accounts@cscherr.de", + languageToolHttpServerUrl = "https://api.languagetoolplus.com/v2/", + } + M.ltex.languageToolHttpServerUrl = "https://api.languagetoolplus.com/v2/" +end + return M diff --git a/lua/custom/configs/overrides.lua b/lua/custom/configs/overrides.lua index d1217f5..5226d76 100644 --- a/lua/custom/configs/overrides.lua +++ b/lua/custom/configs/overrides.lua @@ -30,38 +30,25 @@ M.treesitter = { M.mason = { ensure_installed = { - -- general purpose "purpose", - - -- lua stuff "lua-language-server", "stylua", - - -- web dev stuff "css-lsp", "html-lsp", "typescript-language-server", "deno", "prettier", - - -- c/cpp stuff "clangd", "clang-format", "cmake-language-server", - - -- rust "rust-analyzer", "taplo", - - -- python "pyright", - - -- english?? - -- "write-good", - - -- shell "shellcheck", "bash-language-server", + "ltex-ls", + "shellcheck", + "taplo", }, } diff --git a/lua/custom/utils.lua b/lua/custom/utils.lua index 11158b9..cef1dc2 100644 --- a/lua/custom/utils.lua +++ b/lua/custom/utils.lua @@ -5,44 +5,44 @@ local M = {} --- @param raw string string of cli args that should be split --- @return table tokens args as a table M.tokenize_args = function(raw) - -- NOTE: string.gmatch is does not use regex, but a smaller pattern matcher! - -- A complete regex parser would be larger than lua itself. See - -- [Programming in Lua 20.2](https://www.lua.org/pil/20.2.html). - -- - -- Notable differences: - -- '-' is ungreedy wildcard - -- '*?' does not work - -- '|' is not or - -- - -- This means we're better of implementing the lexer with an algorithm. - local t = {} - local current = "" - local in_str = false - local str_seek - for c in string.gmatch(raw, ".") do -- iterate through all chars - if c == ' ' and not in_str then - if string.len(current) > 0 then - table.insert(t, current) - current = "" - end - elseif c == '"' and not in_str then - in_str = true - str_seek = '"' - elseif c == "'" and not in_str then - in_str = true - str_seek = "'" - elseif c == str_seek and in_str then - in_str = false - table.insert(t, current) - current = "" - else - current = current .. c - end - end - if string.len(current) > 0 then + -- NOTE: string.gmatch is does not use regex, but a smaller pattern matcher! + -- A complete regex parser would be larger than lua itself. See + -- [Programming in Lua 20.2](https://www.lua.org/pil/20.2.html). + -- + -- Notable differences: + -- '-' is ungreedy wildcard + -- '*?' does not work + -- '|' is not or + -- + -- This means we're better of implementing the lexer with an algorithm. + local t = {} + local current = "" + local in_str = false + local str_seek + for c in string.gmatch(raw, ".") do -- iterate through all chars + if c == " " and not in_str then + if string.len(current) > 0 then table.insert(t, current) + current = "" + end + elseif c == '"' and not in_str then + in_str = true + str_seek = '"' + elseif c == "'" and not in_str then + in_str = true + str_seek = "'" + elseif c == str_seek and in_str then + in_str = false + table.insert(t, current) + current = "" + else + current = current .. c end - return t + end + if string.len(current) > 0 then + table.insert(t, current) + end + return t end --- dumps a variable into a string, so it can be printed. This is meant for @@ -50,18 +50,34 @@ end --- @param t any variable --- @return string t_dumped t dumped to string M.dump = function(t) - if type(t) == 'table' then - local s = '{ ' - for k, v in pairs(t) do - if type(k) ~= 'number' then k = '"' .. k .. '"' end - if k ~= 1 then - s = s .. ', ' - end - s = s .. '[' .. k .. '] = \'' .. M.dump(v) .. '\'' - end - return s .. ' }' - else - return tostring(t) + if type(t) == "table" then + local s = "{ " + for k, v in pairs(t) do + if type(k) ~= "number" then + k = '"' .. k .. '"' + end + if k ~= 1 then + s = s .. ", " + end + s = s .. "[" .. k .. "] = '" .. M.dump(v) .. "'" end + return s .. " }" + else + return tostring(t) + end end + +--- Try to require a module +--- @param module string module name +--- @return boolean available is the module available? +--- @return any loaded_module the loaded module if it is available +M.try_require = function(module) + local available, loaded_module = pcall(require, module) + if not available then + return available, nil + else + return available, loaded_module + end +end + return M