Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Implement Math Block Align #115

Merged
merged 8 commits into from
Oct 29, 2023
Merged

Conversation

taooceros
Copy link
Contributor

@taooceros taooceros commented Oct 22, 2023

resolve #113

Implement a basic alignment for math block based on the align point. Current rule is:

  • Align against the farthest align point with the same index
  • If the first align point is in a new line, then it will get a indent

It scan the syntax twice to retrieve the information (farthest align point). I can't think a way to do it at one pass.

Maybe we can do better for math:

  • Add a space around all kind of operator (+-/*) and the align mark &
  • A special rule for matrix (I think everything collapse to single line for now.

Copy link
Owner

@astrale-sharp astrale-sharp left a comment

Choose a reason for hiding this comment

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

Hey!

If you have any questions about the review, don't hesitate to ask!

lib/src/math.rs Outdated Show resolved Hide resolved
lib/src/tests/math.rs Show resolved Hide resolved
@taooceros
Copy link
Contributor Author

@astrale-sharp done

description: "INPUT\n===\n\"$\\nmat(\\n 1,2,3,4;\\n5,2,2,3\\n)\\n$\"\n===\n$\nmat(\n 1,2,3,4;\n5,2,2,3\n)\n$\n===\nFORMATTED\n===\n$\n mat(1, 2, 3, 4;5, 2, 2, 3)\n$"
expression: formatted
---
"$\n mat(1, 2, 3, 4;5, 2, 2, 3)\n$"
Copy link
Owner

Choose a reason for hiding this comment

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

not sure if this is in scope for this PR but we might want a space to be present after ; in the formatted result, what's your opinion?

If comma and semicolon are the only things that can appear in math functions as argument separator then maybe, If so it would be good to add a little //TODO in your PR so that we may remember it later!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like a new line there that makes it similar to the matrix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know whether matrix is a special syntax since I cannot find a way to define a function similar to the matrix. If only matrix behaves like this I would even want to align the commas.

Copy link
Owner

Choose a reason for hiding this comment

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

We should be careful about giving special meaning to ; and breaking lines, we don't want to end up with

$func_call(a;
b;
c;
)$

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah that makes thing much complicate, and I don't it to delay this pr.

@astrale-sharp
Copy link
Owner

Thanks to these new files I can see the output of your work and it looks really good ! Nice!

You're actually one of the rare person to have edited the formatting code itself and not the tooling, I'm curious, was it difficult?

Most of the examples you chose are quite alike, I think not all of them are necessary although it's not a big deal to leave them as is.

Copy link
Owner

@astrale-sharp astrale-sharp left a comment

Choose a reason for hiding this comment

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

I would add a test like this where the linebreak is not there and the formatter adds it.
"$#xx(a,b) &= 1 \ #xx(a,b) &= 1 \$" and then we would be good to go, thanks for this nice contribution !

@astrale-sharp
Copy link
Owner

Thanks again for your nice contribution, we're almost good to go!

@taooceros
Copy link
Contributor Author

Could you do it for me since I will be afk for a while? Thanks 😃

@taooceros
Copy link
Contributor Author

I would add a test like this where the linebreak is not there and the formatter adds it.

"$#xx(a,b) &= 1 \ #xx(a,b) &= 1 \$" and then we would be good to go, thanks for this nice contribution !

I feel like the current implementation won't do it. Maybe I can revise it sometimes. Though maybe some user will prefer to not having a line break unless it is too long? Idk

@astrale-sharp
Copy link
Owner

astrale-sharp commented Oct 24, 2023

Just curious, what would be the output of such a test? (It's okay to add the test to verify that it doesn't break or behave weirdly)

@astrale-sharp
Copy link
Owner

Could you do it for me since I will be afk for a while? Thanks 😃

I didn't see that message,

sure! I'll do/test that soon !

@taooceros
Copy link
Contributor Author

Could you do it for me since I will be afk for a while? Thanks 😃

I didn't see that message,

sure! I'll do/test that soon !

no worry I can do it now.

@taooceros
Copy link
Contributor Author

taooceros commented Oct 25, 2023

Just curious, what would be the output of such a test? (It's okay to add the test to verify that it doesn't break or behave weirdly)

basically without changing anything.

@taooceros
Copy link
Contributor Author

@astrale-sharp do you think we should add space around &?

@astrale-sharp
Copy link
Owner

Just curious, what would be the output of such a test? (It's okay to add the test to verify that it doesn't break or behave weirdly)

basically without changing anything.

I'm not sure, it would help to see an example of what you mean!

@astrale-sharp
Copy link
Owner

With the new test, everything looks good and we're covered so I would be keen to merge as is, but If you want to try with spaces around, we can see what it looks like!

@astrale-sharp
Copy link
Owner

One test fails, seems like maybe the snapshot was not registered ?

@taooceros
Copy link
Contributor Author

taooceros commented Oct 26, 2023

Ah I made a mistake there. I will fix it tonight

EDIT by @astrale-sharp
night is for sleeping

@taooceros
Copy link
Contributor Author

Sorry for the delay. Now it should be good.

@astrale-sharp
Copy link
Owner

No need to apologize! Thanks a lot for this contribution it looks amazing 😁 !

@astrale-sharp astrale-sharp merged commit aa621f3 into astrale-sharp:master Oct 29, 2023
1 check passed
);

if position == 0 && first_align {
should_indent = true;
Copy link
Owner

Choose a reason for hiding this comment

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

should_indent = position == 0 && first_align;

Could be rewritten as such, clippy would help

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

Successfully merging this pull request may close these issues.

typstfmt remove all indent of math block
2 participants