dsh-gufa-code 1.3.215 → 1.3.218
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 +59 -20
- package/client.js +216 -28
- package/docs/screenshot-git.jpg +0 -0
- package/docs/screenshot-main.jpg +0 -0
- package/docs/screenshot-moyu.jpg +0 -0
- package/host/code.js +86 -5
- package/host/fs.js +98 -3
- package/package.json +1 -1
- package/screenshots.json +3 -1
- package/scripts/selftest-host.mjs +47 -0
- package/src/client/00-core.js +2 -0
- package/src/client/20-editor.js +68 -15
- package/src/client/50-panels.js +116 -5
- package/src/client/58b-drag-ref.js +3 -0
- package/src/client/90-app.js +26 -7
package/host/code.js
CHANGED
|
@@ -190,7 +190,67 @@ function importTargetPaths(currentPath, spec) {
|
|
|
190
190
|
]
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
|
|
193
|
+
/** 解析 PHP 的 use 语句:短类名 → 完全限定类名(支持 as 别名 / use function / use const)。 */
|
|
194
|
+
function parsePhpImports(text) {
|
|
195
|
+
const map = new Map()
|
|
196
|
+
const useRe = /^[ \t]*use[ \t]+(?:function[ \t]+|const[ \t]+)?([A-Za-z_][\w\\]*)(?:[ \t]+as[ \t]+([A-Za-z_]\w*))?[ \t]*;/gm
|
|
197
|
+
let match
|
|
198
|
+
while ((match = useRe.exec(String(text))) !== null) {
|
|
199
|
+
const fqcn = match[1]
|
|
200
|
+
const segments = fqcn.split('\\').filter((part) => part !== '')
|
|
201
|
+
if (segments.length === 0) continue
|
|
202
|
+
const alias = match[2] !== undefined ? match[2] : segments[segments.length - 1]
|
|
203
|
+
if (/^[A-Za-z_]\w*$/.test(alias)) map.set(alias, fqcn)
|
|
204
|
+
}
|
|
205
|
+
return map
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 读 composer.json 的 PSR-4 映射,按 mtime 缓存。 */
|
|
209
|
+
const PSR4_CACHE = new Map()
|
|
210
|
+
async function psr4Map(root) {
|
|
211
|
+
let stamp = 'none'
|
|
212
|
+
try {
|
|
213
|
+
stamp = String((await stat(join(root, 'composer.json'))).mtimeMs)
|
|
214
|
+
} catch {
|
|
215
|
+
stamp = 'none'
|
|
216
|
+
}
|
|
217
|
+
const cached = PSR4_CACHE.get(root)
|
|
218
|
+
if (cached !== undefined && cached.stamp === stamp) return cached.map
|
|
219
|
+
let map = {}
|
|
220
|
+
try {
|
|
221
|
+
const parsed = JSON.parse(await readFile(join(root, 'composer.json'), 'utf8'))
|
|
222
|
+
const autoload = parsed !== null && typeof parsed === 'object' ? parsed.autoload : null
|
|
223
|
+
const psr4 = autoload !== null && typeof autoload === 'object' ? autoload['psr-4'] : null
|
|
224
|
+
if (psr4 !== null && typeof psr4 === 'object') {
|
|
225
|
+
for (const [prefix, target] of Object.entries(psr4)) {
|
|
226
|
+
const value = Array.isArray(target) ? target[0] : target
|
|
227
|
+
if (typeof value !== 'string') continue
|
|
228
|
+
map[String(prefix).replace(/\\+$/, '')] = value.replace(/\/+$/, '').replace(/^\.\//, '')
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
} catch {
|
|
232
|
+
map = {}
|
|
233
|
+
}
|
|
234
|
+
PSR4_CACHE.set(root, { stamp, map })
|
|
235
|
+
return map
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** 完全限定 PHP 类名 → 项目内相对路径候选(PSR-4)。 */
|
|
239
|
+
async function phpClassPaths(root, fqcn) {
|
|
240
|
+
const name = String(fqcn).replace(/^\\/, '')
|
|
241
|
+
const psr4 = await psr4Map(root)
|
|
242
|
+
const paths = []
|
|
243
|
+
for (const [prefix, dir] of Object.entries(psr4)) {
|
|
244
|
+
if (name !== prefix && !name.startsWith(`${prefix}\\`)) continue
|
|
245
|
+
const rest = name.slice(prefix.length).replace(/^\\/, '').replace(/\\/g, '/')
|
|
246
|
+
if (rest === '') continue
|
|
247
|
+
const base = dir === '' ? rest : `${dir}/${rest}`
|
|
248
|
+
paths.push(`${base}.php`, `${base}/index.php`)
|
|
249
|
+
}
|
|
250
|
+
return paths
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** 解析当前文件的 import/require,得到 标识符 → 模块 spec 的映射。 */async function listCandidates(root, textBudgets) {
|
|
194
254
|
/* Git 项目用 git ls-files(尊重 .gitignore、含未跟踪文件),否则 BFS 遍历。 */
|
|
195
255
|
let isGit = false
|
|
196
256
|
try {
|
|
@@ -269,6 +329,13 @@ function findDefinitionsInText(text, relPath, word, patterns, results, current)
|
|
|
269
329
|
for (const pattern of patterns) {
|
|
270
330
|
const match = pattern.regex.exec(line)
|
|
271
331
|
if (match === null) continue
|
|
332
|
+
/* method 模式的修饰符段会把调用语句当成定义(`throw new X(...)` 的 `throw new ` 也符合),
|
|
333
|
+
导致跳到调用行自身;按前缀关键字排除。 */
|
|
334
|
+
if (pattern.kind === 'method') {
|
|
335
|
+
const wordIndex = match[0].indexOf(match[1])
|
|
336
|
+
const prefix = wordIndex >= 0 ? match[0].slice(0, wordIndex) : ''
|
|
337
|
+
if (/(?:^|\s)(?:new|throw|return|echo|print|yield|await|instanceof|elseif|else|while|for|foreach|switch|case|catch|if|require|include)\s*$/.test(prefix)) continue
|
|
338
|
+
}
|
|
272
339
|
const column = (match.index ?? 0) + match[0].indexOf(match[1]) + 1
|
|
273
340
|
results.push({
|
|
274
341
|
path: relPath,
|
|
@@ -321,6 +388,22 @@ export async function definition(body, env) {
|
|
|
321
388
|
const importMap = parseImportMap(text)
|
|
322
389
|
const importedSpec = importMap.get(word)
|
|
323
390
|
const importTargets = new Set(importedSpec === undefined ? [] : importTargetPaths(relPath, importedSpec))
|
|
391
|
+
/* PHP:use 导入 + composer.json PSR-4;静态调用 ItemLogic::getItemData 点到方法名时按限定符反查 */
|
|
392
|
+
try {
|
|
393
|
+
const phpImports = parsePhpImports(text)
|
|
394
|
+
if (phpImports.size > 0) {
|
|
395
|
+
const owners = []
|
|
396
|
+
if (phpImports.has(word)) owners.push(word)
|
|
397
|
+
const prefix = lineText.slice(0, Math.max(column, 1))
|
|
398
|
+
const qualified = /([A-Za-z_]\w*)::\s*[\A-Za-z_0-9]*$/.exec(prefix)
|
|
399
|
+
if (qualified !== null && phpImports.has(qualified[1]) && !owners.includes(qualified[1])) owners.push(qualified[1])
|
|
400
|
+
for (const owner of owners) {
|
|
401
|
+
for (const target of await phpClassPaths(root, phpImports.get(owner))) importTargets.add(target)
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
} catch {
|
|
405
|
+
/* PHP 解析失败不影响其它语言 */
|
|
406
|
+
}
|
|
324
407
|
const results = []
|
|
325
408
|
/* 1) 当前文件(用编辑器传来的内容,能命中未保存的改动) */
|
|
326
409
|
findDefinitionsInText(text, relPath, word, patterns, results, current)
|
|
@@ -420,7 +503,5 @@ export async function resolvePath(body, env) {
|
|
|
420
503
|
return { path: pool[0], matches: pool.slice(0, 10) }
|
|
421
504
|
}
|
|
422
505
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
router.method('code.resolvePath', (body) => resolvePath(body, env))
|
|
426
|
-
}
|
|
506
|
+
/** 故意不注册路由:code.definition/code.resolvePath 由 host/fs.js 统一注册(热重载入口)。 */
|
|
507
|
+
export function register() {}
|
package/host/fs.js
CHANGED
|
@@ -6,12 +6,12 @@ import { createHash, randomUUID } from 'node:crypto'
|
|
|
6
6
|
import { statSync } from 'node:fs'
|
|
7
7
|
import { createReadStream } from 'node:fs'
|
|
8
8
|
import { access, cp, lstat, mkdir, open, readFile, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
9
|
-
import { execFile } from 'node:child_process'
|
|
9
|
+
import { execFile, spawn } from 'node:child_process'
|
|
10
10
|
import { promisify } from 'node:util'
|
|
11
11
|
import { constants } from 'node:fs'
|
|
12
12
|
import { tmpdir } from 'node:os'
|
|
13
13
|
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
14
|
-
import { fileURLToPath } from 'node:url'
|
|
14
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
15
15
|
import { ApiError, badRequest } from './context.js'
|
|
16
16
|
import { registerLsp } from './lsp.js'
|
|
17
17
|
|
|
@@ -99,7 +99,31 @@ async function readClipboardFilePaths() {
|
|
|
99
99
|
'JSON.stringify(out);',
|
|
100
100
|
].join('\n')
|
|
101
101
|
const { stdout } = await runCommand('osascript', ['-l', 'JavaScript', '-e', script], { timeout: 5000, maxBuffer: 2 * 1024 * 1024 })
|
|
102
|
-
|
|
102
|
+
const direct = parseClipboardPaths(stdout)
|
|
103
|
+
if (direct.length > 0) return direct
|
|
104
|
+
/* 古法编程自己拷的文件是 AppleScript alias 列表(不是 Finder 的 file-url 项),
|
|
105
|
+
用 the clipboard as list 再取一次 POSIX 路径。 */
|
|
106
|
+
const listScript = [
|
|
107
|
+
'on run',
|
|
108
|
+
'try',
|
|
109
|
+
'set theList to the clipboard as list',
|
|
110
|
+
'on error',
|
|
111
|
+
'return ""',
|
|
112
|
+
'end try',
|
|
113
|
+
'set out to ""',
|
|
114
|
+
'repeat with f in theList',
|
|
115
|
+
'try',
|
|
116
|
+
'set out to out & POSIX path of f & linefeed',
|
|
117
|
+
'end try',
|
|
118
|
+
'end repeat',
|
|
119
|
+
'return out',
|
|
120
|
+
'end run',
|
|
121
|
+
].join('\n')
|
|
122
|
+
const fallback = await runCommand('osascript', ['-e', listScript], { timeout: 5000, maxBuffer: 2 * 1024 * 1024 }).catch(() => ({ stdout: '' }))
|
|
123
|
+
return String(fallback.stdout ?? '')
|
|
124
|
+
.split(/\r?\n/)
|
|
125
|
+
.map((line) => line.trim())
|
|
126
|
+
.filter((line) => line !== '')
|
|
103
127
|
}
|
|
104
128
|
if (process.platform === 'win32') {
|
|
105
129
|
const script = [
|
|
@@ -144,6 +168,57 @@ function parseClipboardPaths(stdout) {
|
|
|
144
168
|
}
|
|
145
169
|
}
|
|
146
170
|
|
|
171
|
+
/** 把项目内文件/文件夹路径写进系统剪贴板(file URL),供另一个古法编程实例/系统粘贴。 */
|
|
172
|
+
async function writeClipboardFilePaths(paths) {
|
|
173
|
+
const list = paths.filter((item) => typeof item === 'string' && item !== '')
|
|
174
|
+
if (list.length === 0) return false
|
|
175
|
+
try {
|
|
176
|
+
if (process.platform === 'darwin') {
|
|
177
|
+
/* 用 AppleScript 写 alias 列表:JXA 的 writeObjects 在 osascript 退出后剪贴板会丢,
|
|
178
|
+
AppleScript 的 set the clipboard to {POSIX file ...} 能跨进程保留。 */
|
|
179
|
+
const script = [
|
|
180
|
+
'on run argv',
|
|
181
|
+
'set out to {}',
|
|
182
|
+
'repeat with p in argv',
|
|
183
|
+
'set end of out to POSIX file p',
|
|
184
|
+
'end repeat',
|
|
185
|
+
'set the clipboard to out',
|
|
186
|
+
'end run',
|
|
187
|
+
].join('\n')
|
|
188
|
+
await runCommand('osascript', ['-e', script, ...list], { timeout: 8000, maxBuffer: 1024 * 1024 })
|
|
189
|
+
return true
|
|
190
|
+
}
|
|
191
|
+
if (process.platform === 'win32') {
|
|
192
|
+
const script = [
|
|
193
|
+
'[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;',
|
|
194
|
+
'Add-Type -AssemblyName System.Windows.Forms;',
|
|
195
|
+
'$paths = ConvertFrom-Json $env:GUFA_CLIP_PATHS;',
|
|
196
|
+
'$col = New-Object System.Collections.Specialized.StringCollection;',
|
|
197
|
+
'foreach ($item in @($paths)) { [void]$col.Add([string]$item) }',
|
|
198
|
+
'[System.Windows.Forms.Clipboard]::SetFileDropList($col);',
|
|
199
|
+
].join('\n')
|
|
200
|
+
await runCommand('powershell', ['-NoProfile', '-NonInteractive', '-Sta', '-Command', script], {
|
|
201
|
+
timeout: 8000,
|
|
202
|
+
maxBuffer: 1024 * 1024,
|
|
203
|
+
env: { ...process.env, GUFA_CLIP_PATHS: JSON.stringify(list) },
|
|
204
|
+
})
|
|
205
|
+
return true
|
|
206
|
+
}
|
|
207
|
+
/* Linux:xclip 写 text/uri-list;没有 xclip 时返回 false 交给前端提示 */
|
|
208
|
+
const uriList = list.map((path) => pathToFileURL(path).href).join('\n') + '\n'
|
|
209
|
+
await new Promise((resolvePromise, reject) => {
|
|
210
|
+
const child = spawn('xclip', ['-selection', 'clipboard', '-t', 'text/uri-list', '-i'], { stdio: ['pipe', 'ignore', 'ignore'] })
|
|
211
|
+
child.once('error', reject)
|
|
212
|
+
child.once('exit', (code) => (code === 0 ? resolvePromise() : reject(new Error(`xclip exit ${code}`))))
|
|
213
|
+
child.stdin.end(uriList)
|
|
214
|
+
})
|
|
215
|
+
return true
|
|
216
|
+
} catch {
|
|
217
|
+
return false
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
147
222
|
export async function resolveProjectPath(root, raw, { create = false } = {}) {
|
|
148
223
|
if (typeof raw !== 'string') throw badRequest('path must be a string')
|
|
149
224
|
if (isAbsolute(raw)) throw new ApiError('path-invalid', '不允许绝对路径', 400)
|
|
@@ -631,6 +706,26 @@ export function register(router, env) {
|
|
|
631
706
|
return { copied, failed }
|
|
632
707
|
})
|
|
633
708
|
|
|
709
|
+
/** 把资源管理器选中的文件/文件夹写入系统剪贴板(跨古法编程实例复制粘贴)。 */
|
|
710
|
+
router.method('fs.copyToSystemClipboard', async (body) => {
|
|
711
|
+
const root = await projectOf(env, body)
|
|
712
|
+
const list = Array.isArray(body.from) ? body.from.filter((item) => typeof item === 'string' && item !== '') : []
|
|
713
|
+
if (list.length === 0) throw badRequest('from is required')
|
|
714
|
+
const absolute = []
|
|
715
|
+
for (const raw of list) {
|
|
716
|
+
const source = await resolveProjectPath(root, raw)
|
|
717
|
+
if (source === root) throw new ApiError('refuse', '不能复制项目根目录', 400)
|
|
718
|
+
const info = await stat(source)
|
|
719
|
+
if (info === undefined) continue
|
|
720
|
+
absolute.push(source)
|
|
721
|
+
}
|
|
722
|
+
if (absolute.length === 0) throw new ApiError('not-found', '选中的文件不存在', 404)
|
|
723
|
+
if (!(await writeClipboardFilePaths(absolute))) {
|
|
724
|
+
throw new ApiError('clipboard-failed', '写入系统剪贴板失败(Linux 需要安装 xclip)', 500)
|
|
725
|
+
}
|
|
726
|
+
return { ok: true, count: absolute.length }
|
|
727
|
+
})
|
|
728
|
+
|
|
634
729
|
/** 把系统剪贴板里的文件匹配回项目内已有文件(供聊天框「粘贴文件→@引用」使用)。 */
|
|
635
730
|
/** 全项目文本搜索(左侧「搜索」面板用)。返回按文件分组、带上行列号的匹配。 */
|
|
636
731
|
router.method('fs.search', async (body) => {
|
package/package.json
CHANGED
package/screenshots.json
CHANGED
|
@@ -436,6 +436,53 @@ try {
|
|
|
436
436
|
check('definition ignores too-short word', emptyResult.word === 'x' && emptyResult.matches.length === 0)
|
|
437
437
|
await call(router, 'fs.delete', { root, path: 'nav' }).catch(() => {})
|
|
438
438
|
|
|
439
|
+
console.log('code.definition PHP')
|
|
440
|
+
const phpComposer = JSON.stringify({ autoload: { 'psr-4': { 'App\\': 'app/' } } }, null, 2)
|
|
441
|
+
await call(router, 'fs.create', { root, path: 'composer.json', kind: 'file' })
|
|
442
|
+
await call(router, 'fs.write', { root, path: 'composer.json', content: phpComposer })
|
|
443
|
+
await call(router, 'fs.create', { root, path: 'app', kind: 'dir' })
|
|
444
|
+
await call(router, 'fs.create', { root, path: 'app/Exception', kind: 'dir' })
|
|
445
|
+
await call(router, 'fs.create', { root, path: 'app/Exception/AppException.php', kind: 'file' })
|
|
446
|
+
await call(router, 'fs.write', { root, path: 'app/Exception/AppException.php', content: '<?php\nnamespace App\\Exception;\n\nclass AppException extends \\RuntimeException\n{\n}\n' })
|
|
447
|
+
await call(router, 'fs.create', { root, path: 'app/Helper', kind: 'dir' })
|
|
448
|
+
await call(router, 'fs.create', { root, path: 'app/Helper/Functions.php', kind: 'file' })
|
|
449
|
+
await call(router, 'fs.write', { root, path: 'app/Helper/Functions.php', content: '<?php\n\nfunction get_return()\n{\n return 1;\n}\n' })
|
|
450
|
+
await call(router, 'fs.create', { root, path: 'app/Logic', kind: 'dir' })
|
|
451
|
+
await call(router, 'fs.create', { root, path: 'app/Logic/ItemLogic.php', kind: 'file' })
|
|
452
|
+
await call(router, 'fs.write', { root, path: 'app/Logic/ItemLogic.php', content: '<?php\nnamespace App\\Logic;\n\nclass ItemLogic\n{\n public function getItemData()\n {\n return [];\n }\n}\n' })
|
|
453
|
+
await call(router, 'fs.create', { root, path: 'app/Controller', kind: 'dir' })
|
|
454
|
+
const phpUse = [
|
|
455
|
+
'<?php',
|
|
456
|
+
'namespace App\\Controller;',
|
|
457
|
+
'',
|
|
458
|
+
'use App\\Logic\\ItemLogic;',
|
|
459
|
+
'use App\\Exception\\AppException;',
|
|
460
|
+
'',
|
|
461
|
+
'class Test',
|
|
462
|
+
'{',
|
|
463
|
+
' public function run()',
|
|
464
|
+
' {',
|
|
465
|
+
' throw new AppException($msg, $code);',
|
|
466
|
+
' $x = get_return();',
|
|
467
|
+
' $y = ItemLogic::getItemData();',
|
|
468
|
+
' }',
|
|
469
|
+
'}',
|
|
470
|
+
'',
|
|
471
|
+
].join('\n')
|
|
472
|
+
await call(router, 'fs.create', { root, path: 'app/Controller/Test.php', kind: 'file' })
|
|
473
|
+
await call(router, 'fs.write', { root, path: 'app/Controller/Test.php', content: phpUse })
|
|
474
|
+
const phpClassDef = await call(router, 'code.definition', { root, path: 'app/Controller/Test.php', line: 11, column: 22, word: 'AppException', content: phpUse })
|
|
475
|
+
check('PHP finds use-imported class via PSR-4', phpClassDef.matches[0]?.path === 'app/Exception/AppException.php', JSON.stringify(phpClassDef.matches.slice(0, 3)))
|
|
476
|
+
check('PHP throw call is not treated as definition', !phpClassDef.matches.some((item) => item.path === 'app/Controller/Test.php'), JSON.stringify(phpClassDef.matches.slice(0, 3)))
|
|
477
|
+
const phpFnDef = await call(router, 'code.definition', { root, path: 'app/Controller/Test.php', line: 12, column: 14, word: 'get_return', content: phpUse })
|
|
478
|
+
check('PHP finds global function', phpFnDef.matches[0]?.path === 'app/Helper/Functions.php', JSON.stringify(phpFnDef.matches.slice(0, 3)))
|
|
479
|
+
const phpStaticClassDef = await call(router, 'code.definition', { root, path: 'app/Controller/Test.php', line: 13, column: 22, word: 'ItemLogic', content: phpUse })
|
|
480
|
+
check('PHP static call class resolves via use', phpStaticClassDef.matches[0]?.path === 'app/Logic/ItemLogic.php', JSON.stringify(phpStaticClassDef.matches.slice(0, 3)))
|
|
481
|
+
const phpStaticMethodDef = await call(router, 'code.definition', { root, path: 'app/Controller/Test.php', line: 13, column: 36, word: 'getItemData', content: phpUse })
|
|
482
|
+
check('PHP static method resolves via qualified use', phpStaticMethodDef.matches[0]?.path === 'app/Logic/ItemLogic.php', JSON.stringify(phpStaticMethodDef.matches.slice(0, 3)))
|
|
483
|
+
await call(router, 'fs.delete', { root, path: 'app' }).catch(() => {})
|
|
484
|
+
await call(router, 'fs.delete', { root, path: 'composer.json' }).catch(() => {})
|
|
485
|
+
|
|
439
486
|
console.log('workspaces')
|
|
440
487
|
const fakeSessions = new Map([
|
|
441
488
|
['s1', { id: 's1', updatedAt: 20 }],
|
package/src/client/00-core.js
CHANGED
|
@@ -1786,6 +1786,8 @@ html[data-platform=darwin][data-fullscreen] .gufa-titlebar{height:44px;padding-l
|
|
|
1786
1786
|
color:var(--gufa-text);cursor:pointer;text-align:left;font-size:12.5px;white-space:nowrap;min-width:0;width:100%}
|
|
1787
1787
|
.gufa-tree-row:hover{background:var(--gufa-hover)}
|
|
1788
1788
|
.gufa-tree-row[data-selected]{background:var(--gufa-active)}
|
|
1789
|
+
.gufa-tree-row[data-drop]{background:color-mix(in srgb, var(--gufa-accent) 18%, transparent);outline:1px dashed var(--gufa-accent);outline-offset:-1px;border-radius:6px}
|
|
1790
|
+
.gufa-panel-body[data-drop-root]{box-shadow:inset 0 0 0 2px color-mix(in srgb, var(--gufa-accent) 55%, transparent)}
|
|
1789
1791
|
.gufa-tree-row[data-kind="dir"]{color:var(--gufa-text)}
|
|
1790
1792
|
.gufa-tree-name{overflow:hidden;text-overflow:ellipsis;flex:1 1 auto;min-width:0}
|
|
1791
1793
|
.gufa-explorer-notice{margin:4px 10px 0;padding:3px 8px;border-radius:6px;font-size:11px;line-height:1.5;color:var(--gufa-text);
|
package/src/client/20-editor.js
CHANGED
|
@@ -413,7 +413,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
413
413
|
*/
|
|
414
414
|
/* JS/TS 的 F12 交回 Monaco 原生 TS DefinitionProvider(之前被我们关掉过);
|
|
415
415
|
Cmd+点击在 click handler 里直接走 Host LanguageService 以拿到最准结果。 */
|
|
416
|
-
const GUFA_DEFINITION_LANGUAGES = ['python']
|
|
416
|
+
const GUFA_DEFINITION_LANGUAGES = ['python', 'php']
|
|
417
417
|
function gufaRegisterDefinitionInfrastructure(monaco) {
|
|
418
418
|
try {
|
|
419
419
|
if (monaco === null || monaco === undefined) return
|
|
@@ -473,7 +473,7 @@ function gufaRegisterDefinitionInfrastructure(monaco) {
|
|
|
473
473
|
language: 'python',
|
|
474
474
|
content: model.getValue(),
|
|
475
475
|
})
|
|
476
|
-
} else {
|
|
476
|
+
} else if (language === 'javascript' || language === 'typescript') {
|
|
477
477
|
/* LanguageService 解析定义(TS 编译器),比正则项目搜索准确;
|
|
478
478
|
JS/TS 不再回退到 code.definition,避免“宁可跳错也不空着”。 */
|
|
479
479
|
result = await G.api('code.tsDefinition', {
|
|
@@ -483,6 +483,16 @@ function gufaRegisterDefinitionInfrastructure(monaco) {
|
|
|
483
483
|
column: position.column,
|
|
484
484
|
content: model.getValue(),
|
|
485
485
|
})
|
|
486
|
+
} else {
|
|
487
|
+
/* PHP 等无内置语言服务的语言:项目级定义搜索(Host code.definition) */
|
|
488
|
+
result = await G.api('code.definition', {
|
|
489
|
+
root: info.root,
|
|
490
|
+
path: info.path,
|
|
491
|
+
line: position.lineNumber,
|
|
492
|
+
column: position.column,
|
|
493
|
+
word,
|
|
494
|
+
content: model.getValue(),
|
|
495
|
+
})
|
|
486
496
|
}
|
|
487
497
|
if (token !== null && token !== undefined && token.isCancellationRequested === true) return []
|
|
488
498
|
return locationsOf(info.root, result !== null && result !== undefined ? result.matches : null, word, monaco)
|
|
@@ -770,6 +780,23 @@ const GUFA_LINT_ENABLED = true
|
|
|
770
780
|
const activePathRef = useRef(activePath)
|
|
771
781
|
const statusRef = useRef('loading')
|
|
772
782
|
const pendingRevealRef = useRef(null)
|
|
783
|
+
/* 打开未加载的文件时,editor 可能还没挂载/模型还没切换;先记住目标位置,
|
|
784
|
+
等编辑器就绪或切到目标文件后再落光标,避免只跳文件、位置不准。 */
|
|
785
|
+
const applyPendingReveal = useCallback((editor) => {
|
|
786
|
+
const pending = pendingRevealRef.current
|
|
787
|
+
if (pending === null || pending === undefined) return false
|
|
788
|
+
if (editor === null || editor === undefined) return false
|
|
789
|
+
if (modelPathRef.current !== pending.path) return false
|
|
790
|
+
pendingRevealRef.current = null
|
|
791
|
+
try {
|
|
792
|
+
editor.setPosition({ lineNumber: pending.line, column: pending.column })
|
|
793
|
+
editor.revealLineInCenter(pending.line)
|
|
794
|
+
editor.focus()
|
|
795
|
+
} catch {
|
|
796
|
+
/* ignore */
|
|
797
|
+
}
|
|
798
|
+
return true
|
|
799
|
+
}, [])
|
|
773
800
|
|
|
774
801
|
/** idle → loading → ready / error */
|
|
775
802
|
const [status, setStatus] = useState('loading')
|
|
@@ -1332,6 +1359,33 @@ const GUFA_LINT_ENABLED = true
|
|
|
1332
1359
|
clearHoverDecoration()
|
|
1333
1360
|
return
|
|
1334
1361
|
}
|
|
1362
|
+
/* 其它语言(PHP / Go / Java …):走项目级定义搜索。
|
|
1363
|
+
queryDefinitions + decideDefinition 已实现,此前没接到点击入口。 */
|
|
1364
|
+
try {
|
|
1365
|
+
const resolved = await queryDefinitions(position, word, model, path, root)
|
|
1366
|
+
const decision = decideDefinition(resolved)
|
|
1367
|
+
if (decision.kind === 'open') {
|
|
1368
|
+
G.bus.emit('editor:open-at', {
|
|
1369
|
+
path: decision.match.path,
|
|
1370
|
+
line: decision.match.line,
|
|
1371
|
+
column: decision.match.column,
|
|
1372
|
+
})
|
|
1373
|
+
clearHoverDecoration()
|
|
1374
|
+
return
|
|
1375
|
+
}
|
|
1376
|
+
if (decision.kind === 'pick') {
|
|
1377
|
+
G.bus.emit('definition:pick', {
|
|
1378
|
+
word,
|
|
1379
|
+
matches: decision.matches,
|
|
1380
|
+
x: Number.isFinite(Number(event.clientX)) ? Number(event.clientX) : 240,
|
|
1381
|
+
y: Number.isFinite(Number(event.clientY)) ? Number(event.clientY) : 240,
|
|
1382
|
+
})
|
|
1383
|
+
clearHoverDecoration()
|
|
1384
|
+
return
|
|
1385
|
+
}
|
|
1386
|
+
} catch {
|
|
1387
|
+
/* 解析失败继续走原生兜底,不跳错 */
|
|
1388
|
+
}
|
|
1335
1389
|
/* 其它语言:保留 Monaco 原生 action 兜底 */
|
|
1336
1390
|
try {
|
|
1337
1391
|
instance.focus()
|
|
@@ -1773,17 +1827,7 @@ const GUFA_LINT_ENABLED = true
|
|
|
1773
1827
|
}
|
|
1774
1828
|
}
|
|
1775
1829
|
reportCursor()
|
|
1776
|
-
|
|
1777
|
-
if (pending !== null && pending !== undefined && pending.path === targetTab.path) {
|
|
1778
|
-
pendingRevealRef.current = null
|
|
1779
|
-
try {
|
|
1780
|
-
editor.setPosition({ lineNumber: pending.line, column: pending.column })
|
|
1781
|
-
editor.revealLineInCenter(pending.line)
|
|
1782
|
-
editor.focus()
|
|
1783
|
-
} catch {
|
|
1784
|
-
/* ignore */
|
|
1785
|
-
}
|
|
1786
|
-
}
|
|
1830
|
+
applyPendingReveal(editor)
|
|
1787
1831
|
/* 打开 Python 文件时预热 pyright,避免第一次 Cmd+点击等语言服务启动 */
|
|
1788
1832
|
if (targetTab.language === 'python' && !GUFA_LSP_WARMED.has(root)) {
|
|
1789
1833
|
GUFA_LSP_WARMED.add(root)
|
|
@@ -1806,19 +1850,28 @@ const GUFA_LINT_ENABLED = true
|
|
|
1806
1850
|
} else if (targetTab !== null) {
|
|
1807
1851
|
modelPathRef.current = targetTab.path
|
|
1808
1852
|
}
|
|
1809
|
-
}, [status, tabs, activePath, root, reportCursor, disposeAllModels, disposeModelAt])
|
|
1853
|
+
}, [status, tabs, activePath, root, reportCursor, applyPendingReveal, disposeAllModels, disposeModelAt])
|
|
1854
|
+
|
|
1855
|
+
/* 编辑器挂载完成/模型已就绪时,把还没落地的定义跳转位置补上 */
|
|
1856
|
+
useEffect(() => {
|
|
1857
|
+
applyPendingReveal(editorRef.current)
|
|
1858
|
+
}, [status, tabs, activePath, applyPendingReveal])
|
|
1810
1859
|
|
|
1811
1860
|
/* 搜索面板点击结果 → 打开文件并跳到对应行列(由 App 通过 bus 发来) */
|
|
1812
1861
|
useEffect(() => {
|
|
1813
1862
|
if (G.bus === null || G.bus === undefined || typeof G.bus.on !== 'function') return undefined
|
|
1814
1863
|
return G.bus.on('editor:reveal', (detail) => {
|
|
1815
1864
|
const editor = editorRef.current
|
|
1816
|
-
if (editor === null || editor === undefined) return
|
|
1817
1865
|
const rawLine = Number(detail?.line)
|
|
1818
1866
|
const rawColumn = Number(detail?.column)
|
|
1819
1867
|
const line = Number.isFinite(rawLine) ? Math.max(1, Math.trunc(rawLine)) : 1
|
|
1820
1868
|
const column = Number.isFinite(rawColumn) ? Math.max(1, Math.trunc(rawColumn)) : 1
|
|
1821
1869
|
const path = typeof detail?.path === 'string' && detail.path !== '' ? detail.path : null
|
|
1870
|
+
if (editor === null || editor === undefined) {
|
|
1871
|
+
/* 编辑器还没挂载(首次打开文件):先记下位置,挂载/切模型后的 effect 会补上 */
|
|
1872
|
+
if (path !== null) pendingRevealRef.current = { path, line, column }
|
|
1873
|
+
return
|
|
1874
|
+
}
|
|
1822
1875
|
if (path !== null && modelPathRef.current !== path) {
|
|
1823
1876
|
pendingRevealRef.current = { path, line, column }
|
|
1824
1877
|
return
|