@codemirror/autocomplete 6.18.7 → 6.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 6.19.0 (2025-09-26)
2
+
3
+ ### New features
4
+
5
+ Completion sections may now set their rank to `dynamic` to indicate their order should be determined by the matching score of their best-matching option.
6
+
1
7
  ## 6.18.7 (2025-09-02)
2
8
 
3
9
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -754,7 +754,7 @@ function score(option) {
754
754
  }
755
755
  function sortOptions(active, state) {
756
756
  let options = [];
757
- let sections = null;
757
+ let sections = null, dynamicSectionScore = null;
758
758
  let addOption = (option) => {
759
759
  options.push(option);
760
760
  let { section } = option.completion;
@@ -781,13 +781,24 @@ function sortOptions(active, state) {
781
781
  for (let option of a.result.options)
782
782
  if (match = matcher.match(option.label)) {
783
783
  let matched = !option.displayLabel ? match.matched : getMatch ? getMatch(option, match.matched) : [];
784
- addOption(new Option(option, a.source, matched, match.score + (option.boost || 0)));
784
+ let score = match.score + (option.boost || 0);
785
+ addOption(new Option(option, a.source, matched, score));
786
+ if (typeof option.section == "object" && option.section.rank === "dynamic") {
787
+ let { name } = option.section;
788
+ if (!dynamicSectionScore)
789
+ dynamicSectionScore = Object.create(null);
790
+ dynamicSectionScore[name] = Math.max(score, dynamicSectionScore[name] || -1e9);
791
+ }
785
792
  }
786
793
  }
787
794
  }
788
795
  if (sections) {
789
796
  let sectionOrder = Object.create(null), pos = 0;
790
- let cmp = (a, b) => { var _a, _b; return ((_a = a.rank) !== null && _a !== void 0 ? _a : 1e9) - ((_b = b.rank) !== null && _b !== void 0 ? _b : 1e9) || (a.name < b.name ? -1 : 1); };
797
+ let cmp = (a, b) => {
798
+ return (a.rank === "dynamic" && b.rank === "dynamic" ? dynamicSectionScore[b.name] - dynamicSectionScore[a.name] : 0) ||
799
+ (typeof a.rank == "number" ? a.rank : 1e9) - (typeof b.rank == "number" ? b.rank : 1e9) ||
800
+ (a.name < b.name ? -1 : 1);
801
+ };
791
802
  for (let s of sections.sort(cmp)) {
792
803
  pos -= 1e5;
793
804
  sectionOrder[s.name] = pos;
package/dist/index.d.cts CHANGED
@@ -106,8 +106,12 @@ interface CompletionSection {
106
106
  By default, sections are ordered alphabetically by name. To
107
107
  specify an explicit order, `rank` can be used. Sections with a
108
108
  lower rank will be shown above sections with a higher rank.
109
+
110
+ When set to `"dynamic"`, the section's position compared to
111
+ other dynamic sections depends on the matching score of the
112
+ best-matching option in the sections.
109
113
  */
110
- rank?: number;
114
+ rank?: number | "dynamic";
111
115
  }
112
116
  /**
113
117
  An instance of this is passed to completion source functions.
package/dist/index.d.ts CHANGED
@@ -106,8 +106,12 @@ interface CompletionSection {
106
106
  By default, sections are ordered alphabetically by name. To
107
107
  specify an explicit order, `rank` can be used. Sections with a
108
108
  lower rank will be shown above sections with a higher rank.
109
+
110
+ When set to `"dynamic"`, the section's position compared to
111
+ other dynamic sections depends on the matching score of the
112
+ best-matching option in the sections.
109
113
  */
110
- rank?: number;
114
+ rank?: number | "dynamic";
111
115
  }
112
116
  /**
113
117
  An instance of this is passed to completion source functions.
package/dist/index.js CHANGED
@@ -752,7 +752,7 @@ function score(option) {
752
752
  }
753
753
  function sortOptions(active, state) {
754
754
  let options = [];
755
- let sections = null;
755
+ let sections = null, dynamicSectionScore = null;
756
756
  let addOption = (option) => {
757
757
  options.push(option);
758
758
  let { section } = option.completion;
@@ -779,13 +779,24 @@ function sortOptions(active, state) {
779
779
  for (let option of a.result.options)
780
780
  if (match = matcher.match(option.label)) {
781
781
  let matched = !option.displayLabel ? match.matched : getMatch ? getMatch(option, match.matched) : [];
782
- addOption(new Option(option, a.source, matched, match.score + (option.boost || 0)));
782
+ let score = match.score + (option.boost || 0);
783
+ addOption(new Option(option, a.source, matched, score));
784
+ if (typeof option.section == "object" && option.section.rank === "dynamic") {
785
+ let { name } = option.section;
786
+ if (!dynamicSectionScore)
787
+ dynamicSectionScore = Object.create(null);
788
+ dynamicSectionScore[name] = Math.max(score, dynamicSectionScore[name] || -1e9);
789
+ }
783
790
  }
784
791
  }
785
792
  }
786
793
  if (sections) {
787
794
  let sectionOrder = Object.create(null), pos = 0;
788
- let cmp = (a, b) => { var _a, _b; return ((_a = a.rank) !== null && _a !== void 0 ? _a : 1e9) - ((_b = b.rank) !== null && _b !== void 0 ? _b : 1e9) || (a.name < b.name ? -1 : 1); };
795
+ let cmp = (a, b) => {
796
+ return (a.rank === "dynamic" && b.rank === "dynamic" ? dynamicSectionScore[b.name] - dynamicSectionScore[a.name] : 0) ||
797
+ (typeof a.rank == "number" ? a.rank : 1e9) - (typeof b.rank == "number" ? b.rank : 1e9) ||
798
+ (a.name < b.name ? -1 : 1);
799
+ };
789
800
  for (let s of sections.sort(cmp)) {
790
801
  pos -= 1e5;
791
802
  sectionOrder[s.name] = pos;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/autocomplete",
3
- "version": "6.18.7",
3
+ "version": "6.19.0",
4
4
  "description": "Autocompletion for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",