Skip to content

Allow Razor generated documents to respect tabs/spaces settings#31211

Merged
allisonchou merged 15 commits into
mainfrom
dev/allichou/TabsSpacesCodeGeneration
Mar 30, 2021
Merged

Allow Razor generated documents to respect tabs/spaces settings#31211
allisonchou merged 15 commits into
mainfrom
dev/allichou/TabsSpacesCodeGeneration

Conversation

@allisonchou
Copy link
Copy Markdown
Contributor

Part of dotnet/razor#3317. Enables generated documents to respect the tabs/spaces settings passed in from Razor tooling.

The bulk of this is Ajay's work from dotnet/razor#1606, so major creds to him. 🎉

@allisonchou allisonchou requested a review from a team as a code owner March 24, 2021 22:45
Copy link
Copy Markdown
Contributor

@TanayParikh TanayParikh left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment thread src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs Outdated
#line hidden
#nullable disable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@TanayParikh It looks like Ajay was seeing the same issue in dotnet/razor#1606. It doesn't look too impactful, but just want to check this change is OK?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hmmm, so this wont have any actual impact on the runnability of the code since it's outside of the line pragma; however, it's concerning that this is being touched at all? Definitely need to look into what's happening here. Also, in the other files I wouldn't imagine that generated locations would change unless there was a mashup of tabs in the source files / interesting settings in the tests.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, @pranavkm does this not worry you?

Comment thread src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs Outdated
public CodeWriter Indent(int size)
{
if (Length == 0 || this[Length - 1] == '\n')
if (size == 0 || (Length != 0 && this[Length - 1] != '\n'))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is unrelated to your PR, but indexing in to StringBuilder (which is what this[..] is doing) is really slow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, interesting. Maybe we can keep track of the last character somehow, possibly storing it in a field?
(Don't know if it's in the scope of this PR, but would be a good follow up later on.)

…on/CodeWriter.cs

Co-authored-by: Pranav K <prkrishn@hotmail.com>
Copy link
Copy Markdown

@NTaylorMullen NTaylorMullen left a comment

Choose a reason for hiding this comment

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

Looking good! One big concern is that the generated files changed. Need to validate that it's expected and that we were doing the wrong thing previously and not doing the wrong thing now. I wouldn't have imagined they'd change is all


var context = new DefaultCodeRenderingContext(
new CodeWriter(),
new CodeWriter(Environment.NewLine, _options.IndentWithTabs, _options.IndentSize),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hmmm I wonder if it makes more sense for the code writer to take in the code gen options instead. @pranavkm thoughts now that you own this? 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think you're right, it would also make it easier in the future if we were to add other code gen option variables to the constructor.

#line hidden
#nullable disable

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hmmm, so this wont have any actual impact on the runnability of the code since it's outside of the line pragma; however, it's concerning that this is being touched at all? Definitely need to look into what's happening here. Also, in the other files I wouldn't imagine that generated locations would change unless there was a mashup of tabs in the source files / interesting settings in the tests.

@allisonchou
Copy link
Copy Markdown
Contributor Author

Okay, so I think I see what's going on here in regards to why the code gen and mappings have changed.
tl;dr I think the current changes are correct.

In the existing code, the indent logic for padding is done here in CodeWriterExtensions. However, I believe this logic is incorrect since it does not properly increment the _currentLineCharacterIndex and _absoluteIndex variables in the backing CodeWriter. The current PR correctly increments the variables, hence the changes.

cc @NTaylorMullen

@NTaylorMullen
Copy link
Copy Markdown

Okay, so I think I see what's going on here in regards to why the code gen and mappings have changed.
tl;dr I think the current changes are correct.

In the existing code, the indent logic for padding is done here in CodeWriterExtensions. However, I believe this logic is incorrect since it does not properly increment the _currentLineCharacterIndex and _absoluteIndex variables in the backing CodeWriter. The current PR correctly increments the variables, hence the changes.

cc @NTaylorMullen

Ahhhh I can totally see that. Thank you for validating @allisonchou !

@ryanbrandenburg
Copy link
Copy Markdown
Contributor

The failure looks random, will probably pass on retry.

@BrennanConroy
Copy link
Copy Markdown
Member

The failure looks random, will probably pass on retry.

Or, you could file an issue and mark the test as quarantined.

@allisonchou allisonchou merged commit 2c4444f into main Mar 30, 2021
@allisonchou allisonchou deleted the dev/allichou/TabsSpacesCodeGeneration branch March 30, 2021 20:45
@allisonchou allisonchou restored the dev/allichou/TabsSpacesCodeGeneration branch March 30, 2021 20:45
@allisonchou allisonchou deleted the dev/allichou/TabsSpacesCodeGeneration branch March 30, 2021 20:45
@allisonchou allisonchou restored the dev/allichou/TabsSpacesCodeGeneration branch March 30, 2021 20:45
@allisonchou allisonchou deleted the dev/allichou/TabsSpacesCodeGeneration branch March 30, 2021 20:45
@BrennanConroy
Copy link
Copy Markdown
Member

#31405
Try not to ignore test failures

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.

8 participants