dsh-gufa-code 1.3.63 → 1.3.64

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/README.md CHANGED
@@ -41,7 +41,7 @@ dsh plugin add dsh-gufa-code
41
41
  - Monaco 编辑器(离线随包分发,无需联网下载);
42
42
  - 多标签、按扩展名语法高亮、面包屑、状态栏(分支 / 行列 / 保存状态);
43
43
  - 保存走磁盘版本冲突检测 + 原子写;未保存标记;
44
- - **全局搜索 / 替换**(左栏放大镜 / `⌘⇧F`):整个项目的文本搜索,支持区分大小写、正则;结果按文件分组、高亮匹配,点击直接跳到文件对应行列;展开替换栏可**全部替换**或对单个文件替换(替换前会先保存未落盘的编辑器内容,确认后直接写文件);Git 项目用 `git ls-files` 枚举文件(尊重 `.gitignore`、含未跟踪文件),非 Git 项目遍历目录并自动跳过 `node_modules` 等依赖/缓存目录;摸鱼悬浮窗里同样可用;
44
+ - **全局搜索 / 替换**(左栏放大镜 / `⌘⇧F`):整个项目的文本搜索,支持区分大小写、正则、包含/排除文件(glob);结果按文件分组、高亮匹配,点击跳转行列;替换栏支持保留大小写(AB)、全部替换(需确认)、按文件替换(悬停文件头铅笔)、按单行匹配替换与忽略(悬停结果行);替换前会自动保存未落盘的编辑器内容;Git 项目用 `git ls-files` 枚举文件(尊重 `.gitignore`、含未跟踪文件),非 Git 项目遍历目录并自动跳过 `node_modules` 等依赖/缓存目录;摸鱼悬浮窗里同样可用;
45
45
  - 资源管理器右键:新建 / 重命名 / 删除 / 复制 / 粘贴 / 打开所在文件夹 / 两文件对比;
46
46
  - **从 Finder 粘贴/拖入文件**:系统剪贴板里复制的文件可直接 ⌘V 粘进资源管理器(写入选中的目录,重名自动加「副本」),也可从 Finder 直接拖进文件树;
47
47
  - **粘贴文件到聊天框 = @引用**:在 Finder 复制文件后在右侧对话输入框 ⌘V,会自动匹配项目内同名同内容文件并插入 `@相对路径` 引用(图片仍按 DSH 默认附件处理);
package/client.js CHANGED
@@ -16,7 +16,7 @@ const GUFA_NS = 'gufa-code'
16
16
  const G = {}
17
17
  G.NS = GUFA_NS
18
18
  /** 客户端构建标记(排查页面是否加载了新代码时看标题栏) */
19
- G.BUILD = '1.3.63'
19
+ G.BUILD = '1.3.64'
20
20
  G.PANEL_ID = 'gufa-code'
21
21
  /** 从哪个工作区点进来的(工作区菜单/节点按钮写入,App 启动时消费)。 */
22
22
  G.pendingEntryWorkspace = null
