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

sdfmt: Explicitly parse shortened function syntax #380

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maxhaton
Copy link
Collaborator

@maxhaton maxhaton commented Sep 1, 2024

This goes some way to Fix #359, but the result could probably be improved somewhat with a clever span somewhere.

This feature is presumably only used for things with fairly short names so one imagines the line wrapping behaviour won't be noticed much anyway...

@maxhaton maxhaton requested a review from deadalnix September 1, 2024 22:55
src/format/parser.d Outdated Show resolved Hide resolved
@@ -2503,6 +2511,18 @@ private:
assert(0);
}

void parseShortenedFunctionBody() in(match(TokenType.FatArrow)) {
auto spanGuard = span!Span();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you not splice it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed it, not entirely sure if I know what the difference is precisely?

Copy link
Contributor

Choose a reason for hiding this comment

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

Dump the IR, but long story short, the span is extended leftward in a way that fits the hierarchy.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if there is already a span that does that, then maybe it's not useful to add one more, but I don't remember what is the generated IR here.

src/format/parser.d Outdated Show resolved Hide resolved
src/format/parser.d Outdated Show resolved Hide resolved
space();
parseExpression();
split();
nextToken();
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to check it is the token you expect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What should it do if it's not? Nothing, then return?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, pretty much.

Comment on lines +3 to +4
int _______________________(int x)
=> x + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

That's good. We also need a test case where the expression in there splits.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added something that breaks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looks ok.

Copy link
Contributor

@deadalnix deadalnix Sep 3, 2024

Choose a reason for hiding this comment

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

Yes, this looks good. One thing I have though of since is that we maybe break after the type, in which case I think we do not indent. The way the span are laid out might break this, so it's a good idea to check.

nextToken();
space();
parseExpression();
if (match(TokenType.Semicolon)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a function that does that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

where?

Copy link
Contributor

Choose a reason for hiding this comment

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

Look for runOnType .

src/format/parser.d Outdated Show resolved Hide resolved
This goes some way to Fix snazzy-d#359, but the result could probably be
improved somewhat with a clever span somewhere.

This feature is presumably only used for things with fairly short names
so one imagines the line wrapping behaviour won't be noticed much
anyway...
@@ -2503,6 +2511,19 @@ private:
assert(0);
}

void parseShortenedFunctionBody() in(match(TokenType.FatArrow)) {
auto spanGuard = spliceSpan();
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it change something if you remove this? There might be a span here already, but if there isn't this is good to have.

@deadalnix deadalnix force-pushed the master branch 3 times, most recently from 0896895 to 5a79292 Compare October 13, 2024 11:52
@deadalnix deadalnix force-pushed the master branch 3 times, most recently from 7ce40a5 to 4dbe602 Compare December 4, 2024 01:18
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.

Formatting of functions using => instead of return
2 participants