diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..df43d2a --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,22 @@ +--- +on: [push, pull_request] +name: default + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + nvim-versions: ['stable', 'nightly'] + name: test + steps: + - name: checkout + uses: actions/checkout@v3 + + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: ${{ matrix.nvim-versions }} + + - name: run tests + run: ./run test diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000..e1b9d70 --- /dev/null +++ b/.luarc.json @@ -0,0 +1,4 @@ +{ + "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json", + "Lua.workspace.checkThirdParty": false +} \ No newline at end of file diff --git a/lua/dial/extras.lua b/lua/dial/extras.lua new file mode 100644 index 0000000..b2dfc90 --- /dev/null +++ b/lua/dial/extras.lua @@ -0,0 +1,24 @@ +local M = {} + +-- Private function that takes care of the filetype resolution +function M._resolve_ft() + local ft = vim.o.filetype + if require("dial.config").augends.group[ft] then + return ft + else + return "default" + end +end + +M.group_from_ft = [["..require("dial.extras")._resolve_ft().."]] + +---@param ... table Tables to be concatenated +function M.concat_lists(...) + local sink = {} + for _, l in ipairs{...} do + vim.list_extend(sink, l) + end + return sink +end + +return M diff --git a/lua/dial/handle.lua b/lua/dial/handle.lua index 18bd048..09fc11c 100644 --- a/lua/dial/handle.lua +++ b/lua/dial/handle.lua @@ -215,7 +215,7 @@ end ---@param selected_range {from: integer, to?: integer} ---@param direction direction ---@param tier integer ----@return {result?: string} +---@return {line?: string} function Handler:operate_visual(line, selected_range, direction, tier) if self.active_augend == nil then return {} diff --git a/lua/dial/map.lua b/lua/dial/map.lua index 667e47a..85e9ae9 100644 --- a/lua/dial/map.lua +++ b/lua/dial/map.lua @@ -1,65 +1,68 @@ local M = {} -local command = require "dial.command" -local util = require "dial.util" - ----Sandwich input string between and . ----@param body string -local function cmdcr(body) - local cmd_sequences = "" - local cr_sequences = "" - return cmd_sequences .. body .. cr_sequences -end - ----Output command sequence which provides dial operation. ---@param direction direction ---@param mode mode ----@param group_name? string +---@param group_name? string|fun():string +---@return fun():nil local function _cmd_sequence(direction, mode, group_name) - local select - if group_name == nil then - select = cmdcr([[lua require"dial.command".select_augend_]] .. mode .. "()") + return function() + local name + if type(group_name) == "function" then + name = group_name() else - select = cmdcr([[lua require"dial.command".select_augend_]] .. mode .. [[("]] .. group_name .. [[")]]) + name = group_name + end + + local selector = require("dial.command")["select_augend_" .. mode] + selector(name) + + vim.go.opfunc = ("dial#operator#%s_%s"):format(direction, mode) + + do + local motion = '' + if mode == 'normal' then motion = 'l' end + vim.cmd.normal('g@'..motion) end - -- command.select_augend_normal(vim.v.count, group_name) - local setopfunc = cmdcr([[let &opfunc="dial#operator#]] .. direction .. "_" .. mode .. [["]]) - local textobj = util.if_expr(mode == "normal", cmdcr [[lua require("dial.command").textobj()]], "") - return select .. setopfunc .. "g@" .. textobj + + if mode == "normal" then + require("dial.command").textobj() + end + + end end ----@param group_name? string ----@return string +---@param group_name? string|fun():string +---@return fun():nil function M.inc_normal(group_name) return _cmd_sequence("increment", "normal", group_name) end ----@param group_name? string ----@return string +---@param group_name? string|fun():string +---@return fun():nil function M.dec_normal(group_name) return _cmd_sequence("decrement", "normal", group_name) end ----@param group_name? string ----@return string +---@param group_name? string|fun():string +---@return fun():nil function M.inc_visual(group_name) return _cmd_sequence("increment", "visual", group_name) end ----@param group_name? string ----@return string +---@param group_name? string|fun():string +---@return fun():nil function M.dec_visual(group_name) return _cmd_sequence("decrement", "visual", group_name) end ----@param group_name? string ----@return string +---@param group_name? string|fun():string +---@return fun():nil function M.inc_gvisual(group_name) return _cmd_sequence("increment", "gvisual", group_name) end ----@param group_name? string ----@return string +---@param group_name? string|fun():string +---@return fun():nil function M.dec_gvisual(group_name) return _cmd_sequence("decrement", "gvisual", group_name) end diff --git a/plugin/dial.vim b/plugin/dial.vim index e6df5b2..39487bb 100644 --- a/plugin/dial.vim +++ b/plugin/dial.vim @@ -4,12 +4,12 @@ let s:save_cpo = &cpo " save user coptions set cpo&vim " reset them to defaults lua << EOF -vim.api.nvim_set_keymap("n", "(dial-increment)", require("dial.map").inc_normal(), {noremap = true}) -vim.api.nvim_set_keymap("n", "(dial-decrement)", require("dial.map").dec_normal(), {noremap = true}) -vim.api.nvim_set_keymap("v", "(dial-increment)", require("dial.map").inc_visual() .. "gv", {noremap = true}) -vim.api.nvim_set_keymap("v", "(dial-decrement)", require("dial.map").dec_visual() .. "gv", {noremap = true}) -vim.api.nvim_set_keymap("v", "g(dial-increment)", require("dial.map").inc_gvisual() .. "gv", {noremap = true}) -vim.api.nvim_set_keymap("v", "g(dial-decrement)", require("dial.map").dec_gvisual() .. "gv", {noremap = true}) +-- vim.api.nvim_set_keymap("n", "(dial-increment)", require("dial.map").inc_normal(), {noremap = true}) +-- vim.api.nvim_set_keymap("n", "(dial-decrement)", require("dial.map").dec_normal(), {noremap = true}) +-- vim.api.nvim_set_keymap("v", "(dial-increment)", require("dial.map").inc_visual() .. "gv", {noremap = true}) +-- vim.api.nvim_set_keymap("v", "(dial-decrement)", require("dial.map").dec_visual() .. "gv", {noremap = true}) +-- vim.api.nvim_set_keymap("v", "g(dial-increment)", require("dial.map").inc_gvisual() .. "gv", {noremap = true}) +-- vim.api.nvim_set_keymap("v", "g(dial-decrement)", require("dial.map").dec_gvisual() .. "gv", {noremap = true}) EOF command! -range -nargs=? DialIncrement lua require"dial.command".command("increment", {from = , to = }, {}) diff --git a/run b/run new file mode 100755 index 0000000..456a8ca --- /dev/null +++ b/run @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +TESTS_INIT=tests/minimal_init.lua +TESTS_DIR=tests/ + + +function test { + + nvim \ + --headless \ + --noplugin \ + -u ${TESTS_INIT} \ + -c "PlenaryBustedDirectory ${TESTS_DIR} { minimal_init = '${TESTS_INIT}' }" +} + + +function help { + echo "$0 " + echo "Tasks:" + compgen -A function | cat -n +} + +TIMEFORMAT="Task completed in %3lR" +time ${@:-default} diff --git a/lua/spec/dial/augend/case_spec.lua b/tests/dial/augend/case_spec.lua similarity index 100% rename from lua/spec/dial/augend/case_spec.lua rename to tests/dial/augend/case_spec.lua diff --git a/lua/spec/dial/augend/constant_spec.lua b/tests/dial/augend/constant_spec.lua similarity index 100% rename from lua/spec/dial/augend/constant_spec.lua rename to tests/dial/augend/constant_spec.lua diff --git a/lua/spec/dial/augend/date_spec.lua b/tests/dial/augend/date_spec.lua similarity index 100% rename from lua/spec/dial/augend/date_spec.lua rename to tests/dial/augend/date_spec.lua diff --git a/lua/spec/dial/augend/integer_spec.lua b/tests/dial/augend/integer_spec.lua similarity index 100% rename from lua/spec/dial/augend/integer_spec.lua rename to tests/dial/augend/integer_spec.lua diff --git a/lua/spec/dial/augend/misc_spec.lua b/tests/dial/augend/misc_spec.lua similarity index 100% rename from lua/spec/dial/augend/misc_spec.lua rename to tests/dial/augend/misc_spec.lua diff --git a/lua/spec/dial/augend/paren_spec.lua b/tests/dial/augend/paren_spec.lua similarity index 100% rename from lua/spec/dial/augend/paren_spec.lua rename to tests/dial/augend/paren_spec.lua diff --git a/tests/dial/extas_spec.lua b/tests/dial/extas_spec.lua new file mode 100644 index 0000000..be946f1 --- /dev/null +++ b/tests/dial/extas_spec.lua @@ -0,0 +1,30 @@ +local et = require("dial.extras") + +describe("Test functions in module extras", function() + + + describe("Test concat_lists", function() + it("concat_lists works with more than 2 lists", function() + assert.are.same(et.concat_lists({ 1 }, { 2 }, { 3, 4 }), { 1, 2, 3, 4 }) + end) + end) + + describe("Test _resolve_ft", function() + it("If there is no group for the current ft, resolve 'default'", function() + vim.o.filetype = "python" + + require("dial.config").augends:register_group {} + assert.are.same(et._resolve_ft(), "default") + end) + + it("if there is then return ft", function() + + require("dial.config").augends:register_group { python = {} } + + require("dial.extras") + vim.o.filetype = "python" + assert.are.same(et._resolve_ft(), "python") + + end) + end) +end) diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua new file mode 100644 index 0000000..0841a82 --- /dev/null +++ b/tests/minimal_init.lua @@ -0,0 +1,11 @@ +local plenary_dir = os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim" +local is_not_a_directory = vim.fn.isdirectory(plenary_dir) == 0 +if is_not_a_directory then + vim.fn.system({"git", "clone", "https://github.com/nvim-lua/plenary.nvim", plenary_dir}) +end + +vim.opt.rtp:append(".") +vim.opt.rtp:append(plenary_dir) + +vim.cmd("runtime plugin/plenary.vim") +require("plenary.busted")