Skip to content

Commit a270be4

Browse files
authored
Fix completion bugs (#6441)
1 parent 7f20261 commit a270be4

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

l10n/bundle.l10n.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"Unexpected error when attaching to C# preview window.": "Unexpected error when attaching to C# preview window.",
111111
"Razor C# copied to clipboard": "Razor C# copied to clipboard",
112112
"Copy C#": "Copy C#",
113+
"{0} Keyword": "{0} Keyword",
113114
"Unexpected completion trigger kind: {0}": "Unexpected completion trigger kind: {0}",
114115
"1 reference": "1 reference",
115116
"{0} references": "{0} references",

src/razor/src/completion/provisionalCompletionOrchestrator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ export class ProvisionalCompletionOrchestrator {
134134
htmlPosition,
135135
provisionalPosition,
136136
completionContext,
137-
projection.languageKind
137+
projection.languageKind,
138+
newDocument
138139
);
139140

140141
// We track when we add provisional dots to avoid doing unnecessary work on commonly invoked events.

src/razor/src/completion/razorCompletionItemProvider.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
3030
hostDocumentPosition: vscode.Position,
3131
projectedPosition: vscode.Position,
3232
context: vscode.CompletionContext,
33-
language: LanguageKind
33+
language: LanguageKind,
34+
razorDocument: vscode.TextDocument
3435
) {
3536
if (projectedUri) {
3637
// "@" is not a valid trigger character for C# / HTML and therefore we need to translate
@@ -86,6 +87,11 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
8687
// In the code behind it's represented as __o = DateTime.
8788
const completionCharacterOffset = projectedPosition.character - hostDocumentPosition.character;
8889
for (const completionItem of completionItems) {
90+
// vscode.CompletionItemKind is off by one compared to the LSP CompletionItemKind.
91+
if (completionItem.kind !== undefined) {
92+
completionItem.kind = completionItem.kind - 1;
93+
}
94+
8995
const doc = completionItem.documentation as vscode.MarkdownString;
9096
if (doc && doc.value) {
9197
// Without this, the documentation doesn't get rendered in the editor.
@@ -166,6 +172,8 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
166172
}
167173
}
168174

175+
this.addUsingKeyword(language, razorDocument, hostDocumentPosition, completionItems);
176+
169177
const isIncomplete = completions instanceof Array ? false : completions ? completions.isIncomplete : false;
170178
return new vscode.CompletionList(completionItems, isIncomplete);
171179
}
@@ -221,7 +229,8 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
221229
position,
222230
projection.position,
223231
context,
224-
projection.languageKind
232+
projection.languageKind,
233+
document
225234
);
226235

227236
return completionList;
@@ -278,6 +287,38 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
278287

279288
return item;
280289
}
290+
291+
private static addUsingKeyword(
292+
language: LanguageKind,
293+
razorDocument: vscode.TextDocument,
294+
hostDocumentPosition: vscode.Position,
295+
completionItems: vscode.CompletionItem[]
296+
) {
297+
// This is an ugly hack, but it's needed to get the "using" keyword to show up in the completion list.
298+
// The reason it doesn't show up is because the C# generated document puts the position of the cursor
299+
// at '__o = [||]', which isn't a valid location for a using statement.
300+
if (language == LanguageKind.CSharp) {
301+
const line = razorDocument.lineAt(hostDocumentPosition.line);
302+
const lineText = line.text.substring(0, hostDocumentPosition.character);
303+
if (
304+
lineText.endsWith('@') ||
305+
lineText.endsWith(
306+
'@u' ||
307+
lineText.endsWith('@us') ||
308+
lineText.endsWith('@usi') ||
309+
lineText.endsWith('@usin') ||
310+
lineText.endsWith('@using')
311+
)
312+
) {
313+
const usingItem = new vscode.CompletionItem('using', vscode.CompletionItemKind.Keyword);
314+
315+
// Matching Roslyn's documentation behavior
316+
(<CompletionItem>usingItem).documentation = vscode.l10n.t('{0} Keyword', 'using');
317+
318+
completionItems.push(usingItem);
319+
}
320+
}
321+
}
281322
}
282323

283324
function getTriggerKind(triggerKind: vscode.CompletionTriggerKind): CompletionTriggerKind {

0 commit comments

Comments
 (0)