Skip to content

Commit

Permalink
feat: add Haskell list support
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorias committed Jan 4, 2025
1 parent 94f6df6 commit 1f803c1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lua/treesj/langs/haskell.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local lang_utils = require('treesj.langs.utils')

return {
list = {
both = {
separator = ',',
last_separator = false,
},
split = {
-- Maintain necessary whitespace: https://github.com/Wansmer/treesj/discussions/163
format_tree = function(tsj)
local len = tsj:child(1):range()[2] -- index of first character of first node
for child in tsj:iter_children() do
if not (child:is_first() or child:is_omit()) then
local indent = not child:is_last()
and (' '):rep(len + vim.fn.shiftwidth())
or (' '):rep(len)
child:update_text(indent .. child:text())
end
end
end,
},
},
}
3 changes: 2 additions & 1 deletion lua/treesj/langs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ M.configured_langs = {
'bash',
'sql',
'dart',
'elixir'
'elixir',
'haskell',
}

M.presets = {}
Expand Down
26 changes: 26 additions & 0 deletions tests/langs/haskell/join_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local tu = require('tests.utils')

local PATH = './tests/sample/index.hs'
local LANG = 'haskell'

local data_for_join = {
{
path = PATH,
mode = 'join',
lang = LANG,
desc = 'lang "%s", node "list", preset default',
cursor = { 5, 0 },
expected = { 1, 2 },
result = { 3, 4 },
},
}

local treesj = require('treesj')
local opts = {}
treesj.setup(opts)

describe('TreeSJ JOIN:', function()
for _, value in ipairs(data_for_join) do
tu._test_format(value, treesj)
end
end)
26 changes: 26 additions & 0 deletions tests/langs/haskell/split_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local tu = require('tests.utils')

local PATH = './tests/sample/index.hs'
local LANG = 'haskell'

local data_for_split = {
{
path = PATH,
mode = 'split',
lang = LANG,
desc = 'lang "%s", node "list", preset default',
cursor = { 2, 12 },
expected = { 3, 8 },
result = { 1, 6 },
},
}

local treesj = require('treesj')
local opts = {}
treesj.setup(opts)

describe('TreeSJ SPLIT:', function()
for _, value in ipairs(data_for_split) do
tu._test_format(value, treesj)
end
end)
8 changes: 8 additions & 0 deletions tests/sample/index.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- RESULT OF JOIN (node "list", preset default)
let x = [1, 2, 3]
-- RESULT OF SPLIT (node "list", preset default)
let x = [
1,
2,
3
]

0 comments on commit 1f803c1

Please sign in to comment.