feat(search): add fuzzy search support#151
Open
kylinstar2019 wants to merge 9 commits into
Open
Conversation
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
@floatboat/nexus-plugin-search当前只支持精确子串、whole-word 和正则匹配。对于「想用缩写快速定位」的典型场景——比如输入hw找到hello world——用户必须输入完整词或切换到正则。模糊搜索在 Obsidian、VS Code、Sublime 等现代编辑器中已是标准能力,缺它会让 plugin-search 的可用性落后一档。
Roadmap #17 标记为 P2 planned,本次实现填补这个缺口。
What
新增
SearchOptions.fuzzy?: boolean选项,开启后findSearchMatches/replaceAllMatches走模糊匹配路径;关闭时行为完全不变。算法
按行 DP,复杂度 O(nLen × L²),对编辑器中典型短行(< 200 字符)足够快。
评分维度(基线 + 增量):
API 变更
新增的可选
minScoreRatio允许调用方按理论最高分的比例过滤低质量匹配。公开导出
packages/plugin-search/src/index.ts顶层导出无新增;SearchOptions扩展FuzzyMatchOptions(caseSensitive,minScoreRatio)。packages/plugin-search/src/fuzzy.ts新增文件,但不作为公开导出(属于实现细节,外部通过SearchOptions.fuzzy间接使用)。Files changed
总计:3 个新文件、4 个修改文件、约 705 行新增。
Testing
pnpm testpnpm typecheckpnpm build新增测试覆盖:
fuzzy.test.ts(20 个):空串、精确匹配、跳跃匹配、词首匹配、camelCase、阈值过滤、大小写、多行、CJK、替换plugin-search.test.ts(10 个新增):集成到findSearchMatches/replaceAllMatches的fuzzy选项、向后兼容、阈值转发Backward compatibility
fuzzy默认false,所有现有调用点行为不变。无破坏性变更。Performance
对 1000 行、每行平均 80 字符的文档、单字符 query,per-line DP 跑 1000 次约 10ms(Node 22)。对长行(> 500 字符)会变慢;生产环境建议配合
minScoreRatio提前过滤或加防抖。性能基准将在后续单独提案中提供(add-missing-features4.2 已规划)。Manual verification
本 PR 不改 UI(
createSearchPlugin未触及,search panel 行为不变),按 CONTRIBUTING §4 不需要在 electron-demo 手动跑。验证方式:如果 maintainer 想要在 search panel UI 里加 fuzzy 开关(这是合理的 follow-up),需要单独 PR + UI 改动走
add-missing-features之外的提案流程。Notes
core,符合最小变更原则createSearchPlugin未变);panel 集成 fuzzy 留待后续提案