Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2979,5 +2979,68 @@ public override void M(in int x)

Assert.Equal(after, actualCodeAfterCommit);
}

[WorkItem(39909, "https://github.com/dotnet/roslyn/issues/39909")]
[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task CommitAddsMissingImports()
{
var markupBeforeCommit = @"
namespace NS1
{
using NS2;

public class Foo
{
public virtual bool Bar(Baz baz) => true;
}
}

namespace NS2
{
public class Baz {}
}

namespace NS3
{
using NS1;

class D : Foo
{
override $$
}
}";

var expectedCodeAfterCommit = @"
namespace NS1
{
using NS2;

public class Foo
{
public virtual bool Bar(Baz baz) => true;
}
}

namespace NS2
{
public class Baz {}
}

namespace NS3
{
using NS1;
using NS2;

class D : Foo
{
public override bool Bar(Baz baz)
{
return base.Bar(baz);$$
}
}
}";

await VerifyCustomCommitProviderAsync(markupBeforeCommit, "Bar(NS2.Baz baz)", expectedCodeAfterCommit);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private TextSpan ComputeDestinationSpan(SyntaxNode insertionRoot)
private async Task<string> GenerateInsertionTextAsync(
Document memberContainingDocument, CancellationToken cancellationToken)
{
memberContainingDocument = await Simplifier.ReduceAsync(memberContainingDocument, Simplifier.Annotation, null, cancellationToken).ConfigureAwait(false);
memberContainingDocument = await Simplifier.ReduceAsync(memberContainingDocument, Simplifier.Annotation, optionSet: null, cancellationToken).ConfigureAwait(false);
memberContainingDocument = await Formatter.FormatAsync(memberContainingDocument, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);

var root = await memberContainingDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -178,8 +178,11 @@ private async Task<string> GenerateInsertionTextAsync(
private async Task<SyntaxNode> GetTreeWithAddedSyntaxNodeRemovedAsync(
Document document, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
// Added imports are annotated for simplification too. Therefore, we simplify the document
// before removing added member node to preserve those imports in the document.
document = await Simplifier.ReduceAsync(document, Simplifier.Annotation, optionSet: null, cancellationToken).ConfigureAwait(false);

var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var members = root.GetAnnotatedNodesAndTokens(_annotation)
.AsImmutable()
.Select(m => m.AsNode());
Expand All @@ -188,7 +191,6 @@ private async Task<SyntaxNode> GetTreeWithAddedSyntaxNodeRemovedAsync(

var dismemberedDocument = document.WithSyntaxRoot(root);

dismemberedDocument = await Simplifier.ReduceAsync(dismemberedDocument, Simplifier.Annotation, null, cancellationToken).ConfigureAwait(false);
dismemberedDocument = await Formatter.FormatAsync(dismemberedDocument, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
return await dismemberedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
}
Expand Down