Skip to content

Commit

Permalink
Replace EOL when is not CRLF with LF
Browse files Browse the repository at this point in the history
  • Loading branch information
maca88 committed Nov 4, 2017
1 parent 51f5781 commit adec556
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ internal static NamespaceDeclarationSyntax ReplaceWithMembers(this NamespaceDecl

// TODO: take the original directive whitespace
internal static TypeDeclarationSyntax RemoveMembersKeepDirectives(this TypeDeclarationSyntax node,
Predicate<MemberDeclarationSyntax> predicate, SyntaxTrivia directiveLeadingWhitespace)
Predicate<MemberDeclarationSyntax> predicate, SyntaxTrivia directiveLeadingWhitespace, SyntaxTrivia eol)
{
var annotations = new List<string>();
foreach (var memberSpan in node.Members.Where(o => predicate(o)).Select(o => o.Span))
Expand All @@ -576,14 +576,14 @@ internal static TypeDeclarationSyntax RemoveMembersKeepDirectives(this TypeDecla
}
foreach (var annotation in annotations)
{
node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace);
node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace, eol);
}
return node;
}

// TODO: take the original directive whitespace
internal static NamespaceDeclarationSyntax RemoveMembersKeepDirectives(this NamespaceDeclarationSyntax node,
Predicate<MemberDeclarationSyntax> predicate, SyntaxTrivia directiveLeadingWhitespace)
Predicate<MemberDeclarationSyntax> predicate, SyntaxTrivia directiveLeadingWhitespace, SyntaxTrivia eol)
{
var annotations = new List<string>();
foreach (var memberSpan in node.Members.Where(o => predicate(o)).Select(o => o.Span))
Expand All @@ -595,13 +595,13 @@ internal static NamespaceDeclarationSyntax RemoveMembersKeepDirectives(this Name
}
foreach (var annotation in annotations)
{
node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace);
node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace, eol);
}
return node;
}

