aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/functional/string.lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-07-08 18:34:38 +0200
committerGitHub <noreply@github.com>2022-07-08 18:34:38 +0200
commit976aa4fbee8a070f362cab6f6ec84e9251a90cf9 (patch)
tree5e8d9c9c59444a25c7801b8f39763c4ba6e1f76d /lua/mason-core/functional/string.lua
parentfeat: add gotests, gomodifytags, impl (#28) (diff)
downloadmason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.gz
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.bz2
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.lz
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.xz
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.zst
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.zip
refactor: add mason-schemas and mason-core modules (#29)
* refactor: add mason-schemas and move generated filetype map to mason-lspconfig * refactor: add mason-core module
Diffstat (limited to 'lua/mason-core/functional/string.lua')
-rw-r--r--lua/mason-core/functional/string.lua74
1 files changed, 74 insertions, 0 deletions
diff --git a/lua/mason-core/functional/string.lua b/lua/mason-core/functional/string.lua
new file mode 100644
index 00000000..7726c8e1
--- /dev/null
+++ b/lua/mason-core/functional/string.lua
@@ -0,0 +1,74 @@
+local fun = require "mason-core.functional.function"
+
+local _ = {}
+
+---@param pattern string
+---@param str string
+_.matches = fun.curryN(function(pattern, str)
+ return str:match(pattern) ~= nil
+end, 2)
+
+---@param template string
+---@param str string
+_.format = fun.curryN(function(template, str)
+ return template:format(str)
+end, 2)
+
+---@param sep string
+---@param str string
+_.split = fun.curryN(function(sep, str)
+ return vim.split(str, sep)
+end, 2)
+
+---@param pattern string
+---@param repl string|function|table
+---@param str string
+_.gsub = fun.curryN(function(pattern, repl, str)
+ return string.gsub(str, pattern, repl)
+end, 3)
+
+_.trim = fun.curryN(function(str)
+ return vim.trim(str)
+end, 1)
+
+---https://github.com/nvim-lua/nvim-package-specification/blob/93475e47545b579fd20b6c5ce13c4163e7956046/lua/packspec/schema.lua#L8-L37
+---@param str string
+---@return string
+_.dedent = fun.curryN(function(str)
+ local lines = {}
+ local indent = nil
+
+ for line in str:gmatch "[^\n]*\n?" do
+ if indent == nil then
+ if not line:match "^%s*$" then
+ -- save pattern for indentation from the first non-empty line
+ indent, line = line:match "^(%s*)(.*)$"
+ indent = "^" .. indent .. "(.*)$"
+ table.insert(lines, line)
+ end
+ else
+ if line:match "^%s*$" then
+ -- replace empty lines with a single newline character.
+ -- empty lines are handled separately to allow the
+ -- closing "]]" to be one indentation level lower.
+ table.insert(lines, "\n")
+ else
+ -- strip indentation on non-empty lines
+ line = assert(line:match(indent), "inconsistent indentation")
+ table.insert(lines, line)
+ end
+ end
+ end
+
+ lines = table.concat(lines)
+ -- trim trailing whitespace
+ return lines:match "^(.-)%s*$"
+end, 1)
+
+---@param prefix string
+---@str string
+_.starts_with = fun.curryN(function(prefix, str)
+ return vim.startswith(str, prefix)
+end, 2)
+
+return _