From 4bc66d375b8a98450a5656ee796ea65b0bc29112 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Wed, 17 May 2023 09:55:01 +0300 Subject: [PATCH] build: fix & run tests on ci --- .github/workflows/develop.yml | 37 ++++++- .github/workflows/main.yml | 51 +++++----- lua/text-transform/config.lua | 14 ++- lua/text-transform/init.lua | 161 ++++++++++++++++++++++++++++++ plugin/text-transform.lua | 182 ---------------------------------- 5 files changed, 231 insertions(+), 214 deletions(-) diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index fdb53df..de77b90 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -2,9 +2,9 @@ name: Dev Release on: push: - branches: [ develop ] + branches: [develop] pull_request: - types: [ opened, synchronize ] + types: [opened, synchronize] concurrency: group: github.head_ref @@ -42,13 +42,42 @@ jobs: - name: check docs diff run: exit $(git status --porcelain doc | wc -l | tr -d " ") + tests: + needs: + - lint + - documentation + runs-on: ubuntu-latest + timeout-minutes: 2 + strategy: + matrix: + neovim_version: ['v0.7.2', 'v0.8.3', 'v0.9.0', 'nightly'] + + steps: + - uses: actions/checkout@v3 + + - run: date +%F > todays-date + + - name: restore cache for today's nightly. + uses: actions/cache@v3 + with: + path: _neovim + key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }} + + - name: setup neovim + uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: ${{ matrix.neovim_version }} + + - name: run tests + run: make test-ci + release: name: dev-release if: ${{ github.ref == 'refs/heads/develop' }} permissions: write-all needs: - - lint - - documentation + - tests runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 43a5ef9..853e080 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -43,43 +43,42 @@ jobs: - name: check docs diff run: exit $(git status --porcelain doc | wc -l | tr -d " ") - # tests: - # needs: - # - lint - # - documentation - # runs-on: ubuntu-latest - # timeout-minutes: 2 - # strategy: - # matrix: - # neovim_version: ['v0.7.2', 'v0.8.3', 'v0.9.0', 'nightly'] + tests: + needs: + - lint + - documentation + runs-on: ubuntu-latest + timeout-minutes: 2 + strategy: + matrix: + neovim_version: ['v0.7.2', 'v0.8.3', 'v0.9.0', 'nightly'] - # steps: - # - uses: actions/checkout@v3 + steps: + - uses: actions/checkout@v3 - # - run: date +%F > todays-date + - run: date +%F > todays-date - # - name: restore cache for today's nightly. - # uses: actions/cache@v3 - # with: - # path: _neovim - # key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }} + - name: restore cache for today's nightly. + uses: actions/cache@v3 + with: + path: _neovim + key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }} - # - name: setup neovim - # uses: rhysd/action-setup-vim@v1 - # with: - # neovim: true - # version: ${{ matrix.neovim_version }} + - name: setup neovim + uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: ${{ matrix.neovim_version }} - # - name: run tests - # run: make test-ci + - name: run tests + run: make test-ci release: name: release if: ${{ github.ref == 'refs/heads/master' }} permissions: write-all needs: - - lint - - documentation + - tests runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/lua/text-transform/config.lua b/lua/text-transform/config.lua index 80bde78..3133859 100644 --- a/lua/text-transform/config.lua +++ b/lua/text-transform/config.lua @@ -26,6 +26,18 @@ function TextTransform.setup(options) TextTransform.options = vim.tbl_deep_extend("keep", options, TextTransform.options) + if vim.api.nvim_get_vvar("vim_did_enter") == 0 then + vim.defer_fn(function() + TextTransform._setup() + end, 0) + else + TextTransform._setup() + end + + return TextTransform.options +end + +function TextTransform._setup() local map = { ["&camelCase"] = "TextTransform.camel_case", ["&snake_case"] = "TextTransform.snake_case", @@ -55,8 +67,6 @@ function TextTransform.setup(options) "popup TransformsSelection", { silent = true } ) - - return TextTransform.options end return TextTransform diff --git a/lua/text-transform/init.lua b/lua/text-transform/init.lua index c9253ee..498eebb 100644 --- a/lua/text-transform/init.lua +++ b/lua/text-transform/init.lua @@ -36,6 +36,167 @@ function TextTransform.setup(opts) _G.TextTransform.config = require("text-transform.config").setup(opts) end +function TextTransform.into_words(str) + local words = {} + local word = "" + + local previous_is_upper = false + for i = 1, #str do + local char = str:sub(i, i) + -- split on uppercase letters + if char:match("%u") and not previous_is_upper then + if word ~= "" then + table.insert(words, word) + end + previous_is_upper = true + word = char + -- split on underscores, hyphens, and spaces + elseif char:match("[%_%-%s]") then + if word ~= "" then + table.insert(words, word) + previous_is_upper = false + end + word = "" + else + word = word .. char + previous_is_upper = char:match("%u") + end + end + if word ~= "" then + table.insert(words, word) + previous_is_upper = false + end + return words +end + +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 + camel_case = camel_case .. word:lower() + else + camel_case = camel_case .. word:sub(1, 1):upper() .. word:sub(2):lower() + end + end + return camel_case +end + +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 + snake_case = snake_case .. word:lower() + else + snake_case = snake_case .. "_" .. word:lower() + end + end + return snake_case +end + +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() + end + return pascal_case +end + +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 + kebab_case = kebab_case .. word:lower() + else + kebab_case = kebab_case .. "-" .. word:lower() + end + end + return kebab_case +end + +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 + dot_case = dot_case .. word:lower() + else + dot_case = dot_case .. "." .. word:lower() + end + end + return dot_case +end + +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() + if i ~= #words then + title_case = title_case .. " " + end + end + return title_case +end + +function TextTransform.const_case(string) + local words = TextTransform.into_words(string) + local const_case = "" + for i, word in ipairs(words) do + if i == 1 then + const_case = const_case .. word:upper() + else + const_case = const_case .. "_" .. word:upper() + end + end + return const_case +end + +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 TextTransform.replace_word(transform) + local word = vim.fn.expand("") + local transformed = transform(word) + vim.cmd("normal ciw" .. transformed) +end + _G.TextTransform = TextTransform return _G.TextTransform diff --git a/plugin/text-transform.lua b/plugin/text-transform.lua index 95028b4..0234a74 100644 --- a/plugin/text-transform.lua +++ b/plugin/text-transform.lua @@ -4,185 +4,3 @@ if _G.TextTransformLoaded then end _G.TextTransformLoaded = true - -function TextTransform.into_words(str) - local words = {} - local word = "" - - local previous_is_upper = false - for i = 1, #str do - local char = str:sub(i, i) - -- split on uppercase letters - if char:match("%u") and not previous_is_upper then - if word ~= "" then - table.insert(words, word) - end - previous_is_upper = true - word = char - -- split on underscores, hyphens, and spaces - elseif char:match("[%_%-%s]") then - if word ~= "" then - table.insert(words, word) - previous_is_upper = false - end - word = "" - else - word = word .. char - previous_is_upper = char:match("%u") - end - end - if word ~= "" then - table.insert(words, word) - previous_is_upper = false - end - return words -end - -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 - camel_case = camel_case .. word:lower() - else - camel_case = camel_case .. word:sub(1, 1):upper() .. word:sub(2):lower() - end - end - return camel_case -end - -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 - snake_case = snake_case .. word:lower() - else - snake_case = snake_case .. "_" .. word:lower() - end - end - return snake_case -end - -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() - end - return pascal_case -end - -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 - kebab_case = kebab_case .. word:lower() - else - kebab_case = kebab_case .. "-" .. word:lower() - end - end - return kebab_case -end - -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 - dot_case = dot_case .. word:lower() - else - dot_case = dot_case .. "." .. word:lower() - end - end - return dot_case -end - -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() - if i ~= #words then - title_case = title_case .. " " - end - end - return title_case -end - -function TextTransform.const_case(string) - local words = TextTransform.into_words(string) - local const_case = "" - for i, word in ipairs(words) do - if i == 1 then - const_case = const_case .. word:upper() - else - const_case = const_case .. "_" .. word:upper() - end - end - return const_case -end - -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 TextTransform.replace_word(transform) - local word = vim.fn.expand("") - local transformed = transform(word) - vim.cmd("normal ciw" .. transformed) -end - -local should_test = false - -if should_test then - local map = { - ["CamelCase"] = TextTransform.camel_case, - ["SnakeCase"] = TextTransform.snake_case, - ["PascalCase"] = TextTransform.pascal_case, - ["KebabCase"] = TextTransform.kebab_case, - ["DotCase"] = TextTransform.dot_case, - ["TitleCase"] = TextTransform.title_case, - ["ConstCase"] = TextTransform.title_case, - } - - for k, tst in pairs(map) do - print(k .. ": " .. "hello_world" .. " => " .. tst("hello_world")) - print(k .. ": " .. "HELLO_WORLD" .. " => " .. tst("HELLO_WORLD")) - print(k .. ": " .. "HelloWorld" .. " => " .. tst("HelloWorld")) - print(k .. ": " .. "Hello-World" .. " => " .. tst("Hello-World")) - end -end