From 3857dec40f622374525e2b17c50a99d4d4b4b87d Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Mon, 15 May 2023 21:02:50 +0300 Subject: [PATCH] fix: selection transforms refactor: cleanup public interfaces chore: cleanup setup script --- lua/text-transform/config.lua | 24 +++++----- plugin/text-transform.lua | 85 ++++++++++++++++++++++------------- scripts/setup.sh | 26 +++++------ 3 files changed, 79 insertions(+), 56 deletions(-) diff --git a/lua/text-transform/config.lua b/lua/text-transform/config.lua index d0d53b7..62672aa 100644 --- a/lua/text-transform/config.lua +++ b/lua/text-transform/config.lua @@ -27,20 +27,20 @@ function TextTransform.setup(options) TextTransform.options = vim.tbl_deep_extend("keep", options, TextTransform.options) -- use input from current word in editor - vim.cmd("amenu TransformsSelection.&camelCase :lua ReplaceCurrentSelection(CamelCase)") - vim.cmd("amenu TransformsSelection.&snake_case :lua ReplaceCurrentSelection(SnakeCase)") - vim.cmd("amenu TransformsSelection.&PascalCase :lua ReplaceCurrentSelection(PascalCase)") - vim.cmd("amenu TransformsSelection.&kebab-case :lua ReplaceCurrentSelection(KebabCase)") - vim.cmd("amenu TransformsSelection.&dot\\.case :lua ReplaceCurrentSelection(DotCase)") - vim.cmd("amenu TransformsSelection.&Title\\ Case :lua ReplaceCurrentSelection(TitleCase)") + vim.cmd("amenu TransformsSelection.&camelCase :lua TextTransform.replace_selection(TextTransform.camel_case)") + vim.cmd("amenu TransformsSelection.&snake_case :lua TextTransform.replace_selection(TextTransform.snake_case)") + vim.cmd("amenu TransformsSelection.&PascalCase :lua TextTransform.replace_selection(TextTransform.pascal_case)") + vim.cmd("amenu TransformsSelection.&kebab-case :lua TextTransform.replace_selection(TextTransform.kebab_case)") + vim.cmd("amenu TransformsSelection.&dot\\.case :lua TextTransform.replace_selection(TextTransform.dot_case)") + vim.cmd("amenu TransformsSelection.&Title\\ Case :lua TextTransform.replace_selection(TextTransform.title_case)") -- use input from current word in editor - vim.cmd("amenu TransformsWord.&camelCase :lua ReplaceCurrentWord(CamelCase)") - vim.cmd("amenu TransformsWord.&snake_case :lua ReplaceCurrentWord(SnakeCase)") - vim.cmd("amenu TransformsWord.&PascalCase :lua ReplaceCurrentWord(PascalCase)") - vim.cmd("amenu TransformsWord.&kebab-case :lua ReplaceCurrentWord(KebabCase)") - vim.cmd("amenu TransformsWord.&dot\\.case :lua ReplaceCurrentWord(DotCase)") - vim.cmd("amenu TransformsWord.&Title\\ Case :lua ReplaceCurrentWord(TitleCase)") + vim.cmd("amenu TransformsWord.&camelCase :lua TextTransform.replace_word(TextTransform.camel_case)") + vim.cmd("amenu TransformsWord.&snake_case :lua TextTransform.replace_word(TextTransform.snake_case)") + vim.cmd("amenu TransformsWord.&PascalCase :lua TextTransform.replace_word(TextTransform.pascal_case)") + vim.cmd("amenu TransformsWord.&kebab-case :lua TextTransform.replace_word(TextTransform.kebab_case)") + vim.cmd("amenu TransformsWord.&dot\\.case :lua TextTransform.replace_word(TextTransform.dot_case)") + vim.cmd("amenu TransformsWord.&Title\\ Case :lua TextTransform.replace_word(TextTransform.title_case)") vim.keymap.set( "n", diff --git a/plugin/text-transform.lua b/plugin/text-transform.lua index 0c2b895..a2600a8 100644 --- a/plugin/text-transform.lua +++ b/plugin/text-transform.lua @@ -1,11 +1,11 @@ -- You can use this loaded variable to enable conditional parts of your plugin. -if _G.TextTransformLoaded then - return -end +-- if _G.TextTransformLoaded then +-- return +-- end _G.TextTransformLoaded = true -local function into_words(str) +function TextTransform.into_words(str) local words = {} local word = "" @@ -38,12 +38,8 @@ local function into_words(str) return words end -function IntoWords(string) - print(vim.inspect(into_words(string))) -end - -function CamelCase(string) - local words = into_words(string) +function TextTransform.camel_case(string) + local words = TextTransform.into_words(string) local camel_case = "" for i, word in ipairs(words) do if i == 1 then @@ -55,8 +51,8 @@ function CamelCase(string) return camel_case end -function SnakeCase(string) - local words = into_words(string) +function TextTransform.snake_case(string) + local words = TextTransform.into_words(string) local snake_case = "" for i, word in ipairs(words) do if i == 1 then @@ -68,8 +64,8 @@ function SnakeCase(string) return snake_case end -function PascalCase(string) - local words = into_words(string) +function TextTransform.pascal_case(string) + local words = TextTransform.into_words(string) local pascal_case = "" for _, word in ipairs(words) do pascal_case = pascal_case .. word:sub(1, 1):upper() .. word:sub(2):lower() @@ -77,8 +73,8 @@ function PascalCase(string) return pascal_case end -function KebabCase(string) - local words = into_words(string) +function TextTransform.kebab_case(string) + local words = TextTransform.into_words(string) local kebab_case = "" for i, word in ipairs(words) do if i == 1 then @@ -90,8 +86,8 @@ function KebabCase(string) return kebab_case end -function DotCase(string) - local words = into_words(string) +function TextTransform.dot_case(string) + local words = TextTransform.into_words(string) local dot_case = "" for i, word in ipairs(words) do if i == 1 then @@ -103,8 +99,8 @@ function DotCase(string) return dot_case end -function TitleCase(string) - local words = into_words(string) +function TextTransform.title_case(string) + local words = TextTransform.into_words(string) local title_case = "" for i, word in ipairs(words) do title_case = title_case .. word:sub(1, 1):upper() .. word:sub(2):lower() @@ -115,13 +111,40 @@ function TitleCase(string) return title_case end -function ReplaceCurrentSelection(transform) - local selection = vim.fn.getline("'<", "'>") - local transformed = transform(selection) - vim.fn.setline("'<", transformed) +function TextTransform.replace_selection(transform) + -- get the current visual selection, and transform the line, only replacing the selected text itself + local _, start_line, start_col = unpack(vim.fn.getpos("'<")) + local _, end_line, end_col = unpack(vim.fn.getpos("'>")) + -- print(vim.inspect(vim.fn.getpos("'<")), vim.inspect(vim.fn.getpos("'>")), + -- start_line, start_col, end_line, end_col) + local lines = vim.fn.getline(start_line, end_line) + print(vim.inspect(lines)) + + -- transform all included lines + local transformed = "" + + if #lines == 1 then + transformed = lines[1]:sub(1, start_col - 1) .. transform(lines[1]:sub(start_col, end_col)) .. lines[1]:sub(end_col + 1) + else + transformed = lines[1]:sub(1, start_col - 1) .. transform(lines[1]:sub(start_col)) .. "\n" + for i = 2, #lines - 1 do + transformed = transformed .. transform(lines[i]) .. "\n" + end + transformed = transformed .. transform(lines[#lines]:sub(1, end_col)) .. lines[#lines]:sub(end_col + 1) + end + + -- replace the lines with the transformed lines + vim.fn.setline(start_line, transformed) + for i = start_line + 1, end_line do + vim.fn.setline(i, "") + end + + -- move the cursor to the end of the transformed text + vim.fn.cursor(end_line, end_col) + end -function ReplaceCurrentWord(transform) +function TextTransform.replace_word(transform) local word = vim.fn.expand("") local transformed = transform(word) vim.cmd("normal ciw" .. transformed) @@ -131,12 +154,12 @@ local should_test = false if should_test then local map = { - ["CamelCase"] = CamelCase, - ["SnakeCase"] = SnakeCase, - ["PascalCase"] = PascalCase, - ["KebabCase"] = KebabCase, - ["DotCase"] = DotCase, - ["TitleCase"] = TitleCase, + ["CamelCase"] = TextTransform.camel_case, + ["SnakeCase"] = TextTransform.snake_case, + ["PascalCase"] = TextTransform.pascal_case, + ["KebabCase"] = TextTransform.kebab_case, + ["DotCase"] = TextTransform.dot_case, + ["TitleCase"] = TextTransform.title_case, } for k, tst in pairs(map) do diff --git a/scripts/setup.sh b/scripts/setup.sh index 2695c62..7f9a59b 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -2,25 +2,25 @@ USAGE="\033[0;37m[INFO] - usage: USERNAME=my-github-username PLUGIN_NAME=my-awesome-plugin REPOSITORY_NAME=my-awesome-plugin.nvim make setup\n\033[0m" -echo -e $USAGE +echo -e "$USAGE" if [[ -z "$USERNAME" ]]; then echo -e "\t> No USERNAME provided, what's your GitHub/GitLab username?" - read USERNAME + read -r USERNAME fi if [[ -z "$REPOSITORY_NAME" ]]; then - REPOSITORY_NAME=$(basename -s .git `git config --get remote.origin.url`) + REPOSITORY_NAME=$(basename -s .git "$(git config --get remote.origin.url)") read -rp $'\t> No REPOSITORY_NAME provided, is \033[1;32m'"$REPOSITORY_NAME"$'\033[0m good? [Y/n]\n' yn case $yn in [Yy]* );; - [Nn]* ) + [Nn]* ) echo -e "\t> Enter your repository name" - read REPOSITORY_NAME + read -r REPOSITORY_NAME ;; - * ) - echo -e $USAGE + * ) + echo -e "$USAGE" exit 1;; esac fi @@ -32,12 +32,12 @@ if [[ -z "$PLUGIN_NAME" ]]; then [Yy]* ) PLUGIN_NAME=$DEFAULT_REPOSITORY_NAME ;; - [Nn]* ) + [Nn]* ) echo -e "\t> Enter your plugin name" - read PLUGIN_NAME + read -r PLUGIN_NAME ;; - * ) - echo -e $USAGE + * ) + echo -e "$USAGE" exit 1;; esac fi @@ -45,8 +45,8 @@ fi echo -e "Username: \033[1;32m$USERNAME\033[0m\nRepository: \033[1;32m$REPOSITORY_NAME\033[0m\nPlugin: \033[1;32m$PLUGIN_NAME\033[0m\n\n\tRenaming placeholder files..." rm -rf doc -mv plugin/your-plugin-name.lua plugin/$PLUGIN_NAME.lua -mv lua/your-plugin-name lua/$PLUGIN_NAME +mv plugin/your-plugin-name.lua "plugin/$PLUGIN_NAME.lua" +mv lua/your-plugin-name "lua/$PLUGIN_NAME" mv README_TEMPLATE.md README.md echo -e "\tReplacing placeholder names..."