Skip to content

Commit

Permalink
Add treesitter queries and tests for commonlisp
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Oct 13, 2024
1 parent 7db7634 commit fa99240
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 5 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ paredit.setup({
-- for new filetypes.
--
-- Defaults to all supported filetypes.
filetypes = { "clojure", "fennel", "scheme" },
filetypes = { "clojure", "fennel", "scheme", "lisp" },

-- This is some language specific configuration. Right now this is just used for
-- setting character lists that are considered whitespace.
Expand Down Expand Up @@ -288,8 +288,13 @@ understand the AST.
Right now `nvim-paredit` has built-in support for:

- `clojure`
- `fennel`
- `scheme`
- `fennel` *
- `scheme` *
- `commonlisp` *

> [!NOTE]
>
> Items marked with `*` only have partial test coverage. Not all language syntax is guaranteed to work.
Take a look at the [Language Queries Spec](./docs/language-queries.md) if you are wanting to add support for languages
not built-in to nvim-paredit, or you want to develop on the existing queries.
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-paredit/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ M.defaults = {
enabled = false,
indentor = require("nvim-paredit.indentation.native").indentor,
},
filetypes = { "clojure", "fennel", "scheme" },
filetypes = { "clojure", "fennel", "scheme", "lisp" },
languages = {
clojure = {
whitespace_chars = { " ", "," },
Expand Down
5 changes: 5 additions & 0 deletions queries/commonlisp/paredit/forms.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(list_lit
open: "(") @form

(loop_macro) @form
(defun) @form
2 changes: 1 addition & 1 deletion tests/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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", "scheme" },
ensure_installed = { "clojure", "fennel", "scheme", "commonlisp" },
sync_install = true,
})
58 changes: 58 additions & 0 deletions tests/nvim-paredit/commonlisp/element_raise_spec.lua
Original file line number Diff line number Diff line change
@@ -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", "lisp", {
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)
54 changes: 54 additions & 0 deletions tests/nvim-paredit/commonlisp/form_raise_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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", "lisp", {
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 },
},
})
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)
49 changes: 49 additions & 0 deletions tests/nvim-paredit/commonlisp/motion_spec.lua
Original file line number Diff line number Diff line change
@@ -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", "lisp", {
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)
86 changes: 86 additions & 0 deletions tests/nvim-paredit/commonlisp/slurp_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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", "lisp", {
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 },
},
})
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 },
},
{
"lambda",
before_content = "(lambda (a b)) a",
before_cursor = { 1, 2 },
after_content = "(lambda (a b) 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)

0 comments on commit fa99240

Please sign in to comment.