Skip to content

Commit 363204d

Browse files
author
ZouYingjun
committed
实测修复:跨文件搜索快捷键改为 Cmd/Ctrl+Shift+H(F 在 macOS 被系统层拦截)+ 同行命中预览去重
1 parent c661d59 commit 363204d

4 files changed

Lines changed: 17 additions & 15 deletions

File tree

‎docs/design-cross-file-search.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Jotpad 虽无显式 workspace,但**每个系统都有默认保存目录**:
1717
→ **这个目录就是用户的笔记根**。本设计把它作为主搜索范围,驱动三个能力(用户原话):
1818

1919
1. **快速打开**(QuickOpen 增强):按文件名在该目录里模糊查找,`Cmd/Ctrl+P` 能找到"从没打开过"的文件。
20-
2. **跨文件搜索**(CrossSearch 新增):按内容在该目录里全文检索,`Cmd/Ctrl+Shift+F`。
20+
2. **跨文件搜索**(CrossSearch 新增):按内容在该目录里全文检索,`Cmd/Ctrl+Shift+H`。
2121

2222
合并后的**搜索源**(优先级降序):
2323

@@ -224,7 +224,7 @@ const pick = async (file: FileHit, line: LineHit) => {
224224
## 七、store / 快捷键 / 菜单 / i18n
225225

226226
- **store**:仿 `quickOpenOpen` 加 `crossSearchOpen` / `setCrossSearchOpen`。
227-
- **快捷键**:`crossSearch` = `Cmd/Ctrl+Shift+F`(业界"在文件中查找"约定),加入 `shortcuts.ts`。
227+
- **快捷键**:`crossSearch` = `Cmd/Ctrl+Shift+H`(原定 Cmd/Ctrl+Shift+F,实测在 macOS 被 WKWebView 之前的系统层吞掉,遂改 H),加入 `shortcuts.ts`。
228228
- **菜单**:「编辑」下 Find 系列加「在文件中查找…」→ `setCrossSearchOpen(true)`。
229229
- **i18n**:新增 `crossSearch.*`(title/placeholder/noResults/scanning/caseSensitive/regexp/wholeWord/results)+ QuickOpen 的「目录」badge。
230230

@@ -271,7 +271,7 @@ const pick = async (file: FileHit, line: LineHit) => {
271271
1. **Rust**(手动 / `cargo test` 可选):`list_dir_files` 递归、隐藏/噪声跳过、扩展名过滤、`max_files` 截断、mtime 排序。
272272
2. **单元**(`crossSearch.test.ts`):多行命中偏移/行号/列、大小写/正则/整词、`MAX_LINES_PER_FILE` 封顶、`isBinary` NUL 检测。
273273
3. **手动**:
274-
- `Cmd+Shift+F` → 输入词 → 实时分组结果(含保存目录文件)
274+
- `Cmd+Shift+H` → 输入词 → 实时分组结果(含保存目录文件)
275275
- `↑/↓/Enter` 跳转到正确行列
276276
- `Cmd+P` 能找到保存目录里"从未打开"的文件
277277
- 三开关(大小写/正则/整词)生效

‎src/lib/crossSearch.test.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@ import {
66
} from "@/lib/crossSearch";
77

88
describe("searchText", () => {
9-
it("单行多命中:行号/列/绝对偏移正确", () => {
9+
it("单行多命中:同行只保留首条(预览去重),偏移正确", () => {
1010
const re = /foo/g;
1111
const hits = searchText("foo bar foo baz", re);
12-
expect(hits).toHaveLength(2);
12+
expect(hits).toHaveLength(1);
1313
expect(hits[0]).toMatchObject({ line: 1, col: 1, from: 0, to: 3 });
14-
expect(hits[1]).toMatchObject({ line: 1, col: 9, from: 8, to: 11 });
1514
});
1615

17-
it("多行:行号随换行推进", () => {
18-
// idx: 0:a 1:\n 2:b 3:c 4:(space) 5:f..7:o | 8:\n 9:f..11:o
19-
const text = "a\nbc foo\nfoo";
16+
it("多行:行号随换行推进,不同行各自保留", () => {
17+
// "a\nbc foo foo\nfoo": 第二个 foo 在同行被去重;第三行 foo from=13
18+
const text = "a\nbc foo foo\nfoo";
2019
const hits = searchText(text, /foo/g);
2120
expect(hits).toHaveLength(2);
2221
expect(hits[0]).toMatchObject({ line: 2, col: 4, from: 5, to: 8 });
23-
expect(hits[1]).toMatchObject({ line: 3, col: 1, from: 9, to: 12 });
22+
expect(hits[1]).toMatchObject({ line: 3, col: 1, from: 13, to: 16 });
2423
});
2524

2625
it("大小写不敏感(由正则 flags 决定)", () => {
27-
expect(searchText("Foo FOO foo", /foo/gi)).toHaveLength(3);
28-
expect(searchText("Foo FOO foo", /foo/g)).toHaveLength(1);
26+
expect(searchText("Foo FOO", /foo/gi)).toHaveLength(1); // 同行去重后 1 条
27+
expect(searchText("Foo\nFOO", /foo/gi)).toHaveLength(2); // 不同行各 1 条
2928
});
3029

3130
it("整词:buildSearchRegExp 包裹 \\b", () => {

‎src/lib/crossSearch.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function searchText(text: string, re: RegExp, cap = MAX_LINES_PER_FILE):
5454
re.lastIndex = 0;
5555
let lineStart = 0;
5656
let line = 1;
57+
let lastHitLine = 0; // 同一行只展示首条命中(预览去重,对齐 VS Code 行为)
5758
let m: RegExpExecArray | null;
5859
while ((m = re.exec(text)) !== null) {
5960
if (m[0].length === 0) {
@@ -69,6 +70,8 @@ export function searchText(text: string, re: RegExp, cap = MAX_LINES_PER_FILE):
6970
line++;
7071
lineStart = nl + 1;
7172
}
73+
if (line === lastHitLine) continue; // 同行后续命中跳过(预览不重复)
74+
lastHitLine = line;
7275
let lineEnd = text.indexOf("\n", from);
7376
if (lineEnd === -1) lineEnd = text.length;
7477
hits.push({

‎src/lib/shortcuts.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ export const SHORTCUTS: readonly ShortcutDef[] = [
134134
},
135135
{
136136
id: "crossSearch",
137-
chords: [{ key: "f", mod: true, shift: true }],
138-
label: `${MOD}+Shift+F`,
139-
accel: A("Shift+F"),
137+
chords: [{ key: "h", mod: true, shift: true }],
138+
label: `${MOD}+Shift+H`,
139+
accel: A("Shift+H"),
140140
},
141141
{
142142
id: "outline",

0 commit comments

Comments
 (0)