Skip to content

Commit 090acf0

Browse files
authored
Merge pull request #5077 from JoeRobich/workaround-razor-completion
Filter problematic 'await' keyword suggestions from Razor completion.
2 parents 65c110a + b85cd22 commit 090acf0

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/features/completionProvider.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { CompletionItemProvider, TextDocument, Position, CompletionContext, CompletionList, CompletionItem, MarkdownString, TextEdit, Range, SnippetString, window, Selection, WorkspaceEdit, workspace } from "vscode";
6+
import { CompletionItemProvider, TextDocument, Position, CompletionContext, CompletionList, CompletionItem, MarkdownString, TextEdit, Range, SnippetString, window, Selection, WorkspaceEdit, workspace, CompletionItemLabel } from "vscode";
77
import AbstractProvider from "./abstractProvider";
88
import * as protocol from "../omnisharp/protocol";
99
import * as serverUtils from '../omnisharp/utils';
1010
import { CancellationToken, CompletionTriggerKind as LspCompletionTriggerKind, InsertTextFormat } from "vscode-languageserver-protocol";
1111
import { createRequest } from "../omnisharp/typeConversion";
1212
import { LanguageMiddlewareFeature } from "../omnisharp/LanguageMiddlewareFeature";
1313
import { OmniSharpServer } from "../omnisharp/server";
14+
import { isVirtualCSharpDocument } from "./virtualDocumentTracker";
1415

1516
export const CompletionAfterInsertCommand = "csharp.completion.afterInsert";
1617

@@ -29,7 +30,35 @@ export default class OmnisharpCompletionProvider extends AbstractProvider implem
2930

3031
try {
3132
const response = await serverUtils.getCompletion(this._server, request, token);
32-
const mappedItems = response.Items.map(arg => this._convertToVscodeCompletionItem(arg));
33+
let mappedItems = response.Items.map(arg => this._convertToVscodeCompletionItem(arg));
34+
35+
if (isVirtualCSharpDocument(document)) {
36+
// The `await` completion item is not compatible with all Razor scenarios.
37+
//
38+
// The `await` completion has been made smarter in that it will now update the containing method signature to include `async`, if it has not been specified.
39+
// This is problematic for Razor because it will now suggest making the code behind method that is "wrapping" the use of C# into an async method.
40+
// It makes this change by including additional text edits in the completion item.
41+
//
42+
// Example that generates an incompatible completion item:
43+
//
44+
// ```
45+
// @Da$$
46+
// ```
47+
//
48+
// Cases where you are in an async method there are no additional text edits and we can continue to offer the `await` keyword.
49+
//
50+
// Example that generates a compatible completion item:
51+
//
52+
// ```
53+
// @code {
54+
// async Task GetNamesAsync()
55+
// {
56+
// var names = awa$$
57+
// }
58+
// }
59+
// ```
60+
mappedItems = mappedItems.filter(item => (<CompletionItemLabel>item.label).label !== "await" || !item.additionalTextEdits);
61+
}
3362

3463
let lastCompletions = new Map();
3564

0 commit comments

Comments
 (0)