From 4db161c4c02e6794f507eba9628e3455f625ea0c Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Thu, 4 Jul 2024 12:47:27 +0200 Subject: [PATCH] utils --- lua/custom/plugins/configs/lsp.lua | 31 ++++++----- lua/custom/utils.lua | 84 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 lua/custom/utils.lua diff --git a/lua/custom/plugins/configs/lsp.lua b/lua/custom/plugins/configs/lsp.lua index 8efd086..0a15549 100644 --- a/lua/custom/plugins/configs/lsp.lua +++ b/lua/custom/plugins/configs/lsp.lua @@ -1,31 +1,30 @@ -local lspconfig = require "lspconfig" -local lspsettings = require "custom.plugins.configs.lspsettings" +local lspconfig = require 'lspconfig' +local lspsettings = require 'custom.plugins.configs.lspsettings' -- these are using the defaults local servers = { - "html", - "cssls", - "tsserver", - "clangd", - "bashls", - "cmake", - "yamlls", - "texlab", - "csharp_ls", + 'html', + 'cssls', + 'tsserver', + 'clangd', + 'bashls', + 'cmake', + 'yamlls', + 'texlab', + 'csharp_ls', } for _, lsp in ipairs(servers) do - lspconfig[lsp].setup() + lspconfig[lsp].setup {} end local server_with_settings = { -- "textlsp", - "ltex", - "rust_analyzer", - "basedpyright", + 'ltex', + 'rust_analyzer', + 'basedpyright', } for _, lsp in ipairs(server_with_settings) do lspconfig[lsp].setup(lspsettings[lsp]) end - diff --git a/lua/custom/utils.lua b/lua/custom/utils.lua new file mode 100644 index 0000000..d9edb54 --- /dev/null +++ b/lua/custom/utils.lua @@ -0,0 +1,84 @@ +local M = {} +--- this function will split a string into a table of tokens, like it would +--- be passed into the `argv` of a program that was executed from the commandline +--- +--- @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 + table.insert(t, current) + end + return t +end + +--- dumps a variable into a string, so it can be printed. This is meant for +--- debug prints +--- @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) + 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 +