From d877625a1efdbf078fa51c6ca2af5eb5171e0db0 Mon Sep 17 00:00:00 2001 From: "Adam P. Regasz-Rethy" Date: Sat, 19 Feb 2022 18:32:43 -0500 Subject: [PATCH] Don't indent case/when/end for Ruby Closes #13 Co-authored-by: npezza93 --- lua/nvim-treesitter/endwise.lua | 9 ++++++--- queries/ruby/endwise.scm | 3 ++- tests/endwise/ruby.rb | 11 +++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lua/nvim-treesitter/endwise.lua b/lua/nvim-treesitter/endwise.lua index 886106d..e9e20f9 100644 --- a/lua/nvim-treesitter/endwise.lua +++ b/lua/nvim-treesitter/endwise.lua @@ -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 @@ -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 @@ -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) diff --git a/queries/ruby/endwise.scm b/queries/ruby/endwise.scm index f9b6f67..65d4ea2 100644 --- a/queries/ruby/endwise.scm +++ b/queries/ruby/endwise.scm @@ -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")) diff --git a/tests/endwise/ruby.rb b/tests/endwise/ruby.rb index 73b6520..e8d7dfc 100644 --- a/tests/endwise/ruby.rb +++ b/tests/endwise/ruby.rb @@ -392,7 +392,7 @@ test "ruby, case stmt", <<~END -case foo█ +case foo -+ ++ +end END @@ -404,7 +404,7 @@ +case foo +when 0 + case bar -+ ++ + end +end END @@ -451,3 +451,10 @@ + +FOO END + +test "ruby, conditionless case stmt", <<~END +-case█ ++case ++ ++end +END