Skip to content

Commit 2aff95f

Browse files
authored
Merge pull request #1 from overleaf/mj-custom-render
Add deduplication keys
2 parents 51ba367 + 7af69bc commit 2aff95f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/completion.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export interface Completion {
5252
section?: string | CompletionSection
5353
/// Can be used to alter the change created when the completion is applied
5454
extend?: ExtendCompletion
55+
/// If multiple sources return the same result, use this field to specifiy a
56+
/// deduplication key as well as a priority. For each unique key, only the
57+
/// completion with the highest priority will be shown.
58+
deduplicate?: { key: string, priority: number }
5559
}
5660

5761
/// The type returned from

src/state.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,23 @@ function sortOptions(active: readonly ActiveSource[], state: EditorState) {
6868
else if (opt.completion.info) result[result.length - 1] = opt
6969
prev = opt.completion
7070
}
71-
return result
71+
// overleaf: Deduplicate results with dedup options
72+
const topPriorities = new Map<string, number>()
73+
for (const opt of result) {
74+
const key = opt.completion.deduplicate?.key
75+
if (!key) continue
76+
const currentPriority = topPriorities.get(key)
77+
if (currentPriority === undefined) {
78+
topPriorities.set(key, opt.completion.deduplicate.priority)
79+
} else {
80+
if (currentPriority < opt.completion.deduplicate.priority) {
81+
topPriorities.set(key, opt.completion.deduplicate.priority)
82+
}
83+
}
84+
}
85+
return result.filter(opt => opt.completion.deduplicate
86+
? topPriorities.get(opt.completion.deduplicate.key) === opt.completion.deduplicate.priority
87+
: true)
7288
}
7389

7490
class CompletionDialog {

0 commit comments

Comments
 (0)