Z ZPI 文档 v0.3.8
介绍页

源码

packages/ui/src/fuzzy-search.ts

typescript20 行查看原文
// Adapted from ZCode mentionSearch / workspaceFileSearch; see licenses/third-party-notices.txt.
/** The caller normalizes the query and applies any field-specific weighting. */
export function fuzzyScore(text: string, query: string, allowSubsequence = true): number {
  const value = text.trim().toLowerCase();
  if (!value) return Number.POSITIVE_INFINITY;
  if (value.startsWith(query)) return value.length - query.length;
  const substring = value.indexOf(query);
  if (substring !== -1) return 100 + substring;
  if (!allowSubsequence) return Number.POSITIVE_INFINITY;
  let score = 200;
  let start = 0;
  for (const char of query) {
    const index = value.indexOf(char, start);
    if (index === -1) return Number.POSITIVE_INFINITY;
    score += index - start;
    start = index + 1;
  }
  return score + value.length - query.length;
}