Sublime Forum

Transpose (possibly again)

#1

I was trying to swap a word with just one character (in fact I started with 0 chars, so just swapping the place of a word) and ST3 (3.2.1.1) decides that that isn’t what I want to do, and swaps a few characters to the right. Very very similar to this post.

Is the fix still the same ?

Thank you

ps Can’t seem to find any info online either, it’s like the transpose command doesn’t exist…weird
Googled transpose command Sublime Text , nope googled list of Sublime Text 3 commands no transpose command… SIGH

0 Likes

#2

The command lives in Default/transpose.py; you can use View Package File from the command palette to see what it’s doing. I don’t use the command myself, but it seems unchanged between ST3 and ST4 and indeed has an internal function that takes that argument but never calls it with the value changed.

As such I would say if whatever that post talks about fixes the problem, that’s probably still relevant. You could also try selecting the word and selecting the character, since it will transpose selections directly.

0 Likes

#3

def full_region(region):

# I'm assuming this region.begin()+1 is why it's selecting xtra characters
    return (sublime.Region(region.begin(), region.begin() + 1)
            if region.empty() else region)


def perform_transposition(edit, view, trans, init_sel):
    " assumes trans is already reverse sorted sequence of regions"
    view.sel().subtract(init_sel)

    for i, (sel, substr) in enumerate(zip(trans, reversed([view.substr(s) for s in trans]))):
        view.replace(edit, sel, substr)
        if not i:
            if init_sel.empty():
                view.sel().add(sublime.Region(init_sel.a + 1))
            else:
                view.sel().add(init_sel)

# And here should set can_transpose_words=True
def transpose_selections(edit, view, can_transpose_words=False):
    for sel in view.sel():
        word_sel = view.word(sel)
        word_extents = (wb, we) = (word_sel.begin(), word_sel.end())
        transpose_words = sel.end() in word_extents

Also I guess it’s a case of copying and pasting into my User folder and then what ? It’s not in the same format as a plugin, so not sure what the next steps are, as I don’t seem to be able to alter the file (read only). Any thoughts ?

Ta

0 Likes

#4

It’s exactly a plugin, but as you have it defined here, it contains no commands and thus won’t actually do anything. You probably want to to either create an override on the version in Default and make the modification there, or copy the entire plugin from Default into a file in your User package and then modify the parts that you need.

0 Likes