diff --git a/lua/plugin-name/utils/logger.lua b/lua/plugin-name/utils/logger.lua new file mode 100644 index 0000000..168de3a --- /dev/null +++ b/lua/plugin-name/utils/logger.lua @@ -0,0 +1,11 @@ +local logger = {} + +function logger.log(level, message) + vim.api.nvim_notify("[" .. level .. "] CodeSnap: " .. tostring(vim.inspect(message)), vim.log.levels[level], {}) +end + +function logger.error(message) + logger.log("ERROR", message) +end + +return logger diff --git a/lua/plugin-name/utils/path.lua b/lua/plugin-name/utils/path.lua new file mode 100644 index 0000000..617bcb7 --- /dev/null +++ b/lua/plugin-name/utils/path.lua @@ -0,0 +1,9 @@ +local path_utils = {} + +function path_utils.back(path) + local parsed_path, _ = path:gsub("/[^\\/]+/?$", "") + + return parsed_path +end + +return path_utils diff --git a/lua/plugin-name/utils/table.lua b/lua/plugin-name/utils/table.lua index 965313e..732912a 100644 --- a/lua/plugin-name/utils/table.lua +++ b/lua/plugin-name/utils/table.lua @@ -1,3 +1,4 @@ +local list_utils = require("codesnap.utils.list") local table_utils = {} function table_utils.assign(t, props) @@ -14,4 +15,56 @@ function table_utils.merge(t1, t2) return t1 end +function table_utils.is_array(t) + return type(t[1]) == "number" +end + +function table_utils.typeof(value) + if type(value) == "table" then + if table_utils.is_array(value) then + return "array" + else + return "table" + end + end + + return type(value) +end + +function table_utils.serialize_array(t) + local result = list_utils.map(t, function(ele) + table_utils.serialize_json(ele) + end) + + return "[" .. result.concat(t, ",") .. "]" +end + +function table_utils.serialize_table(t) + local result = {} + + for key, value in pairs(t) do + table.insert(result, string.format('"%s":%s', key, table_utils.serialize_json(value))) + end + + return "{" .. table.concat(result, ",") .. "}" +end + +function table_utils.serialize_string(value) + return '"' .. value .. '"' +end + +function table_utils.serialize_json(t) + local complex_type_parser = { + array = table_utils.serialize_array, + table = table_utils.serialize_table, + string = table_utils.serialize_string, + } + + local parse = complex_type_parser[table_utils.typeof(t)] or function(v) + return v + end + + return parse(t) +end + return table_utils diff --git a/project.toml b/project.toml index 52505b5..6dd355a 100644 --- a/project.toml +++ b/project.toml @@ -1,6 +1,6 @@ [project] name = "plugin-name" -version = "0.0.3" +version = "0.0.4" description = "description" author = "Mist" email = "mist.zzh@gmail.com"