Skip to content

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
@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
Copy link
Collaborator

@superle3 superle3 left a comment

Choose a reason for hiding this comment

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

will review further some time this week.

Comment on lines 136 to 159
if (newPos > envBound.end) {
if (trimEmptyLineAfterEnv && line.text.trim() === "") {
replaceRange(view, line.from, nextLine.from, "");

d = view.state.doc;
lineNo--;
line = d.line(lineNo);
nextLine = d.line(lineNo + 1);
newPos = nextLine.to;
}

if (addLineBreakAfterEnv && !envText.trimEnd().endsWith("\\\\")) {
setCursor(view, line.to);

applySeparator(END_LINE_BREAK, view);

d = view.state.doc;
nextLine = d.line(lineNo + 1);
newPos = nextLine.to;
}
}

setCursor(view, newPos);
}
Copy link
Collaborator

@superle3 superle3 Nov 12, 2025

Choose a reason for hiding this comment

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

I know this may make things complex, but consider applying only one dispatch instead of 2-3.
The functions that dispatch things are:
applySeparator, setCursor, andreplaceRange.
Either you can collect transactions and compose changes to those so you can keep the logic together or consider decoupling the logic.
Multiple dispatches have an impact on performance, for sure if the document is large/ lots of text is in the viewport.

if (trimWhitespace) {
// If at the beginning of the line
if (textBeforeFrom === "") {
separator = separator.match(/^[ \t]*([\s\S]*)$/)[1];
Copy link
Collaborator

@superle3 superle3 Nov 12, 2025

Choose a reason for hiding this comment

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

why custom function over trimStart? why match over replace and is this preparing for user defined separators or smth? because the separators above don't have a leading newline.

Comment on lines +25 to +28
const isMultiLineBreak = (separator: string): boolean => {
return separator.contains("\\\\") && separator.contains("\n");
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a specific reason why you're not checking for LINE_BREAK or just

const regex = /\\\\\s*\n/

or smth? What custom separator did you have in mind? because there is no reason to have this generalization in the current version.

@superle3
Copy link
Collaborator

I think thats all? Not experienced with reviewing so if some comments are too outrageous or unclear, just let me know.

(for example I am not sure how much I should govern the style, hopefully everything I said was reasonable?)

@Testudinidae Testudinidae marked this pull request as draft November 13, 2025 15:52
Changed `d.lineAt` from `range.from` to `range.to` to correctly compute `toLine`.
- Use `ChangeSet` and `changeByRange` in `applySeparator` to apply separator changes, update selections and remap cursors.
- Flatten matrix shortcut control flow for clearer, more linear logic.
- Update `generateSeparatorChange` to take `doc` and `settings` instead of the full `view`.
- Rename some variables for better readability.
@Testudinidae Testudinidae force-pushed the feature/matrix-shortcut branch from b8275c0 to adeafbe Compare November 13, 2025 16:22
Copy link
Collaborator

@superle3 superle3 left a comment

Choose a reason for hiding this comment

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

Note that I will probably extract/refactor the 3 different shortcuts of shift-enter, enter and tab into their own function in #443, as it keeping the distinction insde wouldn't make sense anymore.

@superle3
Copy link
Collaborator

Hi @Testudinidae ,

(Not trying to rush you, it's oss and volunteer work so things take time/ take your time)

I am not in a hurry/dont have a lot of free time this week, but do you mind me merging other prs and make a release since this pr is in draft or were you planning on completing it soon?

Also since you resolved every comment in the other pr (except the last one but that doesn't need to be answered before merging), does that mean I can merge it?

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