Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .run/Watch Playground.run.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Watch Playground" type="RunDotNetWatch" factoryName="RunDotNetWatch">
<option name="exePath" value="$PROJECT_DIR$/Src/CSharpier.Playground/bin/Debug/net10.0/CSharpier.Playground.exe" />
<configuration
default="false"
name="Watch Playground"
type="RunDotNetWatch"
factoryName="RunDotNetWatch"
>
<option
name="exePath"
value="$PROJECT_DIR$/Src/CSharpier.Playground/bin/Debug/net10.0/CSharpier.Playground.exe"
/>
<option name="programParameters" value="" />
<option name="projectFilePath" value="$PROJECT_DIR$/Src/CSharpier.Playground/CSharpier.Playground.csproj" />
<option
name="projectFilePath"
value="$PROJECT_DIR$/Src/CSharpier.Playground/CSharpier.Playground.csproj"
/>
<option name="projectTfm" value="net10.0" />
<option name="suppressHotReload" value="true" />
<option name="terminalMode" value="Auto" />
<option name="watchParameters" value="" />
<option name="workingDirectory" value="$PROJECT_DIR$/Src/CSharpier.Playground" />
<method v="2" />
</configuration>
</component>
</component>
8 changes: 8 additions & 0 deletions Src/CSharpier.Core/Xml/XNodePrinters/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ Doc PrintLineAfterChildren()
return Doc.IfBreak(Doc.SoftLine, "", attrGroupId);
}

if (
rawNode.Nodes.LastOrDefault() is { } node
&& Tag.PrintParentClosingTagStartWithContent(node, context)
)
{
return Doc.Null;
}

if (
rawNode.Attributes.Length == 0
&& rawNode.Nodes is [{ NodeType: XmlNodeType.Text }]
Expand Down
67 changes: 25 additions & 42 deletions Src/CSharpier.Core/Xml/XNodePrinters/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public static Doc PrintClosingTag(RawNode rawNode, PrintingContext context)
{
return Doc.Concat(
rawNode.IsEmpty ? Doc.Null : PrintClosingTagStart(rawNode, context),
PrintClosingTagEnd(rawNode, context)
rawNode.Nodes.LastOrDefault() is { } node
&& PrintParentClosingTagStartWithContent(node, context)
? Doc.Null
: Doc.Concat(
PrintClosingTagEndMarker(rawNode),
PrintClosingTagSuffix(rawNode, context)
)
);
}

Expand All @@ -63,14 +69,6 @@ public static Doc PrintClosingTagStartMarker(RawNode rawNode, PrintingContext co
return $"</{rawNode.Name}";
}

public static Doc PrintClosingTagEnd(RawNode rawNode, PrintingContext context)
{
return Doc.Concat(
PrintClosingTagEndMarker(rawNode),
PrintClosingTagSuffix(rawNode, context)
);
}

public static Doc PrintClosingTagEndMarker(RawNode rawNode)
{
return rawNode.IsEmpty ? "/>" : ">";
Expand All @@ -79,7 +77,10 @@ public static Doc PrintClosingTagEndMarker(RawNode rawNode)
public static Doc PrintClosingTagSuffix(RawNode rawNode, PrintingContext context)
{
return PrintParentClosingTagStartWithContent(rawNode, context)
? PrintClosingTagStartMarker(rawNode.Parent!, context)
? Doc.Concat(
PrintClosingTagStartMarker(rawNode.Parent!, context),
PrintClosingTagEndMarker(rawNode.Parent!)
)
: NeedsToBorrowNextOpeningTagStartMarker(rawNode)
? PrintOpeningTagStartMarker(rawNode.NextNode!, context)
: Doc.Null;
Expand All @@ -92,38 +93,23 @@ private static Doc PrintOpeningTagStartMarker(RawNode rawNode, PrintingContext c

private static bool NeedsToBorrowNextOpeningTagStartMarker(RawNode rawNode)
{
/*
* 123<p
* ^^
* >
*/
/* 123<p
^^
> */
return rawNode.NextNode is not null
&& !rawNode.NextNode.IsTextLike()
&& rawNode.IsTextLike()
&& rawNode.NodeType is XmlNodeType.Text and not XmlNodeType.CDATA
// && node.isTrailingSpaceSensitive
// prettier does something with removing end of line nodes and setting this value, I don't know
// that we have that functionality
// && !node.hasTrailingSpaces
;
&& rawNode.NodeType is XmlNodeType.Text and not XmlNodeType.CDATA;
}

private static bool PrintParentClosingTagStartWithContent(
public static bool PrintParentClosingTagStartWithContent(
RawNode rawNode,
PrintingContext context
)
{
/*
* <p>
* 123</p
* ^^^
* >
*
* 123</b
* ></a
* ^^^
* >
*/
/* <p>
123</p>
^^^^*/
// TODO #1790 we really want this last condition only if the indentation of the last line of the text value matches
// the indentation of the start element. Bleh.
/*
Expand Down Expand Up @@ -161,15 +147,12 @@ public static bool NeedsToBorrowParentOpeningTagEndMarker(
PrintingContext context
)
{
/*
* <p
* >123
* ^
*
* <p
* ><a
* ^
*/
/* <p
>123
^
<p
><a
^ */
return rawNode.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Strict
&& rawNode.PreviousNode is null
&& rawNode.NodeType is XmlNodeType.Text
Expand Down
18 changes: 9 additions & 9 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public async Task Format_Writes_File_With_Directory_Path()
var unformattedFilePath = "Unformatted.cs";
context.WhenAFileExists(unformattedFilePath, UnformattedClassContent);

Format(context);
await Format(context);

context.GetFileContent(unformattedFilePath).Should().Be(FormattedClassContent);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ public async Task Format_Writes_File_With_File_Path()
const string unformattedFilePath = "Unformatted.cs";
context.WhenAFileExists(unformattedFilePath, UnformattedClassContent);

Format(context, directoryOrFilePaths: "Unformatted.cs");
await Format(context, directoryOrFilePaths: "Unformatted.cs");

context.GetFileContent(unformattedFilePath).Should().Be(FormattedClassContent);
}
Expand All @@ -359,7 +359,7 @@ public async Task Format_Supports_Skip_Write()
const string unformattedFilePath = "Unformatted.cs";
context.WhenAFileExists(unformattedFilePath, UnformattedClassContent);

Format(context, skipWrite: true);
await Format(context, skipWrite: true);

context.GetFileContent(unformattedFilePath).Should().Be(UnformattedClassContent);
}
Expand Down Expand Up @@ -948,7 +948,7 @@ public async Task Should_Support_Config_Path()
var configPath = context.WhenAFileExists("config/.csharpierrc", "printWidth: 10");
context.WhenAFileExists("file1.cs", "var myVariable = someLongValue;");

Format(context, configPath: configPath);
await Format(context, configPath: configPath);

context.GetFileContent("file1.cs").Should().Be("var myVariable =\n someLongValue;\n");
}
Expand All @@ -966,7 +966,7 @@ public async Task Should_Support_Config_Path_With_Editor_Config()
);
var fileName = context.WhenAFileExists("file1.cs", "var myVariable = someLongValue;");

Format(context, configPath: configPath);
await Format(context, configPath: configPath);

context.GetFileContent(fileName).Should().Be("var myVariable =\n someLongValue;\n");
}
Expand All @@ -984,7 +984,7 @@ max_line_length 10
);
var fileName = context.WhenAFileExists("file1.cs", "var myVariable = someLongValue;");

