Git Product home page Git Product logo

Comments (3)

Julian avatar Julian commented on July 24, 2024 1

The issue comes from the fact that, in a list, every item is both a pattern and a substitution. The code is supposed to switch from the first to the second and from the second to the first, which means s/<or>/<and>/ and the replacement gets the < characters, because they're not special in the replacement part.

aaahh of course, thanks that makes total sense.

And the helpers work perfectly as well. I just put them to good use. (And probably agreed on avoiding parametrizing unless there are more axes to deal with).

Thanks again for both the tip and the plugin!

from switch.vim.

AndrewRadev avatar AndrewRadev commented on July 24, 2024

Thank you for your kind words :).

Regarding the issue, I don't think the problem is with case normalization -- in fact, if you put ['\<or\>', '\<and\>'], I think you'll get the same result, or at least I get it locally.

The issue comes from the fact that, in a list, every item is both a pattern and a substitution. The code is supposed to switch from the first to the second and from the second to the first, which means s/\<or\>/\<and\>/ and the replacement gets the < characters, because they're not special in the replacement part. The list version is useful for simple transformations, but doesn't work that hot when you need to use regexes.

What I can recommend in this case is using the hash form, which is basically what the plugin generates, without the \< parts:

let g:switch_custom_definitions = [
      \   { '\C\<and\>': 'or', '\C\<or\>': 'and' },
      \   { '\C\<And\>': 'Or', '\C\<Or\>': 'And' },
      \   { '\C\<AND\>': 'OR', '\C\<OR\>': 'AND' },
      \ ]

Having said that, It feels like it'd be useful to also make a helper like switch#NormalizedCaseWords to also add the word boundaries. (Plus a switch#Words one for the same thing without case normalization). I'll try it out.

from switch.vim.

AndrewRadev avatar AndrewRadev commented on July 24, 2024

I've tried adding two more helper functions -- take a look, try them out, let me know what you think:

switch.vim/doc/switch.txt

Lines 201 to 225 in 23243cf

You might want this to work for different capitalizations, like with `true`
and `True` and `TRUE`. You might also want to also affect only word
boundaries. While you could use the more complicated dict definition, a simple
way to tackle these scenarios is with modifier functions:
- `switch#NormalizedCase`
- `switch#Words`
- `switch#NormalizedCaseWords`
Here's how you might use these:
>
let g:switch_custom_definitions =
\ [
\ switch#NormalizedCase(['one', 'two']),
\ switch#Words(['three', 'four']),
\ switch#NormalizedCaseWords(['five', 'six']),
\ ]
<
The result of this is that:
- The first definition would switch between "one" and "two", between "One" and
"Two", and between "ONE" and "TWO".
- The second definition would switch between "three" and "four" only at word
boundaries, as if the patterns have |\<| and |\>| modifiers added to them.
- The third would switch between "five"/"six", "Five"/"Six", "FIVE"/"SIX" only
at word boundaries with a combination of the above.

I could add one parameterized function instead, but I'd like to keep the old one for compatibility, so I've held off. Plus, the more "programmatic" of an interface I come up with, the harder it might be for someone with little Vimscript experience.

In the future, if there's a third dimension to parameterize by, I'll introduce a common function with a dict parameter and keep the old ones as fronts.

from switch.vim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.