@evandu/super-pi 0.1.238 → 0.1.239

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.
@@ -8,9 +8,9 @@
8
8
  <link rel="alternate icon" href="/favicon.ico" />
9
9
  <link rel="apple-touch-icon" href="/favicon.svg" />
10
10
  <title>Super Pi</title>
11
- <script type="module" crossorigin src="/assets/index-D9r0K1_Y.js"></script>
11
+ <script type="module" crossorigin src="/assets/index-C5kxyPTT.js"></script>
12
12
  <link rel="modulepreload" crossorigin href="/assets/rolldown-runtime-XKyaHtl9.js">
13
- <link rel="stylesheet" crossorigin href="/assets/index-sDaqj666.css">
13
+ <link rel="stylesheet" crossorigin href="/assets/index-DN0oBG6l.css">
14
14
  </head>
15
15
  <body>
16
16
  <div id="root"></div>
@@ -288,7 +288,7 @@ export class FileNameIndex {
288
288
  const files = [...this.entries.values()].filter(entry => entry.kind === "file");
289
289
  const ranked = query ? rankPathMatches(files, query) : files;
290
290
  if (query && !ranked.length && this.indexed) {
291
- const approximate = new Fuse(files, { keys: ["path"], threshold: 0.38, ignoreLocation: true });
291
+ const approximate = new Fuse(files, { keys: [{ name: "name", weight: 2 }, { name: "path", weight: 1 }], threshold: 0.38, ignoreLocation: true });
292
292
  ranked.push(...approximate.search(query, { limit: QUICK_OPEN_PAGE_SIZE }).map(match => match.item));
293
293
  }
294
294
  return {
@@ -10,28 +10,50 @@ export function findMentionToken(text, caret, selectionEnd = caret) {
10
10
  const remaining = match[1] !== undefined ? /^[^"\n]*(?:"|$)/u.exec(tail)?.[0] : /^[^\s@"\n]*/u.exec(tail)?.[0];
11
11
  return { start, end: caret + (remaining?.length ?? 0), query: match[1] ?? match[2] ?? "" };
12
12
  }
13
- /** Ordered-character matching supports abbreviations such as sct -> src/client/theme.tsx. */
13
+ var MatchRank;
14
+ (function (MatchRank) {
15
+ MatchRank[MatchRank["ExactPath"] = 0] = "ExactPath";
16
+ MatchRank[MatchRank["FoldedPath"] = 1] = "FoldedPath";
17
+ MatchRank[MatchRank["ExactName"] = 2] = "ExactName";
18
+ MatchRank[MatchRank["NamePrefix"] = 3] = "NamePrefix";
19
+ MatchRank[MatchRank["NameSubstring"] = 4] = "NameSubstring";
20
+ MatchRank[MatchRank["NameSubsequence"] = 5] = "NameSubsequence";
21
+ MatchRank[MatchRank["PathSubstring"] = 6] = "PathSubstring";
22
+ MatchRank[MatchRank["PathSubsequence"] = 7] = "PathSubsequence";
23
+ })(MatchRank || (MatchRank = {}));
24
+ function subsequenceScore(text, query, rank) {
25
+ let start = -1;
26
+ let end = 0;
27
+ for (const character of query) {
28
+ const position = text.indexOf(character, end);
29
+ if (position < 0)
30
+ return;
31
+ if (start < 0)
32
+ start = position;
33
+ end = position + character.length;
34
+ }
35
+ // Count gaps within the match. Long directories should never improve a sparse match.
36
+ return [rank, end - start - query.length, start];
37
+ }
14
38
  function fuzzyScore(path, query) {
15
- const name = path.slice(path.lastIndexOf("/") + 1);
16
39
  if (path === query)
17
- return -1;
40
+ return [MatchRank.FoldedPath];
41
+ const name = path.slice(path.lastIndexOf("/") + 1);
18
42
  if (name === query)
19
- return 0;
20
- if (name.startsWith(query))
21
- return 10 + name.length / 1000;
22
- if (name.includes(query))
23
- return 20 + name.indexOf(query) / 100;
24
- if (path.includes(query))
25
- return 30 + path.indexOf(query) / 100;
26
- let position = -1, gaps = 0;
27
- for (const character of query) {
28
- const next = path.indexOf(character, position + 1);
29
- if (next < 0)
30
- return undefined;
31
- gaps += next - position - 1;
32
- position = next;
33
- }
34
- return 50 + gaps / Math.max(1, path.length);
43
+ return [MatchRank.ExactName];
44
+ const nameStart = name.indexOf(query);
45
+ if (nameStart === 0)
46
+ return [MatchRank.NamePrefix, name.length];
47
+ if (nameStart > 0)
48
+ return [MatchRank.NameSubstring, nameStart, name.length];
49
+ const nameMatch = subsequenceScore(name, query, MatchRank.NameSubsequence);
50
+ if (nameMatch)
51
+ return nameMatch;
52
+ const pathStart = path.indexOf(query);
53
+ return pathStart >= 0 ? [MatchRank.PathSubstring, pathStart] : subsequenceScore(path, query, MatchRank.PathSubsequence);
54
+ }
55
+ function compareScores(a, b) {
56
+ return a[0] - b[0] || (a[1] ?? 0) - (b[1] ?? 0) || (a[2] ?? 0) - (b[2] ?? 0);
35
57
  }
36
58
  export function rankPathMatches(entries, query) {
37
59
  const normalized = query.trim().replaceAll("\\", "/");
@@ -41,11 +63,21 @@ export function rankPathMatches(entries, query) {
41
63
  if (!entry.path || entry.path.startsWith("/") || /^[a-z]:/iu.test(entry.path) || entry.path.split("/").includes(".."))
42
64
  continue;
43
65
  const path = entry.path.toLowerCase();
44
- const score = needle ? entry.path === normalized ? -2 : fuzzyScore(path, needle) : 0;
66
+ const score = !needle || entry.path === normalized
67
+ ? [MatchRank.ExactPath] : fuzzyScore(path, needle);
45
68
  if (score !== undefined)
46
69
  matches.push({ entry, score, depth: entry.path.split("/").length });
47
70
  }
48
- matches.sort((a, b) => a.score - b.score || a.depth - b.depth || Number(b.entry.kind === "directory") - Number(a.entry.kind === "directory") || (a.entry.path < b.entry.path ? -1 : a.entry.path > b.entry.path ? 1 : 0));
71
+ matches.sort((a, b) => {
72
+ const order = compareScores(a.score, b.score);
73
+ if (order)
74
+ return order;
75
+ const directoriesFirst = Number(b.entry.kind === "directory") - Number(a.entry.kind === "directory");
76
+ // Keep files ahead of tied fuzzy directory suggestions in path mentions.
77
+ return (a.score[0] === MatchRank.PathSubsequence ? -directoriesFirst : 0)
78
+ || a.depth - b.depth || directoriesFirst
79
+ || (a.entry.path < b.entry.path ? -1 : a.entry.path > b.entry.path ? 1 : 0);
80
+ });
49
81
  return matches.map(item => item.entry);
50
82
  }
51
83
  export function matchPathMentions(entries, query, limit = 30) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandu/super-pi",
3
- "version": "0.1.238",
3
+ "version": "0.1.239",
4
4
  "description": "Web control plane and companion for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",