Skip to content

Commit

Permalink
Don't indent case/when/end for Ruby
Browse files Browse the repository at this point in the history
Closes #13

Co-authored-by: npezza93 <pezza@hey.com>
  • Loading branch information
RRethy and npezza93 committed Feb 19, 2022
1 parent 62bbbf0 commit d877625
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 6 additions & 3 deletions lua/nvim-treesitter/endwise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ local function lacks_end(node, end_text)
return false
end

local function add_end_node(indent_node_range, end_text)
local function add_end_node(indent_node_range, end_text, shiftcount)
local crow = unpack(vim.api.nvim_win_get_cursor(0))
local indentation = strip_leading_whitespace(vim.fn.getline(indent_node_range[1] + 1))
vim.fn.append(crow, indentation..end_text)

local line = vim.fn.getline(crow)
local _, text = strip_leading_whitespace(line)
local cursor_indentation = indentation..tabstr()
local cursor_indentation = indentation..string.rep(tabstr(), shiftcount)
vim.fn.setline(crow, cursor_indentation..text)
vim.fn.cursor(crow, #cursor_indentation + 1)
end
Expand Down Expand Up @@ -139,7 +139,7 @@ local function endwise(bufnr)
if metadata.endwise_end_suffix then
end_text = end_text..text_for_range({metadata.endwise_end_suffix:range()})
end
add_end_node(indent_node_range, end_text)
add_end_node(indent_node_range, end_text, metadata.endwise_shiftcount)
return
end
end
Expand All @@ -156,10 +156,13 @@ end
-- child of the @endable captured node. This is required because the
-- endwise_end_text won't match the nodetype if it's dynamic for langauges like
-- vimscript. nil to use endwise_end_text as the node type.
-- @param endwise_shiftcount number a non-negative number of shifts to indent with,
-- defaults to 1
vim.treesitter.query.add_directive('endwise!', function(match, _, _, predicate, metadata)
metadata.endwise_end_text = predicate[2]
metadata.endwise_end_suffix = match[predicate[3]]
metadata.endwise_end_node_type = predicate[4]
metadata.endwise_shiftcount = predicate[5] or 1
end)

vim.on_key(function(key)
Expand Down
3 changes: 2 additions & 1 deletion queries/ruby/endwise.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
((if condition: (_) @cursor) @endable @indent (#endwise! "end"))
((begin "begin" @cursor . (rescue "rescue" @cursor exceptions: (_)? @cursor)? . (ensure "ensure" @cursor)?) @endable @indent (#endwise! "end"))
((unless condition: (_) @cursor) @endable @indent (#endwise! "end"))
((case value: (_) @cursor) @endable @indent (#endwise! "end"))
((case value: (_) @cursor) @endable @indent (#endwise! "end" nil "end" 0))
((case) @cursor @endable @indent (#endwise! "end" nil "end" 0))

((ERROR ("module" @indent . [(constant) (scope_resolution)] @cursor)) (#endwise! "end"))
((ERROR ("class" @indent . [(constant) (scope_resolution)] @cursor . (superclass)? @cursor)) (#endwise! "end"))
Expand Down
11 changes: 9 additions & 2 deletions tests/endwise/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@
test "ruby, case stmt", <<~END
-case foo█
+case foo
+
+
+end
END

Expand All @@ -404,7 +404,7 @@
+case foo
+when 0
+ case bar
+
+
+ end
+end
END
Expand Down Expand Up @@ -451,3 +451,10 @@
+
+FOO
END

test "ruby, conditionless case stmt", <<~END
-case█
+case
+
+end
END

0 comments on commit d877625

Please sign in to comment.