Format(context);
await Format(context);

context.GetFileContent(fileName).Should().Be("var myVariable = someLongValue;\n");
}
Expand All @@ -1005,7 +1005,7 @@ public async Task Should_Respect_Ignored_Editor_Config(string ignoreFileName)
context.WhenAFileExists(ignoreFileName, ".editorconfig");
var fileName = context.WhenAFileExists("file1.cs", "var myVariable = someLongValue;");

Format(context);
await Format(context);

context.GetFileContent(fileName).Should().Be("var myVariable =\n someLongValue;\n");
}
Expand All @@ -1021,7 +1021,7 @@ public async Task Should_Work_With_Extensionless_File()
"""
);

Format(context, directoryOrFilePaths: "LICENSE");
await Format(context, directoryOrFilePaths: "LICENSE");
}

[Test]
Expand All @@ -1045,7 +1045,7 @@ string lineBreak

context.WhenAFileExists("Xml.xml", content.ToString());

Format(context);
await Format(context);

context.GetFileContent("Xml.xml").Should().Be(content.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Condition=" '$(MSBuildProjectName)' != 'Microsoft.TestCommon'
AND '$(MSBuildProjectName)' != 'System.Net.Http.Formatting.NetCore.Test'
AND '$(MSBuildProjectName)' != 'System.Net.Http.Formatting.NetStandard.Test' "
>v4.5.2</TargetFrameworkVersion
>
>v4.5.2</TargetFrameworkVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<ElementWithAttribute Attribute="AttributeValue________________">TextValue</ElementWithAttribute>
<ElementWithAttribute Attribute="AttributeValue_________________">TextValue</ElementWithAttribute>
<ElementWithAttribute Attribute="AttributeValue__________________"
>TextValue</ElementWithAttribute
>
>TextValue</ElementWithAttribute>
<LongElementWithAttribute
Attribute="AttributeValue_________________________________________________________"
>TextValue</LongElementWithAttribute>
<ElementWithEncodedText>&lt;b&gt;SomeText&lt;/b&gt;</ElementWithEncodedText>
</Root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<PropertyGroup>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))"
>true</IsTrimmable>
<IsAotCompatible
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))"
>true</IsAotCompatible>
</PropertyGroup>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
<HtmlTypeElement>Some text<a href="http://url.com">url.com</a> more text.</HtmlTypeElement>
<HtmlTypeElement
>Some long text to make things break <a href="http://url.com"
>url.com</a
> more text.</HtmlTypeElement
>
>url.com</a> more text.</HtmlTypeElement>
</Root>
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
>A <see cref="T:System.Int32" /> containing a value that reflects the sort order of
<paramref name="x" /> as compared to <paramref name="y" />. The following table defines the conditions
under which the returned value is a negative number, zero, or a positive
number.</para
>
number.</para>
</returns>
<para>Some text that ends with a space. </para>
<para
>Some loooooooooooooooooooooooooooooooooong text with this <br /> and a space after this. </para
>
>Some loooooooooooooooooooooooooooooooooong text with this <br /> and a space after this. </para>
<exception cref="T:System.InvalidOperationException"
>The current instance is read-only and a set operation was attempted. </exception
>
>The current instance is read-only and a set operation was attempted. </exception>
<example>
<code>public void MethodName() {
}
Expand Down
Loading