Skip to content

Comments

Add new option to control attribute indent style#12625

Merged
davidwengier merged 5 commits intodev/dawengie/LetsDoHtmlFormattingfrom
dev/dawengie/HtmlAttributeIndentOption
Jan 7, 2026
Merged

Add new option to control attribute indent style#12625
davidwengier merged 5 commits intodev/dawengie/LetsDoHtmlFormattingfrom
dev/dawengie/HtmlAttributeIndentOption

Conversation

@davidwengier
Copy link
Member

@davidwengier davidwengier commented Dec 29, 2025

Fixes #6551
Fixes #8413

Adding an option is way too annoying! We'll have to fix that up when we get rid of the language server.

@davidwengier davidwengier requested a review from a team as a code owner December 29, 2025 04:26
<value>Align with first attribute</value>
</data>
<data name="Setting_AttributeIndentStyleIndentByOne" xml:space="preserve">
<value>Indent by one level</value>
Copy link
Contributor

Choose a reason for hiding this comment

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

Indent by one level

Does this mean indent one level from the html tag? Or one level from the first non-whitespace on a line? Or something else?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added docs to the enum, its one level more indentation than the line that the start tag is on.

// We need to line up with the first attribute, but the start tag might not be the first thing on the line,
// so it's really relative to the first non-whitespace character on the line
var lineStart = _sourceText.Lines[GetLineNumber(startTag)].GetFirstNonWhitespacePosition().GetValueOrDefault();
htmlIndentLevel = FormattingUtilities.GetIndentationLevel(nameSpan.Start - lineStart, _tabSize, out additionalIndentation);
Copy link
Contributor

Choose a reason for hiding this comment

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

nameSpan.Start - lineStart

Same concern from the other PR about whether this could be negative

Copy link
Member Author

Choose a reason for hiding this comment

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

The Html formatter always puts the first attribute on the same line as the start tag. Will add a test.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you certain? In vscode, if I create a tag that starts at column 100 or so, it's first attribute is placed on the subsequent line. I thought the html formatter in VS did something similar at some point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh interesting, I was only testing at column 0. Thank you! Will add a test and make sure nothing bad happens.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the test I added in the other PR, to cover both indent styles

"AutoInsertAttributeQuotes": false,
"CommitElementsWithSpace": false
"CommitElementsWithSpace": false,
"AttributeIndentStyle": 1
Copy link
Contributor

Choose a reason for hiding this comment

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

1

consider removing the 1 and changing this string to use {expectedOptions.AttributeIndentStyle} instead.

using Microsoft.CodeAnalysis.Razor.CodeActions.Razor;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Settings;
Copy link
Contributor

Choose a reason for hiding this comment

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

Was there a change that made this necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

The AttributeIndentStyle reference on line 847

Copy link
Contributor

Choose a reason for hiding this comment

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

I totally missed that change. The contrast in codeflow between the background and changed lines is evidently a bit tricky for my eyes. Switching to dark theme and it's a lot more evident now.

…ing' into dev/dawengie/HtmlAttributeIndentOption

# Conflicts:
#	src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.CSharpDocumentGenerator.cs
@davidwengier davidwengier merged commit e4feaa9 into dev/dawengie/LetsDoHtmlFormatting Jan 7, 2026
@davidwengier davidwengier deleted the dev/dawengie/HtmlAttributeIndentOption branch January 7, 2026 06:43
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.

2 participants