dsh-gufa-code 1.3.88 → 1.3.90

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
@@ -40,6 +40,7 @@ dsh plugin add dsh-gufa-code
40
40
 
41
41
  - Monaco 编辑器(离线随包分发,无需联网下载);
42
42
  - 多标签、按扩展名语法高亮、面包屑、状态栏(分支 / 行列 / 保存状态);
43
+ - **Copilot 风格 Tab 行内补全**:光标停/打字后,基于 Harness 自己的 DeepSeek 模型(默认 `deepseek-flash`,不需要额外 Copilot 订阅)流式生成灰色 ghost text,Tab 接受、Esc 忽略;支持 JS/TS/Python/Java/Go/Rust/C++/C#/JSON/YAML/Shell 等常见语言;
43
44
  - 保存走磁盘版本冲突检测 + 原子写;未保存标记;
44
45
  - **⌘/Ctrl + 点击跳转定义 / 方法实现**:Python 走 **Pyright 语言服务(LSP)**,JS/TS 走 Monaco 内置 TypeScript 语言服务(点击前会按需把当前文件的 import 依赖加载进 Monaco,跨文件变量/函数由 TS 语言服务解析);其他语言暂用项目级定义搜索兜底;优先走 Monaco 语言服务;目标文件没在 Monaco 里打开、或语言没有内置语言服务时,自动用项目级定义搜索兜底(函数 / 类 / 方法 / const / type 等声明)。兜底会解析 import/require 直接定位来源文件,并跳过压缩产物/大文件;同一个文件里的声明、import 命中的定义、目录距离近的定义直接跳,只有多个真正同名的声明才弹「跳转到定义」列表,点同名属性/变量时不会拿远端无关的 const 来凑数;按住 ⌘/Ctrl 悬停时,可跳转的符号会显示下划线 + 手型光标;
45
46
  - **全局搜索 / 替换**(左栏放大镜 / `⌘⇧F`):整个项目的文本搜索,支持区分大小写、正则、包含/排除文件(glob);结果按文件分组、高亮匹配,点击跳转行列;替换栏支持保留大小写(AB)、全部替换(需确认)、按文件替换(悬停文件头铅笔)、按单行匹配替换与忽略(悬停结果行);替换前会自动保存未落盘的编辑器内容;Git 项目用 `git ls-files` 枚举文件(尊重 `.gitignore`、含未跟踪文件),非 Git 项目遍历目录并自动跳过 `node_modules` 等依赖/缓存目录;摸鱼悬浮窗里同样可用;
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.88'
19
+ G.BUILD = '1.3.90'
20
20
  G.PANEL_ID = 'gufa-code'
21
21
  /** 从哪个工作区点进来的(工作区菜单/节点按钮写入,App 启动时消费)。 */
22
22
  G.pendingEntryWorkspace = null
@@ -2750,6 +2750,7 @@ G.FooterAction = function FooterAction(props) {
2750
2750
  }
2751
2751
 
2752
2752
  /** 这些声明类型算「定义级」,多个同名时值得让用户选;普通 const/赋值等弱匹配不轻易弹列表。 */
2753
+ const GUFA_LSP_WARMED = new Set()
2753
2754
  const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'def', 'func', 'fn', 'type', 'export', 'lsp'])
2754
2755
 
2755
2756
  /* ── model / URI ────────────────────────────────────────────────────── */
@@ -2830,6 +2831,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
2830
2831
  scrollbar: { verticalScrollbarSize: 10, horizontalScrollbarSize: 10, useShadows: false },
2831
2832
  mouseWheelZoom: false,
2832
2833
  contextmenu: true,
2834
+ inlineSuggest: { enabled: true, showToolbar: 'never' },
2833
2835
  }
2834
2836
  }
2835
2837
 
@@ -3485,6 +3487,83 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
3485
3487
  } catch {
3486
3488
  /* ignore */
3487
3489
  }
3490
+ /* ── Copilot 风格 Tab 行内补全(复用 Harness 模型服务) ───────── */
3491
+ try {
3492
+ if (typeof monaco.languages.registerInlineCompletionsProvider === 'function') {
3493
+ const inlineLanguages = ['python', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'java', 'go', 'rust', 'csharp', 'cpp', 'c', 'shell', 'json', 'yaml', 'sql', 'html', 'css']
3494
+ const inlineCache = new Map()
3495
+ let inlineLastRequestAt = 0
3496
+ const inlineProvider = {
3497
+ provideInlineCompletions(model, position, context, token) {
3498
+ const path = modelPathRef.current
3499
+ const root = modelsRootRef.current
3500
+ if (path === null || path === undefined || root === null || root === undefined || root === '') {
3501
+ return Promise.resolve({ items: [] })
3502
+ }
3503
+ if (token !== null && token !== undefined && token.isCancellationRequested === true) return Promise.resolve({ items: [] })
3504
+ return new Promise((resolve) => {
3505
+ let settled = false
3506
+ const finish = (value) => {
3507
+ if (settled) return
3508
+ settled = true
3509
+ resolve(value)
3510
+ }
3511
+ const timer = window.setTimeout(() => {
3512
+ void (async () => {
3513
+ try {
3514
+ if (token !== null && token !== undefined && token.isCancellationRequested === true) return finish({ items: [] })
3515
+ const now = Date.now()
3516
+ if (now - inlineLastRequestAt < 500) return finish({ items: [] })
3517
+ inlineLastRequestAt = now
3518
+ const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
3519
+ if (!inlineLanguages.includes(language)) return finish({ items: [] })
3520
+ const lineCount = model.getLineCount()
3521
+ const startLine = Math.max(1, position.lineNumber - 140)
3522
+ const endLine = Math.min(lineCount, position.lineNumber + 30)
3523
+ const endColumn = model.getLineMaxColumn(endLine)
3524
+ let prefix = ''
3525
+ let suffix = ''
3526
+ try {
3527
+ prefix = model.getValueInRange(new monaco.Range(startLine, 1, position.lineNumber, position.column)).slice(-3200)
3528
+ suffix = model.getValueInRange(new monaco.Range(position.lineNumber, position.column, endLine, endColumn)).slice(0, 800)
3529
+ } catch {
3530
+ return finish({ items: [] })
3531
+ }
3532
+ const version = typeof model.getVersionId === 'function' ? model.getVersionId() : 0
3533
+ const cacheKey = `${path}|${version}|${position.lineNumber}:${position.column}|${language}`
3534
+ const cached = inlineCache.get(cacheKey)
3535
+ if (cached !== undefined) return finish(cached)
3536
+ const value = await G.api('completion.suggest', { root, path, language, prefix, suffix })
3537
+ if (token !== null && token !== undefined && token.isCancellationRequested === true) return finish({ items: [] })
3538
+ const text = value !== null && value !== undefined && typeof value.text === 'string' ? value.text : ''
3539
+ const result = text === ''
3540
+ ? { items: [] }
3541
+ : { items: [{ insertText: text, range: new monaco.Range(position.lineNumber, position.column, position.lineNumber, position.column) }] }
3542
+ if (inlineCache.size > 30) inlineCache.clear()
3543
+ inlineCache.set(cacheKey, result)
3544
+ finish(result)
3545
+ } catch {
3546
+ finish({ items: [] })
3547
+ }
3548
+ })()
3549
+ }, 260)
3550
+ if (token !== null && token !== undefined && typeof token.onCancellationRequested === 'function') {
3551
+ token.onCancellationRequested(() => {
3552
+ window.clearTimeout(timer)
3553
+ finish({ items: [] })
3554
+ })
3555
+ }
3556
+ })
3557
+ },
3558
+ freeInlineCompletions() {},
3559
+ }
3560
+ const inlineDisposable = monaco.languages.registerInlineCompletionsProvider(inlineLanguages, inlineProvider)
3561
+ if (inlineDisposable !== null && inlineDisposable !== undefined && typeof inlineDisposable.dispose === 'function') subscriptions.push(inlineDisposable)
3562
+ }
3563
+ } catch {
3564
+ /* ignore */
3565
+ }
3566
+
3488
3567
  G.__gufaEditorDebug = {
3489
3568
  focus: () => {
3490
3569
  try {
@@ -3718,6 +3797,11 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
3718
3797
  /* ignore */
3719
3798
  }
3720
3799
  }
3800
+ /* 打开 Python 文件时预热 pyright,避免第一次 Cmd+点击等语言服务启动 */
3801
+ if (targetTab.language === 'python' && !GUFA_LSP_WARMED.has(root)) {
3802
+ GUFA_LSP_WARMED.add(root)
3803
+ void G.api('lsp.warm', { root }).catch(() => { GUFA_LSP_WARMED.delete(root) })
3804
+ }
3721
3805
  /* 打开/切换文件不抢焦点:只有用户点击编辑区时才进入键盘编辑 */
3722
3806
  }
