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

Enhance Matrix Shortcut with Multi-selection #394

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

Testudinidae
Copy link
Contributor

@Testudinidae Testudinidae commented Feb 19, 2025

This is a significant update with multiple enhancements and refactors to improve matrix shortcut handling and internal logic. Below is a summary of the changes made:

  • Matrix Shortcut - Multi-selection Support
    Users can now use multi-cursor with selection within matrix shortcuts, enabling more efficient edits.
  • Refactor isWithinEnvironment
    The isWithinEnvironment function has been split into two separate functions:
    • getEnvironmentBound
    • isWithinEnvironment
      This refactor improves clarity and enhances the function's usability.
  • Handling \hline
    The logic now ignores adding a line break (\\) after encountering \hline, ensuring no unnecessary line breaks are inserted in these situations.

Optional Features:

  • Trim Excess Whitespace (default true)
    When enabled, extra spaces around the cursor are trimmed automatically. This helps address common cases where users accidentally add extra spaces, either by pressing the spacebar unintentionally or due to snippets automatically adding them. For example, pressing Tab after "1(a lot of space)" will result in "1 & " rather than "1(a lot of space) & " to prevent accidental spaces from accumulating.
  • Trim Excess Alignment (default true)
    This trims unnecessary alignment symbols that can occur if users accidentally press Tab at the end of a line. For instance, pressing Enter after "1 & " could result in an extra "&" being inserted, but with this feature enabled, it will correctly turn into "1 \\" instead of "1 & \\" to avoid such mistakes.
  • Enable \\ After \hline (default false)
    If enabled, a line break \\ will be inserted after \hline.
  • Trim Empty Line After Environment (default true)
    When exiting an environment using Shift+Enter, an empty line after the environment is removed to prevent unnecessary blank spaces. For instance:
...
    1 & 2 & 3 \\
     <- (cursor)
\end{pmatrix}

would transform to:

...
    1 & 2 & 3 \\
\end{pmatrix} <- (cursor)
  • Add Line Break After Environment (default false)
    If enabled, Shift+Enter will add a line break after exiting an environment. For example:
...
    1 & 2 & 3 <- (cursor)
\end{pmatrix}

will transform to:

...
    1 & 2 & 3 \\
\end{pmatrix} <- (cursor)

These updates aim to enhance the user experience and provide better control over formatting and matrix shortcuts.

@llakala
Copy link
Contributor

llakala commented Feb 19, 2025

Probably want to rebase and squash merge commits, as well as refactor commits.

@Testudinidae Testudinidae force-pushed the feature/matrix-shortcut branch from d40a3b9 to 54a0e20 Compare February 19, 2025 21:39

isInsideAnEnv = ctx.isWithinEnvironment(ctx.pos, env);
if (isInsideAnEnv) break;
}

if (!isInsideAnEnv) return false;

// Take main cursor since ctx.mode takes the main cursor, weird behaviour is expected with multicursor because of this.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry to be nitpicky but why was this removed. Weird behavior still arises when the main cursor is in different environment then the other cursors.
For example if nothing is selected in the main cursor but something is selected in another cursor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to apologize—I believe all features should be discussed to find the most convenient solution for everyone. I'm not sure what you mean by "weird behavior." I think in multi-selection cases, all selections should behave consistently—either all selected lines increase indentation or all selections are replaced with " & ", always following the main selection. Other potentially strange behaviors, like some selections being outside the environment or even outside the math block, shouldn’t be an issue because users using multi-selection should clearly know what they are doing. I hope you can provide more specific examples of what you mean by "weird behavior."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original thought was that a cursor according to its "environment" (like math, code or text).
So any cursor that doesn't follow the behaviour of its environment is messy behaviour, but since I couldn't give a use case I just called it weird behaviour.
Cause if you have the following code where | is the cursor

\begin{pmatrix}
1 & 2 |
\end{pmatrix}
| some selected text|

then either the lines are indented or the selected text is replaced with a &, which can be weird but also no use case.
The main cursor was indeed taken to be consistent with the rest of the plugin and put the comment there if someone wanted ctx cover multicursor.

But I still can't think of a use case, why someone would use cursors in different environments besides accidents, so it can be left out.

hope this explained it better.

@Testudinidae
Copy link
Contributor Author

I might reorganize the git commit history in the next couple of days. Feel free to point out any unexpected behavior.

@Testudinidae Testudinidae force-pushed the feature/matrix-shortcut branch from 54a0e20 to 8484466 Compare February 20, 2025 11:15
@Testudinidae Testudinidae force-pushed the feature/matrix-shortcut branch from 8484466 to 0a2cd87 Compare February 20, 2025 11:31
@Testudinidae Testudinidae force-pushed the feature/matrix-shortcut branch from 0a2cd87 to 46f495b Compare February 23, 2025 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants