Skip to content

Commit

Permalink
Fix completion bugs (#6441)
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonchou authored Sep 27, 2023
1 parent 7f20261 commit a270be4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"Unexpected error when attaching to C# preview window.": "Unexpected error when attaching to C# preview window.",
"Razor C# copied to clipboard": "Razor C# copied to clipboard",
"Copy C#": "Copy C#",
"{0} Keyword": "{0} Keyword",
"Unexpected completion trigger kind: {0}": "Unexpected completion trigger kind: {0}",
"1 reference": "1 reference",
"{0} references": "{0} references",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export class ProvisionalCompletionOrchestrator {
htmlPosition,
provisionalPosition,
completionContext,
projection.languageKind
projection.languageKind,
newDocument
);

// We track when we add provisional dots to avoid doing unnecessary work on commonly invoked events.
Expand Down
45 changes: 43 additions & 2 deletions src/razor/src/completion/razorCompletionItemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
hostDocumentPosition: vscode.Position,
projectedPosition: vscode.Position,
context: vscode.CompletionContext,
language: LanguageKind
language: LanguageKind,
razorDocument: vscode.TextDocument
) {
if (projectedUri) {
// "@" is not a valid trigger character for C# / HTML and therefore we need to translate
Expand Down Expand Up @@ -86,6 +87,11 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
// In the code behind it's represented as __o = DateTime.
const completionCharacterOffset = projectedPosition.character - hostDocumentPosition.character;
for (const completionItem of completionItems) {
// vscode.CompletionItemKind is off by one compared to the LSP CompletionItemKind.
if (completionItem.kind !== undefined) {
completionItem.kind = completionItem.kind - 1;
}

const doc = completionItem.documentation as vscode.MarkdownString;
if (doc && doc.value) {
// Without this, the documentation doesn't get rendered in the editor.
Expand Down Expand Up @@ -166,6 +172,8 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
}
}

this.addUsingKeyword(language, razorDocument, hostDocumentPosition, completionItems);

const isIncomplete = completions instanceof Array ? false : completions ? completions.isIncomplete : false;
return new vscode.CompletionList(completionItems, isIncomplete);
}
Expand Down Expand Up @@ -221,7 +229,8 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem
position,
projection.position,
context,
projection.languageKind
projection.languageKind,
document
);

return completionList;
Expand Down Expand Up @@ -278,6 +287,38 @@ export class RazorCompletionItemProvider extends RazorLanguageFeatureBase implem

return item;
}

private static addUsingKeyword(
language: LanguageKind,
razorDocument: vscode.TextDocument,
hostDocumentPosition: vscode.Position,
completionItems: vscode.CompletionItem[]
) {
// This is an ugly hack, but it's needed to get the "using" keyword to show up in the completion list.
// The reason it doesn't show up is because the C# generated document puts the position of the cursor
// at '__o = [||]', which isn't a valid location for a using statement.
if (language == LanguageKind.CSharp) {
const line = razorDocument.lineAt(hostDocumentPosition.line);
const lineText = line.text.substring(0, hostDocumentPosition.character);
if (
lineText.endsWith('@') ||
lineText.endsWith(
'@u' ||
lineText.endsWith('@us') ||
lineText.endsWith('@usi') ||
lineText.endsWith('@usin') ||
lineText.endsWith('@using')
)
) {
const usingItem = new vscode.CompletionItem('using', vscode.CompletionItemKind.Keyword);

// Matching Roslyn's documentation behavior
(<CompletionItem>usingItem).documentation = vscode.l10n.t('{0} Keyword', 'using');

completionItems.push(usingItem);
}
}
}
}

function getTriggerKind(triggerKind: vscode.CompletionTriggerKind): CompletionTriggerKind {
Expand Down

0 comments on commit a270be4

Please sign in to comment.