3723
3807
  } else if (targetTab !== 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.88</string>
11
- <key>CFBundleVersion</key><string>1.3.88</string>
10
+ <key>CFBundleShortVersionString</key><string>1.3.90</string>
11
+ <key>CFBundleVersion</key><string>1.3.90</string>
12
12
  <key>LSUIElement</key><true/>
13
13
  <key>NSHighResolutionCapable</key><true/>
14
14
  <key>NSPrincipalClass</key><string>NSApplication</string>
@@ -23,6 +23,13 @@
23
23
  - LSP 细节:server→client 请求(workspace/configuration、registerCapability、workDoneProgress)必须回 null,否则 Pyright 初始化会挂住;`textDocument/definition` 返回 LocationLink 也要归一化(targetUri/targetRange)。
24
24
  - fs.js 热重载只重载 fs.js 本身,`lsp.js` 是静态 import 缓存的;session 在模块级 Map 里,改 lsp.js 需要重启 Host 才生效(不影响功能)。
25
25
 
26
+ ## 0.7 Copilot 风格 Tab 行内补全(1.3.89)
27
+
28
+ - 不依赖 Copilot/Codeium 等外部订阅:直接复用 Harness 的 `ctx.llm` 服务(`llm.stream`),provider/model 取 `ctx.get('agentDefaultModel').currentSelection()`,默认 `deepseek-official/deepseek-flash`,约 1s 返回。
29
+ - 客户端 Monaco:`editor options inlineSuggest.enabled=true` + `monaco.languages.registerInlineCompletionsProvider`,返回单条 `insertText`,Monaco 自带 Tab 接受;不要自己绑 Tab,否则会和缩进冲突。
30
+ - 必须做防串扰:`provideInlineCompletions` 里 260ms 防抖 + 全局 500ms 限流 + `token.isCancellationRequested` 取消 + 按 `path/version/position` 缓存;否则每敲一个字符都打模型、又慢又费 token。
31
+ - Host prompt 只带 `prefix`(末尾 3200 字)和 `suffix`(800 字),`maxTokens 160`、`temperature 0.2`;返回前剥掉 markdown 围栏。上下文太大不仅慢,而且容易让模型续写整段而不是补一行。
32
+
26
33
  ## 1. 弹窗/菜单层级:只有一个正确做法(反复踩过多次,最终定案)
27
34
 
28
35
  **现象**:弹窗被右侧「古法 AI」聊天栏压住、只显示一半、关闭按钮点不到;或者反过来把官方设置/会话弹窗盖住。
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Tab 行内补全:借用 Harness 自己的模型服务(ctx.llm),不需要额外 Copilot 订阅。
3
+ *
4
+ * 客户端 Monaco 在光标处请求 `completion.suggest`,Host 拼一小段 FIM 风格 prompt,
5
+ * 用当前默认模型(ctx.agentDefaultModel)流式生成,只回插入文本。为了保持轻量,
6
+ * prompt 只带光标前后有限上下文、maxTokens 很小,客户端做防抖/限流/取消。
7
+ */
8
+ import { ApiError, badRequest } from './context.js'
9
+
10
+ const SYSTEM_PROMPT = [
11
+ 'You are an inline code completion engine inside an IDE.',
12
+ 'Given the code before and after the cursor, predict the exact text the developer would type next.',
13
+ 'Return ONLY the insertion text: no markdown fences, no comments explaining, no repetition of existing text.',
14
+ 'Keep it short (usually one line to a few lines). Match the indentation of the surrounding code.',
15
+ ].join(' ')
16
+
17
+ const SUPPORTED = new Set([
18
+ 'python', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact',
19
+ 'json', 'markdown', 'css', 'scss', 'less', 'html', 'java', 'go', 'rust',
20
+ 'csharp', 'cpp', 'c', 'shell', 'yaml', 'sql', 'php', 'ruby',
21
+ ])
22
+
23
+ function sanitize(text) {
24
+ let value = String(text ?? '')
25
+ /* 去掉模型偶尔带出的 markdown 围栏 */
26
+ value = value.replace(/^```[a-zA-Z0-9_-]*\r?\n?/, '').replace(/\r?\n?```\s*$/, '')
27
+ /* 只取第一段有意义的插入内容,去掉结尾整行解释 */
28
+ value = value.replace(/\r\n/g, '\n')
29
+ const lines = value.split('\n')
30
+ while (lines.length > 0 && lines[0].trim() === '') lines.shift()
31
+ while (lines.length > 0 && lines[lines.length - 1].trim() === '') lines.pop()
32
+ value = lines.join('\n')
33
+ if (value.length > 1200) value = value.slice(0, 1200)
34
+ return value
35
+ }
36
+
37
+ function selectionOf(ctx) {
38
+ try {
39
+ const service = typeof ctx.get === 'function' ? ctx.get('agentDefaultModel') : undefined
40
+ if (service !== undefined && service !== null && typeof service.currentSelection === 'function') return service.currentSelection()
41
+ } catch {
42
+ /* ignore */
43
+ }
44
+ return undefined
45
+ }
46
+
47
+ export async function suggest(body, env) {
48
+ if (typeof body.prefix !== 'string' && typeof body.suffix !== 'string') throw badRequest('prefix is required')
49
+ const ctx = env.ctx
50
+ const llm = typeof ctx.get === 'function' ? ctx.get('llm') : undefined
51
+ if (llm === undefined || llm === null || typeof llm.stream !== 'function') {
52
+ throw new ApiError('llm-unavailable', '当前环境没有可用的模型服务', 503)
53
+ }
54
+ const language = typeof body.language === 'string' ? body.language : ''
55
+ if (!SUPPORTED.has(language)) return { skipped: true, text: '' }
56
+ const path = typeof body.path === 'string' ? body.path : ''
57
+ const prefix = String(body.prefix ?? '').slice(-6000)
58
+ const suffix = String(body.suffix ?? '').slice(0, 2000)
59
+ const fallback = selectionOf(ctx)
60
+ const provider = typeof body.provider === 'string' && body.provider !== '' ? body.provider : fallback?.provider
61
+ const model = typeof body.model === 'string' && body.model !== '' ? body.model : fallback?.model
62
+ if (typeof provider !== 'string' || provider === '' || typeof model !== 'string' || model === '') {
63
+ throw new ApiError('llm-model-unavailable', '没有可用的默认模型', 503)
64
+ }
65
+ const userPrompt = [
66
+ `File: ${path || '(untitled)'}`,
67
+ `Language: ${language}`,
68
+ '',
69
+ 'Code before cursor:',
70
+ '<<<',
71
+ prefix,
72
+ '>>>',
73
+ '',
74
+ 'Code after cursor:',
75
+ '<<<',
76
+ suffix,
77
+ '>>>',
78
+ '',
79
+ 'Insertion:',
80
+ ].join('\n')
81
+
82
+ const controller = new AbortController()
83
+ const timer = setTimeout(() => controller.abort(), 9000)
84
+ let text = ''
85
+ let failure = null
86
+ const startedAt = Date.now()
87
+ try {
88
+ const stream = llm.stream({
89
+ provider,
90
+ model,
91
+ system: SYSTEM_PROMPT,
92
+ messages: [{ role: 'user', content: [{ type: 'text', text: userPrompt }] }],
93
+ maxTokens: 160,
94
+ temperature: 0.2,
95
+ stop: ['\n\n\n'],
96
+ signal: controller.signal,
97
+ })
98
+ for await (const chunk of stream) {
99
+ if (chunk === null || typeof chunk !== 'object') continue
100
+ if (chunk.type === 'text-delta' && typeof chunk.text === 'string') text += chunk.text
101
+ if (chunk.type === 'reasoning-delta') continue
102
+ if (chunk.type === 'finish') {
103
+ const reason = chunk.reason
104
+ if (reason !== null && typeof reason === 'object' && (reason.kind === 'error' || reason.kind === 'aborted')) {
105
+ failure = reason.failure ?? { message: reason.kind }
106
+ }
107
+ break
108
+ }
109
+ }
110
+ } catch (error) {
111
+ failure = { message: String(error?.message ?? error) }
112
+ } finally {
113
+ clearTimeout(timer)
114
+ }
115
+ if (failure !== null && failure !== undefined) {
116
+ const message = typeof failure.message === 'string' ? failure.message : '模型调用失败'
117
+ if (controller.signal.aborted) throw new ApiError('llm-timeout', '补全请求超时', 504)
118
+ throw new ApiError('llm-failed', `补全模型调用失败:${message}`, 502)
119
+ }
120
+ return {
121
+ provider,
122
+ model,
123
+ text: sanitize(text),
124
+ latencyMs: Date.now() - startedAt,
125
+ }
126
+ }
127
+
128
+ export function registerCompletion(router, env) {
129
+ router.method('completion.suggest', (body) => suggest(body, env))
130
+ }
package/host/fs.js CHANGED
@@ -13,6 +13,7 @@ import { tmpdir } from 'node:os'
13
13
  import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
14
14
  import { ApiError, badRequest } from './context.js'
15
15
  import { registerLsp } from './lsp.js'
16
+ import { registerCompletion } from './completion.js'
16
17
 
17
18
  const TEXT_LIMIT = 4 * 1024 * 1024
18
19
  const runCommand = promisify(execFile)
@@ -248,6 +249,9 @@ export function register(router, env) {
248
249
  /* Python 语言服务(LSP/pyright):/lsp/definition API;Cmd+点击与悬停由客户端调用 */
249
250
  registerLsp(router, env)
250
251
 
252
+ /* Tab 行内补全:借用 Harness 自己的模型服务 */
253
+ registerCompletion(router, env)
254
+
251
255
  /* 代码导航(Cmd+点击跳定义):实现放在 code.js,但 Host 热重载只按 host/*.js 的 mtime
252
256
  触发,因此这里用带 mtime 的 query 动态 import,code.js 改动后下一次请求就能热生效。 */
253
257
  router.method('code.definition', async (body) => {
package/host/lsp.js CHANGED
@@ -320,6 +320,13 @@ async function definition(body, env) {
320
320
  return { matches, current: { path: relative(root, absolute).split(sep).join('/'), line, column }, language: 'python', server: 'pyright' }
321
321
  }
322
322
 
323
+ async function warm(body, env) {
324
+ if (typeof body.root !== 'string' || body.root === '') throw badRequest('root is required')
325
+ const project = env.projects.ensure(body.root)
326
+ await getSession(project.root)
327
+ return { ready: true, server: 'pyright', languages: ['python'] }
328
+ }
329
+
323
330
  async function status(body, env) {
324
331
  const entry = pyrightEntry()
325
332
  const available = await fileExists(entry)
@@ -343,6 +350,7 @@ async function status(body, env) {
343
350
 
344
351
  export function registerLsp(router, env) {
345
352
  router.method('lsp.status', (body) => status(body, env))
353
+ router.method('lsp.warm', (body) => warm(body, env))
346
354
  router.method('lsp.definition', (body) => definition(body, env))
347
355
  }
348
356
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-gufa-code",
3
- "version": "1.3.88",
3
+ "version": "1.3.90",
4
4
  "description": "古法编程 · AI 辅助:一键把 DSH 主面板变成 VS Code 风格 IDE(Monaco 编辑器、文件树、Git、SSH、真实终端 + 右侧官方会话);主张亲手写、每个改动心中有数,AI 是助理、你是第一责任人",
5
5
  "icon": "icon.svg",
6
6
  "keywords": [
@@ -400,6 +400,7 @@
400
400
  }
401
401
 
402
402
  /** 这些声明类型算「定义级」,多个同名时值得让用户选;普通 const/赋值等弱匹配不轻易弹列表。 */
403
+ const GUFA_LSP_WARMED = new Set()
403
404
  const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'def', 'func', 'fn', 'type', 'export', 'lsp'])
404
405
 
405
406
  /* ── model / URI ────────────────────────────────────────────────────── */
@@ -480,6 +481,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
480
481
  scrollbar: { verticalScrollbarSize: 10, horizontalScrollbarSize: 10, useShadows: false },
481
482
  mouseWheelZoom: false,
482
483
  contextmenu: true,
484
+ inlineSuggest: { enabled: true, showToolbar: 'never' },
483
485
  }
484
486
  }
485
487
 
@@ -1135,6 +1137,83 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
1135
1137
  } catch {
1136
1138
  /* ignore */
1137
1139
  }
1140
+ /* ── Copilot 风格 Tab 行内补全(复用 Harness 模型服务) ───────── */
1141
+ try {
1142
+ if (typeof monaco.languages.registerInlineCompletionsProvider === 'function') {
1143
+ const inlineLanguages = ['python', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'java', 'go', 'rust', 'csharp', 'cpp', 'c', 'shell', 'json', 'yaml', 'sql', 'html', 'css']
1144
+ const inlineCache = new Map()
1145
+ let inlineLastRequestAt = 0
1146
+ const inlineProvider = {
1147
+ provideInlineCompletions(model, position, context, token) {
1148
+ const path = modelPathRef.current
1149
+ const root = modelsRootRef.current
1150
+ if (path === null || path === undefined || root === null || root === undefined || root === '') {
1151
+ return Promise.resolve({ items: [] })
1152
+ }
1153
+ if (token !== null && token !== undefined && token.isCancellationRequested === true) return Promise.resolve({ items: [] })
1154
+ return new Promise((resolve) => {
1155
+ let settled = false
1156
+ const finish = (value) => {
1157
+ if (settled) return
1158
+ settled = true
1159
+ resolve(value)
1160
+ }
1161
+ const timer = window.setTimeout(() => {
1162
+ void (async () => {
1163
+ try {
1164
+ if (token !== null && token !== undefined && token.isCancellationRequested === true) return finish({ items: [] })
1165
+ const now = Date.now()
1166
+ if (now - inlineLastRequestAt < 500) return finish({ items: [] })
1167
+ inlineLastRequestAt = now
1168
+ const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
1169
+ if (!inlineLanguages.includes(language)) return finish({ items: [] })
1170
+ const lineCount = model.getLineCount()
1171
+ const startLine = Math.max(1, position.lineNumber - 140)
1172
+ const endLine = Math.min(lineCount, position.lineNumber + 30)
1173
+ const endColumn = model.getLineMaxColumn(endLine)
1174
+ let prefix = ''
1175
+ let suffix = ''
1176
+ try {
1177
+ prefix = model.getValueInRange(new monaco.Range(startLine, 1, position.lineNumber, position.column)).slice(-3200)
1178
+ suffix = model.getValueInRange(new monaco.Range(position.lineNumber, position.column, endLine, endColumn)).slice(0, 800)
1179
+ } catch {
1180
+ return finish({ items: [] })
1181
+ }
1182
+ const version = typeof model.getVersionId === 'function' ? model.getVersionId() : 0
1183
+ const cacheKey = `${path}|${version}|${position.lineNumber}:${position.column}|${language}`
1184
+ const cached = inlineCache.get(cacheKey)
1185
+ if (cached !== undefined) return finish(cached)
1186
+ const value = await G.api('completion.suggest', { root, path, language, prefix, suffix })
1187
+ if (token !== null && token !== undefined && token.isCancellationRequested === true) return finish({ items: [] })
1188
+ const text = value !== null && value !== undefined && typeof value.text === 'string' ? value.text : ''
1189
+ const result = text === ''
1190
+ ? { items: [] }
1191
+ : { items: [{ insertText: text, range: new monaco.Range(position.lineNumber, position.column, position.lineNumber, position.column) }] }
1192
+ if (inlineCache.size > 30) inlineCache.clear()
1193
+ inlineCache.set(cacheKey, result)
1194
+ finish(result)
1195
+ } catch {
1196
+ finish({ items: [] })
1197
+ }
1198
+ })()
1199
+ }, 260)
1200
+ if (token !== null && token !== undefined && typeof token.onCancellationRequested === 'function') {
1201
+ token.onCancellationRequested(() => {
1202
+ window.clearTimeout(timer)
1203
+ finish({ items: [] })
1204
+ })
1205
+ }
1206
+ })
1207
+ },
1208
+ freeInlineCompletions() {},
1209
+ }
1210
+ const inlineDisposable = monaco.languages.registerInlineCompletionsProvider(inlineLanguages, inlineProvider)
1211
+ if (inlineDisposable !== null && inlineDisposable !== undefined && typeof inlineDisposable.dispose === 'function') subscriptions.push(inlineDisposable)
1212
+ }
1213
+ } catch {
1214
+ /* ignore */
1215
+ }
1216
+
1138
1217
  G.__gufaEditorDebug = {
1139
1218
  focus: () => {
1140
1219
  try {
@@ -1368,6 +1447,11 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
1368
1447
  /* ignore */
1369
1448
  }
1370
1449
  }
1450
+ /* 打开 Python 文件时预热 pyright,避免第一次 Cmd+点击等语言服务启动 */
1451
+ if (targetTab.language === 'python' && !GUFA_LSP_WARMED.has(root)) {
1452
+ GUFA_LSP_WARMED.add(root)
1453
+ void G.api('lsp.warm', { root }).catch(() => { GUFA_LSP_WARMED.delete(root) })
1454
+ }
1371
1455
  /* 打开/切换文件不抢焦点:只有用户点击编辑区时才进入键盘编辑 */
1372
1456
  }
1373
1457
  } else if (targetTab !== null) {