From a1a09192f63ccf4db565c1758da0e4a3a040ab90 Mon Sep 17 00:00:00 2001 From: Dorra <640993+amdorra@users.noreply.github.com> Date: Thu, 15 Aug 2024 08:22:58 +0200 Subject: [PATCH] feat: Template function for camel case file names (#52) --- README.md | 2 ++ lua/template/init.lua | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8904613..6f5b119 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,8 @@ end} - `{{_upper_file_}}` all-caps file name +- `{{_camel_case_file_}}` converts snake_case file name to CamelCase + - `{{_lua:vim.fn.expand(%:.:r)_}}` set by lua script ### Define your template diff --git a/lua/template/init.lua b/lua/template/init.lua index 0feca87..d594c97 100644 --- a/lua/template/init.lua +++ b/lua/template/init.lua @@ -47,6 +47,7 @@ local function expand_expr() '{{_upper_file_}}', '{{_lua:(.-)_}}', '{{_tomorrow_}}', + '{{_cammel_file_}}', } local function expand_recursively(line, expr, replacement) @@ -98,6 +99,23 @@ local function expand_expr() local next = os.date('%c', os.time(t)) return expand_recursively(line, expr[9], next) end, + [expr[10]] = function(line) + local file_name = fn.expand('%:t:r') + local camel_case_file_name = '' + local up_next = true + for i = 1, #file_name do + local char = file_name:sub(i,i) + if char == '_' then + up_next = true + elseif up_next then + camel_case_file_name = camel_case_file_name..string.upper(char) + up_next = false + else + camel_case_file_name = camel_case_file_name..char + end + end + return expand_recursively(line, expr[10], camel_case_file_name) + end, } return function(line) @@ -107,7 +125,7 @@ local function expand_expr() if line:find(expr[2]) then cursor = true end - + for i, item in ipairs(expr) do line = expr_map[item](line) end