dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.47k stars 4.76k forks source link

[API Proposal]: Whitespace options on System.Xml.XmlTextWriter #90155

Open lor1mp opened 1 year ago

lor1mp commented 1 year ago

Background and motivation

Recently we discovered that .editorconfig files aren't respected for .xml and .csproj files in Visual Studio and dotnet format. It looks like this is a Rider feature. However, across our team the formatting of the project files becomes an issue because the default Visual Studio formatting is different from the .editorconfig that Rider is able to format to. So, in lieu of available tooling, I looked to build a tool. However, the .NET XML library doesn't support the options for the XmlTextWriter, and there doesn't seem to be a viable alternative to writing XML files.

The options I'm proposing to support are:

insert_final_newline - inserts a newline after the closing XML document tag
space_inside_empty_tag - ???
space_before_self_closing - inserts a space before the /> in a self closing tag (XmlTextWriter default)
space_after_last_attribute - inserts a space after the last attribute in a tag
spaces_around_eq_in_attribute - inserts spaces around the '=' character in attributes

Basically, there's not an easy way to configure the XmlTextWriter to output with the spacing required for the options in our .editorconfig. So, I am proposing to add these options.

API Proposal

namespace System.Xml;

public class XmlTextWriter : XmlWriter
{
        public bool WriteSpaceBeforeSelfClose { get; set; } = true;
        public bool WriteSpaceAfterLastAttribute { get; set; }
        public bool WriteSpacesAroundAttributeEq { get; set; }
        public bool InsertFinalNewline { get; set; }

       /// Other implementation stays the same, other than 
       /// - WriteEndStartTag for WriterSpaceBeforeSelfClose, and WriteSpaceAfterLastAttribute. 
       /// - WriteStartAttribute changes to support WriteSpacesAroundAttributeEq
       /// - WriteEndDocument changes to support adding a final new line.
}

API Usage

var writer = new XmlTextWriter(...);

writer.WriteSpaceBeforeSelfClose = false;

writer.WriteStartElement("foo");
writer.WriteEndElement(); // writes <foo/>

// etc..

Alternative Designs

These options could also be moved to XmlWriterSettings, and supported in other writers. However, this is the minimal change required to support formatting XML with spacing options in the basic XmlTextWriter.

Risks

It is a public API change, but it's only additive. Would need to ensure default values keep format unchanged.

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-xml See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation Recently we discovered that `.editorconfig` files aren't respected for `.xml` and `.csproj` files in Visual Studio and `dotnet format`. It looks like this is a Rider feature. However, across our team the formatting of the project files becomes an issue because the default Visual Studio formatting is different from the `.editorconfig` that Rider is able to format to. So, in lieu of available tooling, I looked to build a tool. However, the .NET XML library doesn't support the options for the `XmlTextWriter`, and there doesn't seem to be a viable alternative to writing XML files. The options I'm proposing to support are: ``` insert_final_newline - inserts a newline after the closing XML document tag space_inside_empty_tag - ??? space_before_self_closing - inserts a space before the /> in a self closing tag (XmlTextWriter default) space_after_last_attribute - inserts a space after the last attribute in a tag spaces_around_eq_in_attribute - inserts spaces around the '=' character in attributes ``` Basically, there's not an easy way to configure the `XmlTextWriter` to output with the spacing required for the options in our `.editorconfig`. So, I am proposing to add these options. ### API Proposal ```csharp namespace System.Xml; public class XmlTextWriter : XmlWriter { public bool WriteSpaceBeforeSelfClose { get; set; } = true; public bool WriteSpaceAfterLastAttribute { get; set; } public bool WriteSpacesAroundAttributeEq { get; set; } public bool InsertFinalNewline { get; set; } /// Other implementation stays the same, other than /// - WriteEndStartTag for WriterSpaceBeforeSelfClose, and WriteSpaceAfterLastAttribute. /// - WriteStartAttribute changes to support WriteSpacesAroundAttributeEq /// - WriteEndDocument changes to support adding a final new line. } ``` ### API Usage ```csharp var writer = new XmlTextWriter(...); writer.WriteSpaceBeforeSelfClose = false; writer.WriteStartElement("foo"); writer.WriteEndElement(); // writes // etc.. ``` ### Alternative Designs These options could also be moved to `XmlWriterSettings`, and supported in other writers. However, this is the minimal change required to support formatting XML with spacing options in the basic `XmlTextWriter`. ### Risks It is a public API change, but it's only additive. Would need to ensure default values keep format unchanged.
Author: lor1mp
Assignees: -
Labels: `api-suggestion`, `area-System.Xml`
Milestone: -
eiriktsarpalis commented 1 year ago

cc @krwq

lor1mp commented 1 year ago

Also -- I have the changes in a branch for this method. However, I'm open to whatever path forward, I can make the changes.