// TODO: take the original directive whitespace
internal static T RemoveNodeKeepDirectives<T>(this T node, string annotation, SyntaxTrivia directiveLeadingWhitespace)
internal static T RemoveNodeKeepDirectives<T>(this T node, string annotation, SyntaxTrivia directiveLeadingWhitespace, SyntaxTrivia eol)
where T : SyntaxNode
{
var toRemoveNode = node.GetAnnotatedNodes(annotation).First();
Expand All @@ -621,7 +621,8 @@ internal static T RemoveNodeKeepDirectives<T>(this T node, string annotation, Sy
{
return null; // TODO: we need to preserve or remove directives!
}
if (!EnvironmentHelper.IsWindows)
// Workaround: When EOL is not CRLF we need to replace all CRLF that may be added by RemoveNode with LF
if (eol.ToFullString() != CarriageReturnLineFeed.ToFullString())
{
node = (T)new UnixEndOfLineTriviaRewriter().Visit(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ private RootNamespaceTransformationResult TransformNamespace(INamespaceAnalyzati
{
if (transformResult.AnalyzationResult.Conversion == NamespaceConversion.Ignore)
{
rootNode = rootNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia);
rootNode = rootNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia,
transformResult.EndOfLineTrivia);
continue;
}

Expand All @@ -99,7 +100,7 @@ private RootNamespaceTransformationResult TransformNamespace(INamespaceAnalyzati
if (transformType.AnalyzationResult.Conversion == TypeConversion.Ignore)
{
// We need to add a whitespace trivia to kept directives as they will not have any leading whitespace
newNode = newNode.RemoveNodeKeepDirectives(transformType.Annotation, memberWhitespace);
newNode = newNode.RemoveNodeKeepDirectives(transformType.Annotation, memberWhitespace, transformResult.EndOfLineTrivia);
continue;
}

Expand All @@ -112,7 +113,8 @@ private RootNamespaceTransformationResult TransformNamespace(INamespaceAnalyzati
}

// We need to remove all other members that are not namespaces or types
newNode = newNode.RemoveMembersKeepDirectives(o => !(o is NamespaceDeclarationSyntax || o is TypeDeclarationSyntax), memberWhitespace);
newNode = newNode.RemoveMembersKeepDirectives(o => !(o is NamespaceDeclarationSyntax || o is TypeDeclarationSyntax), memberWhitespace,
transformResult.EndOfLineTrivia);

// TODO: adding namespaces can introduce name conflicts, we should avoid it
if (!transformResult.TaskConflict && !rootResult.Node.HasUsing("System.Threading.Tasks"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ private RootTypeTransformationResult TransformType(ITypeAnalyzationResult rootTy
!typeResult.GetSelfAndDescendantsTypes().SelectMany(o => o.MethodsAndAccessors).Any(o => o.Missing)
))
{
rootTypeNode = rootTypeNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia);
rootTypeNode = rootTypeNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia,
transformResult.EndOfLineTrivia);
continue;
}

Expand All @@ -237,7 +238,8 @@ private RootTypeTransformationResult TransformType(ITypeAnalyzationResult rootTy

// We need to remove all other members that are not methods, properties or types
newTypeNode = newTypeNode.RemoveMembersKeepDirectives(o =>
!(o is BaseMethodDeclarationSyntax || o is TypeDeclarationSyntax || o is PropertyDeclarationSyntax), memberWhitespace);
!(o is BaseMethodDeclarationSyntax || o is TypeDeclarationSyntax || o is PropertyDeclarationSyntax), memberWhitespace,
transformResult.EndOfLineTrivia);
newTypeNode = TransformMethodsAndProperties(newTypeNode, transformResult, namespaceMetadata, memberWhitespace, onlyMissingMembers);
newTypeNode = RunTypeTransformers(newTypeNode, transformResult, namespaceMetadata, onlyMissingMembers);
transformResult.Transformed = newTypeNode;
Expand Down Expand Up @@ -316,7 +318,8 @@ private TypeDeclarationSyntax TransformSpecialMethod(FunctionTransformationResul
if (methodTransform.AnalyzationResult.Conversion == MethodConversion.Ignore || onlyMissingMembers)
{
// We need to add a whitespace trivia to keep directives as they will not have any leading whitespace
newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace);
newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace,
transformResult.EndOfLineTrivia);
return newTypeNode;
}
var methodNode = newTypeNode.GetAnnotatedNodes(methodTransform.Annotation)
Expand All @@ -339,7 +342,7 @@ private TypeDeclarationSyntax TransformMethod(MethodTransformationResult methodT
if (methodTransform.AnalyzationResult.Conversion == MethodConversion.Ignore || (onlyMissingMembers && !methodTransform.AnalyzationResult.Missing))
{
// We need to add a whitespace trivia to keep directives as they will not have any leading whitespace
newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace);
newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace, transformResult.EndOfLineTrivia);
return newTypeNode;
}
var methodNode = newTypeNode.GetAnnotatedNodes(methodTransform.Annotation)
Expand Down Expand Up @@ -431,7 +434,8 @@ private TypeDeclarationSyntax TransformProperty(PropertyTransformationResult pro
if (onlyMissingMembers || propertyTransform.AnalyzationResult.Conversion == PropertyConversion.Ignore)
{
// We need to add a whitespace trivia to keep directives as they will not have any leading whitespace
newTypeNode = newTypeNode.RemoveNodeKeepDirectives(propertyTransform.Annotation, memberWhitespace);
newTypeNode = newTypeNode.RemoveNodeKeepDirectives(propertyTransform.Annotation, memberWhitespace,
transformResult.EndOfLineTrivia);
}
return newTypeNode;
}
Expand Down
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
version: 1.0.{build}
image: Visual Studio 2017
init:
- git config --global core.autocrlf false
build_script:
- cmd: PowerShell -Version 2.0 .\build.ps1
test: off

0 comments on commit adec556

Please sign in to comment.