Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle different styles of fraction, arrow etc. support request #2402

Closed
pilgrimlyieu opened this issue May 27, 2022 · 7 comments
Closed

Toggle different styles of fraction, arrow etc. support request #2402

pilgrimlyieu opened this issue May 27, 2022 · 7 comments

Comments

@pilgrimlyieu
Copy link

pilgrimlyieu commented May 27, 2022

I've checked that the fraction toggle only works for .../... and \frac{...}{...}.

I consider that it should be useful to toggle different styles of fraction such as cycling in \frac{...}{...}, \dfrac{...}{...} and \cfrac{...}{...}.

Besides, this can also apply to arrow toggle, which means we can cycle in ->, \rightarrow, \Rightarrow and so on.

I've tried it through another vim plugin called UltiSnips

snippet code
global

command_mapping = ['🚀', '🚁', '🚂', '🚃', '🚄', '🚅', '🚆', '🚇', '🚈', '🚉', '🚊', '🚋', '🚌', '🚍', '🚎', '🚏']

def choose_next(string, array, length = 0):
    return array[array.index(string) - (length or len(array)) + 1]

def command_cycle(target, commands, bracketnum = 1):
    length = len(commands)
    command_map = command_mapping[:length]
    for i in range(length):
        target = target.replace(commands[i], command_map[i])
    string = list(target)
    depth = brackets = 0
    for i in range(len(string) - 1, -1, -1):
        if string[i] == '}': depth += 1
        elif string[i] == '{': depth -= 1
        elif brackets == bracketnum:
            if not depth:
                try:
                    string[i] = choose_next(string[i], command_map, length)
                    break
                except:
                    pass
        brackets += 0 if depth else 1
    result = ''.join(string)
    for i in range(length):
        result = result.replace(command_map[i], commands[i])
    return result

fractions = ['\\frac', '\\dfrac' ,'\\cfrac']

endglobal

snippet "(\\(frac|dfrac|cfrac)\{.*\}\{.*\})" "Fraction Transformation" r
`!p
snip.rv = command_cycle(match.group(1), fractions, 2)
`
endsnippet

However, this solution is easy-trigger, which means even in case like \frac{...}{...}...{...}<tab> can trigger the transformation.

I've also struggled to use commands like vimtex#delim#get_surrounding to match the brackets via UltiSnips, but cause I'm a new learner so I failed.

I wonder if there is a simple solution that can apply for different cases to achieve my request, Or it can be taken as a feature into later version.

Appreciating your wonderful work!

@pilgrimlyieu
Copy link
Author

pilgrimlyieu commented May 27, 2022

In my view, we can use vimtex#delim#get_surrounding command to get the position of the matching bracket.

\frac{...}{...<cmd>}</cmd>
-->
\frac{...}<cursor>{</cursor>...}
-->
\frac{...<cmd>}</cmd>{...}
-->
\frac<cursor>{</cursor>...}{...}
-->
<select>\frac</select>{...}{...}
-->
\dfrac{...}{...}<cursor>

for \frac{...}{...} and \dfrac{...}{...}

\bar{...<cmd>}</cmd>
-->
\bar<cursor>{</cursor>...}
-->
<select>\bar</select>{...}
-->
\overline{...}<cursor>

for \bar{...} and \overline{...}

\rightarrow<cursor>
-->
\Rightarrow<cursor>

for \rightarrow and \Rightarrow

@lervag
Copy link
Owner

lervag commented May 28, 2022

I consider that it should be useful to toggle different styles of fraction such as cycling in \frac{...}{...}, \dfrac{...}{...} and \cfrac{...}{...}.

You want something like this:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac', 'dfrac', 'cfrac' ]

where g:vimtex_toggle_fractions specify the various fraction commands you want to toggle between, including the ... / ... (INLINE) variant. This should not be too hard to implement with a default option value similar to the current behaviour:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac' ]

Does this sound like a sensible solution to you?

Besides, this can also apply to arrow toggle, which means we can cycle in ->, \rightarrow, \Rightarrow and so on.

This is something else entirely. UltiSnips is very neat and I use it myself; but I don't use it for toggles like this. It may work for that as well, but it feels a little bit out of scope.

I think the general behaviour of toggling or cyclign between related concepts is captured by some other plugins. At least for the more simple cases, such as the arrows. I suggest you look into one of these:

I believe all of these should work for you with some configuration (although at least one of these require neovim).

Appreciating your wonderful work!

Glad to hear it! :)

@pilgrimlyieu
Copy link
Author

pilgrimlyieu commented May 29, 2022

You want something like this:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac', 'dfrac', 'cfrac' ]

where g:vimtex_toggle_fractions specify the various fraction commands you want to toggle between, including the ... / ... (INLINE) variant. This should not be too hard to implement with a default option value similar to the current behaviour:

let g:vimtex_toggle_fractions = [ 'INLINE', 'frac' ]

Does this sound like a sensible solution to you?

That's what I want exactly! Thanks you very much!

This is something else entirely. UltiSnips is very neat and I use it myself; but I don't use it for toggles like this. It may work for that as well, but it feels a little bit out of scope.

I think the general behaviour of toggling or cyclign between related concepts is captured by some other plugins. At least for the more simple cases, such as the arrows. I suggest you look into one of these:

I believe all of these should work for you with some configuration (although at least one of these require neovim).

Thanks, I'll try some of them.

Should I close the issue currently or after the feature is implemented?

@lervag
Copy link
Owner

lervag commented May 29, 2022

Should I close the issue currently or after the feature will be implemented?

Nah, let's leave the issue opened until I get around to implementing this. Won't make promises on when, but I'll look into it when I get the time :)

@pilgrimlyieu
Copy link
Author

but I'll look into it when I get the time :)

Thanks!

lervag added a commit that referenced this issue Oct 3, 2022
@lervag
Copy link
Owner

lervag commented Oct 3, 2022

Sorry, took a while before I got around to it. But I believe this should work as promised now!

@lervag lervag closed this as completed Oct 3, 2022
@pilgrimlyieu
Copy link
Author

Great! Appreciating your excellent works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants