move.nvim icon indicating copy to clipboard operation
move.nvim copied to clipboard

Unpredictable behavior

Open aminnairi opened this issue 1 year ago • 3 comments

This plugin kind of works sometimes, and sometimes it does not in an unpredictable way and I can't really find a pattern in here.

record

I have followed the installation instructions (without Legendary) and I'm using LazyNvim.

return {
  "fedepujol/move.nvim",
  config = function()
    local opts = { noremap = true, silent = true }

    -- Normal-mode commands
    vim.keymap.set('n', '<A-j>', ':MoveLine(1)<CR>', opts)
    vim.keymap.set('n', '<A-k>', ':MoveLine(-1)<CR>', opts)
    vim.keymap.set('n', '<A-h>', ':MoveHChar(-1)<CR>', opts)
    vim.keymap.set('n', '<A-l>', ':MoveHChar(1)<CR>', opts)
    vim.keymap.set('n', '<leader>wf', ':MoveWord(1)<CR>', opts)
    vim.keymap.set('n', '<leader>wb', ':MoveWord(-1)<CR>', opts)

    -- Visual-mode commands
    vim.keymap.set('v', '<A-j>', ':MoveBlock(1)<CR>', opts)
    vim.keymap.set('v', '<A-k>', ':MoveBlock(-1)<CR>', opts)
    vim.keymap.set('v', '<A-h>', ':MoveHBlock(-1)<CR>', opts)
    vim.keymap.set('v', '<A-l>', ':MoveHBlock(1)<CR>', opts)
  end,
}

Is there something I'm missing?

aminnairi avatar Jan 06 '24 20:01 aminnairi

The auto-indentation part is not the best (there's room for improvement). You're experiencing this because it tries to calculate the amount of indentation to move the block/line with:

  • The immediate above or below line
  • And then it uses == with the selected line/block to calculate the new indentation
  • Depending on the diff between the new and old indentation it moves it again

I'm trying to get back to this plugin for a while now. The whole indentation part will be opt-in in the future through a config. Will try to come up with a new method, if you have a suggestion PR's are always welcome!.

fedepujol avatar Jan 25 '24 17:01 fedepujol

Will try to come up with a new method, if you have a suggestion PR's are always welcome!.

Free mode (without indentation): Use collision detection based on the block/selection the plugin was started with to store the context and update it on movements until "movement is finished" to apply the change. see https://github.com/echasnovski/mini.nvim/issues/838.

auto-indentation

use lsp info or treesitter info, if present. There is nothing decent yet to get AST change notifications and/or formatter diffs, so its not solvable efficiently yet.

matu3ba avatar Apr 24 '24 15:04 matu3ba

An algorithm that worked a little bit better:

vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv", { desc = "Move line down", silent = true })
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv", { desc = "Move line up", silent = true })

I had this in my config for a while (I think I got it from ThePrimeagen) but it was flickery and felt slower than this plugin. But this one has no problems with indentation:

https://github.com/fedepujol/move.nvim/assets/300791/1894ef63-eb41-473b-a046-d5d5f5bbd0c6

mikavilpas avatar Jun 10 '24 09:06 mikavilpas