switch.vim icon indicating copy to clipboard operation
switch.vim copied to clipboard

SwitchReverse doesn't seem to work with regex?

Open ckangnz opened this issue 5 months ago • 1 comments

let g:switch_mapping = ""
let g:switch_custom_definitions =
    \ [
    \   ['!NOTE', '!TIP', '!IMPORTANT','!CAUTION', '!WARNING'],
    \   {
    \     '\<\(\l\)\(\l\+\(\u\l\+\)\+\)\>': '\=toupper(submatch(1)) . submatch(2)',
    \     '\<\(\u\l\+\)\(\u\l\+\)\+\>': "\\=tolower(substitute(submatch(0), '\\(\\l\\)\\(\\u\\)', '\\1_\\2', 'g'))",
    \     '\<\(\l\+\)\(_\l\+\)\+\>': '\U\0',
    \     '\<\(\u\+\)\(_\u\+\)\+\>': "\\=tolower(substitute(submatch(0), '_', '-', 'g'))",
    \     '\<\(\l\+\)\(-\l\+\)\+\>': "\\=substitute(submatch(0), '-\\(\\l\\)', '\\u\\1', 'g')",
    \   }
    \ ]

nnoremap <Bar>> :Switch<CR>
nnoremap <Bar>< :SwitchReverse<CR>

I have this set up. !NOTE !TIP switch works fine. But reversing on the regex doesn't seem to work.

ckangnz avatar Jun 15 '25 22:06 ckangnz

Yes, this is described in the help here: https://github.com/AndrewRadev/switch.vim/blob/df58397cfa36d2f428ddc2652ba32d1db2af6d02/doc/switch.txt#L63-L69

There is no way for the plugin to automatically reverse the direction of regex-based switches. A dictionary definition says "if you match this regex, perform this substitution". There is no way for the code to go in the other direction. If you have a list of words, it can be converted into regex + substitution in either direction.

If this is important to you, I could think of a method to describe regexes in both directions, but you'll have to essentially write each regex + substitution twice, and I make no promises on when I can get around to it.

AndrewRadev avatar Jun 17 '25 12:06 AndrewRadev

I can see why there's this limitation. I'm happy with as it is. 🙂 thank you!

ckangnz avatar Jun 23 '25 02:06 ckangnz