Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/XamlStyler.Console/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,8 @@ public sealed partial class CommandLineOptions

[Option("comment-spaces", HelpText = "Override: comment spaces.")]
public int? CommentSpaces { get; set; }

[Option("end-of-line", HelpText = "Override: end-of-line.")]
public EndOfLine? EndOfLine { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/XamlStyler.Console/XamlStylerConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ private void ApplyOptionOverrides(CommandLineOptions options, IStylerOptions sty
{
stylerOptions.CommentSpaces = options.CommentSpaces.Value;
}

if (options.EndOfLine != null)
{
stylerOptions.EndOfLine = options.EndOfLine.Value;
}
}

public void Process(ProcessType processType)
Expand Down
7 changes: 5 additions & 2 deletions src/XamlStyler/DocumentProcessors/CDATADocumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
using System.Text;
using System.Xml;
using Xavalon.XamlStyler.Extensions;
using Xavalon.XamlStyler.Options;
using Xavalon.XamlStyler.Parser;
using Xavalon.XamlStyler.Services;

namespace Xavalon.XamlStyler.DocumentProcessors
{
internal class CDATADocumentProcessor : IDocumentProcessor
{
private readonly IStylerOptions options;
private readonly IndentService indentService;

public CDATADocumentProcessor(IndentService indentService)
public CDATADocumentProcessor(IStylerOptions options, IndentService indentService)
{
this.options = options;
this.indentService = indentService;
}

Expand All @@ -40,7 +43,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
// http://www.w3.org/TR/2008/REC-xml-20081126/#sec-line-ends
// Change them back into the environment newline characters.
output.Append("<![CDATA[")
.Append(xmlReader.Value.Replace("\n", Environment.NewLine))
.Append(xmlReader.Value.Replace("\n", this.options.NewLine))
.Append("]]>");
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/XamlStyler/DocumentProcessors/CommentDocumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon

if ((output.Length > 0) && !output.IsNewLine())
{
output.Append(Environment.NewLine);
output.Append(this.options.NewLine);
}

if (content.Contains("<") && content.Contains(">"))
Expand All @@ -41,7 +41,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon

if (content.Contains("\n"))
{
output.Append(String.Join(Environment.NewLine, content.GetLines().Select(_ => _.TrimEnd(' '))));
output.Append(String.Join(this.options.NewLine, content.GetLines().Select(_ => _.TrimEnd(' '))));

if (content.TrimEnd(' ').EndsWith("\n", StringComparison.Ordinal))
{
Expand All @@ -66,10 +66,10 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
var contentIndentString = this.indentService.GetIndentString(xmlReader.Depth + 1);
foreach (var line in content.Trim().GetLines())
{
output.Append(Environment.NewLine).Append(contentIndentString).Append(line.Trim());
output.Append(this.options.NewLine).Append(contentIndentString).Append(line.Trim());
}

output.Append(Environment.NewLine).Append(currentIndentString).Append("-->");
output.Append(this.options.NewLine).Append(currentIndentString).Append("-->");
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions src/XamlStyler/DocumentProcessors/ElementDocumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
}
else
{
output.Append(Environment.NewLine).Append(currentIndentString);
output.Append(this.options.NewLine).Append(currentIndentString);
}
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
if (putEndingBracketOnNewLine)
{
// Indent ending bracket just like an attribute.
output.Append(Environment.NewLine).Append(attributeIndetationString);
output.Append(this.options.NewLine).Append(attributeIndetationString);
}

if (isEmptyElement)
Expand Down Expand Up @@ -302,7 +302,7 @@ private void ProcessAttributes(
}
else
{
output.Append(Environment.NewLine)
output.Append(this.options.NewLine)
.Append(this.indentService.Normalize(attributeIndentationString + attributeLines[i].Trim()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Process(

if (!output.IsNewLine())
{
output.Append(Environment.NewLine);
output.Append(this.options.NewLine);
}

output.Append(currentIndentString).Append("</").Append(xmlReader.Name).Append(">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
using System.Text;
using System.Xml;
using Xavalon.XamlStyler.Extensions;
using Xavalon.XamlStyler.Options;
using Xavalon.XamlStyler.Parser;
using Xavalon.XamlStyler.Services;

namespace Xavalon.XamlStyler.DocumentProcessors
{
internal class ProcessInstructionDocumentProcessor : IDocumentProcessor
{
private readonly IStylerOptions options;
private readonly IndentService indentService;

public ProcessInstructionDocumentProcessor(IndentService indentService)
public ProcessInstructionDocumentProcessor(IStylerOptions options, IndentService indentService)
{
this.options = options;
this.indentService = indentService;
}

Expand All @@ -26,7 +29,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon

if (!output.IsNewLine())
{
output.Append(Environment.NewLine);
output.Append(this.options.NewLine);
}

output.Append($"{currentIndentString}<?{xmlReader.Name} {xmlReader.Value}?>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
using System;
using System.Text;
using System.Xml;
using Xavalon.XamlStyler.Options;

namespace Xavalon.XamlStyler.DocumentProcessors
{
internal class SignificantWhitespaceDocumentProcessor : IDocumentProcessor
{
private readonly IStylerOptions options;

public SignificantWhitespaceDocumentProcessor(IStylerOptions options)
{
this.options = options;
}

public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessContext elementProcessContext)
{
// All newlines are returned by XmlReader as '\n' due to requirements in the XML Specification.
// http://www.w3.org/TR/2008/REC-xml-20081126/#sec-line-ends
// Change them back into the environment newline characters.
output.Append(xmlReader.Value.Replace("\n", Environment.NewLine));
output.Append(xmlReader.Value.Replace("\n", this.options.NewLine));
}
}
}
9 changes: 6 additions & 3 deletions src/XamlStyler/DocumentProcessors/TextDocumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
using System.Text;
using System.Xml;
using Xavalon.XamlStyler.Extensions;
using Xavalon.XamlStyler.Options;
using Xavalon.XamlStyler.Parser;
using Xavalon.XamlStyler.Services;

namespace Xavalon.XamlStyler.DocumentProcessors
{
internal class TextDocumentProcessor : IDocumentProcessor
{
private readonly IStylerOptions options;
private readonly IndentService indentService;

public TextDocumentProcessor(IndentService indentService)
public TextDocumentProcessor(IStylerOptions options, IndentService indentService)
{
this.options = options;
this.indentService = indentService;
}

Expand All @@ -27,7 +30,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
var xmlEncodedContent = xmlReader.Value.ToXmlEncodedString(ignoreCarrier: true);
if (elementProcessContext.Current.IsPreservingSpace)
{
output.Append(xmlEncodedContent.Replace("\n", Environment.NewLine));
output.Append(xmlEncodedContent.Replace("\n", this.options.NewLine));
}
else
{
Expand All @@ -42,7 +45,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
var trimmedLine = line.Trim();
if (trimmedLine.Length > 0)
{
output.Append(Environment.NewLine).Append(currentIndentString).Append(trimmedLine);
output.Append(this.options.NewLine).Append(currentIndentString).Append(trimmedLine);
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/XamlStyler/DocumentProcessors/WhitespaceDocumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
using System.Linq;
using System.Text;
using System.Xml;
using Xavalon.XamlStyler.Options;

namespace Xavalon.XamlStyler.DocumentProcessors
{
internal class WhitespaceDocumentProcessor : IDocumentProcessor
{
private readonly IStylerOptions options;

public WhitespaceDocumentProcessor(IStylerOptions options)
{
this.options = options;
}

public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessContext elementProcessContext)
{
var hasNewline = xmlReader.Value.Contains('\n');
Expand All @@ -27,7 +35,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
.Replace(" ", String.Empty)
.Replace("\t", String.Empty)
.Replace("\r", String.Empty)
.Replace("\n", Environment.NewLine));
.Replace("\n", this.options.NewLine));
}
else
{
Expand All @@ -38,7 +46,7 @@ public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessCon
// B
// </Run>
// </TextBlock>
output.Append(xmlReader.Value.Replace("\n", Environment.NewLine));
output.Append(xmlReader.Value.Replace("\n", this.options.NewLine));
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/XamlStyler/Options/EndOfLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// (c) Xavalon. All rights reserved.

namespace Xavalon.XamlStyler.Options
{
/// <summary>
/// Type of end of line
/// </summary>
public enum EndOfLine
{
/// <summary>
/// Uses <see cref="System.Environment.NewLine"/>.
/// </summary>
Default,
/// <summary>
/// The line feed `\n` character.
/// </summary>
LF,
/// <summary>
/// The carriage return line feed `\r\n` sequence.
/// </summary>
CRLF,
/// <summary>
/// The carriage return `\r` character.
/// </summary>
CR,
}
}
4 changes: 4 additions & 0 deletions src/XamlStyler/Options/IStylerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public interface IStylerOptions

int CommentSpaces { get; set; }

string NewLine { get; }

EndOfLine EndOfLine { get; set; }

#endregion Misc

#region Configuration
Expand Down
24 changes: 24 additions & 0 deletions src/XamlStyler/Options/StylerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,30 @@ public string ConfigPath
[Browsable(false)]
public bool SuppressProcessing { get; set; }

private EndOfLine endOfLine;

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
[JsonProperty(nameof(EndOfLine), DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(EndOfLine.Default)]
public EndOfLine EndOfLine
{
get { return endOfLine; }
set
{
endOfLine = value;
NewLine = value == EndOfLine.CRLF ? "\r\n"
: value == EndOfLine.LF ? "\n"
: value == EndOfLine.CR ? "\r"
: Environment.NewLine;
}
}

[JsonIgnore]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public string NewLine { get; private set; } = Environment.NewLine;

/// <summary>
/// Creates a clone from the current instance.
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions src/XamlStyler/StylerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ private void ApplyOptions(IList<string> ignoredNamespacesPrefixes, bool ignoreDe
// { XmlNodeType.None, null },
{ XmlNodeType.Element, new ElementDocumentProcessor(options, xamlLanguageOptions, attributeInfoFactory, attributeInfoFormatter, indentService, xmlEscapingService) },
// { XmlNodeType.Attribute, null },
{ XmlNodeType.Text, new TextDocumentProcessor(indentService) },
{ XmlNodeType.CDATA, new CDATADocumentProcessor(indentService) },
{ XmlNodeType.Text, new TextDocumentProcessor(this.options, indentService) },
{ XmlNodeType.CDATA, new CDATADocumentProcessor(this.options, indentService) },
// { XmlNodeType.EntityReference, null },
// { XmlNodeType.Entity, null },
{ XmlNodeType.ProcessingInstruction, new ProcessInstructionDocumentProcessor(indentService) },
{ XmlNodeType.ProcessingInstruction, new ProcessInstructionDocumentProcessor(this.options, indentService) },
{ XmlNodeType.Comment, new CommentDocumentProcessor(options, indentService) },
// { XmlNodeType.Document, null },
// { XmlNodeType.DocumentType, null },
// { XmlNodeType.DocumentFragment, null },
// { XmlNodeType.Notation, null },
{ XmlNodeType.Whitespace, new WhitespaceDocumentProcessor() },
{ XmlNodeType.SignificantWhitespace, new SignificantWhitespaceDocumentProcessor() },
{ XmlNodeType.Whitespace, new WhitespaceDocumentProcessor(this.options) },
{ XmlNodeType.SignificantWhitespace, new SignificantWhitespaceDocumentProcessor(this.options) },
{ XmlNodeType.EndElement, new EndElementDocumentProcessor(options,indentService) },
// { XmlNodeType.EndEntity, null },
// ignoring xml declarations for Xamarin support
Expand Down