fedepujol / move.nvim

Gain the power to move lines and blocks and auto-indent them!
GNU General Public License v3.0
348 stars 6 forks source link

Allow using some characters as "wrapper" and "anchor" #20

Open sangdth opened 1 year ago

sangdth commented 1 year ago

Thanks for the great plugin! I would like to ask for something extra. It would be great if move.nvim can detect some popular wrapper and anchor characters and use it to swap horizontally everything between.

Please see examples below, | is the cursor:

Example of config:

require("move.nvim").setup({
    custom_anchors: { "{,}", "<,>" },
})

Example in usages:

In TypeScript, sometimes I want to swap the imports:

// before
import { |Something, type Other, Else } from 'somewhere';

// move.nvim can detect my cursor is inside the wrapper `{ ... }` with `,` anchor,
// so if I use MoveWord(1), it will become:
import { type Other, |Something, Else } from 'somewhere';

This is also useful in some cases like:

// before
forwardRef<SomeProps, typeof Other>(...)

// move.nvim can detect my cursor is inside `< ... >` wrapper with `,` anchor
// so if I use MoveWord(), it will become:
forwardRef<typeof Other, SomeProps>(...)
fedepujol commented 1 year ago

Hey! Sorry for taking a long time to answer. MoveWord almost achieved that behaviour:

// before
import { |Something, Other, Else } from 'somewhere';

// doing MoveWord
import { Other, Something, Else } from 'somewhere';

But it doesn't work nice on the edges or in complex cases like the one you specified:

// before
import { |Something, type Other, Else } from 'somewhere';

// after MoveWord
import { type |Something, Other, Else } from 'somewhere';
// before
import { Something, type Other, Else| } from 'somewhere';

// after MoveWord
import { Something,  type Else| Other, } from 'somewhere';

I like the example config you proposed and we could extend it:


require('move').setup({
    word = {
        -- Creates the MoveWord command
        enable = true,
        -- We could change how MoveWord operates.
        -- delimited -> inside predefined wrappers with anchors
        delimited = {
            enable = true, -- by default this will be false
            -- Required only when `enable = true`. And it could be extendable.
            -- not sure about the naming `formulas`.
            formulas = { "<,>", "{,}" }
        },
    },
})
sangdth commented 1 year ago

Hi @fedepujol, thanks for your response, I understand that everybody has busy stuffs to handle, so no problem at all.

I agree with your suggestion. I'm also not sure about the naming but it still look good to me. Naming is hard, nobody can blame you :D

Really appreciate your time and effort 🙏🏻