dsh-gufa-code 1.3.62 → 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.
@@ -515,12 +515,22 @@ 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({})
529
+ const [ignored, setIgnored] = useState(() => new Set())
530
+ const [confirming, setConfirming] = useState(null)
531
+ const [replacing, setReplacing] = useState(false)
532
+ const [replaceError, setReplaceError] = useState('')
533
+ const [notice, setNotice] = useState('')
524
534
  const inputRef = useRef(null)
525
535
  const requestRef = useRef(0)
526
536
 
@@ -549,13 +559,20 @@ G.SearchPanel = function SearchPanel(props) {
549
559
  setStatus('loading')
550
560
  setError('')
551
561
  try {
552
- const value = await G.api('fs.search', {
562
+ const found = await G.api('fs.search', {
553
563
  root,
554
564
  query: text,
555
- 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
+ },
556
573
  })
557
574
  if (seq !== requestRef.current) return
558
- setResult(value)
575
+ setResult(found)
559
576
  setStatus('ready')
560
577
  } catch (err) {
561
578
  if (seq !== requestRef.current) return
@@ -563,7 +580,7 @@ G.SearchPanel = function SearchPanel(props) {
563
580
  setError(G.errorText(err))
564
581
  setStatus('error')
565
582
  }
566
- }, [root, caseSensitive, regex])
583
+ }, [root, caseSensitive, regex, includeText, excludeText])
567
584
 
568
585
  /* 输入防抖搜索(回车立即搜索) */
