Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/textobjects.lua

Raw
local puk = require 'std.puk'
local map = require 'std.map'

local textobject_select_keymaps = {
  ['af'] = { query = '@call.outer', description = 'outer function call' },
  ['if'] = { query = '@call.inner', description = 'inner function call' },

  ['am'] = { query = '@function.outer', description = 'outer function definition' },
  ['im'] = { query = '@function.inner', description = 'inner function definition' },

  ['ai'] = { query = '@conditional.outer', description = 'outer conditional' },
  ['ii'] = { query = '@conditional.inner', description = 'inner conditional' },

  ['aa'] = { query = '@parameter.outer', description = 'outer parameter' },
  ['ia'] = { query = '@parameter.inner', description = 'inner parameter' },

  ['al'] = { query = '@loop.outer', description = 'outer loop' },
  ['il'] = { query = '@loop.inner', description = 'inner loop' },

  ['ab'] = { query = '@block.outer', description = 'outer block' },
  ['ib'] = { query = '@block.inner', description = 'inner block' },

  ['as'] = { query = '@statement.outer', description = 'outer statement' },
  ['is'] = { query = '@statement.inner', description = 'inner statement' },
}

for keys, maps in pairs(textobject_select_keymaps) do
  map.visual.operator_pending {
    description = maps.description,
    category = 'operator',
    keys = keys,
    command = function()
      require 'nvim-treesitter-textobjects.select'.select_textobject(maps.query, 'textobjects')
    end,
  }
end

map.normal {
  description = 'swap inner parameter',
  category = 'operator',
  keys = '<M-l>',
  command = function()
    require 'nvim-treesitter-textobjects.swap'.swap_next '@parameter.inner'
  end,
}

map.normal {
  description = 'swap outer parameter',
  category = 'operator',
  keys = '<M-h>',
  command = function()
    require 'nvim-treesitter-textobjects.swap'.swap_previous '@parameter.outer'
  end,
}

local textobject_move_keymaps = {
  ['m'] = { query = '@function.outer', description = 'to %s function definition' },
  ['i'] = { query = '@conditional.outer', description = 'to %s conditional' },
  ['a'] = { query = '@parameter.outer', description = 'to %s parameter' },
}

for keys, maps in pairs(textobject_move_keymaps) do
  map.normal.visual.operator_pending {
    description = string.format(maps.description, 'next'),
    category = 'operator',
    keys = ']' .. keys,
    command = function()
      require 'nvim-treesitter-textobjects.move'.goto_next(maps.query, 'textobjects')
    end,
  }
  map.normal.visual.operator_pending {
    description = string.format(maps.description, 'previous'),
    category = 'operator',
    keys = '[' .. keys,
    command = function()
      require 'nvim-treesitter-textobjects.move'.goto_previous(maps.query, 'textobjects')
    end,
  }
end

puk.defer 'nvim-treesitter-textobjects' (function(textobjects)
  textobjects.setup {
    select = {
      -- Automatically jump forward to textobj, similar to targets.vim
      lookahead = true,
      -- You can choose the select mode (default is charwise 'v')
      --
      -- Can also be a function which gets passed a table with the keys
      -- * query_string: eg '@function.inner'
      -- * method: eg 'v' or 'o'
      -- and should return the mode ('v', 'V', or '<c-v>') or a table
      -- mapping query_strings to modes.
      selection_modes = {
        ['@parameter.outer'] = 'v', -- charwise
        ['@function.outer'] = 'V',  -- linewise
        ['@class.outer'] = '<c-v>', -- blockwise
      },
      -- If you set this to `true` (default is `false`) then any textobject is
      -- extended to include preceding or succeeding whitespace. Succeeding
      -- whitespace has priority in order to act similarly to eg the built-in
      -- `ap`.
      --
      -- Can also be a function which gets passed a table with the keys
      -- * query_string: eg '@function.inner'
      -- * selection_mode: eg 'v'
      -- and should return true of false
      include_surrounding_whitespace = false,
    },

    move = {
      -- whether to set jumps in the jumplist
      set_jumps = true,
    },
  }
end)