From 65f9ac4263bbc19f19822a8ee1a2ed81e4da09ec Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Sun, 13 Oct 2024 12:18:04 +0100 Subject: [PATCH] Add scheme treesitter queries and tests --- lua/nvim-paredit/defaults.lua | 2 +- queries/scheme/paredit/forms.scm | 6 ++ tests/config.lua | 2 +- .../scheme/element_raise_spec.lua | 58 ++++++++++++ tests/nvim-paredit/scheme/form_raise_spec.lua | 61 ++++++++++++ tests/nvim-paredit/scheme/motion_spec.lua | 49 ++++++++++ tests/nvim-paredit/scheme/slurp_spec.lua | 93 +++++++++++++++++++ 7 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 queries/scheme/paredit/forms.scm create mode 100644 tests/nvim-paredit/scheme/element_raise_spec.lua create mode 100644 tests/nvim-paredit/scheme/form_raise_spec.lua create mode 100644 tests/nvim-paredit/scheme/motion_spec.lua create mode 100644 tests/nvim-paredit/scheme/slurp_spec.lua diff --git a/lua/nvim-paredit/defaults.lua b/lua/nvim-paredit/defaults.lua index 8db835e..b6c2380 100644 --- a/lua/nvim-paredit/defaults.lua +++ b/lua/nvim-paredit/defaults.lua @@ -119,7 +119,7 @@ M.defaults = { enabled = false, indentor = require("nvim-paredit.indentation.native").indentor, }, - filetypes = { "clojure", "fennel" }, + filetypes = { "clojure", "fennel", "scheme" }, languages = { clojure = { whitespace_chars = { " ", "," }, diff --git a/queries/scheme/paredit/forms.scm b/queries/scheme/paredit/forms.scm new file mode 100644 index 0000000..8d9ea5d --- /dev/null +++ b/queries/scheme/paredit/forms.scm @@ -0,0 +1,6 @@ +(list) @form +(vector) @form +(byte_vector) @form + +(comment) @comment +(block_comment) @comment diff --git a/tests/config.lua b/tests/config.lua index a4c60d3..37c0dd1 100644 --- a/tests/config.lua +++ b/tests/config.lua @@ -11,6 +11,6 @@ vim.bo.swapfile = false require("nvim-treesitter.configs").setup({ parser_install_dir = vim.fn.getcwd() .. "/.build/parsers", - ensure_installed = { "clojure", "fennel" }, + ensure_installed = { "clojure", "fennel", "scheme" }, sync_install = true, }) diff --git a/tests/nvim-paredit/scheme/element_raise_spec.lua b/tests/nvim-paredit/scheme/element_raise_spec.lua new file mode 100644 index 0000000..359b73b --- /dev/null +++ b/tests/nvim-paredit/scheme/element_raise_spec.lua @@ -0,0 +1,58 @@ +local paredit = require("nvim-paredit.api") + +local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer +local expect = require("tests.nvim-paredit.utils").expect + +describe("element raising", function() + vim.api.nvim_set_option_value("filetype", "scheme", { + buf = 0, + }) + + it("should raise the element", function() + prepare_buffer({ + content = "(a (b))", + cursor = { 1, 4 }, + }) + paredit.raise_element() + expect({ + content = "(a b)", + cursor = { 1, 3 }, + }) + end) + + it("should raise form elements when cursor is placed on edge", function() + prepare_buffer({ + content = "(a (b))", + cursor = { 1, 3 }, + }) + + paredit.raise_element() + expect({ + content = "(b)", + cursor = { 1, 0 }, + }) + + prepare_buffer({ + content = "(a #(b))", + cursor = { 1, 3 }, + }) + + paredit.raise_element() + expect({ + content = "#(b)", + cursor = { 1, 0 }, + }) + end) + + it("should do nothing if it is a direct child of the document root", function() + prepare_buffer({ + content = { "a", "b" }, + cursor = { 1, 0 }, + }) + paredit.raise_form() + expect({ + content = { "a", "b" }, + cursor = { 1, 0 }, + }) + end) +end) diff --git a/tests/nvim-paredit/scheme/form_raise_spec.lua b/tests/nvim-paredit/scheme/form_raise_spec.lua new file mode 100644 index 0000000..8c80cc8 --- /dev/null +++ b/tests/nvim-paredit/scheme/form_raise_spec.lua @@ -0,0 +1,61 @@ +local paredit = require("nvim-paredit.api") + +local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer +local expect_all = require("tests.nvim-paredit.utils").expect_all +local expect = require("tests.nvim-paredit.utils").expect + +describe("form raising", function() + vim.api.nvim_set_option_value("filetype", "scheme", { + buf = 0, + }) + + it("should raise the form", function() + expect_all(paredit.raise_form, { + { + "list", + before_content = "(a (b c))", + before_cursor = { 1, 6 }, + after_content = "(b c)", + after_cursor = { 1, 0 }, + }, + { + "list with reader", + before_content = "(a #(b c))", + before_cursor = { 1, 5 }, + after_content = "#(b c)", + after_cursor = { 1, 0 }, + }, + { + "list with byte vector", + before_content = "(a #vu8(1 2))", + before_cursor = { 1, 3 }, + after_content = "#vu8(1 2)", + after_cursor = { 1, 0 }, + }, + }) + end) + + it("should do nothing if it is a direct child of the document root", function() + prepare_buffer({ + content = { "(a)", "b" }, + cursor = { 1, 1 }, + }) + paredit.raise_form() + expect({ + content = { "(a)", "b" }, + cursor = { 1, 1 }, + }) + end) + + it("should do nothing if it is outside of a form", function() + prepare_buffer({ + content = { "a", "b" }, + cursor = { 1, 0 }, + }) + paredit.raise_form() + expect({ + content = { "a", "b" }, + cursor = { 1, 0 }, + }) + end) +end) diff --git a/tests/nvim-paredit/scheme/motion_spec.lua b/tests/nvim-paredit/scheme/motion_spec.lua new file mode 100644 index 0000000..c87ac9e --- /dev/null +++ b/tests/nvim-paredit/scheme/motion_spec.lua @@ -0,0 +1,49 @@ +local paredit = require("nvim-paredit.api") + +local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer +local expect = require("tests.nvim-paredit.utils").expect + +describe("motions", function() + vim.api.nvim_set_option_value("filetype", "scheme", { + buf = 0, + }) + + it("should jump to next element in form (tail)", function() + prepare_buffer({ + content = "(aa (bb) #(cc))", + cursor = { 1, 2 }, + }) + + paredit.move_to_next_element_tail() + expect({ + cursor = { 1, 7 }, + }) + + paredit.move_to_next_element_tail() + expect({ + cursor = { 1, 13 }, + }) + + paredit.move_to_next_element_tail() + expect({ + cursor = { 1, 13 }, + }) + end) + + it("should jump to next element in form (head)", function() + prepare_buffer({ + content = "(aa (bb) #(cc))", + cursor = { 1, 2 }, + }) + + paredit.move_to_next_element_head() + expect({ + cursor = { 1, 4 }, + }) + + paredit.move_to_next_element_head() + expect({ + cursor = { 1, 9 }, + }) + end) +end) diff --git a/tests/nvim-paredit/scheme/slurp_spec.lua b/tests/nvim-paredit/scheme/slurp_spec.lua new file mode 100644 index 0000000..2ed7d20 --- /dev/null +++ b/tests/nvim-paredit/scheme/slurp_spec.lua @@ -0,0 +1,93 @@ +local paredit = require("nvim-paredit.api") + +local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer +local expect_all = require("tests.nvim-paredit.utils").expect_all +local expect = require("tests.nvim-paredit.utils").expect + +describe("slurping backwards", function() + vim.api.nvim_set_option_value("filetype", "scheme", { + buf = 0, + }) + + it("should slurp different form types", function() + expect_all(paredit.slurp_backwards, { + { + "list", + before_content = "a ()", + before_cursor = { 1, 3 }, + after_content = "(a )", + after_cursor = { 1, 3 }, + }, + { + "vector", + before_content = "a #()", + before_cursor = { 1, 2 }, + after_content = "#(a )", + after_cursor = { 1, 2 }, + }, + { + "byte vector", + before_content = "a #vu8()", + before_cursor = { 1, 4 }, + after_content = "#vu8(a )", + after_cursor = { 1, 4 }, + }, + }) + end) + + it("should skip comments", function() + prepare_buffer({ + content = { "a", ";; comment", "()" }, + cursor = { 3, 0 }, + }) + paredit.slurp_backwards() + expect({ + content = { "(a", ";; comment", ")" }, + cursor = { 3, 0 }, + }) + end) +end) + +describe("slurping forward", function() + vim.api.nvim_set_option_value("filetype", "scheme", { + buf = 0, + }) + + it("should slurp forward different form types", function() + expect_all(paredit.slurp_forwards, { + { + "list", + before_content = "() a", + before_cursor = { 1, 1 }, + after_content = "( a)", + after_cursor = { 1, 1 }, + }, + { + "vector", + before_content = "#() a", + before_cursor = { 1, 1 }, + after_content = "#( a)", + after_cursor = { 1, 1 }, + }, + { + "byte vector", + before_content = "#vu8() a", + before_cursor = { 1, 2 }, + after_content = "#vu8( a)", + after_cursor = { 1, 2 }, + }, + }) + end) + + it("should skip comments", function() + prepare_buffer({ + content = { "()", ";; comment", "a" }, + cursor = { 1, 1 }, + }) + paredit.slurp_forwards() + expect({ + content = { "(", ";; comment", "a)" }, + cursor = { 1, 0 }, + }) + end) +end)