dsh-gufa-code 1.3.55 → 1.3.56

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/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.55'
19
+ G.BUILD = '1.3.56'
20
20
  G.PANEL_ID = 'gufa-code'
21
21
  /** 从哪个工作区点进来的(工作区菜单/节点按钮写入,App 启动时消费)。 */
22
22
  G.pendingEntryWorkspace = null
@@ -2690,7 +2690,7 @@ G.FooterAction = function FooterAction(props) {
2690
2690
 
2691
2691
  /* ── 组件 ───────────────────────────────────────────────────────────── */
2692
2692
 
2693
- G.EditorPane = function EditorPane(props) {
2693
+ function GufaEditorPaneComponent(props) {
2694
2694
  const tabs = useMemo(() => (Array.isArray(props.tabs) ? props.tabs : []), [props.tabs])
2695
2695
  const activePath = typeof props.activePath === 'string' ? props.activePath : null
2696
2696
  const root = typeof props.root === 'string' ? props.root : ''
@@ -3543,6 +3543,56 @@ G.FooterAction = function FooterAction(props) {
3543
3543
  )
3544
3544
  }
3545
3545
 
3546
+ /* 光标事件外部 store:App 不再为每次光标移动重渲染整个 IDE,状态栏订阅即可。 */
3547
+ G.cursorStore = (() => {
3548
+ const listeners = new Set()
3549
+ let value = null
3550
+ return {
3551
+ get: () => value,
3552
+ subscribe: (listener) => {
3553
+ listeners.add(listener)
3554
+ return () => listeners.delete(listener)
3555
+ },
3556
+ set: (next) => {
3557
+ if (value === next) return
3558
+ if (value !== null && next !== null &&
3559
+ value.path === next.path && value.line === next.line && value.column === next.column &&
3560
+ value.lineCount === next.lineCount && value.selectedText === next.selectedText) return
3561
+ value = next
3562
+ for (const listener of [...listeners]) {
3563
+ try {
3564
+ listener(next)
3565
+ } catch {
3566
+ /* 订阅者异常不影响编辑器 */
3567
+ }
3568
+ }
3569
+ },
3570
+ }
3571
+ })()
3572
+ G.useCursorStore = function useCursorStore() {
3573
+ const [snapshot, setSnapshot] = React.useState(G.cursorStore.get())
3574
+ React.useEffect(() => {
3575
+ setSnapshot(G.cursorStore.get())
3576
+ return G.cursorStore.subscribe(setSnapshot)
3577
+ }, [])
3578
+ return snapshot
3579
+ }
3580
+ /* 状态栏行数/光标:只订阅 cursorStore,光标移动不会触发 App 根组件重渲染。 */
3581
+ G.StatusCursor = function StatusCursor(props) {
3582
+ const activePath = props !== null && props !== undefined ? props.activePath : null
3583
+ const fallbackLineCount = props !== null && props !== undefined && Number.isFinite(props.fallbackLineCount) ? props.fallbackLineCount : 0
3584
+ const cursor = G.useCursorStore()
3585
+ const matched = cursor !== null && cursor.path === activePath
3586
+ const lineCount = matched && Number.isFinite(cursor.lineCount) ? cursor.lineCount : fallbackLineCount
3587
+ const cursorText = matched ? `${G.t('status.ln')} ${cursor.line},${G.t('status.col')} ${cursor.column}` : ''
3588
+ return React.createElement(React.Fragment, null,
3589
+ activePath !== null ? React.createElement('span', { className: 'gufa-status-item' }, `${lineCount} ${G.t('status.lines')}`) : null,
3590
+ cursorText !== '' ? React.createElement('span', { className: 'gufa-status-item' }, cursorText) : null,
3591
+ )
3592
+ }
3593
+ /* EditorPane 只在 props 真正变化时重渲染(光标移动不再穿透到 Monaco 外层)。 */
3594
+ G.EditorPane = typeof React.memo === 'function' ? React.memo(GufaEditorPaneComponent) : GufaEditorPaneComponent
3595
+
3546
3596
  /* 纯函数/加载状态测试钩子:供 scripts/test-editor.mjs 在无浏览器环境下校验
3547
3597
  * URI、光标推进、主题映射与 loader 失败降级 / 重试 / 单例注入。 */
3548
3598
  G.EditorPane.internals = Object.freeze({
@@ -9815,7 +9865,15 @@ G.GufaApp = function GufaApp(props) {
9815
9865
  const [banner, setBanner] = useState('')
9816
9866
  const [tabs, setTabs] = useState([])
9817
9867
  const [activePath, setActivePath] = useState(null)
9818
- const [cursor, setCursor] = useState(null)
9868
+ /* 选区(给右侧 AI 用)单独更新;光标位置/行数走 G.cursorStore,避免每次按键重渲染整个 IDE。 */
9869
+ const [selection, setSelection] = useState(null)
9870
+ /* 编辑器内容缓冲:Monaco 每次按键只写这里,300ms 停顿后才同步进 tabs(保存/自动保存直接读缓冲)。 */
9871
+ const editorBuffersRef = useRef(new Map())
9872
+ const tabsRef = useRef([])
9873
+ const flushTimerRef = useRef(null)
9874
+ const selectionTimerRef = useRef(null)
9875
+ const pendingSelectionRef = useRef(null)
9876
+ const flushBuffersRef = useRef(null)
9819
9877
  const [refreshToken, setRefreshToken] = useState(0)
9820
9878
  const [gitRefreshToken, setGitRefreshToken] = useState(0)
9821
9879
  const [sshRefreshToken, setSshRefreshToken] = useState(0)
@@ -9838,6 +9896,9 @@ G.GufaApp = function GufaApp(props) {
9838
9896
  const [explorerWidth, explorerDrag] = G.useSplitter({ initial: 260, min: 190, max: 520, storageKey: 'gufa-code:w:explorer' })
9839
9897
  const [chatWidth, chatDrag] = G.useSplitter({ initial: 430, min: 320, max: 780, axis: 'x', invert: true, storageKey: 'gufa-code:w:chat' })
9840
9898
  const [terminalHeight, terminalDrag] = G.useSplitter({ initial: 230, min: 120, max: 560, axis: 'y', invert: true, storageKey: 'gufa-code:h:terminal' })
9899
+ useEffect(() => {
9900
+ tabsRef.current = tabs
9901
+ }, [tabs])
9841
9902
 
9842
9903
  const patchUi = useCallback((patch) => {
9843
9904
  setUi((current) => {
@@ -9854,7 +9915,10 @@ G.GufaApp = function GufaApp(props) {
9854
9915
  writeJson(GUFA_LS.project, value?.root ?? null)
9855
9916
  setTabs([])
9856
9917
  setActivePath(null)
9857
- setCursor(null)
9918
+ setSelection(null)
9919
+ editorBuffersRef.current.clear()
9920
+ if (flushTimerRef.current !== null) { window.clearTimeout(flushTimerRef.current); flushTimerRef.current = null }
9921
+ G.cursorStore.set(null)
9858
9922
  setGitStatus(null)
9859
9923
  setRefreshToken((token) => token + 1)
9860
9924
  setGitRefreshToken((token) => token + 1)
@@ -9960,6 +10024,7 @@ G.GufaApp = function GufaApp(props) {
9960
10024
  /* ── 文件打开 / 保存 / 关闭 ─────────────────────────────────────────── */
9961
10025
  const openFile = useCallback(async (path) => {
9962
10026
  if (project === null) return
10027
+ flushBuffersRef.current?.()
9963
10028
  const existing = tabs.find((tab) => tab.path === path)
9964
10029
  if (existing !== undefined) {
9965
10030
  setActivePath(path)
@@ -9982,6 +10047,7 @@ G.GufaApp = function GufaApp(props) {
9982
10047
  /* 原子恢复标签集合:按快照顺序、去重、最后激活指定文件(避免 openFile 并发产生重复标签) */
9983
10048
  const applyTabs = useCallback(async (paths, active) => {
9984
10049
  if (project === null || !Array.isArray(paths)) return
10050
+ flushBuffersRef.current?.()
9985
10051
  const docs = []
9986
10052
  const seen = new Set()
9987
10053
  for (const path of paths.slice(0, 12)) {
@@ -9994,6 +10060,7 @@ G.GufaApp = function GufaApp(props) {
9994
10060
  /* 快照里的文件可能已被删除:忽略 */
9995
10061
  }
9996
10062
  }
10063
+ editorBuffersRef.current.clear()
9997
10064
  setTabs(docs)
9998
10065
  const wanted = typeof active === 'string' && docs.some((doc) => doc.path === active) ? active : (docs.length > 0 ? docs[docs.length - 1].path : null)
9999
10066
  setActivePath(wanted)
@@ -10002,27 +10069,85 @@ G.GufaApp = function GufaApp(props) {
10002
10069
  const activeTab = useMemo(() => tabs.find((tab) => tab.path === activePath) ?? null, [tabs, activePath])
10003
10070
 
10004
10071
  const patchTab = useCallback((path, patch) => {
10005
- setTabs((current) => current.map((tab) => (tab.path === path ? { ...tab, ...patch } : tab)))
10072
+ setTabs((current) => {
10073
+ const next = current.map((tab) => (tab.path === path ? { ...tab, ...patch } : tab))
10074
+ tabsRef.current = next
10075
+ return next
10076
+ })
10006
10077
  }, [])
10007
10078
 
10008
- const changeFile = useCallback((path, content) => {
10009
- setTabs((current) => current.map((tab) => (
10010
- tab.path === path ? { ...tab, content, dirty: content !== tab.savedContent } : tab
10011
- )))
10079
+ /* 把缓冲内容写回 tabs(停顿 / 保存 / 切换标签时调用),不会在每次按键触发渲染。 */
10080
+ const flushEditorBuffers = useCallback(() => {
10081
+ const buffers = editorBuffersRef.current
10082
+ if (buffers.size === 0) return
10083
+ setTabs((current) => {
10084
+ let changed = false
10085
+ const next = current.map((tab) => {
10086
+ if (tab === null || !buffers.has(tab.path)) return tab
10087
+ const content = buffers.get(tab.path)
10088
+ const dirty = content !== tab.savedContent
10089
+ if (tab.content === content && tab.dirty === dirty) return tab
10090
+ changed = true
10091
+ return { ...tab, content, dirty }
10092
+ })
10093
+ if (!changed) return current
10094
+ tabsRef.current = next
10095
+ return next
10096
+ })
10012
10097
  }, [])
10013
10098
 
10099
+ flushBuffersRef.current = flushEditorBuffers
10100
+
10101
+ const scheduleEditorFlush = useCallback(() => {
10102
+ if (flushTimerRef.current !== null) window.clearTimeout(flushTimerRef.current)
10103
+ flushTimerRef.current = window.setTimeout(() => {
10104
+ flushTimerRef.current = null
10105
+ flushEditorBuffers()
10106
+ }, 300)
10107
+ }, [flushEditorBuffers])
10108
+
10109
+ const changeFile = useCallback((path, content) => {
10110
+ editorBuffersRef.current.set(path, content)
10111
+ const tab = tabsRef.current.find((item) => item !== null && item.path === path)
10112
+ if (tab !== undefined && tab.dirty !== (content !== tab.savedContent)) {
10113
+ /* 只在「干净 ↔ 脏」变化时立刻更新标签圆点,避免每个按键重渲染整棵 IDE 树。 */
10114
+ setTabs((current) => {
10115
+ let changed = false
10116
+ const next = current.map((item) => {
10117
+ if (item === null || item.path !== path) return item
10118
+ const dirty = content !== item.savedContent
10119
+ if (item.dirty === dirty) return item
10120
+ changed = true
10121
+ return { ...item, dirty }
10122
+ })
10123
+ if (!changed) return current
10124
+ tabsRef.current = next
10125
+ return next
10126
+ })
10127
+ }
10128
+ scheduleEditorFlush()
10129
+ }, [scheduleEditorFlush])
10130
+
10014
10131
  const saveFile = useCallback(async (path) => {
10015
10132
  if (project === null) return
10016
- const tab = tabs.find((item) => item.path === path)
10017
- if (tab === undefined || tab.dirty !== true) return
10133
+ const tab = tabsRef.current.find((item) => item !== null && item.path === path)
10134
+ if (tab === undefined) return
10135
+ /* 缓冲里可能有比 tabs 更新的内容(按键后 300ms 内保存/自动保存) */
10136
+ const content = editorBuffersRef.current.has(path) ? editorBuffersRef.current.get(path) : tab.content
10137
+ if (content === tab.savedContent) {
10138
+ editorBuffersRef.current.delete(path)
10139
+ if (tab.dirty === true) patchTab(path, { content, dirty: false })
10140
+ return
10141
+ }
10018
10142
  try {
10019
10143
  const result = await G.api('fs.write', {
10020
10144
  root: project.root,
10021
10145
  path: tab.path,
10022
- content: tab.content,
10146
+ content,
10023
10147
  expectedRevision: tab.revision,
10024
10148
  })
10025
- patchTab(path, { revision: result.revision, savedRevision: result.revision, savedContent: tab.content, dirty: false, size: result.size })
10149
+ editorBuffersRef.current.delete(path)
10150
+ patchTab(path, { content, revision: result.revision, savedRevision: result.revision, savedContent: content, dirty: false, size: result.size })
10026
10151
  setBanner('')
10027
10152
  // 内容保存不改变目录结构:只刷新 Git 状态,不重载文件树(避免整页闪烁)
10028
10153
  setGitRefreshToken((token) => token + 1)
@@ -10031,7 +10156,7 @@ G.GufaApp = function GufaApp(props) {
10031
10156
  if (error.code === 'revision-conflict') setBanner(t('editor.saveConflict'))
10032
10157
  else setBanner(G.errorText(error))
10033
10158
  }
10034
- }, [project, tabs, patchTab, refreshGit, t])
10159
+ }, [project, patchTab, refreshGit, t])
10035
10160
 
10036
10161
  /** 顶栏提示:错误常驻红色;成功用绿色 toast,1.2 秒后自动消失。 */
10037
10162
  const successTimerRef = useRef(null)
@@ -10057,6 +10182,8 @@ G.GufaApp = function GufaApp(props) {
10057
10182
  }, [])
10058
10183
 
10059
10184
  const closeTab = useCallback((path) => {
10185
+ editorBuffersRef.current.delete(path)
10186
+ flushBuffersRef.current?.()
10060
10187
  setTabs((current) => {
10061
10188
  const next = current.filter((tab) => tab.path !== path)
10062
10189
  if (activePath === path) {
@@ -11367,10 +11494,7 @@ G.GufaApp = function GufaApp(props) {
11367
11494
  return { path: activeTab.path, content: activeTab.content ?? '', language: activeTab.language }
11368
11495
  }, [activeTab])
11369
11496
 
11370
- const selectionForChat = useMemo(() => {
11371
- if (cursor === null || typeof cursor.selectedText !== 'string' || cursor.selectedText === '') return null
11372
- return { text: cursor.selectedText, path: cursor.path, line: cursor.line, column: cursor.column }
11373
- }, [cursor])
11497
+ const selectionForChat = useMemo(() => selection, [selection])
11374
11498
 
11375
11499
  /* ── IDE 专属快捷键(参考 VS Code;经典模式下不生效) ──────────────── */
11376
11500
  const editorFocusedRef = useRef(false)
@@ -11582,7 +11706,7 @@ G.GufaApp = function GufaApp(props) {
11582
11706
  role: 'tab',
11583
11707
  tabIndex: 0,
11584
11708
  title: tab.title !== undefined && tab.title !== null ? tab.title : tab.path,
11585
- onClick: () => setActivePath(tab.path),
11709
+ onClick: () => handleSelectTab(tab.path),
11586
11710
  onAuxClick: (event) => { if (event.button === 1) closeTabWithConfirm(tab.path) },
11587
11711
  },
11588
11712
  h(G.Icon, { name: 'file', size: 13 }),
@@ -11637,10 +11761,39 @@ G.GufaApp = function GufaApp(props) {
11637
11761
  )
11638
11762
  : null
11639
11763
 
11640
- const cursorText =
11641
- cursor !== null && cursor.path === activePath
11642
- ? `${t('status.ln')} ${cursor.line},${t('status.col')} ${cursor.column}`
11643
- : ''
11764
+ const handleEditorCursor = useCallback((value) => {
11765
+ if (value === null || value === undefined) return
11766
+ /* 状态栏订阅 cursorStore,不再触发 App 根组件渲染 */
11767
+ G.cursorStore.set(value)
11768
+ const selectedText = typeof value.selectedText === 'string' ? value.selectedText : ''
11769
+ if (selectedText === '') {
11770
+ if (selectionTimerRef.current !== null) { window.clearTimeout(selectionTimerRef.current); selectionTimerRef.current = null }
11771
+ pendingSelectionRef.current = null
11772
+ setSelection((current) => (current === null ? current : null))
11773
+ return
11774
+ }
11775
+ pendingSelectionRef.current = { text: selectedText, path: value.path, line: value.line, column: value.column }
11776
+ if (selectionTimerRef.current !== null) return
11777
+ selectionTimerRef.current = window.setTimeout(() => {
11778
+ selectionTimerRef.current = null
11779
+ const latest = pendingSelectionRef.current
11780
+ if (latest === null) return
11781
+ setSelection((current) => {
11782
+ if (current !== null && current.text === latest.text && current.path === latest.path &&
11783
+ current.line === latest.line && current.column === latest.column) return current
11784
+ return latest
11785
+ })
11786
+ }, 180)
11787
+ }, [])
11788
+ const handleEditorSave = useCallback((path) => { void saveFile(path) }, [saveFile])
11789
+ const handleSelectTab = useCallback((path) => {
11790
+ flushBuffersRef.current?.()
11791
+ setActivePath(path)
11792
+ }, [])
11793
+ useEffect(() => () => {
11794
+ if (flushTimerRef.current !== null) { window.clearTimeout(flushTimerRef.current); flushTimerRef.current = null }
11795
+ if (selectionTimerRef.current !== null) { window.clearTimeout(selectionTimerRef.current); selectionTimerRef.current = null }
11796
+ }, [])
11644
11797
 
11645
11798
  const statusBranch = project?.isGit === true && gitStatus?.branch ? gitStatus.branch : null
11646
11799
  const lineCount = activeTab === null || activeTab.kind === 'graph' ? 0 : String(activeTab.content ?? '').split('\n').length
@@ -11835,12 +11988,12 @@ G.GufaApp = function GufaApp(props) {
11835
11988
  theme,
11836
11989
  palette: solidPalette,
11837
11990
  root: project?.root ?? '',
11838
- onSelectTab: setActivePath,
11991
+ onSelectTab: handleSelectTab,
11839
11992
  onCloseTab: closeTabWithConfirm,
11840
11993
  onChange: changeFile,
11841
- onSave: (path) => void saveFile(path),
11994
+ onSave: handleEditorSave,
11842
11995
  backdropMode: backdropActive,
11843
- onCursor: (value) => setCursor({ ...value, lineCount }),
11996
+ onCursor: handleEditorCursor,
11844
11997
  }),
11845
11998
  ),
11846
11999
  ),
@@ -11913,11 +12066,10 @@ G.GufaApp = function GufaApp(props) {
11913
12066
  : h('span', { className: 'gufa-status-item' }, t('status.noGit')),
11914
12067
  h('div', { className: 'gufa-status-fill' }),
11915
12068
  activeTab !== null ? h('span', { className: 'gufa-status-item' }, activeTab.title !== undefined && activeTab.title !== null ? activeTab.title : G.shortPath(activeTab.path, 56)) : null,
11916
- activeTab !== null ? h('span', { className: 'gufa-status-item' }, `${lineCount} ${t('status.lines')}`) : null,
11917
12069
  activeTab !== null && problems[activeTab.path] !== undefined
11918
12070
  ? h('span', { className: 'gufa-status-item', style: { color: problems[activeTab.path].errors > 0 ? '#f85149' : '#e5c07b' } }, `${problems[activeTab.path].errors} ✕ ${problems[activeTab.path].warnings} ⚠`)
11919
12071
  : null,
11920
- cursorText !== '' ? h('span', { className: 'gufa-status-item' }, cursorText) : null,
12072
+ h(G.StatusCursor, { activePath, fallbackLineCount: lineCount }),
11921
12073
  activeTab !== null && activeTab.dirty === true ? h('span', { className: 'gufa-status-item', style: { color: 'var(--gufa-accent)' } }, t('status.dirty')) : null,
11922
12074
  activeTab !== null && activeTab.dirty !== true ? h('span', { className: 'gufa-status-item' }, G.Icon ? h(G.Icon, { name: 'check', size: 11 }) : null, t('status.saved')) : null,
11923
12075
  h('button', {
@@ -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.55</string>
11
- <key>CFBundleVersion</key><string>1.3.55</string>
10
+ <key>CFBundleShortVersionString</key><string>1.3.56</string>
11
+ <key>CFBundleVersion</key><string>1.3.56</string>
12
12
  <key>LSUIElement</key><true/>
13
13
  <key>NSHighResolutionCapable</key><true/>
14
14
  <key>NSPrincipalClass</key><string>NSApplication</string>
@@ -0,0 +1,37 @@
1
+ # 编辑器性能设计(避免按键「不跟手」)
2
+
3
+ Monaco 本身很快;古法编程编辑器之前卡顿的主要原因是 **React 状态更新链路**:
4
+ 每按一个键,Monaco 的 `onDidChangeModelContent` 会同步到 App 状态,导致整个 IDE
5
+ (资源管理器、终端、右侧 AI 会话、状态栏…)重渲染一次;光标位置变化又触发一次。
6
+ 快捷键连打时就是每秒几十次整树渲染。
7
+
8
+ ## 现在的实现
9
+
10
+ 1. **内容缓冲区(不在每次按键时 setState)**
11
+ - `onChange(path, content)` 只写入 `editorBuffersRef`(ref,不触发渲染);
12
+ - 只有「干净 ↔ 脏」变化时更新一次 tabs(标签圆点),每个编辑段最多一次;
13
+ - 停顿 **300ms** 后用 `flushEditorBuffers()` 把缓冲内容同步进 tabs(一次渲染);
14
+ - 保存 / 自动保存(700ms)/ 关闭标签前**直接读缓冲**,所以不会写入旧内容;
15
+ - 撤销到与磁盘一致时会自动变回「已保存」。
16
+
17
+ 2. **光标/行号走外部 store**
18
+ - Monaco 的光标事件写入 `G.cursorStore`(module-level,去重 + 订阅),
19
+ - 状态栏的 `G.StatusCursor` 用 `useSyncExternalStore` 订阅,**只渲染状态栏那一小块**;
20
+ - App 根组件不再因为光标移动而重渲染;选区(AI 引用用)180ms 防抖后单独更新。
21
+
22
+ 3. **EditorPane 用 React.memo + 稳定回调**
23
+ - 父组件因其它原因重渲染时,只要 tabs/activePath/theme 等 props 没变,Monaco 外层不重渲染;
24
+ - `onChange/onSave/onSelectTab/onCursor` 都用 `useCallback`,依赖只包含真正会变的数据。
25
+
26
+ ## 实测(macOS,113 行文本文件)
27
+
28
+ - 连续输入 `abc`:标签立即变脏、状态栏 Ln/Col 正常更新;
29
+ - 自动保存在 700ms 档内把 `abc` 写入磁盘(通过 Host `fs.read` 校验内容 +3 字节);
30
+ - `Cmd+Z` 撤销后自动保存把文件恢复为原始内容(与 baseline 全等);
31
+ - 三套测试 99 / 44 / 19 全过。
32
+
33
+ ## 后续可继续优化(未做)
34
+
35
+ - 超大文件(数万行)时 Monaco `model.getValue()` 每次变更复制整个字符串,可改为增量取内容/仅在 flush 时取;
36
+ - 右侧 AI 流式回复时 App 的频繁更新可进一步拆分订阅;
37
+ - 资源管理器大目录可做虚拟滚动。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-gufa-code",
3
- "version": "1.3.55",
3
+ "version": "1.3.56",
4
4
  "description": "古法编程 · AI 辅助:一键把 DSH 主面板变成 VS Code 风格 IDE(Monaco 编辑器、文件树、Git、SSH、真实终端 + 右侧官方会话);主张亲手写、每个改动心中有数,AI 是助理、你是第一责任人",
5
5
  "icon": "icon.svg",
6
6
  "keywords": [
@@ -494,7 +494,7 @@
494
494
 
495
495
  /* ── 组件 ───────────────────────────────────────────────────────────── */
496
496
 
497
- G.EditorPane = function EditorPane(props) {
497
+ function GufaEditorPaneComponent(props) {
498
498
  const tabs = useMemo(() => (Array.isArray(props.tabs) ? props.tabs : []), [props.tabs])
499
499
  const activePath = typeof props.activePath === 'string' ? props.activePath : null
500
500
  const root = typeof props.root === 'string' ? props.root : ''
@@ -1347,6 +1347,56 @@
1347
1347
  )
1348
1348
  }
1349
1349
 
1350
+ /* 光标事件外部 store:App 不再为每次光标移动重渲染整个 IDE,状态栏订阅即可。 */
1351
+ G.cursorStore = (() => {
1352
+ const listeners = new Set()
1353
+ let value = null
1354
+ return {
1355
+ get: () => value,
1356
+ subscribe: (listener) => {
1357
+ listeners.add(listener)
1358
+ return () => listeners.delete(listener)
1359
+ },
1360
+ set: (next) => {
1361
+ if (value === next) return
1362
+ if (value !== null && next !== null &&
1363
+ value.path === next.path && value.line === next.line && value.column === next.column &&
1364
+ value.lineCount === next.lineCount && value.selectedText === next.selectedText) return
1365
+ value = next
1366
+ for (const listener of [...listeners]) {
1367
+ try {
1368
+ listener(next)
1369
+ } catch {
1370
+ /* 订阅者异常不影响编辑器 */
1371
+ }
1372
+ }
1373
+ },
1374
+ }
1375
+ })()
1376
+ G.useCursorStore = function useCursorStore() {
1377
+ const [snapshot, setSnapshot] = React.useState(G.cursorStore.get())
1378
+ React.useEffect(() => {
1379
+ setSnapshot(G.cursorStore.get())
1380
+ return G.cursorStore.subscribe(setSnapshot)
1381
+ }, [])
1382
+ return snapshot
1383
+ }
1384
+ /* 状态栏行数/光标:只订阅 cursorStore,光标移动不会触发 App 根组件重渲染。 */
1385
+ G.StatusCursor = function StatusCursor(props) {
1386
+ const activePath = props !== null && props !== undefined ? props.activePath : null
1387
+ const fallbackLineCount = props !== null && props !== undefined && Number.isFinite(props.fallbackLineCount) ? props.fallbackLineCount : 0
1388
+ const cursor = G.useCursorStore()
1389
+ const matched = cursor !== null && cursor.path === activePath
1390
+ const lineCount = matched && Number.isFinite(cursor.lineCount) ? cursor.lineCount : fallbackLineCount
1391
+ const cursorText = matched ? `${G.t('status.ln')} ${cursor.line},${G.t('status.col')} ${cursor.column}` : ''
1392
+ return React.createElement(React.Fragment, null,
1393
+ activePath !== null ? React.createElement('span', { className: 'gufa-status-item' }, `${lineCount} ${G.t('status.lines')}`) : null,
1394
+ cursorText !== '' ? React.createElement('span', { className: 'gufa-status-item' }, cursorText) : null,
1395
+ )
1396
+ }
1397
+ /* EditorPane 只在 props 真正变化时重渲染(光标移动不再穿透到 Monaco 外层)。 */
1398
+ G.EditorPane = typeof React.memo === 'function' ? React.memo(GufaEditorPaneComponent) : GufaEditorPaneComponent
1399
+
1350
1400
  /* 纯函数/加载状态测试钩子:供 scripts/test-editor.mjs 在无浏览器环境下校验
1351
1401
  * URI、光标推进、主题映射与 loader 失败降级 / 重试 / 单例注入。 */
1352
1402
  G.EditorPane.internals = Object.freeze({
@@ -250,7 +250,15 @@ G.GufaApp = function GufaApp(props) {
250
250
  const [banner, setBanner] = useState('')
251
251
  const [tabs, setTabs] = useState([])
252
252
  const [activePath, setActivePath] = useState(null)
253
- const [cursor, setCursor] = useState(null)
253
+ /* 选区(给右侧 AI 用)单独更新;光标位置/行数走 G.cursorStore,避免每次按键重渲染整个 IDE。 */
254
+ const [selection, setSelection] = useState(null)
255
+ /* 编辑器内容缓冲:Monaco 每次按键只写这里,300ms 停顿后才同步进 tabs(保存/自动保存直接读缓冲)。 */
256
+ const editorBuffersRef = useRef(new Map())
257
+ const tabsRef = useRef([])
258
+ const flushTimerRef = useRef(null)
259
+ const selectionTimerRef = useRef(null)
260
+ const pendingSelectionRef = useRef(null)
261
+ const flushBuffersRef = useRef(null)
254
262
  const [refreshToken, setRefreshToken] = useState(0)
255
263
  const [gitRefreshToken, setGitRefreshToken] = useState(0)
256
264
  const [sshRefreshToken, setSshRefreshToken] = useState(0)
@@ -273,6 +281,9 @@ G.GufaApp = function GufaApp(props) {
273
281
  const [explorerWidth, explorerDrag] = G.useSplitter({ initial: 260, min: 190, max: 520, storageKey: 'gufa-code:w:explorer' })
274
282
  const [chatWidth, chatDrag] = G.useSplitter({ initial: 430, min: 320, max: 780, axis: 'x', invert: true, storageKey: 'gufa-code:w:chat' })
275
283
  const [terminalHeight, terminalDrag] = G.useSplitter({ initial: 230, min: 120, max: 560, axis: 'y', invert: true, storageKey: 'gufa-code:h:terminal' })
284
+ useEffect(() => {
285
+ tabsRef.current = tabs
286
+ }, [tabs])
276
287
 
277
288
  const patchUi = useCallback((patch) => {
278
289
  setUi((current) => {
@@ -289,7 +300,10 @@ G.GufaApp = function GufaApp(props) {
289
300
  writeJson(GUFA_LS.project, value?.root ?? null)
290
301
  setTabs([])
291
302
  setActivePath(null)
292
- setCursor(null)
303
+ setSelection(null)
304
+ editorBuffersRef.current.clear()
305
+ if (flushTimerRef.current !== null) { window.clearTimeout(flushTimerRef.current); flushTimerRef.current = null }
306
+ G.cursorStore.set(null)
293
307
  setGitStatus(null)
294
308
  setRefreshToken((token) => token + 1)
295
309
  setGitRefreshToken((token) => token + 1)
@@ -395,6 +409,7 @@ G.GufaApp = function GufaApp(props) {
395
409
  /* ── 文件打开 / 保存 / 关闭 ─────────────────────────────────────────── */
396
410
  const openFile = useCallback(async (path) => {
397
411
  if (project === null) return
412
+ flushBuffersRef.current?.()
398
413
  const existing = tabs.find((tab) => tab.path === path)
399
414
  if (existing !== undefined) {
400
415
  setActivePath(path)
@@ -417,6 +432,7 @@ G.GufaApp = function GufaApp(props) {
417
432
  /* 原子恢复标签集合:按快照顺序、去重、最后激活指定文件(避免 openFile 并发产生重复标签) */
418
433
  const applyTabs = useCallback(async (paths, active) => {
419
434
  if (project === null || !Array.isArray(paths)) return
435
+ flushBuffersRef.current?.()
420
436
  const docs = []
421
437
  const seen = new Set()
422
438
  for (const path of paths.slice(0, 12)) {
@@ -429,6 +445,7 @@ G.GufaApp = function GufaApp(props) {
429
445
  /* 快照里的文件可能已被删除:忽略 */
430
446
  }
431
447
  }
448
+ editorBuffersRef.current.clear()
432
449
  setTabs(docs)
433
450
  const wanted = typeof active === 'string' && docs.some((doc) => doc.path === active) ? active : (docs.length > 0 ? docs[docs.length - 1].path : null)
434
451
  setActivePath(wanted)
@@ -437,27 +454,85 @@ G.GufaApp = function GufaApp(props) {
437
454
  const activeTab = useMemo(() => tabs.find((tab) => tab.path === activePath) ?? null, [tabs, activePath])
438
455
 
439
456
  const patchTab = useCallback((path, patch) => {
440
- setTabs((current) => current.map((tab) => (tab.path === path ? { ...tab, ...patch } : tab)))
457
+ setTabs((current) => {
458
+ const next = current.map((tab) => (tab.path === path ? { ...tab, ...patch } : tab))
459
+ tabsRef.current = next
460
+ return next
461
+ })
441
462
  }, [])
442
463
 
443
- const changeFile = useCallback((path, content) => {
444
- setTabs((current) => current.map((tab) => (
445
- tab.path === path ? { ...tab, content, dirty: content !== tab.savedContent } : tab
446
- )))
464
+ /* 把缓冲内容写回 tabs(停顿 / 保存 / 切换标签时调用),不会在每次按键触发渲染。 */
465
+ const flushEditorBuffers = useCallback(() => {
466
+ const buffers = editorBuffersRef.current
467
+ if (buffers.size === 0) return
468
+ setTabs((current) => {
469
+ let changed = false
470
+ const next = current.map((tab) => {
471
+ if (tab === null || !buffers.has(tab.path)) return tab
472
+ const content = buffers.get(tab.path)
473
+ const dirty = content !== tab.savedContent
474
+ if (tab.content === content && tab.dirty === dirty) return tab
475
+ changed = true
476
+ return { ...tab, content, dirty }
477
+ })
478
+ if (!changed) return current
479
+ tabsRef.current = next
480
+ return next
481
+ })
447
482
  }, [])
448
483
 
484
+ flushBuffersRef.current = flushEditorBuffers
485
+
486
+ const scheduleEditorFlush = useCallback(() => {
487
+ if (flushTimerRef.current !== null) window.clearTimeout(flushTimerRef.current)
488
+ flushTimerRef.current = window.setTimeout(() => {
489
+ flushTimerRef.current = null
490
+ flushEditorBuffers()
491
+ }, 300)
492
+ }, [flushEditorBuffers])
493
+
494
+ const changeFile = useCallback((path, content) => {
495
+ editorBuffersRef.current.set(path, content)
496
+ const tab = tabsRef.current.find((item) => item !== null && item.path === path)
497
+ if (tab !== undefined && tab.dirty !== (content !== tab.savedContent)) {
498
+ /* 只在「干净 ↔ 脏」变化时立刻更新标签圆点,避免每个按键重渲染整棵 IDE 树。 */
499
+ setTabs((current) => {
500
+ let changed = false
501
+ const next = current.map((item) => {
502
+ if (item === null || item.path !== path) return item
503
+ const dirty = content !== item.savedContent
504
+ if (item.dirty === dirty) return item
505
+ changed = true
506
+ return { ...item, dirty }
507
+ })
508
+ if (!changed) return current
509
+ tabsRef.current = next
510
+ return next
511
+ })
512
+ }
513
+ scheduleEditorFlush()
514
+ }, [scheduleEditorFlush])
515
+
449
516
  const saveFile = useCallback(async (path) => {
450
517
  if (project === null) return
451
- const tab = tabs.find((item) => item.path === path)
452
- if (tab === undefined || tab.dirty !== true) return
518
+ const tab = tabsRef.current.find((item) => item !== null && item.path === path)
519
+ if (tab === undefined) return
520
+ /* 缓冲里可能有比 tabs 更新的内容(按键后 300ms 内保存/自动保存) */
521
+ const content = editorBuffersRef.current.has(path) ? editorBuffersRef.current.get(path) : tab.content
522
+ if (content === tab.savedContent) {
523
+ editorBuffersRef.current.delete(path)
524
+ if (tab.dirty === true) patchTab(path, { content, dirty: false })
525
+ return
526
+ }
453
527
  try {
454
528
  const result = await G.api('fs.write', {
455
529
  root: project.root,
456
530
  path: tab.path,
457
- content: tab.content,
531
+ content,
458
532
  expectedRevision: tab.revision,
459
533
  })
460
- patchTab(path, { revision: result.revision, savedRevision: result.revision, savedContent: tab.content, dirty: false, size: result.size })
534
+ editorBuffersRef.current.delete(path)
535
+ patchTab(path, { content, revision: result.revision, savedRevision: result.revision, savedContent: content, dirty: false, size: result.size })
461
536
  setBanner('')
462
537
  // 内容保存不改变目录结构:只刷新 Git 状态,不重载文件树(避免整页闪烁)
463
538
  setGitRefreshToken((token) => token + 1)
@@ -466,7 +541,7 @@ G.GufaApp = function GufaApp(props) {
466
541
  if (error.code === 'revision-conflict') setBanner(t('editor.saveConflict'))
467
542
  else setBanner(G.errorText(error))
468
543
  }
469
- }, [project, tabs, patchTab, refreshGit, t])
544
+ }, [project, patchTab, refreshGit, t])
470
545
 
471
546
  /** 顶栏提示:错误常驻红色;成功用绿色 toast,1.2 秒后自动消失。 */
472
547
  const successTimerRef = useRef(null)
@@ -492,6 +567,8 @@ G.GufaApp = function GufaApp(props) {
492
567
  }, [])
493
568
 
494
569
  const closeTab = useCallback((path) => {
570
+ editorBuffersRef.current.delete(path)
571
+ flushBuffersRef.current?.()
495
572
  setTabs((current) => {
496
573
  const next = current.filter((tab) => tab.path !== path)
497
574
  if (activePath === path) {
@@ -1802,10 +1879,7 @@ G.GufaApp = function GufaApp(props) {
1802
1879
  return { path: activeTab.path, content: activeTab.content ?? '', language: activeTab.language }
1803
1880
  }, [activeTab])
1804
1881
 
1805
- const selectionForChat = useMemo(() => {
1806
- if (cursor === null || typeof cursor.selectedText !== 'string' || cursor.selectedText === '') return null
1807
- return { text: cursor.selectedText, path: cursor.path, line: cursor.line, column: cursor.column }
1808
- }, [cursor])
1882
+ const selectionForChat = useMemo(() => selection, [selection])
1809
1883
 
1810
1884
  /* ── IDE 专属快捷键(参考 VS Code;经典模式下不生效) ──────────────── */
1811
1885
  const editorFocusedRef = useRef(false)
@@ -2017,7 +2091,7 @@ G.GufaApp = function GufaApp(props) {
2017
2091
  role: 'tab',
2018
2092
  tabIndex: 0,
2019
2093
  title: tab.title !== undefined && tab.title !== null ? tab.title : tab.path,
2020
- onClick: () => setActivePath(tab.path),
2094
+ onClick: () => handleSelectTab(tab.path),
2021
2095
  onAuxClick: (event) => { if (event.button === 1) closeTabWithConfirm(tab.path) },
2022
2096
  },
2023
2097
  h(G.Icon, { name: 'file', size: 13 }),
@@ -2072,10 +2146,39 @@ G.GufaApp = function GufaApp(props) {
2072
2146
  )
2073
2147
  : null
2074
2148
 
2075
- const cursorText =
2076
- cursor !== null && cursor.path === activePath
2077
- ? `${t('status.ln')} ${cursor.line},${t('status.col')} ${cursor.column}`
2078
- : ''
2149
+ const handleEditorCursor = useCallback((value) => {
2150
+ if (value === null || value === undefined) return
2151
+ /* 状态栏订阅 cursorStore,不再触发 App 根组件渲染 */
2152
+ G.cursorStore.set(value)
2153
+ const selectedText = typeof value.selectedText === 'string' ? value.selectedText : ''
2154
+ if (selectedText === '') {
2155
+ if (selectionTimerRef.current !== null) { window.clearTimeout(selectionTimerRef.current); selectionTimerRef.current = null }
2156
+ pendingSelectionRef.current = null
2157
+ setSelection((current) => (current === null ? current : null))
2158
+ return
2159
+ }
2160
+ pendingSelectionRef.current = { text: selectedText, path: value.path, line: value.line, column: value.column }
2161
+ if (selectionTimerRef.current !== null) return
2162
+ selectionTimerRef.current = window.setTimeout(() => {
2163
+ selectionTimerRef.current = null
2164
+ const latest = pendingSelectionRef.current
2165
+ if (latest === null) return
2166
+ setSelection((current) => {
2167
+ if (current !== null && current.text === latest.text && current.path === latest.path &&
2168
+ current.line === latest.line && current.column === latest.column) return current
2169
+ return latest
2170
+ })
2171
+ }, 180)
2172
+ }, [])
2173
+ const handleEditorSave = useCallback((path) => { void saveFile(path) }, [saveFile])
2174
+ const handleSelectTab = useCallback((path) => {
2175
+ flushBuffersRef.current?.()
2176
+ setActivePath(path)
2177
+ }, [])
2178
+ useEffect(() => () => {
2179
+ if (flushTimerRef.current !== null) { window.clearTimeout(flushTimerRef.current); flushTimerRef.current = null }
2180
+ if (selectionTimerRef.current !== null) { window.clearTimeout(selectionTimerRef.current); selectionTimerRef.current = null }
2181
+ }, [])
2079
2182
 
2080
2183
  const statusBranch = project?.isGit === true && gitStatus?.branch ? gitStatus.branch : null
2081
2184
  const lineCount = activeTab === null || activeTab.kind === 'graph' ? 0 : String(activeTab.content ?? '').split('\n').length
@@ -2270,12 +2373,12 @@ G.GufaApp = function GufaApp(props) {
2270
2373
  theme,
2271
2374
  palette: solidPalette,
2272
2375
  root: project?.root ?? '',
2273
- onSelectTab: setActivePath,
2376
+ onSelectTab: handleSelectTab,
2274
2377
  onCloseTab: closeTabWithConfirm,
2275
2378
  onChange: changeFile,
2276
- onSave: (path) => void saveFile(path),
2379
+ onSave: handleEditorSave,
2277
2380
  backdropMode: backdropActive,
2278
- onCursor: (value) => setCursor({ ...value, lineCount }),
2381
+ onCursor: handleEditorCursor,
2279
2382
  }),
2280
2383
  ),
2281
2384
  ),
@@ -2348,11 +2451,10 @@ G.GufaApp = function GufaApp(props) {
2348
2451
  : h('span', { className: 'gufa-status-item' }, t('status.noGit')),
2349
2452
  h('div', { className: 'gufa-status-fill' }),
2350
2453
  activeTab !== null ? h('span', { className: 'gufa-status-item' }, activeTab.title !== undefined && activeTab.title !== null ? activeTab.title : G.shortPath(activeTab.path, 56)) : null,
2351
- activeTab !== null ? h('span', { className: 'gufa-status-item' }, `${lineCount} ${t('status.lines')}`) : null,
2352
2454
  activeTab !== null && problems[activeTab.path] !== undefined
2353
2455
  ? h('span', { className: 'gufa-status-item', style: { color: problems[activeTab.path].errors > 0 ? '#f85149' : '#e5c07b' } }, `${problems[activeTab.path].errors} ✕ ${problems[activeTab.path].warnings} ⚠`)
2354
2456
  : null,
2355
- cursorText !== '' ? h('span', { className: 'gufa-status-item' }, cursorText) : null,
2457
+ h(G.StatusCursor, { activePath, fallbackLineCount: lineCount }),
2356
2458
  activeTab !== null && activeTab.dirty === true ? h('span', { className: 'gufa-status-item', style: { color: 'var(--gufa-accent)' } }, t('status.dirty')) : null,
2357
2459
  activeTab !== null && activeTab.dirty !== true ? h('span', { className: 'gufa-status-item' }, G.Icon ? h(G.Icon, { name: 'check', size: 11 }) : null, t('status.saved')) : null,
2358
2460
  h('button', {