Skip to content

h3pei/ruby-block-toggle.nvim

Repository files navigation

ruby-block-toggle.nvim

Toggle Ruby blocks between do...end and {} instantly.

do...end{}

ruby-block-toggle-demo

Multi-line block

items.each { |item|
  puts item
}

items.each do |item|
  puts item
end

Single-line block

items.map do |x| x * 2 end

items.map { |x| x * 2 }

Nested blocks (smart detection)

items.each { |item|
  item.process do
    puts item
  end
}

items.each do |item|
  item.process do
    puts item
  end
end

Features

  • One command: No configuration required
  • Smart detection: Intuitively finds the right block
  • Treesitter-powered: Accurate parsing, no regex hacks

Requirements

Installation

-- lazy.nvim
{
  'h3pei/ruby-block-toggle.nvim',
  dependencies = { 'nvim-treesitter/nvim-treesitter' },
  ft = 'ruby',
  opts = {},
}
Other package managers
" vim-plug
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'h3pei/ruby-block-toggle.nvim'

Usage

:RubyBlockToggle — that's it!

Example keymap:

vim.keymap.set('n', '<leader>rb', '<cmd>RubyBlockToggle<cr>', { desc = 'Toggle Ruby block' })

Block Detection

The plugin uses an intelligent strategy to find the right block:

  1. Cursor line priority — If a block starts on the cursor line, that block is selected
    • For nested blocks on the same line, the innermost block is chosen
  2. Parent traversal — If no block starts on the cursor line, traverse upward to find the nearest parent block
  3. Nearest fallback — If neither finds a block, select the closest block by distance
items.each do |item|         # ← cursor here: toggles `each`
                             # ← cursor here: toggles `each` (parent)
  item.process do            # ← cursor here: toggles `process`
    puts item                # ← cursor here: toggles `process` (parent)
  end
end

About

Instantly toggle Ruby blocks in Neovim: do...end ⇄ {}

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published