Skip to content

Commit

Permalink
Add heredoc support for Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
RRethy committed Feb 20, 2022
1 parent de216e5 commit f85e418
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lua/nvim-treesitter/endwise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ end

local function lacks_end(node, end_text)
local end_node = node:child(node:child_count() - 1)
if end_node == nil then
return true
end
if end_node:type() ~= end_text then
return false
end
Expand Down Expand Up @@ -150,7 +153,12 @@ local function endwise(bufnr)
if not endable_node or lacks_end(endable_node, end_node_type) then
local end_text = metadata.endwise_end_text
if metadata.endwise_end_suffix then
end_text = end_text..text_for_range({metadata.endwise_end_suffix:range()})
local suffix = text_for_range({metadata.endwise_end_suffix:range()})
local s, e = vim.regex(metadata.endwise_end_suffix_pattern):match_str(suffix)
if s then
suffix = string.sub(suffix, s+1, e)
end
end_text = end_text..suffix
end
local endable_node_range = endable_node and {endable_node:range()} or nil
add_end_node(indent_node_range, endable_node_range, end_text, metadata.endwise_shiftcount)
Expand All @@ -172,11 +180,14 @@ end
-- 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
-- @param endwise_end_suffix_pattern string regex pattern to apply onto
-- endwise_end_suffix, defaults to matching the whole string
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
metadata.endwise_end_suffix_pattern = predicate[6] or '^.*$'
end)

vim.on_key(function(key)
Expand Down
1 change: 1 addition & 0 deletions queries/ruby/endwise.scm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
((unless condition: (_) @cursor) @endable @indent (#endwise! "end"))
((case value: (_) @cursor) @endable @indent (#endwise! "end" nil "end" 0))
((case) @cursor @endable @indent (#endwise! "end" nil "end" 0))
((heredoc_beginning) @cursor @endable @indent (#endwise! "" @cursor "heredoc_end" 0 "<<\\~\\?\\zs.*"))

((ERROR ("module" @indent . [(constant) (scope_resolution)] @cursor)) (#endwise! "end"))
((ERROR ("class" @indent . [(constant) (scope_resolution)] @cursor . (superclass)? @cursor)) (#endwise! "end"))
Expand Down
4 changes: 2 additions & 2 deletions tests/endwise/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@
END

test "ruby, this will fail", <<~END
-test "foo" <<~FOO█
+test "foo" <<~FOO
-test "foo", <<~FOO█
+test "foo", <<~FOO
+
+FOO
END

0 comments on commit f85e418

Please sign in to comment.