Skip to content

Commit

Permalink
Fixing more issues
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Nov 11, 2024
1 parent 6264407 commit 968cef9
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 191 deletions.
82 changes: 44 additions & 38 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ CancellationToken cancellationToken
cancellationToken
);

var originalDirectoryOrFile = commandLineOptions.OriginalDirectoryOrFilePaths[
x
].Replace("\\", "/");
var originalDirectoryOrFile = commandLineOptions
.OriginalDirectoryOrFilePaths[x]
.Replace("\\", "/");

var formattingCache = await FormattingCacheFactory.InitializeAsync(
commandLineOptions,
Expand Down Expand Up @@ -254,8 +254,12 @@ await FormatPhysicalFile(
return 1;
}

var tasks = fileSystem.Directory
.EnumerateFiles(directoryOrFilePath, "*.*", SearchOption.AllDirectories)
var tasks = fileSystem
.Directory.EnumerateFiles(
directoryOrFilePath,
"*.*",
SearchOption.AllDirectories
)
.Select(o =>
{
var normalizedPath = o.Replace("\\", "/");
Expand Down Expand Up @@ -423,45 +427,47 @@ CancellationToken cancellationToken
fileIssueLogger.WriteError(codeFormattingResult.FailureMessage);
return;
}

// TODO #819 review this https://github.com/belav/csharpier-repos/pull/67
// TODO #819 what about allowing lines between elements? new issue?

if (!commandLineOptions.Fast && fileToFormatInfo.Path.EndsWithIgnoreCase(".cs"))
{
// TODO #819 the thing above should do this if we are using the csharp formatter
var sourceCodeKind = Path.GetExtension(fileToFormatInfo.Path).EqualsIgnoreCase(".csx")
? SourceCodeKind.Script
: SourceCodeKind.Regular;

var syntaxNodeComparer = new SyntaxNodeComparer(
fileToFormatInfo.FileContents,
codeFormattingResult.Code,
codeFormattingResult.ReorderedModifiers,
codeFormattingResult.ReorderedUsingsWithDisabledText,
sourceCodeKind,
cancellationToken
);

try
if (!commandLineOptions.Fast)
{
if (printerOptions.Formatter is Formatter.CSharp or Formatter.CSharpScript)
{
var failure = await syntaxNodeComparer.CompareSourceAsync(cancellationToken);
if (!string.IsNullOrEmpty(failure))
var sourceCodeKind =
printerOptions.Formatter is Formatter.CSharpScript
? SourceCodeKind.Script
: SourceCodeKind.Regular;

var syntaxNodeComparer = new SyntaxNodeComparer(
fileToFormatInfo.FileContents,
codeFormattingResult.Code,
codeFormattingResult.ReorderedModifiers,
codeFormattingResult.ReorderedUsingsWithDisabledText,
sourceCodeKind,
cancellationToken
);

try
{
var failure = await syntaxNodeComparer.CompareSourceAsync(cancellationToken);
if (!string.IsNullOrEmpty(failure))
{
Interlocked.Increment(
ref commandLineFormatterResult.FailedSyntaxTreeValidation
);
fileIssueLogger.WriteError($"Failed syntax tree validation.\n{failure}");
}
}
catch (Exception ex)
{
Interlocked.Increment(
ref commandLineFormatterResult.FailedSyntaxTreeValidation
ref commandLineFormatterResult.ExceptionsValidatingSource
);
fileIssueLogger.WriteError($"Failed syntax tree validation.\n{failure}");
}
}
catch (Exception ex)
{
Interlocked.Increment(ref commandLineFormatterResult.ExceptionsValidatingSource);

fileIssueLogger.WriteError(
"Failed with exception during syntax tree validation.",
ex
);
fileIssueLogger.WriteError(
"Failed with exception during syntax tree validation.",
ex
);
}
}
}

Expand Down
14 changes: 5 additions & 9 deletions Src/CSharpier.Cli/Options/ConfigurationFileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ namespace CSharpier.Cli.Options;
internal class ConfigurationFileOptions
{
public int PrintWidth { get; init; } = 100;

// TODO #819 how do we get this to default to 2 for xml?
public int TabWidth { get; init; } = 4;
public int? TabWidth { get; init; }
public bool UseTabs { get; init; }

[JsonConverter(typeof(CaseInsensitiveEnumConverter<EndOfLine>))]
Expand Down Expand Up @@ -42,14 +40,12 @@ out var parsedFormatter
};
}

// TODO #819 we need a default one for xml as well
if (filePath.EndsWith(".cs") || filePath.EndsWith(".csx"))
var formatter = PrinterOptions.GetFormatter(filePath);
if (formatter != Formatter.Unknown)
{
return new PrinterOptions(
filePath.EndsWith(".cs") ? Formatter.CSharp : Formatter.CSharpScript
)
return new PrinterOptions(formatter)
{
IndentSize = this.TabWidth,
IndentSize = this.TabWidth ?? (formatter == Formatter.XML ? 2 : 4),
UseTabs = this.UseTabs,
Width = this.PrintWidth,
EndOfLine = this.EndOfLine,
Expand Down
24 changes: 2 additions & 22 deletions Src/CSharpier.Cli/Options/OptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,8 @@ public static async Task<OptionsProvider> Create(
return resolvedEditorConfig.ConvertToPrinterOptions(filePath);
}

if (filePath.EndsWith(".cs"))
{
return new PrinterOptions(Formatter.CSharp);
}

if (filePath.EndsWith(".csx"))
{
return new PrinterOptions(Formatter.CSharpScript);
}

if (
filePath.EndsWith(".csproj")
|| filePath.EndsWith(".props")
|| filePath.EndsWith(".targets")
|| filePath.EndsWith(".xml")
|| filePath.EndsWith(".config")
)
{
return new PrinterOptions(Formatter.XML);
}

return null;
var formatter = PrinterOptions.GetFormatter(filePath);
return formatter != Formatter.Unknown ? new PrinterOptions(formatter) : null;
}

public bool IsIgnored(string actualFilePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- ====================================================================== -->
<!-- This XML is a description of the Common Language Infrastructure (CLI) library. -->
<!-- This file is a normative part of Partition IV of the following standards: ISO/IEC 23271 and ECMA 335 -->
<!-- ====================================================================== -->
<!DOCTYPE Libraries SYSTEM "CLILibraryTypes.dtd">
<root></root>
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/Samples/AllInOne.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Doc.HardLineIfNoPreviousLine

2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/Samples/Scratch.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Doc.HardLineIfNoPreviousLine

9 changes: 9 additions & 0 deletions Src/CSharpier/Formatters/Xml/XmlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ internal static CodeFormatterResult Format(string xml, PrinterOptions printerOpt
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);

// TODO #819 Error ./efcore/eng/sdl-tsa-vars.config - Threw exception while formatting.
// Data at the root level is invalid. Line 1, position 1.
// not all configs are xml, should we try to detect that?

// TODO #819 review this https://github.com/belav/csharpier-repos/pull/67

/* TODO #819 all of these things
XmlDocumentType
https://github.com/belav/csharpier-repos/blob/main/mono/mcs/tools/mdoc/Test/TestEcmaDocs.xml
still getting this
warning: in the working copy of 'runtime/src/tests/Common/mergedrunner.targets', LF will be replaced by CRLF the next time Git touches it
Expand Down
12 changes: 0 additions & 12 deletions Src/CSharpier/Formatters/Xml/XmlNodePrinters/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ public static Doc Print(XmlElement node, PrintingContext context)
node.Attributes.Count == 1
&& !node.Attributes[0].Value.Contains('\n')
&& (node.ChildNodes.Cast<XmlNode>().Any(o => o is XmlElement) || node.IsEmpty);
// node.type === "element" &&
// node.fullName === "script" &&
// node.attrs.length === 1 &&
// node.attrs[0].fullName === "src" &&
// node.children.length === 0;

// TODO #819 probably attribute per line if there are more than x attributes
// const shouldPrintAttributePerLine =
// options.singleAttributePerLine &&
// node.attrs.length > 1 &&
// !isVueSfcBlock(node, options);
// const attributeLine = shouldPrintAttributePerLine ? hardline : line;
var attributeLine = Doc.Line;

var parts = new List<Doc>
Expand Down
27 changes: 17 additions & 10 deletions Src/CSharpier/Formatters/Xml/XmlNodePrinters/Node.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Xml;
using System.Xml.Linq;
using CSharpier.SyntaxPrinter;

namespace CSharpier.Formatters.Xml.XmlNodePrinters;
Expand All @@ -25,6 +26,11 @@ internal static Doc Print(XmlNode xmlNode, PrintingContext context)
return xmlDeclaration.OuterXml;
}

if (xmlNode is XmlDocumentType xmlDocumentType)
{
return xmlDocumentType.OuterXml.Replace("[]", string.Empty);
}

if (xmlNode is XmlElement xmlElement)
{
return Element.Print(xmlElement, context);
Expand All @@ -35,7 +41,7 @@ internal static Doc Print(XmlNode xmlNode, PrintingContext context)
List<Doc> doc =
[
Tag.PrintOpeningTagPrefix(xmlText),
.. Utils.GetTextValueParts(xmlText),
.. GetTextValueParts(xmlText),
Tag.PrintClosingTagSuffix(xmlText),
];

Expand All @@ -46,15 +52,6 @@ .. Utils.GetTextValueParts(xmlText),
}

return Doc.Concat(doc);

// var printed = CleanDoc(doc);
//
//
// // if (Array.isArray(printed)) {
// // return fill(printed);
// // }
//
// return printed;
}

if (xmlNode is XmlComment)
Expand All @@ -69,4 +66,14 @@ .. Utils.GetTextValueParts(xmlText),

throw new Exception("Need to handle + " + xmlNode);
}

private static IEnumerable<Doc> GetTextValueParts(XmlText xmlText)
{
if (xmlText.Value is null)
{
yield break;
}

yield return new XElement("EncodeText", xmlText.Value).LastNode!.ToString();
}
}
98 changes: 0 additions & 98 deletions Src/CSharpier/Formatters/Xml/XmlNodePrinters/Utils.cs

This file was deleted.

Loading

0 comments on commit 968cef9

Please sign in to comment.