Skip to content

Commit

Permalink
Fixes #34 - Markup extension formatting does work as expected when ne…
Browse files Browse the repository at this point in the history
…sted
  • Loading branch information
Dave Grochocki committed Dec 13, 2016
1 parent b7c776d commit 1e09904
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ namespace Xavalon.XamlStyler.Core.MarkupExtensions.Formatter
public class MarkupExtensionFormatter
{
private readonly IList<string> singleLineTypes;
private readonly MarkupExtensionFormatterBase singleLineFormatter = new SingleLineMarkupExtensionFormatter();
private readonly MarkupExtensionFormatterBase multiLineFormatter = new MultiLineMarkupExtensionFormatter();
private readonly MarkupExtensionFormatterBase singleLineFormatter;
private readonly MarkupExtensionFormatterBase multiLineFormatter;

public MarkupExtensionFormatter(IList<string> singleLineTypes)
{
this.singleLineTypes = singleLineTypes;

this.singleLineFormatter = new SingleLineMarkupExtensionFormatter(this);
this.multiLineFormatter = new MultiLineMarkupExtensionFormatter(this);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ namespace Xavalon.XamlStyler.Core.MarkupExtensions.Formatter
{
internal abstract class MarkupExtensionFormatterBase
{
protected readonly MarkupExtensionFormatter markupExtensionFormatter;

protected MarkupExtensionFormatterBase(MarkupExtensionFormatter markupExtensionFormatter)
{
this.markupExtensionFormatter = markupExtensionFormatter;
}

public IEnumerable<string> Format(MarkupExtension markupExtension)
{
return markupExtension.Arguments.Any()
Expand All @@ -29,7 +36,16 @@ protected IEnumerable<string> Format(Argument argument)

if (type == typeof(PositionalArgument))
{
return this.Format((PositionalArgument)argument);
var positionalArgument = (PositionalArgument)argument;

if (positionalArgument.Value.GetType() == typeof(MarkupExtension))
{
return this.markupExtensionFormatter.Format((MarkupExtension)positionalArgument.Value);
}
else
{
return this.Format(positionalArgument);
}
}

throw new ArgumentException($"Unhandled type {type.FullName}", nameof(argument));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Xavalon.XamlStyler.Core.MarkupExtensions.Formatter
{
internal class MultiLineMarkupExtensionFormatter : MarkupExtensionFormatterBase
{
internal MultiLineMarkupExtensionFormatter(MarkupExtensionFormatter markupExtensionFormatter)
: base(markupExtensionFormatter)
{
}

protected override IEnumerable<string> Format(Argument[] arguments)
{
var list = new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Xavalon.XamlStyler.Core.MarkupExtensions.Formatter
{
internal class SingleLineMarkupExtensionFormatter : MarkupExtensionFormatterBase
{
internal SingleLineMarkupExtensionFormatter(MarkupExtensionFormatter markupExtensionFormatter)
: base(markupExtensionFormatter)
{
}

protected override IEnumerable<string> Format(Argument[] arguments)
{
StringBuilder stringBuilder = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion XamlStyler.Core/MarkupExtensions/Parser/MarkupExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static IEnumerable<Argument> GetArguments(IEnumerable<ParseTreeNode> nod
else
{
// Unwrap argument.
foreach (var markupExtensionArgument in GetArguments(node.ChildNodes))
foreach (var markupExtensionArgument in MarkupExtension.GetArguments(node.ChildNodes))
{
yield return markupExtensionArgument;
}
Expand Down
14 changes: 14 additions & 0 deletions XamlStyler.UnitTests/FileHandlingIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ public void TestMarkupExtensionHandling(bool indentWithTabs, int tabSize)
this.DoTestCase(stylerOptions, $"{tabSize}_{(indentWithTabs ? "tabs" : "spaces")}");
}

[Test]
public void TestNestedCustomMarkupExtensionsWithBindings()
{
var stylerOptions = new StylerOptions(
config: this.GetConfiguration(@"TestConfigurations\LegacyTestSettings.json"))
{
KeepFirstAttributeOnSameLine = false,
AttributesTolerance = 1,
NoNewLineMarkupExtensions = "x:Bind, Binding"
};

this.DoTest(stylerOptions);
}

[Test]
public void TestMarkupWithAttributeNotOnFirstLine()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Window
x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
Title="MainWindow">
<Grid>
<Button
Content="Click Me"
Foreground="Black"
IsEnabled="{LogicalAnd {Binding IsWritable},
{Binding ElementName=CheckBox, Path=IsChecked},
{CustomMarkupExtension ElementName=CheckBox,
Path=IsChecked}}" />
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Window
x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
Title="MainWindow">
<Grid>
<Button
Content="Click Me"
Foreground="Black"
IsEnabled="{LogicalAnd {Binding IsWritable}, {Binding ElementName=CheckBox, Path=IsChecked}, {CustomMarkupExtension ElementName=CheckBox, Path=IsChecked}}" />
</Grid>
</Window>
6 changes: 6 additions & 0 deletions XamlStyler.UnitTests/XamlStyler.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
<None Include="TestFiles\TestAttributeIndentationHandling_0.expected">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="TestFiles\TestNestedCustomMarkupExtensionsWithBindings.testxaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="TestFiles\TestNestedCustomMarkupExtensionsWithBindings.expected">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="TestFiles\TestCommentHandling_1.expected">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 1e09904

Please sign in to comment.