Skip to content

Commit

Permalink
Ruby unless (#144)
Browse files Browse the repository at this point in the history
* feat: Ruby unless_modifier

* feat: Ruby unless/else join into ternary
  • Loading branch information
iovis committed Jan 17, 2024
1 parent 1d6e89f commit 936621b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lua/treesj/langs/ruby.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ return {
end,
},
}),
unless_modifier = lang_utils.set_default_preset({
join = { enable = false },
split = {
omit = { lang_utils.helpers.if_second },
format_tree = function(tsj)
local if_node = tsj:child('unless')
local end_ = tsj:create_child({ text = 'end', type = 'end' })
tsj:update_children({ if_node, if_node:next(), tsj:child(1), end_ })
end,
},
}),
['if'] = lang_utils.set_default_preset({
split = { enable = false },
join = {
Expand Down Expand Up @@ -92,6 +103,40 @@ return {
end,
},
}),
['unless'] = lang_utils.set_default_preset({
split = { enable = false },
join = {
enable = function(tsn)
local check = {
tsn:field('consequence')[1],
tsn:field('alternative')[1],
}

return utils.every(check, function(el)
return not el and true or el:named_child_count() == 1
end)
end,
space_in_brackets = true,
format_tree = function(tsj)
if tsj:child('else') then
local unless_ = tsj:child('unless')
local else_ = tsj:child('else')
if else_:has_to_format() then
local text = else_:child(1):text()
else_:child(1):update_text(text:gsub('^else', '?'))
else
local text = else_:text()
else_:update_text(text:gsub('^else', '?'))
end
unless_:update_text(': ')
tsj:update_children({ tsj:child(2), else_, unless_, tsj:child('then') })
else
local unless_node = tsj:child('unless')
tsj:update_children({ tsj:child('then'), unless_node, unless_node:next() })
end
end,
},
}),
conditional = lang_utils.set_default_preset({
join = { enable = false },
split = {
Expand Down
18 changes: 18 additions & 0 deletions tests/langs/ruby/join_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ local data_for_join = {
expected = { 108, 109 },
result = { 113, 114 },
},
{
path = PATH,
mode = 'join',
lang = LANG,
desc = 'lang "%s", node "unless to unless_modifier", preset default',
cursor = { 123, 1 },
expected = { 119, 120 },
result = { 122, 123 },
},
{
path = PATH,
mode = 'join',
lang = LANG,
desc = 'lang "%s", node "unless to conditional", preset default',
cursor = { 131, 1 },
expected = { 127, 128 },
result = { 130, 131 },
},
}

local treesj = require('treesj')
Expand Down
9 changes: 9 additions & 0 deletions tests/langs/ruby/split_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ local data_for_split = {
expected = { 113, 116 },
result = { 108, 111 },
},
{
path = PATH,
mode = 'split',
lang = LANG,
desc = 'lang "%s", node "unless_modifier to unless", preset default',
cursor = { 120, 5 },
expected = { 122, 125 },
result = { 119, 122 },
},
}

local treesj = require('treesj')
Expand Down
18 changes: 18 additions & 0 deletions tests/sample/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,21 @@ def config
method
end
end

# RESULT OF JOIN (node "unless_modifier to unless", preset default)
return false unless true

# RESULT OF SPLIT (node "unless to unless_modifier", preset default)
unless true
return false
end

# RESULT OF JOIN (node "conditional to unless", preset default)
cond ? do_that('cond') : do_this('not nond')

# RESULT OF SPLIT (node "unless to conditional", preset default)
unless cond
do_this('not nond')
else
do_that('cond')
end

0 comments on commit 936621b

Please sign in to comment.