Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix completion in AdhocWorkspace #20108

Merged
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 @@ -32,9 +32,7 @@ public AbstractMemberInsertingCompletionProvider()

public override async Task<CompletionChange> GetChangeAsync(Document document, CompletionItem item, char? commitKey = default(char?), CancellationToken cancellationToken = default(CancellationToken))
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var newDocument = await DetermineNewDocumentAsync(item, text, cancellationToken).ConfigureAwait(false);

var newDocument = await DetermineNewDocumentAsync(document, item, cancellationToken).ConfigureAwait(false);
var newText = await newDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -90,14 +88,12 @@ private TextChange Collapse(SourceText newText, List<TextChange> changes)
return new TextChange(totalOldSpan, newText.ToString(totalNewSpan));
}

private async Task<Document> DetermineNewDocumentAsync(CompletionItem completionItem, SourceText sourceText, CancellationToken cancellationToken)
private async Task<Document> DetermineNewDocumentAsync(Document document, CompletionItem completionItem, CancellationToken cancellationToken)
{
// The span we're going to replace
var line = sourceText.Lines[MemberInsertionCompletionItem.GetLine(completionItem)];
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

//var sourceText = textSnapshot.AsText();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was probably a good hint that this code was suspect.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally

var document = sourceText.GetOpenDocumentInCurrentContextWithChanges();
Contract.ThrowIfNull(document);
// The span we're going to replace
var line = text.Lines[MemberInsertionCompletionItem.GetLine(completionItem)];

// Annotate the line we care about so we can find it after adding usings
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -112,7 +108,8 @@ private async Task<Document> DetermineNewDocumentAsync(CompletionItem completion

var destinationSpan = ComputeDestinationSpan(insertionRoot, insertionText);

var finalText = insertionRoot.GetText(sourceText.Encoding).Replace(destinationSpan, insertionText.Trim());
var finalText = insertionRoot.GetText(text.Encoding)
.Replace(destinationSpan, insertionText.Trim());

document = document.WithText(finalText);
var newRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Expand Down