Skip to content

Commit 7c8e0d5

Browse files
committed
Add support for Completion.sortText
FEATURE: Completions now support a `sortText` property to influence sort order. See https://discuss.codemirror.net/t/lsp-client-request-support-for-sorttext-on-completionitem/9576
1 parent 8f88a0e commit 7c8e0d5

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

src/completion.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export interface Completion {
1414
/// provide a [`getMatch`](#autocomplete.CompletionResult.getMatch)
1515
/// function.
1616
displayLabel?: string
17+
/// Overrides the text that is used to sort completions. Will
18+
/// default to `label` if not given.
19+
sortText?: string
1720
/// An optional short piece of information to show (with a different
1821
/// style) after the label.
1922
detail?: string

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const completionConfig = Facet.define<CompletionConfig, Required<Completi
110110
addToOptions: [],
111111
positionInfo: defaultPositionInfo as any,
112112
filterStrict: false,
113-
compareCompletions: (a, b) => a.label.localeCompare(b.label),
113+
compareCompletions: (a, b) => (a.sortText || a.label).localeCompare(b.sortText || b.label),
114114
interactionDelay: 75,
115115
updateSyncTime: 100
116116
}, {

test/webtest-autocomplete.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ describe("autocomplete", () => {
148148

149149
run.options("sorts alphabetically when score is equal", "a", [from("ac ab acc")], "ab ac acc")
150150

151+
run.options("sorts by sortText when given", "a",
152+
[cx => ({from: 0, to: 1, options: [{label: "ab"}, {label: "ac", sortText: "aa"}]})], "ac ab")
153+
151154
run.options("removes duplicate options", "t", [from("two"), from("two three")], "three two")
152155

153156
run.options("handles all-uppercase words", "sel", [from("SCOPE_CATALOG SELECT SELECTIVE")], "SELECT SELECTIVE SCOPE_CATALOG")

0 commit comments

Comments
 (0)