repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/in_and_out.lua

Raw
File outline
-- Stolen from https://github.com/ysmb-wtsg/in-and-out.nvim
local map = require 'std.map'

local function escape_lua_pattern( s )
  local matches = {
    ['^'] = '%^',
    ['$'] = '%$',
    ['('] = '%(',
    [')'] = '%)',
    ['%'] = '%%',
    ['.'] = '%.',
    ['['] = '%[',
    [']'] = '%]',
    ['*'] = '%*',
    ['+'] = '%+',
    ['-'] = '%-',
    ['?'] = '%?',
  }
  return s:gsub( '.', matches )
end

local targets = { '"', "'", '(', ')', '{', '}', '[', ']', '`' }

local in_and_out = function ()
  local current_row, current_col = unpack( vim.api.nvim_win_get_cursor( 0 ) )
  local lines_in_buf = vim.api.nvim_buf_get_lines( 0, current_row - 1, -1, false )

  local target_row = 0
  local target_col = nil
  for i, line in ipairs( lines_in_buf ) do
    for _, char in ipairs( targets ) do
      local found_col = string.find( line, escape_lua_pattern( char ), current_col + 1 )
      if found_col and (not target_col or found_col < target_col) then
        -- If char is a multibyte character, we need to take into account the extra bytes.
        target_col = found_col + vim.fn.strlen( char ) - 1
      end
    end
    if target_col then
      target_row = current_row + i - 1
      break
    end
    current_col = 0
  end
  if target_col then
    vim.api.nvim_win_set_cursor( 0, { target_row, target_col } )
  end
end

map.insert {
  description = 'Moves in or out of balanced tokens',
  category = 'editing',
  keys = '<C-CR>',
  command = in_and_out,
}