提交 54acac73 编写于 作者: C CyrusNajmabadi

Properly handle adding multiple usings when there is a banner at the top of the file

上级 3799c349
......@@ -1544,5 +1544,52 @@ class Derived : Base
public override int Prop => throw new NotImplementedException();
}", options: Option(ImplementTypeOptions.InsertionBehavior, ImplementTypeInsertionBehavior.AtTheEnd));
}
[WorkItem(17274, "https://github.com/dotnet/roslyn/issues/17274")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementAbstractClass)]
public async Task TestAddedUsingWithBanner1()
{
await TestAsync(
@"// Copyright ...
using Microsoft.Win32;
namespace My
{
public abstract class Foo
{
public abstract void Bar(System.Collections.Generic.List<object> values);
}
public class [|Foo2|] : Foo // Implement Abstract Class
{
}
}",
@"// Copyright ...
using System;
using System.Collections.Generic;
using Microsoft.Win32;
namespace My
{
public abstract class Foo
{
public abstract void Bar(System.Collections.Generic.List<object> values);
}
public class Foo2 : Foo // Implement Abstract Class
{
public override void Bar(List<object> values)
{
throw new NotImplementedException();
}
}
}", compareTokens: false);
}
#if false
#endif
}
}
\ No newline at end of file
......@@ -154,15 +154,21 @@ private static TextSpan GetUsingsSpan(CompilationUnitSyntax root, NamespaceDecla
firstToken.LeadingTrivia.Where(t => !IsDocCommentOrElastic(t)));
usings[0] = newFirstUsing;
}
else if (usings[0] != root.Usings[0])
else
{
// We added a new first-using. Take the trivia on the existing first using
// And move it to the new using.
var newFirstUsing = usings[0].WithLeadingTrivia(usings[1].GetLeadingTrivia())
.WithAppendedTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed);
usings[0] = newFirstUsing;
usings[1] = usings[1].WithoutLeadingTrivia();
var originalFirstUsing = root.Usings[0];
if (usings[0] != originalFirstUsing)
{
// We added a new first-using. Take the trivia on the existing first using
// And move it to the new using.
var originalFirstUsingCurrentIndex = usings.IndexOf(originalFirstUsing);
var newFirstUsing = usings[0].WithLeadingTrivia(originalFirstUsing.GetLeadingTrivia())
.WithAppendedTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed);
usings[0] = newFirstUsing;
usings[originalFirstUsingCurrentIndex] = originalFirstUsing.WithoutLeadingTrivia();
}
}
}
......@@ -181,9 +187,9 @@ private static bool IsDocCommentOrElastic(SyntaxTrivia t)
// We need to try and not place the using inside of a directive if possible.
var usings = new List<UsingDirectiveSyntax>();
var endOfList = root.Usings.Count - 1;
int startOfLastDirective = -1;
int endOfLastDirective = -1;
for (int i = 0; i < root.Usings.Count; i++)
var startOfLastDirective = -1;
var endOfLastDirective = -1;
for (var i = 0; i < root.Usings.Count; i++)
{
if (root.Usings[i].GetLeadingTrivia().Any(trivia => trivia.IsKind(SyntaxKind.IfDirectiveTrivia)))
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册