569
586
  useEffect(() => {
@@ -579,6 +596,11 @@ G.SearchPanel = function SearchPanel(props) {
579
596
  return () => window.clearTimeout(timer)
580
597
  }, [query, runSearch, root])
581
598
 
599
+ /* 搜索条件变化后,之前「忽略的匹配」不再适用 */
600
+ useEffect(() => {
601
+ setIgnored(new Set())
602
+ }, [query, caseSensitive, regex, includeText, excludeText])
603
+
582
604
  /* 快捷键/按钮打开搜索面板时聚焦输入框 */
583
605
  useEffect(() => {
584
606
  if (G.bus === null || G.bus === undefined || typeof G.bus.on !== 'function') return undefined
@@ -589,12 +611,28 @@ G.SearchPanel = function SearchPanel(props) {
589
611
  if (props.active === true) focusInput()
590
612
  }, [props.active, focusInput])
591
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
+
592
630
  const summary = (() => {
593
631
  if (status === 'loading') return t('search.searching')
594
- if (status === 'error' || result === null) return ''
595
- if (result.total === 0) return result.truncated === true ? t('search.truncated') : t('search.noResult')
596
- const base = t('search.summary', { count: result.total, files: result.files.length })
597
- 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
598
636
  })()
599
637
 
600
638
  const highlight = (match) => {
@@ -610,6 +648,85 @@ G.SearchPanel = function SearchPanel(props) {
610
648
  ]
611
649
  }
612
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
+
613
730
  const body = (() => {
614
731
  if (typeof root !== 'string' || root === '') {
615
732
  return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, t('explorer.openProjectFirst'))
@@ -619,45 +736,107 @@ G.SearchPanel = function SearchPanel(props) {
619
736
  }
620
737
  if (status === 'idle') return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, t('search.idle'))
621
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'))
622
- if (result === null || result.total === 0) {
623
- 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')
624
741
  return h('div', { className: 'gufa-hint', style: { padding: '10px 12px' } }, emptyText)
625
742
  }
626
743
  return h(
627
744
  'div',
628
745
  { className: 'gufa-search-results' },
629
- ...result.files.map((file) => {
746
+ ...view.files.map((file) => {
630
747
  const isCollapsed = collapsed[file.path] === true
631
748
  return h(
632
749
  'div',
633
750
  { key: file.path, className: 'gufa-search-file' },
634
751
  h(
635
- 'button',
752
+ 'div',
636
753
  {
637
- type: 'button',
638
754
  className: 'gufa-search-file-head',
755
+ role: 'button',
756
+ tabIndex: 0,
639
757
  title: file.path,
640
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
+ },
641
764
  },
642
765
  h(G.Icon, { name: isCollapsed ? 'chevron-right' : 'chevron-down', size: 11 }),
643
766
  h('span', { className: 'gufa-search-file-name' }, G.basename(file.path)),
644
767
  h('span', { className: 'gufa-search-file-dir' }, G.dirname(file.path)),
645
768
  h('span', { className: 'gufa-search-file-count' }, String(file.matches.length)),
769
+ replaceOpen
770
+ ? h(
771
+ 'span',
772
+ {
773
+ className: 'gufa-search-file-replace',
774
+ title: t('search.replaceThisFile'),
775
+ role: 'button',
776
+ onClick: (event) => {
777
+ event.stopPropagation()
778
+ event.preventDefault()
779
+ replaceFile(file)
780
+ },
781
+ },
782
+ h(G.Icon, { name: 'pencil', size: 11 }),
783
+ )
784
+ : null,
646
785
  ),
647
786
  isCollapsed
648
787
  ? null
649
788
  : file.matches.map((match, index) =>
650
789
  h(
651
- 'button',
790
+ 'div',
652
791
  {
653
792
  key: `${file.path}:${match.line}:${match.column}:${index}`,
654
- type: 'button',
655
793
  className: 'gufa-search-match',
794
+ role: 'button',
795
+ tabIndex: 0,
656
796
  title: `${file.path}:${match.line}:${match.column}`,
657
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
+ },
658
803
  },
659
804
  h('span', { className: 'gufa-search-line' }, String(match.line)),
660
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
+ ),
661
840
  ),
662
841
  ),
663
842
  )
@@ -698,6 +877,7 @@ G.SearchPanel = function SearchPanel(props) {
698
877
  query === ''
699
878
  ? null
700
879
  : h(G.IconButton, { title: t('search.clear'), onClick: () => { setQuery(''); focusInput() } }, h(G.Icon, { name: 'close', size: 11 })),
880
+ h(G.IconButton, { title: t('search.replaceToggle'), onClick: () => setReplaceOpen((value) => !value) }, h(G.Icon, { name: replaceOpen ? 'chevron-up' : 'chevron-down', size: 12 })),
701
881
  ),
702
882
  h(
703
883
  'div',
@@ -716,8 +896,102 @@ G.SearchPanel = function SearchPanel(props) {
716
896
  title: t('search.regex'),
717
897
  onClick: () => setRegex((value) => !value),
718
898
  }, '.*'),
899
+ ),
900
+ replaceOpen
901
+ ? h(
902
+ React.Fragment,
903
+ null,
904
+ h(
905
+ 'div',
906
+ { className: 'gufa-search-input-wrap gufa-search-replace-row' },
907
+ h(G.Icon, { name: 'pencil', size: 12 }),
908
+ h('input', {
909
+ className: 'gufa-search-input',
910
+ type: 'text',
911
+ value: replacement,
912
+ placeholder: t('search.replacePlaceholder'),
913
+ spellCheck: false,
914
+ onChange: (event) => setReplacement(event.target.value),
915
+ onKeyDown: (event) => {
916
+ if (event.key !== 'Enter') return
917
+ event.preventDefault()
918
+ requestReplaceAll()
919
+ },
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'),
928
+ ),
929
+ h(
930
+ 'div',
931
+ { className: 'gufa-search-actions' },
932
+ h(
933
+ 'button',
934
+ {
935
+ type: 'button',
936
+ className: 'gufa-search-action',
937
+ 'data-primary': '',
938
+ disabled: replacing || view === null || view.total === 0,
939
+ onClick: requestReplaceAll,
940
+ },
941
+ replacing ? t('search.replacing') : t('search.replaceAll'),
942
+ ),
943
+ ),
944
+ )
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' },
719
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,
720
979
  ),
980
+ confirming !== null
981
+ ? h(
982
+ 'div',
983
+ { className: 'gufa-search-confirm' },
984
+ h('div', null, t('search.confirmReplace', { count: confirming.count, files: confirming.files })),
985
+ h(
986
+ 'div',
987
+ { className: 'gufa-search-confirm-actions' },
988
+ h('button', { type: 'button', className: 'gufa-search-action', onClick: () => setConfirming(null) }, t('search.confirmNo')),
989
+ h('button', { type: 'button', className: 'gufa-search-action', 'data-primary': '', onClick: doReplaceAll }, t('search.confirmYes')),
990
+ ),
991
+ )
992
+ : null,
993
+ notice !== '' ? h('div', { className: 'gufa-search-notice' }, notice) : null,
994
+ replaceError !== '' ? h('div', { className: 'gufa-search-error' }, `${t('search.replaceError')}:${replaceError}`) : null,
721
995
  ),
722
996
  h('div', { className: 'gufa-panel-body gufa-scroll' }, body),
723
997
  )
@@ -608,6 +608,49 @@ G.GufaApp = function GufaApp(props) {
608
608
  }
609
609
  }, [project, patchTab, refreshGit, t])
610
610
 
611
+ /* 搜索面板批量替换前:先把所有未落盘的编辑器缓冲保存掉,避免磁盘旧内容覆盖用户输入 */
612
+ const beforeSearchReplace = useCallback(async () => {
613
+ flushBuffersRef.current?.()
614
+ const pending = new Set(editorBuffersRef.current.keys())
615
+ for (const tab of tabsRef.current ?? []) {
616
+ if (tab !== null && tab !== undefined && tab.dirty === true) pending.add(tab.path)
617
+ }
618
+ for (const path of pending) {
619
+ try {
620
+ await saveFile(path)
621
+ } catch {
622
+ /* 单个保存失败不阻断替换 */
623
+ }
624
+ }
625
+ }, [saveFile])
626
+
627
+ /* 搜索面板替换完成后:刷新目录/Git,并把已打开标签同步成磁盘最新内容 */
628
+ const onSearchFilesChanged = useCallback(async (paths) => {
629
+ if (project !== null && Array.isArray(paths) && paths.length > 0) {
630
+ for (const path of paths.slice(0, 30)) {
631
+ const tab = (tabsRef.current ?? []).find((item) => item !== null && item !== undefined && item.path === path)
632
+ if (tab === undefined) continue
633
+ try {
634
+ const doc = await G.api('fs.read', { root: project.root, path })
635
+ editorBuffersRef.current.delete(path)
636
+ patchTab(path, {
637
+ content: doc.content,
638
+ revision: doc.revision,
639
+ savedRevision: doc.revision,
640
+ savedContent: doc.content,
641
+ dirty: false,
642
+ size: doc.size,
643
+ })
644
+ } catch {
645
+ /* 文件已被删除:忽略 */
646
+ }
647
+ }
648
+ }
649
+ setRefreshToken((token) => token + 1)
650
+ setGitRefreshToken((token) => token + 1)
651
+ void refreshGit()
652
+ }, [project, patchTab, refreshGit])
653
+
611
654
  /** 顶栏提示:错误常驻红色;成功用绿色 toast,1.2 秒后自动消失。 */
612
655
  const successTimerRef = useRef(null)
613
656
  const showBanner = useCallback((text, tone) => {
@@ -2135,6 +2178,8 @@ G.GufaApp = function GufaApp(props) {
2135
2178
  root: project?.root ?? null,
2136
2179
  active: ui.view === 'search',
2137
2180
  onOpenFile: (path, line, column) => void openFileAt(path, line, column),
2181
+ onBeforeReplace: beforeSearchReplace,
2182
+ onFilesChanged: onSearchFilesChanged,
2138
2183
  })),
2139
2184
  h('div', { key: 'git', style: { display: ui.view === 'git' ? 'flex' : 'none', flex: '1 1 auto', minHeight: 0, flexDirection: 'column' } },
2140
2185
  h(G.GitPanel, {