@@ -199,10 +199,19 @@ const DICT_ZH = {
199
199
  'search.searching': '搜索中…',
200
200
  'search.idle': '输入关键字搜索整个项目',
201
201
  'search.noResult': '没有找到匹配结果',
202
- 'search.summary': '{count} 个结果 · {files} 个文件',
202
+ 'search.summary': '{files} 个文件中有 {count} 个结果',
203
203
  'search.truncated': '结果过多已截断,请缩小范围',
204
204
  'search.error': '搜索失败',
205
205
  'search.replaceToggle': '显示/隐藏替换',
206
+ 'search.include': '包含的文件',
207
+ 'search.exclude': '排除的文件',
208
+ 'search.includePlaceholder': '例如 *.js, src/**',
209
+ 'search.excludePlaceholder': '例如 **/*.min.js, test/**',
210
+ 'search.preserveCase': '保留大小写(AB)',
211
+ 'search.replaceOne': '替换此匹配',
212
+ 'search.dismiss': '忽略此匹配',
213
+ 'search.openInEditor': '在编辑器中打开',
214
+ 'search.stale': '匹配位置已变化,已重新搜索',
206
215
  'search.replacePlaceholder': '替换为…',
207
216
  'search.replaceAll': '全部替换',
208
217
  'search.replaceThisFile': '替换此文件',
@@ -441,10 +450,19 @@ const DICT_EN = {
441
450
  'search.searching': 'Searching…',
442
451
  'search.idle': 'Type a keyword to search the whole project',
443
452
  'search.noResult': 'No matches found',
444
- 'search.summary': '{count} results · {files} files',
453
+ 'search.summary': '{count} results in {files} files',
445
454
  'search.truncated': 'Too many results, truncated — narrow the query',
446
455
  'search.error': 'Search failed',
447
456
  'search.replaceToggle': 'Show / hide replace',
457
+ 'search.include': 'files to include',
458
+ 'search.exclude': 'files to exclude',
459
+ 'search.includePlaceholder': 'e.g. *.js, src/**',
460
+ 'search.excludePlaceholder': 'e.g. **/*.min.js, test/**',
461
+ 'search.preserveCase': 'Preserve case (AB)',
462
+ 'search.replaceOne': 'Replace this match',
463
+ 'search.dismiss': 'Dismiss this match',
464
+ 'search.openInEditor': 'Open in editor',
465
+ 'search.stale': 'Match moved; search refreshed',
448
466
  'search.replacePlaceholder': 'Replace with…',
449
467
  'search.replaceAll': 'Replace all',
450
468
  'search.replaceThisFile': 'Replace in this file',
@@ -1737,6 +1755,23 @@ html[data-platform=darwin][data-fullscreen] .gufa-titlebar{height:44px;padding-l
1737
1755
  .gufa-search-error{margin:6px 8px 0;padding:5px 9px;border-radius:7px;font-size:11.5px;color:var(--dsw-alias-state-error-primary);border:1px solid color-mix(in srgb, var(--dsw-alias-state-error-primary) 45%, transparent)}
1738
1756
  .gufa-search-file-replace{flex:0 0 auto;display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;margin-left:2px;border-radius:4px;color:var(--gufa-text-dim);cursor:pointer}
1739
1757
  .gufa-search-file-replace:hover{background:var(--gufa-active);color:var(--gufa-text)}
1758
+ .gufa-search-file-replace{opacity:0;transition:opacity .12s ease}
1759
+ .gufa-search-file-head:hover .gufa-search-file-replace,.gufa-search-file-head:focus-within .gufa-search-file-replace{opacity:1}
1760
+ .gufa-search-match{min-height:23px;align-items:center}
1761
+ .gufa-search-match-actions{display:none;flex:0 0 auto;align-items:center;gap:4px;margin-left:auto}
1762
+ .gufa-search-match:hover .gufa-search-match-actions,.gufa-search-match:focus-within .gufa-search-match-actions{display:flex}
1763
+ .gufa-search-row-action{display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;border-radius:4px;color:var(--gufa-text-dim);cursor:pointer}
1764
+ .gufa-search-row-action:hover{background:var(--gufa-active);color:var(--gufa-text)}
1765
+ .gufa-search-filter{display:flex;flex-direction:column;gap:3px;margin-top:5px}
1766
+ .gufa-search-filter-label{font-size:10.5px;color:var(--gufa-text-dim)}
1767
+ .gufa-search-filter-input{width:100%;height:24px;padding:0 8px;border-radius:6px;border:1px solid var(--gufa-border);background:var(--gufa-layer2);color:var(--gufa-text);font-size:11.5px;outline:none}
1768
+ .gufa-search-filter-input:focus{border-color:var(--gufa-accent)}
1769
+ .gufa-search-filter-input::placeholder{color:var(--gufa-text-dim)}
1770
+ .gufa-search-summarybar{display:flex;align-items:center;gap:8px}
1771
+ .gufa-search-summarybar .gufa-search-summary{flex:1 1 auto;margin-left:0}
1772
+ .gufa-search-summarybar .gufa-search-link{flex:0 0 auto}
1773
+ .gufa-search-link{border:0;background:transparent;color:var(--gufa-accent);font-size:11px;cursor:pointer;padding:0}
1774
+ .gufa-search-link:hover{text-decoration:underline}
1740
1775
 
1741
1776
  .gufa-tree-chevron{width:14px;flex:0 0 auto;opacity:.7;display:flex;align-items:center;justify-content:center}
1742
1777
  .gufa-tree-icon{width:15px;flex:0 0 auto;opacity:.85;display:flex;align-items:center;justify-content:center}
@@ -7322,16 +7357,20 @@ G.SearchPanel = function SearchPanel(props) {
7322
7357
  const t = G.useT()
7323
7358
  const root = props.root
7324
7359
  const [query, setQuery] = useState('')
7360
+ const [replacement, setReplacement] = useState('')
7325
7361
  const [caseSensitive, setCaseSensitive] = useState(false)
7326
7362
  const [regex, setRegex] = useState(false)
7363
+ const [preserveCase, setPreserveCase] = useState(false)
7364
+ const [includeText, setIncludeText] = useState('')
7365
+ const [excludeText, setExcludeText] = useState('')
7366
+ const [replaceOpen, setReplaceOpen] = useState(false)
7327
7367
  const [status, setStatus] = useState('idle')
7328
7368
  const [result, setResult] = useState(null)
7329
7369
  const [error, setError] = useState('')
7330
7370
  const [collapsed, setCollapsed] = useState({})
7331
- const [replaceOpen, setReplaceOpen] = useState(false)
7332
- const [replacement, setReplacement] = useState('')
7333
- const [replacing, setReplacing] = useState(false)
7371
+ const [ignored, setIgnored] = useState(() => new Set())
7334
7372
  const [confirming, setConfirming] = useState(null)
7373
+ const [replacing, setReplacing] = useState(false)
7335
7374
  const [replaceError, setReplaceError] = useState('')
7336
7375
  const [notice, setNotice] = useState('')
7337
7376
  const inputRef = useRef(null)
@@ -7362,13 +7401,20 @@ G.SearchPanel = function SearchPanel(props) {
7362
7401
  setStatus('loading')
7363
7402
  setError('')
7364
7403
  try {
7365
- const value = await G.api('fs.search', {
7404
+ const found = await G.api('fs.search', {
7366
7405
  root,
7367
7406
  query: text,
7368
- options: { caseSensitive, regex, maxFiles: 200, maxMatches: 500 },
7407
+ options: {
7408
+ caseSensitive,
7409
+ regex,
7410
+ include: includeText,
7411
+ exclude: excludeText,
7412
+ maxFiles: 200,
7413
+ maxMatches: 500,
7414
+ },
7369
7415
  })
7370
7416
  if (seq !== requestRef.current) return
7371
- setResult(value)
7417
+ setResult(found)
7372
7418
  setStatus('ready')
7373
7419
  } catch (err) {
7374
7420
  if (seq !== requestRef.current) return
@@ -7376,42 +7422,7 @@ G.SearchPanel = function SearchPanel(props) {
7376
7422
  setError(G.errorText(err))
7377
7423
  setStatus('error')
7378
7424
  }
7379
- }, [root, caseSensitive, regex])
7380
-
7381
- const requestReplace = useCallback((paths, count, files) => {
7382
- if (typeof root !== 'string' || root === '' || query.trim() === '' || replacing) return
7383
- setReplaceError('')
7384
- setNotice('')
7385
- setConfirming({ paths: Array.isArray(paths) ? paths : null, count: Number(count) || 0, files: Number(files) || 0 })
7386
- }, [root, query, replacing])
7387
-
7388
- const doReplace = useCallback(async () => {
7389
- const pending = confirming
7390
- if (pending === null || replacing) return
7391
- setConfirming(null)
7392
- setReplacing(true)
7393
- setReplaceError('')
7394
- setNotice('')
7395
- try {
7396
- await props.onBeforeReplace?.()
7397
- const value = await G.api('fs.replace', {
7398
- root,
7399
- query: query.trim(),
7400
- replacement,
7401
- options: { caseSensitive, regex },
7402
- paths: Array.isArray(pending.paths) ? pending.paths : undefined,
7403
- })
7404
- const count = Number(value?.replacedCount) || 0
7405
- const replacedFiles = Array.isArray(value?.replacedFiles) ? value.replacedFiles : []
7406
- setNotice(t('search.replaced', { count, files: replacedFiles.length }))
7407
- await props.onFilesChanged?.(replacedFiles)
7408
- await runSearch(query)
7409
- } catch (error) {
7410
- setReplaceError(G.errorText(error))
7411
- } finally {
7412
- setReplacing(false)
7413
- }
7414
- }, [confirming, replacing, props.onBeforeReplace, props.onFilesChanged, root, query, replacement, caseSensitive, regex, runSearch, t])
7425
+ }, [root, caseSensitive, regex, includeText, excludeText])
7415
7426
 
7416
7427
  /* 输入防抖搜索(回车立即搜索) */
7417
7428
  useEffect(() => {
@@ -7427,6 +7438,11 @@ G.SearchPanel = function SearchPanel(props) {
7427
7438
  return () => window.clearTimeout(timer)
7428
7439
  }, [query, runSearch, root])
7429
7440
 
7441
+ /* 搜索条件变化后,之前「忽略的匹配」不再适用 */
7442
+ useEffect(() => {
7443
+ setIgnored(new Set())
7444
+ }, [query, caseSensitive, regex, includeText, excludeText])
7445
+
7430
7446
  /* 快捷键/按钮打开搜索面板时聚焦输入框 */
7431
7447
  useEffect(() => {
7432
7448
  if (G.bus === null || G.bus === undefined || typeof G.bus.on !== 'function') return undefined
@@ -7437,12 +7453,28 @@ G.SearchPanel = function SearchPanel(props) {
7437
7453
  if (props.active === true) focusInput()
7438
7454
  }, [props.active, focusInput])
7439
7455
 
7456
+ const matchKey = useCallback((path, match) => `${path}:${match.line}:${match.column}`, [])
7457
+
7458
+ /* 过滤掉用户手动「忽略」的匹配,并重新汇总数量 */
7459
+ const view = useMemo(() => {
7460
+ if (result === null) return null
7461
+ const files = []
7462
+ let total = 0
7463
+ for (const file of result.files) {
7464
+ const matches = file.matches.filter((match) => !ignored.has(matchKey(file.path, match)))
7465
+ if (matches.length === 0) continue
7466
+ files.push({ path: file.path, matches })
7467
+ total += matches.length
7468
+ }
7469
+ return { ...result, files, total }
7470
+ }, [result, ignored, matchKey])
7471
+
7440
7472
  const summary = (() => {
7441
7473
  if (status === 'loading') return t('search.searching')
7442
- if (status === 'error' || result === null) return ''
7443
- if (result.total === 0) return result.truncated === true ? t('search.truncated') : t('search.noResult')
7444
- const base = t('search.summary', { count: result.total, files: result.files.length })
7445
- return result.truncated === true ? `${base} · ${t('search.truncated')}` : base
7474
+ if (status === 'error' || view === null) return ''
7475
+ if (view.total === 0) return view.truncated === true ? t('search.truncated') : t('search.noResult')
7476
+ const base = t('search.summary', { count: view.total, files: view.files.length })
7477
+ return view.truncated === true ? `${base} · ${t('search.truncated')}` : base
7446
7478
  })()
7447
7479
 
7448
7480
  const highlight = (match) => {
@@ -7458,6 +7490,85 @@ G.SearchPanel = function SearchPanel(props) {
7458
7490
  ]
7459
7491
  }
7460
7492
 
7493
+ const applyReplace = useCallback(async (payload) => {
7494
+ if (typeof root !== 'string' || root === '' || replacing) return
7495
+ setReplacing(true)
7496
+ setReplaceError('')
7497
+ setNotice('')
7498
+ try {
7499
+ await props.onBeforeReplace?.()
7500
+ const value = await G.api('fs.replace', payload)
7501
+ if (value?.stale === true) {
7502
+ setReplaceError(t('search.stale'))
7503
+ setIgnored(new Set())
7504
+ await runSearch(query)
7505
+ return
7506
+ }
7507
+ const count = Number(value?.replacedCount) || 0
7508
+ const files = Array.isArray(value?.replacedFiles) ? value.replacedFiles : []
7509
+ setNotice(t('search.replaced', { count, files: files.length }))
7510
+ await props.onFilesChanged?.(files)
7511
+ setIgnored(new Set())
7512
+ await runSearch(query)
7513
+ } catch (err) {
7514
+ setReplaceError(G.errorText(err))
7515
+ } finally {
7516
+ setReplacing(false)
7517
+ }
7518
+ }, [root, replacing, props.onBeforeReplace, props.onFilesChanged, t, runSearch, query])
7519
+
7520
+ const requestReplaceAll = () => {
7521
+ if (view === null || view.total === 0 || replacing) return
7522
+ setReplaceError('')
7523
+ setNotice('')
7524
+ setConfirming({ count: view.total, files: view.files.length })
7525
+ }
7526
+ const doReplaceAll = () => {
7527
+ const pending = confirming
7528
+ setConfirming(null)
7529
+ if (pending === null) return
7530
+ void applyReplace({
7531
+ root,
7532
+ query: query.trim(),
7533
+ replacement,
7534
+ options: { caseSensitive, regex, preserveCase, include: includeText, exclude: excludeText },
7535
+ })
7536
+ }
7537
+ const replaceFile = (file) => {
7538
+ if (replacing) return
7539
+ void applyReplace({
7540
+ root,
7541
+ query: query.trim(),
7542
+ replacement,
7543
+ options: { caseSensitive, regex, preserveCase },
7544
+ paths: [file.path],
7545
+ })
7546
+ }
7547
+ const replaceOne = (path, match) => {
7548
+ if (replacing) return
7549
+ void applyReplace({
7550
+ root,
7551
+ query: query.trim(),
7552
+ replacement,
7553
+ options: { caseSensitive, regex, preserveCase },
7554
+ target: { path, line: match.line, column: match.column },
7555
+ })
7556
+ }
7557
+ const dismiss = (path, match) => {
7558
+ setIgnored((current) => {
7559
+ const next = new Set(current)
7560
+ next.add(matchKey(path, match))
7561
+ return next
7562
+ })
7563
+ }
7564
+ const openFirst = () => {
7565
+ const file = view?.files?.[0]
7566
+ const match = file?.matches?.[0]
7567
+ if (file !== undefined && file !== null && match !== undefined && match !== null) {
7568
+ props.onOpenFile?.(file.path, match.line, match.column)
7569
+ }
7570
+ }
7571
+
7461
7572
  const body = (() => {
7462
7573
  if (typeof root !== 'string' || root === '') {
7463
7574
  return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, t('explorer.openProjectFirst'))
@@ -7467,25 +7578,31 @@ G.SearchPanel = function SearchPanel(props) {
7467
7578
  }
7468
7579
  if (status === 'idle') return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, t('search.idle'))
7469
7580
  if (status === 'loading') return h('div', { className: 'gufa-hint', style: { padding: '10px 12px', display: 'flex', gap: 8, alignItems: 'center' } }, h(G.Spinner), t('search.searching'))
7470
- if (result === null || result.total === 0) {
7471
- const emptyText = result !== null && result.truncated === true ? t('search.truncated') : t('search.noResult')
7581
+ if (view === null || view.total === 0) {
7582
+ const emptyText = view !== null && view.truncated === true ? t('search.truncated') : t('search.noResult')
7472
7583
  return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, emptyText)
7473
7584
  }
7474
7585
  return h(
7475
7586
  'div',
7476
7587
  { className: 'gufa-search-results' },
7477
- ...result.files.map((file) => {
7588
+ ...view.files.map((file) => {
7478
7589
  const isCollapsed = collapsed[file.path] === true
7479
7590
  return h(
7480
7591
  'div',
7481
7592
  { key: file.path, className: 'gufa-search-file' },
7482
7593
  h(
7483
- 'button',
7594
+ 'div',
7484
7595
  {
7485
- type: 'button',
7486
7596
  className: 'gufa-search-file-head',
7597
+ role: 'button',
7598
+ tabIndex: 0,
7487
7599
  title: file.path,
7488
7600
  onClick: () => setCollapsed((current) => ({ ...current, [file.path]: !isCollapsed })),
7601
+ onKeyDown: (event) => {
7602
+ if (event.key !== 'Enter' && event.key !== ' ') return
7603
+ event.preventDefault()
7604
+ setCollapsed((current) => ({ ...current, [file.path]: !isCollapsed }))
7605
+ },
7489
7606
  },
7490
7607
  h(G.Icon, { name: isCollapsed ? 'chevron-right' : 'chevron-down', size: 11 }),
7491
7608
  h('span', { className: 'gufa-search-file-name' }, G.basename(file.path)),
@@ -7501,7 +7618,7 @@ G.SearchPanel = function SearchPanel(props) {
7501
7618
  onClick: (event) => {
7502
7619
  event.stopPropagation()
7503
7620
  event.preventDefault()
7504
- requestReplace([file.path], file.matches.length, 1)
7621
+ replaceFile(file)
7505
7622
  },
7506
7623
  },
7507
7624
  h(G.Icon, { name: 'pencil', size: 11 }),
@@ -7512,16 +7629,56 @@ G.SearchPanel = function SearchPanel(props) {
7512
7629
  ? null
7513
7630
  : file.matches.map((match, index) =>
7514
7631
  h(
7515
- 'button',
7632
+ 'div',
7516
7633
  {
7517
7634
  key: `${file.path}:${match.line}:${match.column}:${index}`,
7518
- type: 'button',
7519
7635
  className: 'gufa-search-match',
7636
+ role: 'button',
7637
+ tabIndex: 0,
7520
7638
  title: `${file.path}:${match.line}:${match.column}`,
7521
7639
  onClick: () => props.onOpenFile?.(file.path, match.line, match.column),
7640
+ onKeyDown: (event) => {
7641
+ if (event.key !== 'Enter') return
7642
+ event.preventDefault()
7643
+ props.onOpenFile?.(file.path, match.line, match.column)
7644
+ },
7522
7645
  },
7523
7646
  h('span', { className: 'gufa-search-line' }, String(match.line)),
7524
7647
  h('span', { className: 'gufa-search-text' }, ...highlight(match)),
7648
+ h(
7649
+ 'span',
7650
+ { className: 'gufa-search-match-actions' },
7651
+ replaceOpen
7652
+ ? h(
7653
+ 'span',
7654
+ {
7655
+ className: 'gufa-search-row-action',
7656
+ title: t('search.replaceOne'),
7657
+ role: 'button',
7658
+ onClick: (event) => {
7659
+ event.stopPropagation()
7660
+ event.preventDefault()
7661
+ replaceOne(file.path, match)
7662
+ },
7663
+ },
7664
+ h(G.Icon, { name: 'pencil', size: 11 }),
7665
+ )
7666
+ : null,
7667
+ h(
7668
+ 'span',
7669
+ {
7670
+ className: 'gufa-search-row-action',
7671
+ title: t('search.dismiss'),
7672
+ role: 'button',
7673
+ onClick: (event) => {
7674
+ event.stopPropagation()
7675
+ event.preventDefault()
7676
+ dismiss(file.path, match)
7677
+ },
7678
+ },
7679
+ h(G.Icon, { name: 'close', size: 11 }),
7680
+ ),
7681
+ ),
7525
7682
  ),
7526
7683
  ),
7527
7684
  )
@@ -7581,7 +7738,6 @@ G.SearchPanel = function SearchPanel(props) {
7581
7738
  title: t('search.regex'),
7582
7739
  onClick: () => setRegex((value) => !value),
7583
7740
  }, '.*'),
7584
- h('span', { className: 'gufa-search-summary', title: summary }, summary),
7585
7741
  ),
7586
7742
  replaceOpen
7587
7743
  ? h(
@@ -7601,9 +7757,16 @@ G.SearchPanel = function SearchPanel(props) {
7601
7757
  onKeyDown: (event) => {
7602
7758
  if (event.key !== 'Enter') return
7603
7759
  event.preventDefault()
7604
- requestReplace(null, result?.total ?? 0, result?.files?.length ?? 0)
7760
+ requestReplaceAll()
7605
7761
  },
7606
7762
  }),
7763
+ h('button', {
7764
+ type: 'button',
7765
+ className: 'gufa-search-toggle',
7766
+ 'data-active': preserveCase ? '' : undefined,
7767
+ title: t('search.preserveCase'),
7768
+ onClick: () => setPreserveCase((value) => !value),
7769
+ }, 'AB'),
7607
7770
  ),
7608
7771
  h(
7609
7772
  'div',
@@ -7614,14 +7777,48 @@ G.SearchPanel = function SearchPanel(props) {
7614
7777
  type: 'button',
7615
7778
  className: 'gufa-search-action',
7616
7779
  'data-primary': '',
7617
- disabled: replacing || result === null || result.total === 0,
7618
- onClick: () => requestReplace(null, result?.total ?? 0, result?.files?.length ?? 0),
7780
+ disabled: replacing || view === null || view.total === 0,
7781
+ onClick: requestReplaceAll,
7619
7782
  },
7620
7783
  replacing ? t('search.replacing') : t('search.replaceAll'),
7621
7784
  ),
7622
7785
  ),
7623
7786
  )
7624
7787
  : null,
7788
+ h(
7789
+ 'label',
7790
+ { className: 'gufa-search-filter' },
7791
+ h('span', { className: 'gufa-search-filter-label' }, t('search.include')),
7792
+ h('input', {
7793
+ className: 'gufa-search-filter-input',
7794
+ type: 'text',
7795
+ value: includeText,
7796
+ placeholder: t('search.includePlaceholder'),
7797
+ spellCheck: false,
7798
+ onChange: (event) => setIncludeText(event.target.value),
7799
+ }),
7800
+ ),
7801
+ h(
7802
+ 'label',
7803
+ { className: 'gufa-search-filter' },
7804
+ h('span', { className: 'gufa-search-filter-label' }, t('search.exclude')),
7805
+ h('input', {
7806
+ className: 'gufa-search-filter-input',
7807
+ type: 'text',
7808
+ value: excludeText,
7809
+ placeholder: t('search.excludePlaceholder'),
7810
+ spellCheck: false,
7811
+ onChange: (event) => setExcludeText(event.target.value),
7812
+ }),
7813
+ ),
7814
+ h(
7815
+ 'div',
7816
+ { className: 'gufa-search-summarybar' },
7817
+ h('span', { className: 'gufa-search-summary', title: summary }, summary),
7818
+ view !== null && view.total > 0
7819
+ ? h('button', { type: 'button', className: 'gufa-search-link', onClick: openFirst }, t('search.openInEditor'))
7820
+ : null,
7821
+ ),
7625
7822
  confirming !== null
7626
7823
  ? h(
7627
7824
  'div',
@@ -7631,7 +7828,7 @@ G.SearchPanel = function SearchPanel(props) {
7631
7828
  'div',
7632
7829
  { className: 'gufa-search-confirm-actions' },
7633
7830
  h('button', { type: 'button', className: 'gufa-search-action', onClick: () => setConfirming(null) }, t('search.confirmNo')),
7634
- h('button', { type: 'button', className: 'gufa-search-action', 'data-primary': '', onClick: () => void doReplace() }, t('search.confirmYes')),
7831
+ h('button', { type: 'button', className: 'gufa-search-action', 'data-primary': '', onClick: doReplaceAll }, t('search.confirmYes')),
7635
7832
  ),
7636
7833
  )
7637
7834
  : null,
@@ -7,8 +7,8 @@
7
7
  <key>CFBundleDisplayName</key><string>古法编程悬浮层</string>
8
8
  <key>CFBundleExecutable</key><string>gufa-float</string>
9
9
  <key>CFBundlePackageType</key><string>APPL</string>
10
- <key>CFBundleShortVersionString</key><string>1.3.63</string>
11
- <key>CFBundleVersion</key><string>1.3.63</string>
10
+ <key>CFBundleShortVersionString</key><string>1.3.64</string>
11
+ <key>CFBundleVersion</key><string>1.3.64</string>
12
12
  <key>LSUIElement</key><true/>
13
13
  <key>NSHighResolutionCapable</key><true/>
14
14
  <key>NSPrincipalClass</key><string>NSApplication</string>
package/host/fs.js CHANGED
@@ -115,6 +115,70 @@ function searchExtensionOf(rel) {
115
115
  function escapeRegExp(text) {
116
116
  return String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
117
117
  }
118
+ /** 解析「包含/排除的文件」:数组或逗号/换行分隔字符串。 */
119
+ function readPatternList(value) {
120
+ if (Array.isArray(value)) return value.filter((item) => typeof item === 'string').map((item) => item.trim()).filter(Boolean)
121
+ if (typeof value === 'string') return value.split(/[,\n]/).map((item) => item.trim()).filter(Boolean)
122
+ return []
123
+ }
124
+ /** 把 VS Code 风格 glob 编译成正则(支持 **、*、?;无 / 的模式匹配任意层级 basename)。 */
125
+ function globToRegExp(pattern) {
126
+ let text = String(pattern).replace(/\\/g, '/').trim()
127
+ if (text === '') return null
128
+ if (text.startsWith('./')) text = text.slice(2)
129
+ let body = ''
130
+ for (let index = 0; index < text.length; index += 1) {
131
+ const char = text[index]
132
+ if (char === '*') {
133
+ if (text[index + 1] === '*') {
134
+ index += 1
135
+ if (text[index + 1] === '/') {
136
+ index += 1
137
+ body += '(?:.*/)?'
138
+ } else {
139
+ body += '.*'
140
+ }
141
+ } else {
142
+ body += '[^/]*'
143
+ }
144
+ continue
145
+ }
146
+ if (char === '?') {
147
+ body += '[^/]'
148
+ continue
149
+ }
150
+ body += char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
151
+ }
152
+ const hasSlash = text.includes('/')
153
+ const anchored = text.startsWith('/') ? `^/${body}$` : hasSlash ? `^${body}$` : `(?:^|/)${body}$`
154
+ try {
155
+ return new RegExp(anchored, 'i')
156
+ } catch {
157
+ return null
158
+ }
159
+ }
160
+ /** 构造路径过滤器:include 任一命中 + 不被 exclude 命中。 */
161
+ function buildPathFilter(options) {
162
+ const include = readPatternList(options.include).map(globToRegExp).filter(Boolean)
163
+ const exclude = readPatternList(options.exclude).map(globToRegExp).filter(Boolean)
164
+ return (rel) => {
165
+ if (include.length > 0 && !include.some((regex) => regex.test(rel))) return false
166
+ if (exclude.length > 0 && exclude.some((regex) => regex.test(rel))) return false
167
+ return true
168
+ }
169
+ }
170
+ /** VS Code 式「保留大小写」:全大写/全小写/首字母大写分别跟随匹配项。 */
171
+ function applyPreserveCase(matched, replacement) {
172
+ const text = String(matched ?? '')
173
+ if (text === '' || replacement === '') return replacement
174
+ const upper = text === text.toUpperCase() && text !== text.toLowerCase()
175
+ const lower = text === text.toLowerCase() && text !== text.toUpperCase()
176
+ if (upper) return replacement.toUpperCase()
177
+ if (lower) return replacement.toLowerCase()
178
+ if (/^[A-Z]/.test(text)) return replacement.charAt(0).toUpperCase() + replacement.slice(1)
179
+ return replacement
180
+ }
181
+
118
182
  async function isGitProject(root, project) {
119
183
  if (project !== null && project !== undefined && project.isGit === true) return true
120
184
  try {
@@ -342,6 +406,7 @@ export function register(router, env) {
342
406
  const options = body.options !== null && typeof body.options === 'object' ? body.options : {}
343
407
  const caseSensitive = options.caseSensitive === true
344
408
  const useRegex = options.regex === true
409
+ const pathFilter = buildPathFilter(options)
345
410
  const clampInt = (value, fallback, min, max) => {
346
411
  const number = Number(value)
347
412
  if (!Number.isFinite(number)) return fallback
@@ -442,6 +507,7 @@ export function register(router, env) {
442
507
  return matches
443
508
  }
444
509
  const acceptFile = async (rel) => {
510
+ if (!pathFilter(rel)) return
445
511
  const matches = await searchFile(rel)
446
512
  if (matches.length === 0) return
447
513
  files.push({ path: rel, matches })
@@ -530,10 +596,11 @@ export function register(router, env) {
530
596
  const root = project.root
531
597
  const query = typeof body.query === 'string' ? body.query.slice(0, 200) : ''
532
598
  const replacement = typeof body.replacement === 'string' ? body.replacement : ''
533
- if (query === '') return { replacedFiles: [], replacedCount: 0, failed: [], scanned: 0, truncated: false }
599
+ if (query === '') return { replacedFiles: [], replacedCount: 0, failed: [], scanned: 0, truncated: false, stale: false }
534
600
  const options = body.options !== null && typeof body.options === 'object' ? body.options : {}
535
601
  const caseSensitive = options.caseSensitive === true
536
602
  const useRegex = options.regex === true
603
+ const preserveCase = options.preserveCase === true
537
604
  const clampInt = (value, fallback, min, max) => {
538
605
  const number = Number(value)
539
606
  if (!Number.isFinite(number)) return fallback
@@ -550,6 +617,85 @@ export function register(router, env) {
550
617
  if (useRegex && new RegExp(query, caseSensitive ? '' : 'i').test('')) {
551
618
  throw new ApiError('bad-regex', '正则表达式不能匹配空字符串', 400)
552
619
  }
620
+ const fileSizeLimit = 2 * 1024 * 1024
621
+ const pathFilter = buildPathFilter(options)
622
+
623
+ /* 模式 A:只替换某一个匹配(搜索结果行上的替换按钮),用行/列定位。 */
624
+ const target = body.target !== null && typeof body.target === 'object' ? body.target : null
625
+ if (target !== null) {
626
+ const rawPath = typeof target.path === 'string' ? target.path : ''
627
+ const targetLine = Number(target.line)
628
+ const targetColumn = Number(target.column)
629
+ if (rawPath === '' || !Number.isInteger(targetLine) || targetLine < 1 || !Number.isInteger(targetColumn) || targetColumn < 1) {
630
+ throw badRequest('target.line / target.column is required')
631
+ }
632
+ let absolute
633
+ try {
634
+ absolute = await resolveProjectPath(root, rawPath)
635
+ } catch {
636
+ return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: true }
637
+ }
638
+ const info = await stat(absolute).catch(() => undefined)
639
+ if (info === undefined || !info.isFile() || info.size > fileSizeLimit) {
640
+ return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: true }
641
+ }
642
+ let buffer
643
+ try {
644
+ buffer = await readFile(absolute)
645
+ } catch {
646
+ return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: false }
647
+ }
648
+ if (buffer.includes(0)) return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: false }
649
+ const text = buffer.toString('utf8')
650
+ regex.lastIndex = 0
651
+ let found = null
652
+ let cursor = 0
653
+ let line = 1
654
+ let lineStart = 0
655
+ let match
656
+ while ((match = regex.exec(text)) !== null) {
657
+ if (match[0].length === 0) {
658
+ regex.lastIndex += 1
659
+ continue
660
+ }
661
+ const index = match.index
662
+ if (index > cursor) {
663
+ const chunk = text.slice(cursor, index)
664
+ const newlines = chunk.split('\n').length - 1
665
+ line += newlines
666
+ const lastNewline = chunk.lastIndexOf('\n')
667
+ if (lastNewline !== -1) lineStart = cursor + lastNewline + 1
668
+ cursor = index
669
+ }
670
+ const column = index - lineStart + 1
671
+ if (line === targetLine && column === targetColumn) {
672
+ found = { index, value: match[0] }
673
+ break
674
+ }
675
+ if (line > targetLine || (line === targetLine && column > targetColumn)) break
676
+ }
677
+ if (found === null) {
678
+ return { replacedFiles: [], replacedCount: 0, failed: [], scanned: 1, truncated: false, stale: true }
679
+ }
680
+ let replacedValue = replacement
681
+ if (useRegex) {
682
+ try {
683
+ replacedValue = found.value.replace(new RegExp(`^(?:${query})$`, caseSensitive ? '' : 'i'), replacement)
684
+ } catch {
685
+ replacedValue = replacement
686
+ }
687
+ }
688
+ if (preserveCase) replacedValue = applyPreserveCase(found.value, replacedValue)
689
+ const updated = `${text.slice(0, found.index)}${replacedValue}${text.slice(found.index + found.value.length)}`
690
+ try {
691
+ await writeFile(absolute, updated, 'utf8')
692
+ } catch {
693
+ return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: false }
694
+ }
695
+ return { replacedFiles: [relativePath(root, absolute)], replacedCount: 1, failed: [], scanned: 1, truncated: false, stale: false }
696
+ }
697
+
698
+ /* 模式 B:批量替换(全部替换 / 单个文件替换)。 */
553
699
  const pathsFilter = Array.isArray(body.paths)
554
700
  ? body.paths.filter((item) => typeof item === 'string' && item !== '').slice(0, 200)
555
701
  : null
@@ -558,20 +704,35 @@ export function register(router, env) {
558
704
  for (const raw of pathsFilter) {
559
705
  try {
560
706
  const absolute = await resolveProjectPath(root, raw)
561
- candidates.push(relativePath(root, absolute))
707
+ const rel = relativePath(root, absolute)
708
+ if (pathFilter(rel)) candidates.push(rel)
562
709
  } catch {
563
710
  /* 路径非法/已删除:跳过 */
564
711
  }
565
712
  }
566
713
  } else {
567
714
  const listed = await listProjectCandidates(root, project, 20000)
568
- candidates = listed.files
715
+ candidates = listed.files.filter((rel) => pathFilter(rel))
569
716
  }
570
- const fileSizeLimit = 2 * 1024 * 1024
571
717
  const replacedFiles = []
572
718
  const failed = []
573
719
  let replacedCount = 0
574
720
  let truncated = false
721
+ const expand = (matched, groups) => {
722
+ let output = replacement
723
+ if (useRegex) {
724
+ output = output
725
+ .replace(/\$\$/g, '\u0000')
726
+ .replace(/\$&/g, matched)
727
+ .replace(/\$(\d{1,2})/g, (piece, index) => {
728
+ const group = groups[Number(index) - 1]
729
+ return group === undefined ? piece : group
730
+ })
731
+ .replace(/\u0000/g, '$')
732
+ }
733
+ if (preserveCase) output = applyPreserveCase(matched, output)
734
+ return output
735
+ }
575
736
  for (const rel of candidates) {
576
737
  if (replacedCount >= maxReplacements) {
577
738
  truncated = true
@@ -598,7 +759,7 @@ export function register(router, env) {
598
759
  }
599
760
  let updated
600
761
  try {
601
- updated = text.replace(regex, useRegex ? replacement : () => replacement)
762
+ updated = text.replace(regex, (matched, ...args) => expand(matched, args.slice(0, Math.max(args.length - 2, 0))))
602
763
  } catch (error) {
603
764
  failed.push(rel)
604
765
  continue
@@ -612,7 +773,7 @@ export function register(router, env) {
612
773
  failed.push(rel)
613
774
  }
614
775
  }
615
- return { replacedFiles, replacedCount, failed, scanned: candidates.length, truncated }
776
+ return { replacedFiles, replacedCount, failed, scanned: candidates.length, truncated, stale: false }
616
777
  })
617
778
 
618
779
  router.method('fs.matchFiles', async (body) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-gufa-code",
3
- "version": "1.3.63",
3
+ "version": "1.3.64",
4
4
  "description": "古法编程 · AI 辅助:一键把 DSH 主面板变成 VS Code 风格 IDE(Monaco 编辑器、文件树、Git、SSH、真实终端 + 右侧官方会话);主张亲手写、每个改动心中有数,AI 是助理、你是第一责任人",
5
5
  "icon": "icon.svg",
6
6
  "keywords": [
@@ -184,6 +184,30 @@ try {
184
184
  emptyRegexRejected = error.code === 'bad-regex'
185
185
  }
186
186
  check('replace rejects empty-matching regex', emptyRegexRejected)
187
+ await call(router, 'fs.write', { root, path: 'replace.txt', content: 'foo FOO Foo\n', expectedRevision: dollarDoc.revision })
188
+ const cased = await call(router, 'fs.replace', { root, query: 'foo', replacement: 'bar', options: { preserveCase: true }, paths: ['replace.txt'] })
189
+ const casedDoc = await call(router, 'fs.read', { root, path: 'replace.txt' })
190
+ check('replace preserve case', cased.replacedCount === 3 && casedDoc.content === 'bar BAR Bar\n', JSON.stringify(casedDoc.content))
191
+ await call(router, 'fs.write', { root, path: 'replace.txt', content: 'one two two\n', expectedRevision: casedDoc.revision })
192
+ const single = await call(router, 'fs.replace', { root, query: 'two', replacement: 'X', target: { path: 'replace.txt', line: 1, column: 5 } })
193
+ const singleDoc = await call(router, 'fs.read', { root, path: 'replace.txt' })
194
+ check('replace single match by line/column', single.replacedCount === 1 && singleDoc.content === 'one X two\n', JSON.stringify(singleDoc.content))
195
+ const stale = await call(router, 'fs.replace', { root, query: 'two', replacement: 'Y', target: { path: 'replace.txt', line: 1, column: 50 } })
196
+ check('replace single stale target', stale.replacedCount === 0 && stale.stale === true)
197
+ await call(router, 'fs.create', { root, path: 'inc.md', kind: 'file' })
198
+ await call(router, 'fs.create', { root, path: 'inc.txt', kind: 'file' })
199
+ await call(router, 'fs.write', { root, path: 'inc.md', content: 'ZzIncToken here\n' })
200
+ await call(router, 'fs.write', { root, path: 'inc.txt', content: 'ZzIncToken here\n' })
201
+ const includeSearch = await call(router, 'fs.search', { root, query: 'ZzIncToken', options: { include: '*.md' } })
202
+ check('search include filter', includeSearch.files.length === 1 && includeSearch.files[0].path === 'inc.md', JSON.stringify(includeSearch.files.map((file) => file.path)))
203
+ const excludeSearch = await call(router, 'fs.search', { root, query: 'ZzIncToken', options: { exclude: '*.md' } })
204
+ check('search exclude filter', excludeSearch.files.length === 1 && excludeSearch.files[0].path === 'inc.txt', JSON.stringify(excludeSearch.files.map((file) => file.path)))
205
+ const includeReplace = await call(router, 'fs.replace', { root, query: 'ZzIncToken', replacement: 'Included', options: { include: '*.md' } })
206
+ const incMd = await call(router, 'fs.read', { root, path: 'inc.md' })
207
+ const incTxt = await call(router, 'fs.read', { root, path: 'inc.txt' })
208
+ check('replace include filter', includeReplace.replacedCount === 1 && incMd.content === 'Included here\n' && incTxt.content === 'ZzIncToken here\n', JSON.stringify([incMd.content, incTxt.content]))
209
+ await call(router, 'fs.delete', { root, path: 'inc.md' })
210
+ await call(router, 'fs.delete', { root, path: 'inc.txt' })
187
211
  await call(router, 'fs.delete', { root, path: 'replace.txt' })
188
212
  writeFileSync(join(projectDir, 'src/main.ts'), 'const answer = 43\n')
189
213
  const diff = await call(router, 'git.diff', { root, path: 'src/main.ts' })
@@ -191,10 +191,19 @@ const DICT_ZH = {
191
191
  'search.searching': '搜索中…',
192
192
  'search.idle': '输入关键字搜索整个项目',
193
193
  'search.noResult': '没有找到匹配结果',
194
- 'search.summary': '{count} 个结果 · {files} 个文件',
194
+ 'search.summary': '{files} 个文件中有 {count} 个结果',
195
195
  'search.truncated': '结果过多已截断,请缩小范围',
196
196
  'search.error': '搜索失败',
197
197
  'search.replaceToggle': '显示/隐藏替换',
198
+ 'search.include': '包含的文件',
199
+ 'search.exclude': '排除的文件',
200
+ 'search.includePlaceholder': '例如 *.js, src/**',
201
+ 'search.excludePlaceholder': '例如 **/*.min.js, test/**',
202
+ 'search.preserveCase': '保留大小写(AB)',
203
+ 'search.replaceOne': '替换此匹配',
204
+ 'search.dismiss': '忽略此匹配',
205
+ 'search.openInEditor': '在编辑器中打开',
206
+ 'search.stale': '匹配位置已变化,已重新搜索',
198
207
  'search.replacePlaceholder': '替换为…',
199
208
  'search.replaceAll': '全部替换',
200
209
  'search.replaceThisFile': '替换此文件',
@@ -433,10 +442,19 @@ const DICT_EN = {
433
442
  'search.searching': 'Searching…',
434
443
  'search.idle': 'Type a keyword to search the whole project',
435
444
  'search.noResult': 'No matches found',
436
- 'search.summary': '{count} results · {files} files',
445
+ 'search.summary': '{count} results in {files} files',
437
446
  'search.truncated': 'Too many results, truncated — narrow the query',
438
447
  'search.error': 'Search failed',
439
448
  'search.replaceToggle': 'Show / hide replace',
449
+ 'search.include': 'files to include',
450
+ 'search.exclude': 'files to exclude',
451
+ 'search.includePlaceholder': 'e.g. *.js, src/**',
452
+ 'search.excludePlaceholder': 'e.g. **/*.min.js, test/**',
453
+ 'search.preserveCase': 'Preserve case (AB)',
454
+ 'search.replaceOne': 'Replace this match',
455
+ 'search.dismiss': 'Dismiss this match',
456
+ 'search.openInEditor': 'Open in editor',
457
+ 'search.stale': 'Match moved; search refreshed',
440
458
  'search.replacePlaceholder': 'Replace with…',
441
459
  'search.replaceAll': 'Replace all',
442
460
  'search.replaceThisFile': 'Replace in this file',
@@ -1729,6 +1747,23 @@ html[data-platform=darwin][data-fullscreen] .gufa-titlebar{height:44px;padding-l
1729
1747
  .gufa-search-error{margin:6px 8px 0;padding:5px 9px;border-radius:7px;font-size:11.5px;color:var(--dsw-alias-state-error-primary);border:1px solid color-mix(in srgb, var(--dsw-alias-state-error-primary) 45%, transparent)}
1730
1748
  .gufa-search-file-replace{flex:0 0 auto;display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;margin-left:2px;border-radius:4px;color:var(--gufa-text-dim);cursor:pointer}
1731
1749
  .gufa-search-file-replace:hover{background:var(--gufa-active);color:var(--gufa-text)}
1750
+ .gufa-search-file-replace{opacity:0;transition:opacity .12s ease}
1751
+ .gufa-search-file-head:hover .gufa-search-file-replace,.gufa-search-file-head:focus-within .gufa-search-file-replace{opacity:1}
1752
+ .gufa-search-match{min-height:23px;align-items:center}
1753
+ .gufa-search-match-actions{display:none;flex:0 0 auto;align-items:center;gap:4px;margin-left:auto}
1754
+ .gufa-search-match:hover .gufa-search-match-actions,.gufa-search-match:focus-within .gufa-search-match-actions{display:flex}
1755
+ .gufa-search-row-action{display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;border-radius:4px;color:var(--gufa-text-dim);cursor:pointer}
1756
+ .gufa-search-row-action:hover{background:var(--gufa-active);color:var(--gufa-text)}
1757
+ .gufa-search-filter{display:flex;flex-direction:column;gap:3px;margin-top:5px}
1758
+ .gufa-search-filter-label{font-size:10.5px;color:var(--gufa-text-dim)}
1759
+ .gufa-search-filter-input{width:100%;height:24px;padding:0 8px;border-radius:6px;border:1px solid var(--gufa-border);background:var(--gufa-layer2);color:var(--gufa-text);font-size:11.5px;outline:none}
1760
+ .gufa-search-filter-input:focus{border-color:var(--gufa-accent)}
1761
+ .gufa-search-filter-input::placeholder{color:var(--gufa-text-dim)}
1762
+ .gufa-search-summarybar{display:flex;align-items:center;gap:8px}
1763
+ .gufa-search-summarybar .gufa-search-summary{flex:1 1 auto;margin-left:0}
1764
+ .gufa-search-summarybar .gufa-search-link{flex:0 0 auto}
1765
+ .gufa-search-link{border:0;background:transparent;color:var(--gufa-accent);font-size:11px;cursor:pointer;padding:0}
1766
+ .gufa-search-link:hover{text-decoration:underline}
1732
1767
 
1733
1768
  .gufa-tree-chevron{width:14px;flex:0 0 auto;opacity:.7;display:flex;align-items:center;justify-content:center}
1734
1769
  .gufa-tree-icon{width:15px;flex:0 0 auto;opacity:.85;display:flex;align-items:center;justify-content:center}
@@ -515,16 +515,20 @@ G.SearchPanel = function SearchPanel(props) {
515
515
  const t = G.useT()
516
516
  const root = props.root
517
517
  const [query, setQuery] = useState('')
518
+ const [replacement, setReplacement] = useState('')
518
519
  const [caseSensitive, setCaseSensitive] = useState(false)
519
520
  const [regex, setRegex] = useState(false)
521
+ const [preserveCase, setPreserveCase] = useState(false)
522
+ const [includeText, setIncludeText] = useState('')
523
+ const [excludeText, setExcludeText] = useState('')
524
+ const [replaceOpen, setReplaceOpen] = useState(false)
520
525
  const [status, setStatus] = useState('idle')
521
526
  const [result, setResult] = useState(null)
522
527
  const [error, setError] = useState('')
523
528
  const [collapsed, setCollapsed] = useState({})
524
- const [replaceOpen, setReplaceOpen] = useState(false)
525
- const [replacement, setReplacement] = useState('')
526
- const [replacing, setReplacing] = useState(false)
529
+ const [ignored, setIgnored] = useState(() => new Set())
527
530
  const [confirming, setConfirming] = useState(null)
531
+ const [replacing, setReplacing] = useState(false)
528
532
  const [replaceError, setReplaceError] = useState('')
529
533
  const [notice, setNotice] = useState('')
530
534
  const inputRef = useRef(null)
@@ -555,13 +559,20 @@ G.SearchPanel = function SearchPanel(props) {
555
559
  setStatus('loading')
556
560
  setError('')
557
561
  try {
558
- const value = await G.api('fs.search', {
562
+ const found = await G.api('fs.search', {
559
563
  root,
560
564
  query: text,
561
- options: { caseSensitive, regex, maxFiles: 200, maxMatches: 500 },
565
+ options: {
566
+ caseSensitive,
567
+ regex,
568
+ include: includeText,
569
+ exclude: excludeText,
570
+ maxFiles: 200,
571
+ maxMatches: 500,
572
+ },
562
573
  })
563
574
  if (seq !== requestRef.current) return
564
- setResult(value)
575
+ setResult(found)
565
576
  setStatus('ready')
566
577
  } catch (err) {
567
578
  if (seq !== requestRef.current) return
@@ -569,42 +580,7 @@ G.SearchPanel = function SearchPanel(props) {
569
580
  setError(G.errorText(err))
570
581
  setStatus('error')
571
582
  }
572
- }, [root, caseSensitive, regex])
573
-
574
- const requestReplace = useCallback((paths, count, files) => {
575
- if (typeof root !== 'string' || root === '' || query.trim() === '' || replacing) return
576
- setReplaceError('')
577
- setNotice('')
578
- setConfirming({ paths: Array.isArray(paths) ? paths : null, count: Number(count) || 0, files: Number(files) || 0 })
579
- }, [root, query, replacing])
580
-
581
- const doReplace = useCallback(async () => {
582
- const pending = confirming
583
- if (pending === null || replacing) return
584
- setConfirming(null)
585
- setReplacing(true)
586
- setReplaceError('')
587
- setNotice('')
588
- try {
589
- await props.onBeforeReplace?.()
590
- const value = await G.api('fs.replace', {
591
- root,
592
- query: query.trim(),
593
- replacement,
594
- options: { caseSensitive, regex },
595
- paths: Array.isArray(pending.paths) ? pending.paths : undefined,
596
- })
597
- const count = Number(value?.replacedCount) || 0
598
- const replacedFiles = Array.isArray(value?.replacedFiles) ? value.replacedFiles : []
599
- setNotice(t('search.replaced', { count, files: replacedFiles.length }))
600
- await props.onFilesChanged?.(replacedFiles)
601
- await runSearch(query)
602
- } catch (error) {
603
- setReplaceError(G.errorText(error))
604
- } finally {
605
- setReplacing(false)
606
- }
607
- }, [confirming, replacing, props.onBeforeReplace, props.onFilesChanged, root, query, replacement, caseSensitive, regex, runSearch, t])
583
+ }, [root, caseSensitive, regex, includeText, excludeText])
608
584
 
609
585
  /* 输入防抖搜索(回车立即搜索) */
610
586
  useEffect(() => {
@@ -620,6 +596,11 @@ G.SearchPanel = function SearchPanel(props) {
620
596
  return () => window.clearTimeout(timer)
621
597
  }, [query, runSearch, root])
622
598
 
599
+ /* 搜索条件变化后,之前「忽略的匹配」不再适用 */
600
+ useEffect(() => {
601
+ setIgnored(new Set())
602
+ }, [query, caseSensitive, regex, includeText, excludeText])
603
+
623
604
  /* 快捷键/按钮打开搜索面板时聚焦输入框 */
624
605
  useEffect(() => {
625
606
  if (G.bus === null || G.bus === undefined || typeof G.bus.on !== 'function') return undefined
@@ -630,12 +611,28 @@ G.SearchPanel = function SearchPanel(props) {
630
611
  if (props.active === true) focusInput()
631
612
  }, [props.active, focusInput])
632
613
 
614
+ const matchKey = useCallback((path, match) => `${path}:${match.line}:${match.column}`, [])
615
+
616
+ /* 过滤掉用户手动「忽略」的匹配,并重新汇总数量 */
617
+ const view = useMemo(() => {
618
+ if (result === null) return null
619
+ const files = []
620
+ let total = 0
621
+ for (const file of result.files) {
622
+ const matches = file.matches.filter((match) => !ignored.has(matchKey(file.path, match)))
623
+ if (matches.length === 0) continue
624
+ files.push({ path: file.path, matches })
625
+ total += matches.length
626
+ }
627
+ return { ...result, files, total }
628
+ }, [result, ignored, matchKey])
629
+
633
630
  const summary = (() => {
634
631
  if (status === 'loading') return t('search.searching')
635
- if (status === 'error' || result === null) return ''
636
- if (result.total === 0) return result.truncated === true ? t('search.truncated') : t('search.noResult')
637
- const base = t('search.summary', { count: result.total, files: result.files.length })
638
- return result.truncated === true ? `${base} · ${t('search.truncated')}` : base
632
+ if (status === 'error' || view === null) return ''
633
+ if (view.total === 0) return view.truncated === true ? t('search.truncated') : t('search.noResult')
634
+ const base = t('search.summary', { count: view.total, files: view.files.length })
635
+ return view.truncated === true ? `${base} · ${t('search.truncated')}` : base
639
636
  })()
640
637
 
641
638
  const highlight = (match) => {
@@ -651,6 +648,85 @@ G.SearchPanel = function SearchPanel(props) {
651
648
  ]
652
649
  }
653
650
 
651
+ const applyReplace = useCallback(async (payload) => {
652
+ if (typeof root !== 'string' || root === '' || replacing) return
653
+ setReplacing(true)
654
+ setReplaceError('')
655
+ setNotice('')
656
+ try {
657
+ await props.onBeforeReplace?.()
658
+ const value = await G.api('fs.replace', payload)
659
+ if (value?.stale === true) {
660
+ setReplaceError(t('search.stale'))
661
+ setIgnored(new Set())
662
+ await runSearch(query)
663
+ return
664
+ }
665
+ const count = Number(value?.replacedCount) || 0
666
+ const files = Array.isArray(value?.replacedFiles) ? value.replacedFiles : []
667
+ setNotice(t('search.replaced', { count, files: files.length }))
668
+ await props.onFilesChanged?.(files)
669
+ setIgnored(new Set())
670
+ await runSearch(query)
671
+ } catch (err) {
672
+ setReplaceError(G.errorText(err))
673
+ } finally {
674
+ setReplacing(false)
675
+ }
676
+ }, [root, replacing, props.onBeforeReplace, props.onFilesChanged, t, runSearch, query])
677
+
678
+ const requestReplaceAll = () => {
679
+ if (view === null || view.total === 0 || replacing) return
680
+ setReplaceError('')
681
+ setNotice('')
682
+ setConfirming({ count: view.total, files: view.files.length })
683
+ }
684
+ const doReplaceAll = () => {
685
+ const pending = confirming
686
+ setConfirming(null)
687
+ if (pending === null) return
688
+ void applyReplace({
689
+ root,
690
+ query: query.trim(),
691
+ replacement,
692
+ options: { caseSensitive, regex, preserveCase, include: includeText, exclude: excludeText },
693
+ })
694
+ }
695
+ const replaceFile = (file) => {
696
+ if (replacing) return
697
+ void applyReplace({
698
+ root,
699
+ query: query.trim(),
700
+ replacement,
701
+ options: { caseSensitive, regex, preserveCase },
702
+ paths: [file.path],
703
+ })
704
+ }
705
+ const replaceOne = (path, match) => {
706
+ if (replacing) return
707
+ void applyReplace({
708
+ root,
709
+ query: query.trim(),
710
+ replacement,
711
+ options: { caseSensitive, regex, preserveCase },
712
+ target: { path, line: match.line, column: match.column },
713
+ })
714
+ }
715
+ const dismiss = (path, match) => {
716
+ setIgnored((current) => {
717
+ const next = new Set(current)
718
+ next.add(matchKey(path, match))
719
+ return next
720
+ })
721
+ }
722
+ const openFirst = () => {
723
+ const file = view?.files?.[0]
724
+ const match = file?.matches?.[0]
725
+ if (file !== undefined && file !== null && match !== undefined && match !== null) {
726
+ props.onOpenFile?.(file.path, match.line, match.column)
727
+ }
728
+ }
729
+
654
730
  const body = (() => {
655
731
  if (typeof root !== 'string' || root === '') {
656
732
  return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, t('explorer.openProjectFirst'))
@@ -660,25 +736,31 @@ G.SearchPanel = function SearchPanel(props) {
660
736
  }
661
737
  if (status === 'idle') return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, t('search.idle'))
662
738
  if (status === 'loading') return h('div', { className: 'gufa-hint', style: { padding: '10px 12px', display: 'flex', gap: 8, alignItems: 'center' } }, h(G.Spinner), t('search.searching'))
663
- if (result === null || result.total === 0) {
664
- const emptyText = result !== null && result.truncated === true ? t('search.truncated') : t('search.noResult')
739
+ if (view === null || view.total === 0) {
740
+ const emptyText = view !== null && view.truncated === true ? t('search.truncated') : t('search.noResult')
665
741
  return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, emptyText)
666
742
  }
667
743
  return h(
668
744
  'div',
669
745
  { className: 'gufa-search-results' },
670
- ...result.files.map((file) => {
746
+ ...view.files.map((file) => {
671
747
  const isCollapsed = collapsed[file.path] === true
672
748
  return h(
673
749
  'div',
674
750
  { key: file.path, className: 'gufa-search-file' },
675
751
  h(
676
- 'button',
752
+ 'div',
677
753
  {
678
- type: 'button',
679
754
  className: 'gufa-search-file-head',
755
+ role: 'button',
756
+ tabIndex: 0,
680
757
  title: file.path,
681
758
  onClick: () => setCollapsed((current) => ({ ...current, [file.path]: !isCollapsed })),
759
+ onKeyDown: (event) => {
760
+ if (event.key !== 'Enter' && event.key !== ' ') return
761
+ event.preventDefault()
762
+ setCollapsed((current) => ({ ...current, [file.path]: !isCollapsed }))
763
+ },
682
764
  },
683
765
  h(G.Icon, { name: isCollapsed ? 'chevron-right' : 'chevron-down', size: 11 }),
684
766
  h('span', { className: 'gufa-search-file-name' }, G.basename(file.path)),
@@ -694,7 +776,7 @@ G.SearchPanel = function SearchPanel(props) {
694
776
  onClick: (event) => {
695
777
  event.stopPropagation()
696
778
  event.preventDefault()
697
- requestReplace([file.path], file.matches.length, 1)
779
+ replaceFile(file)
698
780
  },
699
781
  },
700
782
  h(G.Icon, { name: 'pencil', size: 11 }),
@@ -705,16 +787,56 @@ G.SearchPanel = function SearchPanel(props) {
705
787
  ? null
706
788
  : file.matches.map((match, index) =>
707
789
  h(
708
- 'button',
790
+ 'div',
709
791
  {
710
792
  key: `${file.path}:${match.line}:${match.column}:${index}`,
711
- type: 'button',
712
793
  className: 'gufa-search-match',
794
+ role: 'button',
795
+ tabIndex: 0,
713
796
  title: `${file.path}:${match.line}:${match.column}`,
714
797
  onClick: () => props.onOpenFile?.(file.path, match.line, match.column),
798
+ onKeyDown: (event) => {
799
+ if (event.key !== 'Enter') return
800
+ event.preventDefault()
801
+ props.onOpenFile?.(file.path, match.line, match.column)
802
+ },
715
803
  },
716
804
  h('span', { className: 'gufa-search-line' }, String(match.line)),
717
805
  h('span', { className: 'gufa-search-text' }, ...highlight(match)),
806
+ h(
807
+ 'span',
808
+ { className: 'gufa-search-match-actions' },
809
+ replaceOpen
810
+ ? h(
811
+ 'span',
812
+ {
813
+ className: 'gufa-search-row-action',
814
+ title: t('search.replaceOne'),
815
+ role: 'button',
816
+ onClick: (event) => {
817
+ event.stopPropagation()
818
+ event.preventDefault()
819
+ replaceOne(file.path, match)
820
+ },
821
+ },
822
+ h(G.Icon, { name: 'pencil', size: 11 }),
823
+ )
824
+ : null,
825
+ h(
826
+ 'span',
827
+ {
828
+ className: 'gufa-search-row-action',
829
+ title: t('search.dismiss'),
830
+ role: 'button',
831
+ onClick: (event) => {
832
+ event.stopPropagation()
833
+ event.preventDefault()
834
+ dismiss(file.path, match)
835
+ },
836
+ },
837
+ h(G.Icon, { name: 'close', size: 11 }),
838
+ ),
839
+ ),
718
840
  ),
719
841
  ),
720
842
  )
@@ -774,7 +896,6 @@ G.SearchPanel = function SearchPanel(props) {
774
896
  title: t('search.regex'),
775
897
  onClick: () => setRegex((value) => !value),
776
898
  }, '.*'),
777
- h('span', { className: 'gufa-search-summary', title: summary }, summary),
778
899
  ),
779
900
  replaceOpen
780
901
  ? h(
@@ -794,9 +915,16 @@ G.SearchPanel = function SearchPanel(props) {
794
915
  onKeyDown: (event) => {
795
916
  if (event.key !== 'Enter') return
796
917
  event.preventDefault()
797
- requestReplace(null, result?.total ?? 0, result?.files?.length ?? 0)
918
+ requestReplaceAll()
798
919
  },
799
920
  }),
921
+ h('button', {
922
+ type: 'button',
923
+ className: 'gufa-search-toggle',
924
+ 'data-active': preserveCase ? '' : undefined,
925
+ title: t('search.preserveCase'),
926
+ onClick: () => setPreserveCase((value) => !value),
927
+ }, 'AB'),
800
928
  ),
801
929
  h(
802
930
  'div',
@@ -807,14 +935,48 @@ G.SearchPanel = function SearchPanel(props) {
807
935
  type: 'button',
808
936
  className: 'gufa-search-action',
809
937
  'data-primary': '',
810
- disabled: replacing || result === null || result.total === 0,
811
- onClick: () => requestReplace(null, result?.total ?? 0, result?.files?.length ?? 0),
938
+ disabled: replacing || view === null || view.total === 0,
939
+ onClick: requestReplaceAll,
812
940
  },
813
941
  replacing ? t('search.replacing') : t('search.replaceAll'),
814
942
  ),
815
943
  ),
816
944
  )
817
945
  : null,
946
+ h(
947
+ 'label',
948
+ { className: 'gufa-search-filter' },
949
+ h('span', { className: 'gufa-search-filter-label' }, t('search.include')),
950
+ h('input', {
951
+ className: 'gufa-search-filter-input',
952
+ type: 'text',
953
+ value: includeText,
954
+ placeholder: t('search.includePlaceholder'),
955
+ spellCheck: false,
956
+ onChange: (event) => setIncludeText(event.target.value),
957
+ }),
958
+ ),
959
+ h(
960
+ 'label',
961
+ { className: 'gufa-search-filter' },
962
+ h('span', { className: 'gufa-search-filter-label' }, t('search.exclude')),
963
+ h('input', {
964
+ className: 'gufa-search-filter-input',
965
+ type: 'text',
966
+ value: excludeText,
967
+ placeholder: t('search.excludePlaceholder'),
968
+ spellCheck: false,
969
+ onChange: (event) => setExcludeText(event.target.value),
970
+ }),
971
+ ),
972
+ h(
973
+ 'div',
974
+ { className: 'gufa-search-summarybar' },
975
+ h('span', { className: 'gufa-search-summary', title: summary }, summary),
976
+ view !== null && view.total > 0
977
+ ? h('button', { type: 'button', className: 'gufa-search-link', onClick: openFirst }, t('search.openInEditor'))
978
+ : null,
979
+ ),
818
980
  confirming !== null
819
981
  ? h(
820
982
  'div',
@@ -824,7 +986,7 @@ G.SearchPanel = function SearchPanel(props) {
824
986
  'div',
825
987
  { className: 'gufa-search-confirm-actions' },
826
988
  h('button', { type: 'button', className: 'gufa-search-action', onClick: () => setConfirming(null) }, t('search.confirmNo')),
827
- h('button', { type: 'button', className: 'gufa-search-action', 'data-primary': '', onClick: () => void doReplace() }, t('search.confirmYes')),
989
+ h('button', { type: 'button', className: 'gufa-search-action', 'data-primary': '', onClick: doReplaceAll }, t('search.confirmYes')),
828
990
  ),
829
991
  )
830
992
  : null,