Allow custom sorting and deduplication of completion items

For the project I’m currently working on, I need to be able to customize the sorting and deduplication of the suggestion options from different sources.

compareCompletions only runs after CodeMirror has already assigned a score and sorted the completions and for some reason, completions from different sources but with the same label get assigned different scores. compareCompletions thus never gets called for these completions.

I think it would make sense make this stuff more configurable so we can more easily:

  • sort completions the way we want (ie. add more weight to completions where the prefix matches)
  • deduplicate completions from different sources

I’m happy to set up a PR to do exactly that but wanted to check here first to see if you think it’s a good idea.

As a first proposal, how would you feel if I added a configuration option like rewriteCompletions(completions: Completion[]) to autocompletion that users can use to rewrite the completion results after CodeMirror has finished compiling the list?

If your completion sources are so similar that they end up returning duplicate completions, would it maybe make sense to group them into a single wrapping source that calls out to your current sources and does this kind of reorganizing? That way you can do this without any extension to the existing interface.

We have a bunch of independent completion sources that all call fairly slow endpoints. Grouping them all in one completion source will mean we always have to wait for the slowest one to resolve, which would gravely reduce the usefulness of our completions.

Additionally, I don’t think that would solve the sorting issue completely since CodeMirror attaches it’s own score to the completion items which currently cannot be configured (it’s either the fuzzy or prefix matchers’ score)

There’s filter: false for cases where you want to do your own matching and sorting.

Matching and sorting uses internal data structures that I don’t really want to make public. It sounds like what you’re asking for here would require that, leading to a bigger package interface and less flexibility in the future to change these internals.

AFAICT: filter: false still does not allow me to fix sorting and filtering between completions from different sources.

I’ll play around with monkey-patching the package to see if I can find a succinct addition to the API.