Skip to content

Commit e222939

Browse files
committed
Fixed encoding issue
The encoding for a Document can be null after modification. In that case just grab the encoding from the original document. This seems like a bug. The code is not explicitly changing the encoding hence it shouldn't be changing. Will followup with Roslyn.
1 parent 06bcea5 commit e222939

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ private async Task FormatAsync(Workspace workspace, IReadOnlyList<DocumentId> do
6767
solution = await RunSyntaxPass(solution, documentIds, cancellationToken);
6868
solution = await RunLocalSemanticPass(solution, documentIds, cancellationToken);
6969
solution = await RunGlobalSemanticPass(solution, documentIds, cancellationToken);
70-
70+
71+
await SaveChanges(solution, originalSolution, cancellationToken);
72+
73+
watch.Stop();
74+
Console.WriteLine("Total time {0}", watch.Elapsed);
75+
}
76+
77+
private async Task SaveChanges(Solution solution, Solution originalSolution, CancellationToken cancellationToken)
78+
{
7179
foreach (var projectChange in solution.GetChanges(originalSolution).GetProjectChanges())
7280
{
7381
foreach (var documentId in projectChange.GetChangedDocuments())
@@ -76,6 +84,17 @@ private async Task FormatAsync(Workspace workspace, IReadOnlyList<DocumentId> do
7684
var sourceText = await document.GetTextAsync(cancellationToken);
7785
using (var file = File.Open(document.FilePath, FileMode.Truncate, FileAccess.Write))
7886
{
87+
var encoding = sourceText.Encoding;
88+
89+
// TODO: It seems like a bug that Encoding could change but it is definitely
90+
// happening. Ex: ArrayBuilder.Enumerator.cs
91+
if (encoding == null)
92+
{
93+
var originalDocument = originalSolution.GetDocument(documentId);
94+
var originalSourceText = await originalDocument.GetTextAsync(cancellationToken);
95+
encoding = originalSourceText.Encoding;
96+
}
97+
7998
using (var writer = new StreamWriter(file, sourceText.Encoding))
8099
{
81100
sourceText.Write(writer, cancellationToken);
@@ -84,8 +103,6 @@ private async Task FormatAsync(Workspace workspace, IReadOnlyList<DocumentId> do
84103
}
85104
}
86105

87-
watch.Stop();
88-
Console.WriteLine("Total time {0}", watch.Elapsed);
89106
}
90107

91108
private async Task<bool> ShouldBeProcessedAsync(Document document)

src/Microsoft.DotNet.CodeFormatting/Rules/ExplicitThisRule.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyn
3838
var name = node.Name.Identifier.ValueText;
3939
if (node.Expression != null &&
4040
node.Expression.CSharpKind() == SyntaxKind.ThisExpression &&
41-
name.StartsWith("_", StringComparison.Ordinal) &&
4241
IsPrivateField(node))
4342
{
4443
_addedAnnotations = true;

src/Microsoft.DotNet.CodeFormatting/Rules/HasCopyrightHeaderFormattingRule.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ internal sealed class HasCopyrightHeaderFormattingRule : ISyntaxFormattingRule
2424

2525
public SyntaxNode Process(SyntaxNode syntaxNode)
2626
{
27+
/*
2728
if (HasCopyrightHeader(syntaxNode))
2829
return syntaxNode;
2930
3031
return AddCopyrightHeader(syntaxNode);
32+
*/
33+
return syntaxNode;
3134
}
3235

3336
private static bool HasCopyrightHeader(SyntaxNode syntaxNode)

src/Microsoft.DotNet.CodeFormatting/Rules/PrivateFieldNamingRule.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,6 @@ private static async Task<Solution> RenameFields(Solution solution, DocumentId d
181181
Solution oldSolution = null;
182182
for (int i = 0; i < count; i++)
183183
{
184-
// If this is not the first field then clean up the Rename annotations left
185-
// in the tree.
186-
if (i > 0)
187-
{
188-
solution = await CleanSolutionAsync(solution, oldSolution, cancellationToken);
189-
}
190-
191184
oldSolution = solution;
192185

193186
var semanticModel = await solution.GetDocument(documentId).GetSemanticModelAsync(cancellationToken);
@@ -203,6 +196,7 @@ private static async Task<Solution> RenameFields(Solution solution, DocumentId d
203196
}
204197

205198
solution = await Renamer.RenameSymbolAsync(solution, fieldSymbol, newName, solution.Workspace.Options, cancellationToken).ConfigureAwait(false);
199+
solution = await CleanSolutionAsync(solution, oldSolution, cancellationToken);
206200
}
207201

208202
return solution;

0 commit comments

Comments
 (0)