Skip to content

Commit

Permalink
Make snippets lower priority than property completions (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenWeatherford authored Jun 19, 2020
1 parent c4c0b19 commit c9895e4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/Completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import * as language from "./Language";
import { UserFunctionNamespaceDefinition } from "./UserFunctionNamespaceDefinition";
import { IVariableDefinition } from "./VariableDefinition";

export enum CompletionPriority {
normal = "normal",
high = "high",
low = "low",
}

/**
* A completion item in the list of completion suggestions that appear when a user invokes auto-completion (Ctrl + Space).
*/
Expand All @@ -23,7 +29,7 @@ export class Item {
public readonly additionalEdits: { span: language.Span; insertText: string }[] | undefined;
public readonly sortText: string | undefined;
public readonly commitCharacters: string[] | undefined;
public readonly highPriority: boolean;
public readonly priority: CompletionPriority;
public readonly preselect: boolean;
public readonly telemetryProperties: { [key: string]: string } | undefined;

Expand Down Expand Up @@ -61,7 +67,7 @@ export class Item {
/**
* Priority for sorting used in addition to sortText.
*/
highPriority?: boolean;
priority?: CompletionPriority;
preselect?: boolean;
/**
* Optional additional telemetry properties for if the completion is activated
Expand All @@ -79,7 +85,7 @@ export class Item {
this.additionalEdits = options.additionalEdits;
this.sortText = options.sortText;
this.commitCharacters = options.commitCharacters;
this.highPriority = !!options.highPriority;
this.priority = options.priority ?? CompletionPriority.normal;
this.preselect = !!options.preselect;
this.telemetryProperties = options.telemetryProperties;
}
Expand Down
4 changes: 3 additions & 1 deletion src/SnippetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export class SnippetManager implements ISnippetManager {
detail,
insertText: snippet.insertText,
span,
kind: Completion.CompletionKind.Snippet
kind: Completion.CompletionKind.Snippet,
// Make sure snippets show up after normal completions
priority: Completion.CompletionPriority.low
}));

}
Expand Down
4 changes: 2 additions & 2 deletions src/getResourceIdCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function getCompletions(
insertText,
span,
kind: Completion.CompletionKind.DtResourceIdResName,
highPriority: true,
priority: Completion.CompletionPriority.high,
// Force the first of the resourceId completions to be preselected, otherwise
// vscode tends to preselect one of the regular function completions based
// on recently-typed text
Expand Down Expand Up @@ -225,7 +225,7 @@ function getResourceTypeCompletions(
insertText,
span,
kind: Completion.CompletionKind.DtResourceIdResType,
highPriority: true,
priority: Completion.CompletionPriority.high,
// Force the first of the resourceId completions to be preselected, otherwise
// vscode tends to preselect one of the regular function completions based
// on recently-typed text
Expand Down
19 changes: 17 additions & 2 deletions src/util/toVsCodeCompletionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@ export function toVsCodeCompletionItem(deploymentFile: DeploymentDocument, item:
vscodeItem.commitCharacters = item.commitCharacters;
vscodeItem.preselect = item.preselect;

// Add priority string to start of sortText;
vscodeItem.sortText = `${item.highPriority ? '0' : '1'}-${item.sortText ?? item.label}`;
let sortPriorityPrefix: string;
switch (item.priority) {
case Completion.CompletionPriority.low:
sortPriorityPrefix = `${String.fromCharCode(255)}-`;
break;
case Completion.CompletionPriority.high:
sortPriorityPrefix = `${String.fromCharCode(1)}-`;
break;
case Completion.CompletionPriority.normal:
sortPriorityPrefix = '';
break;
default:
assertNever(item.priority);
}

// Add priority string to start of sortText, use label if no sortText
vscodeItem.sortText = `${sortPriorityPrefix}${item.sortText ?? item.label}`;

switch (item.kind) {
case Completion.CompletionKind.Function:
Expand Down

0 comments on commit c9895e4

Please sign in to comment.