diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ad95f48 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,39 @@ +name: Run Tests + +on: + push: + branches: [ "*" ] + pull_request: + branches: [ main ] + +jobs: + test: + + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Install Make Dependencies + run: | + sudo apt-get update + sudo apt-get install libtool-bin autoconf automake cmake g++ pkg-config unzip gettext curl -y + + - name: Install Neovim + run: | + sudo snap install nvim --classic + + - name: Install Plenary + run: | + git clone https://github.com/nvim-lua/plenary.nvim.git + mkdir -p .local/share/nvim/lazy/ + mv plenary.nvim .local/share/nvim/lazy/ + + - name: Run Tests + env: + XDG_CONFIG_HOME: ${{ github.workspace }}/.config + XDG_DATA_HOME: ${{ github.workspace }}/.local/share + XDG_STATE_HOME: ${{ github.workspace }}/.local/state + XDG_CACHE_HOME: ${{ github.workspace }}/.cache + run: make test diff --git a/Makefile b/Makefile index fe134cd..3d8fa51 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,6 @@ TESTS_DIR=tests .PHONY: test -# TODO I want test to contain a lua-language-server pass - test_nvim: @nvim \ --headless \ @@ -13,7 +11,7 @@ test_nvim: -c "PlenaryBustedDirectory ${TESTS_DIR} { minimal_init = '${TESTS_INIT}' }" test: - -$(MAKE) test_nvim || exit 1 + $(MAKE) test_nvim test-watch: - nodemon -e lua -x "$(MAKE) test" + nodemon -e lua -x "$(MAKE) test || exit 1" diff --git a/README.md b/README.md index cd4d2bb..52f5958 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Moving slowly, showing each command { "aaronik/treewalker.nvim", opts = { - highlight = true -- default is false + highlight = true -- briefly highlight the node after jumping to it } } ``` diff --git a/lua/treewalker/init.lua b/lua/treewalker/init.lua index 286b396..437503d 100644 --- a/lua/treewalker/init.lua +++ b/lua/treewalker/init.lua @@ -8,8 +8,11 @@ local Treewalker = {} ---@alias Opts { highlight: boolean } +-- Default setup() options ---@type Opts -Treewalker.opts = {} +Treewalker.opts = { + highlight = true +} ---@param opts Opts | nil function Treewalker.setup(opts) diff --git a/lua/treewalker/nodes.lua b/lua/treewalker/nodes.lua index 12333be..f26024d 100644 --- a/lua/treewalker/nodes.lua +++ b/lua/treewalker/nodes.lua @@ -1,20 +1,10 @@ local util = require "treewalker.util" local lines= require "treewalker.lines" -local NON_TARGET_NODE_MATCHERS = { - -- "chunk", -- lua +local TARGET_TYPE_BLACKLIST = { "^.*comment.*$", } -local TARGET_DESCENDANT_TYPES = { - "body_statement", -- lua, rb - "block", -- lua - "statement_block", -- lua - - -- "then", -- helps rb, hurts lua - "do_block", -- rb -} - local HIGHLIGHT_BLACKLIST_TYPES = { "body_statement", -- lua, rb "block", -- lua @@ -27,8 +17,7 @@ local M = {} ---@param node TSNode ---@return boolean function M.is_jump_target(node) - for _, matcher in ipairs(NON_TARGET_NODE_MATCHERS) do - -- If it's a banned type + for _, matcher in ipairs(TARGET_TYPE_BLACKLIST) do if node:type():match(matcher) then return false end @@ -36,14 +25,6 @@ function M.is_jump_target(node) return true end -function M.is_descendant_jump_target(node) - return util.contains(TARGET_DESCENDANT_TYPES, node:type()) -end - -function M.is_highlight_target(node) - return util.contains(HIGHLIGHT_BLACKLIST_TYPES, node:type()) -end - ---Do the nodes have the same starting point ---@param node1 TSNode ---@param node2 TSNode @@ -113,6 +94,27 @@ function M.get_descendants(node) return descendants end +---@param node TSNode +---@return TSNode +function M.get_farthest_ancestor_with_same_srow(node) + local node_row = node:range() + local farthest_ancestor = node + local iter_row = node:range() + local iter = node:parent() + + + while iter do + iter_row = iter:range() + if iter_row ~= node_row then + break + end + farthest_ancestor = iter + iter = iter:parent() + end + + return farthest_ancestor +end + --- Take a list of nodes and unique them based on line start ---@param nodes TSNode[] ---@return TSNode[] diff --git a/lua/treewalker/util.lua b/lua/treewalker/util.lua index 5a9b25f..75110e9 100644 --- a/lua/treewalker/util.lua +++ b/lua/treewalker/util.lua @@ -98,12 +98,6 @@ M.guid = function() end) end ----@param env_key string ----@return boolean -M.has_env_var = function(env_key) - return type(os.getenv(env_key)) ~= type(nil) -end - ---reverse an array table ---@param t table M.reverse = function (t) diff --git a/plugin/init.lua b/plugin/init.lua index fc1a3f5..388e7ef 100644 --- a/plugin/init.lua +++ b/plugin/init.lua @@ -1,6 +1,5 @@ -local util = require "treewalker.util" - local function tw() + -- local util = require "treewalker.util" -- return util.R('treewalker') return require('treewalker') end diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index 1117c70..c8be46f 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -2,13 +2,6 @@ local lazypath = vim.fn.stdpath("data") .. "/lazy" vim.notify = print vim.opt.rtp:append(".") vim.opt.rtp:append(lazypath .. "/plenary.nvim") -vim.opt.rtp:append(lazypath .. "/nui.nvim") -vim.opt.rtp:append(lazypath .. "/telescope.nvim") --- vim.opt.rtp:append(lazypath .. "/nvim-nio") - --- -- Get all our normal plugins into the test env --- local suite = os.getenv("SUITE") --- vim.opt.rtp:append(suite .. "nvim") vim.opt.swapfile = false diff --git a/tests/treewalker/acceptance_spec.lua b/tests/treewalker/acceptance_spec.lua index e53310a..6dfe34a 100644 --- a/tests/treewalker/acceptance_spec.lua +++ b/tests/treewalker/acceptance_spec.lua @@ -80,15 +80,15 @@ describe("Treewalker", function() it("respects highlight config option", function() local highlight_stub = stub(ops, "highlight") - - treewalker.setup() + treewalker.setup() -- highlight defaults to true, doesn't blow up with empty setup vim.fn.cursor(23, 5) treewalker.move_out() treewalker.move_down() treewalker.move_up() treewalker.move_in() - assert.equal(0, #highlight_stub.calls) + assert.equal(4, #highlight_stub.calls) + highlight_stub = stub(ops, "highlight") treewalker.setup({ highlight = false }) vim.fn.cursor(23, 5) treewalker.move_out() @@ -97,6 +97,7 @@ describe("Treewalker", function() treewalker.move_in() assert.equal(0, #highlight_stub.calls) + highlight_stub = stub(ops, "highlight") treewalker.setup({ highlight = true }) vim.fn.cursor(23, 5) treewalker.move_out() diff --git a/tests/treewalker/util_spec.lua b/tests/treewalker/util_spec.lua index 0e569ad..163941b 100644 --- a/tests/treewalker/util_spec.lua +++ b/tests/treewalker/util_spec.lua @@ -91,19 +91,6 @@ describe("util", function() end) end) - describe("ensure_env_var", function() - it("returns true", function() - -- always set - local res = util.has_env_var("SHELL") - assert.is_true(res) - end) - - it("returns false", function() - local res = util.has_env_var("IM_SUPER_SURE_THIS_ENV_VAR_WONT_BE_SET_FR_FR") - assert.is_false(res) - end) - end) - describe("reverse", function() it("reverses an array table", function() local t = { 1, 2, 3, 4, 5 }