dsh-gufa-code 1.3.88 → 1.3.91
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 +3 -0
- package/client.js +645 -1
- package/desktop/float/bin/gufa-float-macos-arm64 +0 -0
- package/desktop/float/bin/gufa-float-macos-universal +0 -0
- package/desktop/float/bin/gufa-float-macos-x64 +0 -0
- package/desktop/float//345/217/244/346/263/225/347/274/226/347/250/213/346/202/254/346/265/256/345/261/202.app/Contents/Info.plist +2 -2
- package/desktop/float//345/217/244/346/263/225/347/274/226/347/250/213/346/202/254/346/265/256/345/261/202.app/Contents/MacOS/gufa-float +0 -0
- package/docs/KNOWN-PITFALLS.md +15 -0
- package/host/completion.js +130 -0
- package/host/fs.js +4 -0
- package/host/lsp.js +8 -0
- package/package.json +1 -1
- package/src/client/20-editor.js +98 -0
- package/src/client/58b-drag-ref.js +533 -0
- package/src/client/99-main.js +10 -0
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.
|
|
19
|
+
G.BUILD = '1.3.91'
|
|
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
|
|
|
@@ -2881,6 +2883,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
2881
2883
|
const viewStatesRef = useRef(new Map()) /* path -> editor view state(滚动/光标) */
|
|
2882
2884
|
const lastPropContentRef = useRef(new Map()) /* path -> 最近一次同步过的 props content */
|
|
2883
2885
|
const modelsRootRef = useRef(root) /* model 所属项目根:root 变了必须重建 */
|
|
2886
|
+
const tsPreloadRef = useRef(null) /* 后台预加载 import 依赖(JS/TS) */
|
|
2884
2887
|
const modelPathRef = useRef(null) /* 当前挂在 editor 上的 tab path */
|
|
2885
2888
|
const suppressChangeRef = useRef(false) /* 程序化 setValue 时抑制 onChange 回环 */
|
|
2886
2889
|
const propsRef = useRef(props)
|
|
@@ -3168,6 +3171,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
3168
3171
|
}
|
|
3169
3172
|
}
|
|
3170
3173
|
}
|
|
3174
|
+
tsPreloadRef.current = preloadTsDependencies
|
|
3171
3175
|
const queryDefinitions = (position, word, model, path, root) => {
|
|
3172
3176
|
const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
|
|
3173
3177
|
if (language === 'python') {
|
|
@@ -3485,6 +3489,83 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
3485
3489
|
} catch {
|
|
3486
3490
|
/* ignore */
|
|
3487
3491
|
}
|
|
3492
|
+
/* ── Copilot 风格 Tab 行内补全(复用 Harness 模型服务) ───────── */
|
|
3493
|
+
try {
|
|
3494
|
+
if (typeof monaco.languages.registerInlineCompletionsProvider === 'function') {
|
|
3495
|
+
const inlineLanguages = ['python', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'java', 'go', 'rust', 'csharp', 'cpp', 'c', 'shell', 'json', 'yaml', 'sql', 'html', 'css']
|
|
3496
|
+
const inlineCache = new Map()
|
|
3497
|
+
let inlineLastRequestAt = 0
|
|
3498
|
+
const inlineProvider = {
|
|
3499
|
+
provideInlineCompletions(model, position, context, token) {
|
|
3500
|
+
const path = modelPathRef.current
|
|
3501
|
+
const root = modelsRootRef.current
|
|
3502
|
+
if (path === null || path === undefined || root === null || root === undefined || root === '') {
|
|
3503
|
+
return Promise.resolve({ items: [] })
|
|
3504
|
+
}
|
|
3505
|
+
if (token !== null && token !== undefined && token.isCancellationRequested === true) return Promise.resolve({ items: [] })
|
|
3506
|
+
return new Promise((resolve) => {
|
|
3507
|
+
let settled = false
|
|
3508
|
+
const finish = (value) => {
|
|
3509
|
+
if (settled) return
|
|
3510
|
+
settled = true
|
|
3511
|
+
resolve(value)
|
|
3512
|
+
}
|
|
3513
|
+
const timer = window.setTimeout(() => {
|
|
3514
|
+
void (async () => {
|
|
3515
|
+
try {
|
|
3516
|
+
if (token !== null && token !== undefined && token.isCancellationRequested === true) return finish({ items: [] })
|
|
3517
|
+
const now = Date.now()
|
|
3518
|
+
if (now - inlineLastRequestAt < 500) return finish({ items: [] })
|
|
3519
|
+
inlineLastRequestAt = now
|
|
3520
|
+
const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
|
|
3521
|
+
if (!inlineLanguages.includes(language)) return finish({ items: [] })
|
|
3522
|
+
const lineCount = model.getLineCount()
|
|
3523
|
+
const startLine = Math.max(1, position.lineNumber - 140)
|
|
3524
|
+
const endLine = Math.min(lineCount, position.lineNumber + 30)
|
|
3525
|
+
const endColumn = model.getLineMaxColumn(endLine)
|
|
3526
|
+
let prefix = ''
|
|
3527
|
+
let suffix = ''
|
|
3528
|
+
try {
|
|
3529
|
+
prefix = model.getValueInRange(new monaco.Range(startLine, 1, position.lineNumber, position.column)).slice(-3200)
|
|
3530
|
+
suffix = model.getValueInRange(new monaco.Range(position.lineNumber, position.column, endLine, endColumn)).slice(0, 800)
|
|
3531
|
+
} catch {
|
|
3532
|
+
return finish({ items: [] })
|
|
3533
|
+
}
|
|
3534
|
+
const version = typeof model.getVersionId === 'function' ? model.getVersionId() : 0
|
|
3535
|
+
const cacheKey = `${path}|${version}|${position.lineNumber}:${position.column}|${language}`
|
|
3536
|
+
const cached = inlineCache.get(cacheKey)
|
|
3537
|
+
if (cached !== undefined) return finish(cached)
|
|
3538
|
+
const value = await G.api('completion.suggest', { root, path, language, prefix, suffix })
|
|
3539
|
+
if (token !== null && token !== undefined && token.isCancellationRequested === true) return finish({ items: [] })
|
|
3540
|
+
const text = value !== null && value !== undefined && typeof value.text === 'string' ? value.text : ''
|
|
3541
|
+
const result = text === ''
|
|
3542
|
+
? { items: [] }
|
|
3543
|
+
: { items: [{ insertText: text, range: new monaco.Range(position.lineNumber, position.column, position.lineNumber, position.column) }] }
|
|
3544
|
+
if (inlineCache.size > 30) inlineCache.clear()
|
|
3545
|
+
inlineCache.set(cacheKey, result)
|
|
3546
|
+
finish(result)
|
|
3547
|
+
} catch {
|
|
3548
|
+
finish({ items: [] })
|
|
3549
|
+
}
|
|
3550
|
+
})()
|
|
3551
|
+
}, 260)
|
|
3552
|
+
if (token !== null && token !== undefined && typeof token.onCancellationRequested === 'function') {
|
|
3553
|
+
token.onCancellationRequested(() => {
|
|
3554
|
+
window.clearTimeout(timer)
|
|
3555
|
+
finish({ items: [] })
|
|
3556
|
+
})
|
|
3557
|
+
}
|
|
3558
|
+
})
|
|
3559
|
+
},
|
|
3560
|
+
freeInlineCompletions() {},
|
|
3561
|
+
}
|
|
3562
|
+
const inlineDisposable = monaco.languages.registerInlineCompletionsProvider(inlineLanguages, inlineProvider)
|
|
3563
|
+
if (inlineDisposable !== null && inlineDisposable !== undefined && typeof inlineDisposable.dispose === 'function') subscriptions.push(inlineDisposable)
|
|
3564
|
+
}
|
|
3565
|
+
} catch {
|
|
3566
|
+
/* ignore */
|
|
3567
|
+
}
|
|
3568
|
+
|
|
3488
3569
|
G.__gufaEditorDebug = {
|
|
3489
3570
|
focus: () => {
|
|
3490
3571
|
try {
|
|
@@ -3718,6 +3799,23 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
3718
3799
|
/* ignore */
|
|
3719
3800
|
}
|
|
3720
3801
|
}
|
|
3802
|
+
/* 打开 Python 文件时预热 pyright,避免第一次 Cmd+点击等语言服务启动 */
|
|
3803
|
+
if (targetTab.language === 'python' && !GUFA_LSP_WARMED.has(root)) {
|
|
3804
|
+
GUFA_LSP_WARMED.add(root)
|
|
3805
|
+
void G.api('lsp.warm', { root }).catch(() => { GUFA_LSP_WARMED.delete(root) })
|
|
3806
|
+
}
|
|
3807
|
+
/* JS/TS:切到文件后后台预加载 import 依赖,点 Cmd+点击时 Monaco 已有跨文件 model,少等网络 */
|
|
3808
|
+
if (targetTab.language === 'typescript' || targetTab.language === 'javascript') {
|
|
3809
|
+
const modelForPreload = targetModel
|
|
3810
|
+
const pathForPreload = targetTab.path
|
|
3811
|
+
window.setTimeout(() => {
|
|
3812
|
+
try {
|
|
3813
|
+
if (tsPreloadRef.current !== null) void tsPreloadRef.current(modelForPreload, pathForPreload, root)
|
|
3814
|
+
} catch {
|
|
3815
|
+
/* ignore */
|
|
3816
|
+
}
|
|
3817
|
+
}, 200)
|
|
3818
|
+
}
|
|
3721
3819
|
/* 打开/切换文件不抢焦点:只有用户点击编辑区时才进入键盘编辑 */
|
|
3722
3820
|
}
|
|
3723
3821
|
} else if (targetTab !== null) {
|
|
@@ -9477,6 +9575,542 @@ G.installChatFilePaste = function installChatFilePaste(ctx) {
|
|
|
9477
9575
|
}
|
|
9478
9576
|
|
|
9479
9577
|
|
|
9578
|
+
/* ─────────────────────── 58b-drag-ref.js ─────────────────────── */
|
|
9579
|
+
/**
|
|
9580
|
+
* 58b-drag-ref:把 dsh-drag-ref 的「拖动文件到输入框 → @引用」「复制代码选区 →
|
|
9581
|
+
* 代码引用」「粘贴引用 chip」「点击 chip 跳回代码」整合进古法编程客户端。
|
|
9582
|
+
*/
|
|
9583
|
+
G.installGufaRefBridge = function installGufaRefBridge(ctx) {
|
|
9584
|
+
if (typeof document === "undefined") return () => {}
|
|
9585
|
+
const DRAG_TYPE = "application/x-dsh-explorer-path";
|
|
9586
|
+
const CODE_REF_TYPE = "application/x-dsh-code-ref";
|
|
9587
|
+
const ROW = "[data-edrv-path]";
|
|
9588
|
+
const COMPOSER = "[data-conversation-region=\"composer\"]";
|
|
9589
|
+
const CHAT = "[data-conversation-content]";
|
|
9590
|
+
|
|
9591
|
+
function toast(text, kind) {
|
|
9592
|
+
/* 只保留错误提示;复制/插入成功不再打扰用户 */
|
|
9593
|
+
if (kind !== "err") return;
|
|
9594
|
+
try { G.refToast(String(text), "err") } catch (_) {}
|
|
9595
|
+
}
|
|
9596
|
+
|
|
9597
|
+
function activeSessionId() {
|
|
9598
|
+
const el = document.querySelector("[data-conversation-session]");
|
|
9599
|
+
const id = el === null ? null : el.getAttribute("data-conversation-session");
|
|
9600
|
+
return typeof id === "string" && id !== "" ? id : void 0;
|
|
9601
|
+
}
|
|
9602
|
+
|
|
9603
|
+
function mentionOf(path) {
|
|
9604
|
+
const rel = String(path).replace(/\\/g, "/");
|
|
9605
|
+
if (/[\u0000-\u001f\u007f-\u009f"]/u.test(rel)) return "@" + path;
|
|
9606
|
+
return /\s/u.test(rel) ? `@"${rel}"` : "@" + rel;
|
|
9607
|
+
}
|
|
9608
|
+
|
|
9609
|
+
function labelOf(path) {
|
|
9610
|
+
const parts = String(path).split(/[\\/]/);
|
|
9611
|
+
return parts[parts.length - 1] || String(path);
|
|
9612
|
+
}
|
|
9613
|
+
|
|
9614
|
+
function rangeText(startLine, endLine) {
|
|
9615
|
+
if (!Number.isFinite(startLine) || !Number.isFinite(endLine)) return "";
|
|
9616
|
+
const a = Math.min(startLine, endLine);
|
|
9617
|
+
const b = Math.max(startLine, endLine);
|
|
9618
|
+
return a === b ? `${a} 行` : `${a}-${b} 行`;
|
|
9619
|
+
}
|
|
9620
|
+
|
|
9621
|
+
function detectEnd(snapshot) {
|
|
9622
|
+
const total = String(snapshot && snapshot.draft !== void 0 ? snapshot.draft : "").length;
|
|
9623
|
+
const occurrences = snapshot && snapshot.occurrences;
|
|
9624
|
+
if (!Array.isArray(occurrences) || occurrences.length === 0) return total;
|
|
9625
|
+
let extra = 0;
|
|
9626
|
+
for (const item of occurrences) {
|
|
9627
|
+
const len = Number(item && item.length);
|
|
9628
|
+
if (Number.isFinite(len) && len > 1) extra += len - 1;
|
|
9629
|
+
}
|
|
9630
|
+
return Math.max(0, total - extra);
|
|
9631
|
+
}
|
|
9632
|
+
|
|
9633
|
+
function caretOffset(input, fallback) {
|
|
9634
|
+
try {
|
|
9635
|
+
if (typeof input.caretSpan !== "function") return fallback;
|
|
9636
|
+
const span = input.caretSpan();
|
|
9637
|
+
const end = span ? Number(span.end) : NaN;
|
|
9638
|
+
if (!Number.isFinite(end)) return fallback;
|
|
9639
|
+
return Math.min(Math.max(0, end), fallback);
|
|
9640
|
+
} catch (_) {
|
|
9641
|
+
return fallback;
|
|
9642
|
+
}
|
|
9643
|
+
}
|
|
9644
|
+
|
|
9645
|
+
function typeIntoComposer(text) {
|
|
9646
|
+
const editor = document.querySelector(COMPOSER + " [contenteditable=\"true\"]") || document.querySelector(COMPOSER);
|
|
9647
|
+
if (editor === null) return false;
|
|
9648
|
+
try {
|
|
9649
|
+
editor.focus();
|
|
9650
|
+
if (document.execCommand("insertText", false, text)) return true;
|
|
9651
|
+
} catch (_) {}
|
|
9652
|
+
try {
|
|
9653
|
+
const data = new DataTransfer();
|
|
9654
|
+
data.setData("text/plain", text);
|
|
9655
|
+
editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true }));
|
|
9656
|
+
return true;
|
|
9657
|
+
} catch (_) {}
|
|
9658
|
+
return false;
|
|
9659
|
+
}
|
|
9660
|
+
|
|
9661
|
+
/** Insert a file/folder reference (optionally with a line range). */
|
|
9662
|
+
function insertRef(ctx, info) {
|
|
9663
|
+
const isDir = info.isDir === true;
|
|
9664
|
+
const mention = mentionOf(info.path);
|
|
9665
|
+
const hasRange = !isDir && Number.isFinite(info.startLine) && Number.isFinite(info.endLine);
|
|
9666
|
+
const ref = hasRange
|
|
9667
|
+
? mention + (info.startLine === info.endLine ? ` L${info.startLine}` : ` L${Math.min(info.startLine, info.endLine)}-${Math.max(info.startLine, info.endLine)}`)
|
|
9668
|
+
: mention;
|
|
9669
|
+
const label = hasRange
|
|
9670
|
+
? labelOf(info.path) + " " + rangeText(info.startLine, info.endLine)
|
|
9671
|
+
: labelOf(info.path);
|
|
9672
|
+
const reference = {
|
|
9673
|
+
source: "reference",
|
|
9674
|
+
ref,
|
|
9675
|
+
label,
|
|
9676
|
+
appearance: isDir ? "folder" : "file",
|
|
9677
|
+
clipboardText: mention
|
|
9678
|
+
};
|
|
9679
|
+
let input = null;
|
|
9680
|
+
let reason = "";
|
|
9681
|
+
try {
|
|
9682
|
+
const sessions = ctx.get("sessions");
|
|
9683
|
+
const conversation = ctx.get("conversation");
|
|
9684
|
+
const sessionId = activeSessionId();
|
|
9685
|
+
if (sessionId === void 0) reason = "找不到当前会话 id";
|
|
9686
|
+
else if (sessions === void 0) reason = "sessions 服务不可用";
|
|
9687
|
+
else if (conversation === void 0) reason = "conversation 服务不可用";
|
|
9688
|
+
else {
|
|
9689
|
+
const actx = typeof sessions.scope === "function" ? sessions.scope(sessionId) : void 0;
|
|
9690
|
+
if (actx === void 0) reason = "sessions.scope(" + sessionId.slice(0, 12) + "…) 为空";
|
|
9691
|
+
else {
|
|
9692
|
+
input = conversation.input.for(actx);
|
|
9693
|
+
if (input === void 0) reason = "conversation.input.for(...) 为空";
|
|
9694
|
+
}
|
|
9695
|
+
}
|
|
9696
|
+
} catch (error) {
|
|
9697
|
+
reason = "门面解析异常:" + String(error && error.message || error);
|
|
9698
|
+
}
|
|
9699
|
+
if (input !== null && typeof input.state?.getSnapshot === "function") {
|
|
9700
|
+
try {
|
|
9701
|
+
const snapshot = input.state.getSnapshot();
|
|
9702
|
+
const offset = caretOffset(input, detectEnd(snapshot));
|
|
9703
|
+
const span = { start: offset, end: offset, draftRev: snapshot && snapshot.draftRev };
|
|
9704
|
+
if (typeof input.insertReference === "function" && input.insertReference(reference, span) === true) return { ok: true, via: "insertReference" };
|
|
9705
|
+
if (typeof input.insertText === "function" && input.insertText(mention + " ", span) === true) return { ok: true, via: "insertText" };
|
|
9706
|
+
reason = reason || "insertReference/insertText 都返回 false";
|
|
9707
|
+
} catch (error) {
|
|
9708
|
+
reason = "插入异常:" + String(error && error.message || error);
|
|
9709
|
+
}
|
|
9710
|
+
}
|
|
9711
|
+
if (info.text && typeIntoComposer(info.text)) return { ok: true, via: "composer-text" };
|
|
9712
|
+
if (typeIntoComposer(mention + " ")) return { ok: true, via: "composer" };
|
|
9713
|
+
return { ok: false, reason: reason || "输入框不可用" };
|
|
9714
|
+
}
|
|
9715
|
+
|
|
9716
|
+
function isOurs(transfer) {
|
|
9717
|
+
if (transfer === null || transfer === void 0) return false;
|
|
9718
|
+
try {
|
|
9719
|
+
return Array.prototype.indexOf.call(transfer.types, DRAG_TYPE) !== -1;
|
|
9720
|
+
} catch (_) {
|
|
9721
|
+
return false;
|
|
9722
|
+
}
|
|
9723
|
+
}
|
|
9724
|
+
|
|
9725
|
+
function payloadOf(transfer) {
|
|
9726
|
+
if (transfer === null || transfer === void 0) return null;
|
|
9727
|
+
try {
|
|
9728
|
+
const raw = transfer.getData(DRAG_TYPE);
|
|
9729
|
+
if (raw !== "") {
|
|
9730
|
+
const parsed = JSON.parse(raw);
|
|
9731
|
+
if (parsed && typeof parsed.path === "string" && parsed.path !== "") return parsed;
|
|
9732
|
+
}
|
|
9733
|
+
} catch (_) {}
|
|
9734
|
+
try {
|
|
9735
|
+
const text = transfer.getData("text/plain");
|
|
9736
|
+
if (text !== "") return { path: text, isDir: false };
|
|
9737
|
+
} catch (_) {}
|
|
9738
|
+
return null;
|
|
9739
|
+
}
|
|
9740
|
+
|
|
9741
|
+
function hasCodeRef(clipboard) {
|
|
9742
|
+
if (clipboard === null || clipboard === void 0) return false;
|
|
9743
|
+
try {
|
|
9744
|
+
return Array.prototype.indexOf.call(clipboard.types, CODE_REF_TYPE) !== -1;
|
|
9745
|
+
} catch (_) {
|
|
9746
|
+
return false;
|
|
9747
|
+
}
|
|
9748
|
+
}
|
|
9749
|
+
|
|
9750
|
+
function codeRefOf(clipboard) {
|
|
9751
|
+
try {
|
|
9752
|
+
const raw = clipboard.getData(CODE_REF_TYPE);
|
|
9753
|
+
if (raw === "") return null;
|
|
9754
|
+
const parsed = JSON.parse(raw);
|
|
9755
|
+
if (!parsed || typeof parsed.path !== "string" || parsed.path === "") return null;
|
|
9756
|
+
return parsed;
|
|
9757
|
+
} catch (_) {
|
|
9758
|
+
return null;
|
|
9759
|
+
}
|
|
9760
|
+
}
|
|
9761
|
+
|
|
9762
|
+
function dropZoneOf(node) {
|
|
9763
|
+
if (!(node instanceof Element)) return null;
|
|
9764
|
+
return node.closest(COMPOSER) || node.closest(CHAT);
|
|
9765
|
+
}
|
|
9766
|
+
|
|
9767
|
+
/** The focused Monaco editor (copy source). */
|
|
9768
|
+
function focusedEditor() {
|
|
9769
|
+
const monaco = window.monaco;
|
|
9770
|
+
if (!monaco || !monaco.editor || typeof monaco.editor.getEditors !== "function") return null;
|
|
9771
|
+
let editors = [];
|
|
9772
|
+
try {
|
|
9773
|
+
editors = monaco.editor.getEditors() || [];
|
|
9774
|
+
} catch (_) {
|
|
9775
|
+
return null;
|
|
9776
|
+
}
|
|
9777
|
+
for (const editor of editors) {
|
|
9778
|
+
try {
|
|
9779
|
+
if (editor && typeof editor.hasTextFocus === "function" && editor.hasTextFocus()) return editor;
|
|
9780
|
+
} catch (_) {}
|
|
9781
|
+
}
|
|
9782
|
+
return editors.length === 1 ? editors[0] : null;
|
|
9783
|
+
}
|
|
9784
|
+
|
|
9785
|
+
/** File path + selected line range + selected text from one Monaco editor.
|
|
9786
|
+
* `fullLines` 只在选区完整覆盖整行时为真;不足一行或首尾带半行的选区
|
|
9787
|
+
* 按普通文本复制,不生成代码引用。 */
|
|
9788
|
+
function editorSelectionOf(editor) {
|
|
9789
|
+
try {
|
|
9790
|
+
const model = editor.getModel();
|
|
9791
|
+
const selection = editor.getSelection();
|
|
9792
|
+
if (!model || !selection) return null;
|
|
9793
|
+
const text = model.getValueInRange(selection);
|
|
9794
|
+
if (!text) return null;
|
|
9795
|
+
const rawPath = model.uri && model.uri.path ? String(model.uri.path) : "";
|
|
9796
|
+
const path = decodeURIComponent(rawPath.replace(/^\//, ""));
|
|
9797
|
+
if (path === "") return null;
|
|
9798
|
+
const startLine = Math.min(selection.startLineNumber, selection.endLineNumber);
|
|
9799
|
+
const endLine = Math.max(selection.startLineNumber, selection.endLineNumber);
|
|
9800
|
+
const startColumn = Math.min(selection.startColumn, selection.endColumn);
|
|
9801
|
+
const endColumn = Math.max(selection.startColumn, selection.endColumn);
|
|
9802
|
+
/* 单行:从行首选到行尾才算引用;多行:任意列都算引用。 */
|
|
9803
|
+
const fullLines = startLine !== endLine
|
|
9804
|
+
? true
|
|
9805
|
+
: startColumn === 1 && endColumn === model.getLineMaxColumn(endLine);
|
|
9806
|
+
return { path, startLine, endLine, fullLines, text };
|
|
9807
|
+
} catch (_) {
|
|
9808
|
+
return null;
|
|
9809
|
+
}
|
|
9810
|
+
}
|
|
9811
|
+
|
|
9812
|
+
function install(ctx) {
|
|
9813
|
+
loadRefs();
|
|
9814
|
+
const onDragStart = (event) => {
|
|
9815
|
+
const target = event.target;
|
|
9816
|
+
const row = target instanceof Element ? target.closest(ROW) : null;
|
|
9817
|
+
if (row === null || event.dataTransfer === null) return;
|
|
9818
|
+
const path = row.getAttribute("data-edrv-path");
|
|
9819
|
+
if (path === null || path === "") return;
|
|
9820
|
+
const chevron = row.querySelector(".edrv-tree-chev");
|
|
9821
|
+
const isDir = chevron !== null && (chevron.textContent || "").trim() !== "";
|
|
9822
|
+
event.dataTransfer.effectAllowed = "copy";
|
|
9823
|
+
event.dataTransfer.setData(DRAG_TYPE, JSON.stringify({ path, isDir }));
|
|
9824
|
+
event.dataTransfer.setData("text/plain", path);
|
|
9825
|
+
};
|
|
9826
|
+
const onDragOver = (event) => {
|
|
9827
|
+
if (!isOurs(event.dataTransfer) || dropZoneOf(event.target) === null) return;
|
|
9828
|
+
event.preventDefault();
|
|
9829
|
+
if (event.dataTransfer !== null) event.dataTransfer.dropEffect = "copy";
|
|
9830
|
+
};
|
|
9831
|
+
const onDrop = (event) => {
|
|
9832
|
+
if (!isOurs(event.dataTransfer)) return;
|
|
9833
|
+
if (dropZoneOf(event.target) === null) {
|
|
9834
|
+
toast("松手位置不在输入框内,未插入", "err");
|
|
9835
|
+
return;
|
|
9836
|
+
}
|
|
9837
|
+
event.preventDefault();
|
|
9838
|
+
event.stopPropagation();
|
|
9839
|
+
const payload = payloadOf(event.dataTransfer);
|
|
9840
|
+
if (payload === null) {
|
|
9841
|
+
return;
|
|
9842
|
+
}
|
|
9843
|
+
const result = insertRef(ctx, { path: payload.path, isDir: payload.isDir === true });
|
|
9844
|
+
if (result.ok) {
|
|
9845
|
+
insertedRefs.set(labelOf(payload.path), { path: payload.path, startLine: void 0, endLine: void 0 });
|
|
9846
|
+
saveRefs();
|
|
9847
|
+
}
|
|
9848
|
+
if (result.ok) toast("已插入引用:" + labelOf(payload.path), "ok");
|
|
9849
|
+
else toast("插入失败:" + result.reason, "err");
|
|
9850
|
+
};
|
|
9851
|
+
|
|
9852
|
+
/** Copying a selection inside Monaco records the path + line range. */
|
|
9853
|
+
const onCopy = (event) => {
|
|
9854
|
+
const target = event.target;
|
|
9855
|
+
if (!(target instanceof Element) || target.closest(".monaco-editor") === null) return;
|
|
9856
|
+
const editor = focusedEditor();
|
|
9857
|
+
if (editor === null) return;
|
|
9858
|
+
const info = editorSelectionOf(editor);
|
|
9859
|
+
if (info === null) return;
|
|
9860
|
+
/* 引用判定:跨行选区一律算引用(哪怕首行/末行只选中半行);
|
|
9861
|
+
单行必须完整整行选中,否则按普通文本复制。 */
|
|
9862
|
+
const multiLine = info.startLine !== info.endLine;
|
|
9863
|
+
if (!multiLine && info.fullLines !== true) return;
|
|
9864
|
+
try {
|
|
9865
|
+
event.preventDefault();
|
|
9866
|
+
event.stopPropagation();
|
|
9867
|
+
if (event.clipboardData !== null) {
|
|
9868
|
+
event.clipboardData.setData("text/plain", info.text);
|
|
9869
|
+
event.clipboardData.setData(CODE_REF_TYPE, JSON.stringify(info));
|
|
9870
|
+
}
|
|
9871
|
+
} catch (_) {}
|
|
9872
|
+
};
|
|
9873
|
+
|
|
9874
|
+
const onPaste = (event) => {
|
|
9875
|
+
if (!hasCodeRef(event.clipboardData)) return;
|
|
9876
|
+
if (dropZoneOf(event.target) === null) return;
|
|
9877
|
+
const info = codeRefOf(event.clipboardData);
|
|
9878
|
+
if (info === null) return;
|
|
9879
|
+
event.preventDefault();
|
|
9880
|
+
event.stopPropagation();
|
|
9881
|
+
const result = insertRef(ctx, { path: info.path, startLine: info.startLine, endLine: info.endLine, text: info.text });
|
|
9882
|
+
if (result.ok) {
|
|
9883
|
+
insertedRefs.set(labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), { path: info.path, startLine: info.startLine, endLine: info.endLine });
|
|
9884
|
+
saveRefs();
|
|
9885
|
+
}
|
|
9886
|
+
if (result.ok) toast("已插入代码引用:" + labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), "ok");
|
|
9887
|
+
else toast("插入失败:" + result.reason, "err");
|
|
9888
|
+
};
|
|
9889
|
+
|
|
9890
|
+
/**
|
|
9891
|
+
* Clicking a reference chip re-opens the target and selects its line range.
|
|
9892
|
+
* Only real chips are considered: a title chip, a Lexical decorator inside
|
|
9893
|
+
* the composer, or an inline leaf whose text matches a known chip label.
|
|
9894
|
+
* Message containers and the editable root never match.
|
|
9895
|
+
*/
|
|
9896
|
+
const onReferenceClick = (event) => {
|
|
9897
|
+
const target = event.target;
|
|
9898
|
+
if (!(target instanceof Element)) return;
|
|
9899
|
+
let info = null;
|
|
9900
|
+
|
|
9901
|
+
/* 1) chip carrying a title (`@path L12-34` or `@path`). */
|
|
9902
|
+
const titleChip = target.closest("[title^=\"@\"]");
|
|
9903
|
+
if (titleChip !== null) {
|
|
9904
|
+
const chipText = (titleChip.textContent || "").trim();
|
|
9905
|
+
if (chipText.length > 80) return;
|
|
9906
|
+
const title = (titleChip.getAttribute("title") || "").trim();
|
|
9907
|
+
if (/^@(?:"[^"\n]+"|\S+)(?:\s+L\d+(?:-\d+)?)?$/u.test(title)) info = parseRefText(title);
|
|
9908
|
+
if (info === null && chipText !== "") info = insertedRefs.get(chipText) || null;
|
|
9909
|
+
if (info !== null && (info.startLine === void 0 || info.endLine === void 0)) {
|
|
9910
|
+
const sibling = titleChip.nextSibling;
|
|
9911
|
+
const tail = sibling !== null && sibling.nodeType === 3 ? sibling.textContent : "";
|
|
9912
|
+
const range = /^\s*L(\d+)(?:-(\d+))?/u.exec(String(tail || ""));
|
|
9913
|
+
if (range) {
|
|
9914
|
+
info.startLine = Number(range[1]);
|
|
9915
|
+
info.endLine = range[2] === void 0 ? info.startLine : Number(range[2]);
|
|
9916
|
+
}
|
|
9917
|
+
}
|
|
9918
|
+
} else {
|
|
9919
|
+
/* 2) Lexical decorator chip inside the composer (no title attribute). */
|
|
9920
|
+
const decorator = target.closest("[data-lexical-decorator=\"true\"], [contenteditable=\"false\"]");
|
|
9921
|
+
if (decorator !== null && decorator.closest("[data-conversation-region=\"composer\"]") !== null) {
|
|
9922
|
+
const label = (decorator.textContent || "").trim();
|
|
9923
|
+
if (label === "" || label.length > 80) return;
|
|
9924
|
+
info = insertedRefs.get(label) || null;
|
|
9925
|
+
} else {
|
|
9926
|
+
/* 3) inline leaf fallback (never the editable root or a block container). */
|
|
9927
|
+
if (target.getAttribute && target.getAttribute("contenteditable") === "true") return;
|
|
9928
|
+
try {
|
|
9929
|
+
const display = getComputedStyle(target).display;
|
|
9930
|
+
if (display !== "inline" && display !== "inline-flex" && display !== "inline-block") return;
|
|
9931
|
+
} catch (_) { return; }
|
|
9932
|
+
if (target.childElementCount > 0) return;
|
|
9933
|
+
const text = (target.textContent || "").trim();
|
|
9934
|
+
if (text === "" || text.length > 80) return;
|
|
9935
|
+
info = insertedRefs.get(text) || null;
|
|
9936
|
+
}
|
|
9937
|
+
}
|
|
9938
|
+
|
|
9939
|
+
if (info === null || info.path === void 0 || info.path === "") return;
|
|
9940
|
+
if (info.startLine === void 0 || info.endLine === void 0) return;
|
|
9941
|
+
event.preventDefault();
|
|
9942
|
+
event.stopPropagation();
|
|
9943
|
+
openAtRange(ctx, info.path, info.startLine, info.endLine);
|
|
9944
|
+
};
|
|
9945
|
+
|
|
9946
|
+
const mark = (root) => {
|
|
9947
|
+
const rows = root && typeof root.querySelectorAll === "function" ? root.querySelectorAll(ROW) : [];
|
|
9948
|
+
for (const row of rows) {
|
|
9949
|
+
if (row.getAttribute("draggable") !== "true") row.setAttribute("draggable", "true");
|
|
9950
|
+
}
|
|
9951
|
+
};
|
|
9952
|
+
|
|
9953
|
+
document.addEventListener("dragstart", onDragStart, true);
|
|
9954
|
+
document.addEventListener("dragover", onDragOver, true);
|
|
9955
|
+
document.addEventListener("drop", onDrop, true);
|
|
9956
|
+
document.addEventListener("click", onReferenceClick, true);
|
|
9957
|
+
document.addEventListener("copy", onCopy, true);
|
|
9958
|
+
document.addEventListener("paste", onPaste, true);
|
|
9959
|
+
mark(document);
|
|
9960
|
+
const observer = new MutationObserver((mutations) => {
|
|
9961
|
+
for (const mutation of mutations) {
|
|
9962
|
+
for (const node of mutation.addedNodes) {
|
|
9963
|
+
if (!(node instanceof Element)) continue;
|
|
9964
|
+
if (node.matches(ROW)) node.setAttribute("draggable", "true");
|
|
9965
|
+
mark(node);
|
|
9966
|
+
}
|
|
9967
|
+
}
|
|
9968
|
+
});
|
|
9969
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
9970
|
+
|
|
9971
|
+
return () => {
|
|
9972
|
+
document.removeEventListener("dragstart", onDragStart, true);
|
|
9973
|
+
document.removeEventListener("dragover", onDragOver, true);
|
|
9974
|
+
document.removeEventListener("drop", onDrop, true);
|
|
9975
|
+
document.removeEventListener("click", onReferenceClick, true);
|
|
9976
|
+
document.removeEventListener("copy", onCopy, true);
|
|
9977
|
+
document.removeEventListener("paste", onPaste, true);
|
|
9978
|
+
observer.disconnect();
|
|
9979
|
+
};
|
|
9980
|
+
}
|
|
9981
|
+
|
|
9982
|
+
/** Inserted references by chip label, for chips that carry no title attribute. */
|
|
9983
|
+
const insertedRefs = new Map();
|
|
9984
|
+
const REF_STORE_KEY = "gufa-code.refs.v1";
|
|
9985
|
+
|
|
9986
|
+
/** Restore label → {path,startLine,endLine} from localStorage (drafts survive restarts). */
|
|
9987
|
+
function loadRefs() {
|
|
9988
|
+
try {
|
|
9989
|
+
const raw = localStorage.getItem(REF_STORE_KEY);
|
|
9990
|
+
if (raw === null) return;
|
|
9991
|
+
const parsed = JSON.parse(raw);
|
|
9992
|
+
if (parsed === null || typeof parsed !== "object") return;
|
|
9993
|
+
for (const label of Object.keys(parsed)) {
|
|
9994
|
+
const info = parsed[label];
|
|
9995
|
+
if (info && typeof info.path === "string" && info.path !== "") insertedRefs.set(label, info);
|
|
9996
|
+
}
|
|
9997
|
+
} catch (_) {}
|
|
9998
|
+
}
|
|
9999
|
+
|
|
10000
|
+
/** Persist the label map so chips restored from a draft still resolve after a restart. */
|
|
10001
|
+
function saveRefs() {
|
|
10002
|
+
try {
|
|
10003
|
+
const out = {};
|
|
10004
|
+
let count = 0;
|
|
10005
|
+
for (const [label, info] of insertedRefs) {
|
|
10006
|
+
out[label] = info;
|
|
10007
|
+
if (++count >= 300) break;
|
|
10008
|
+
}
|
|
10009
|
+
localStorage.setItem(REF_STORE_KEY, JSON.stringify(out));
|
|
10010
|
+
} catch (_) {}
|
|
10011
|
+
}
|
|
10012
|
+
|
|
10013
|
+
/** Parse `@path L12-34` / `@"a b" L12` into { path, startLine, endLine }. */
|
|
10014
|
+
function parseRefText(text) {
|
|
10015
|
+
const raw = String(text == null ? "" : text).trim();
|
|
10016
|
+
if (!raw.startsWith("@")) return null;
|
|
10017
|
+
let body = raw.slice(1);
|
|
10018
|
+
let path = "";
|
|
10019
|
+
if (body.startsWith('"')) {
|
|
10020
|
+
const end = body.indexOf('"', 1);
|
|
10021
|
+
if (end < 0) return null;
|
|
10022
|
+
path = body.slice(1, end);
|
|
10023
|
+
body = body.slice(end + 1);
|
|
10024
|
+
} else {
|
|
10025
|
+
const space = body.indexOf(" ");
|
|
10026
|
+
path = space < 0 ? body : body.slice(0, space);
|
|
10027
|
+
body = space < 0 ? "" : body.slice(space);
|
|
10028
|
+
}
|
|
10029
|
+
if (path === "") return null;
|
|
10030
|
+
const range = /^\s*L(\d+)(?:-(\d+))?\s*$/u.exec(body);
|
|
10031
|
+
if (!range) return { path, startLine: void 0, endLine: void 0 };
|
|
10032
|
+
const start = Number(range[1]);
|
|
10033
|
+
const end = range[2] === void 0 ? start : Number(range[2]);
|
|
10034
|
+
return { path, startLine: start, endLine: end };
|
|
10035
|
+
}
|
|
10036
|
+
|
|
10037
|
+
/** Find the Monaco model for one workspace-relative path. */
|
|
10038
|
+
function modelFor(path) {
|
|
10039
|
+
const monaco = window.monaco;
|
|
10040
|
+
if (!monaco || !monaco.editor || typeof monaco.editor.getModels !== "function") return null;
|
|
10041
|
+
let models = [];
|
|
10042
|
+
try { models = monaco.editor.getModels() || []; } catch (_) { return null; }
|
|
10043
|
+
const wanted = String(path).replace(/\\/g, "/").replace(/^\.\//, "");
|
|
10044
|
+
for (const model of models) {
|
|
10045
|
+
const raw = model && model.uri && model.uri.path ? String(model.uri.path) : "";
|
|
10046
|
+
const candidate = decodeURIComponent(raw.replace(/^\//, ""));
|
|
10047
|
+
if (candidate === wanted || candidate.endsWith("/" + wanted) || wanted.endsWith("/" + candidate)) return model;
|
|
10048
|
+
}
|
|
10049
|
+
return null;
|
|
10050
|
+
}
|
|
10051
|
+
|
|
10052
|
+
/** Select the line range once the editor for that file is mounted. */
|
|
10053
|
+
function selectRange(path, startLine, endLine) {
|
|
10054
|
+
const start = Number(startLine);
|
|
10055
|
+
const end = Number(endLine) || start;
|
|
10056
|
+
if (!Number.isFinite(start) || start <= 0) return;
|
|
10057
|
+
const deadline = Date.now() + 4000;
|
|
10058
|
+
const tick = () => {
|
|
10059
|
+
const monaco = window.monaco;
|
|
10060
|
+
const model = modelFor(path);
|
|
10061
|
+
if (monaco && model) {
|
|
10062
|
+
let editors = [];
|
|
10063
|
+
try { editors = monaco.editor.getEditors() || []; } catch (_) {}
|
|
10064
|
+
const editor = editors.find((item) => item.getModel && item.getModel() === model);
|
|
10065
|
+
if (editor) {
|
|
10066
|
+
try {
|
|
10067
|
+
const range = new monaco.Range(start, 1, end, 1);
|
|
10068
|
+
if (typeof editor.setSelection === "function") editor.setSelection(range);
|
|
10069
|
+
if (typeof editor.revealRangeInCenter === "function") editor.revealRangeInCenter(range);
|
|
10070
|
+
else if (typeof editor.revealLineInCenter === "function") editor.revealLineInCenter(start);
|
|
10071
|
+
if (typeof editor.focus === "function") editor.focus();
|
|
10072
|
+
} catch (_) {}
|
|
10073
|
+
return;
|
|
10074
|
+
}
|
|
10075
|
+
}
|
|
10076
|
+
if (Date.now() < deadline) setTimeout(tick, 120);
|
|
10077
|
+
};
|
|
10078
|
+
tick();
|
|
10079
|
+
}
|
|
10080
|
+
|
|
10081
|
+
/** Open a file through the official resource route (falls back to workspaces.openPath). */
|
|
10082
|
+
function openAtRange(ctx, path, startLine, endLine) {
|
|
10083
|
+
try {
|
|
10084
|
+
if (G.bus !== null && G.bus !== undefined && typeof G.bus.emit === "function") {
|
|
10085
|
+
G.bus.emit("editor:open-at", { path, line: Number(startLine) || 1, column: 1 });
|
|
10086
|
+
selectRange(path, startLine, endLine);
|
|
10087
|
+
return;
|
|
10088
|
+
}
|
|
10089
|
+
} catch (_) {}
|
|
10090
|
+
try {
|
|
10091
|
+
const sessionId = activeSessionId();
|
|
10092
|
+
const sidebarRight = ctx.get("sidebarRight");
|
|
10093
|
+
if (sessionId !== void 0 && sidebarRight !== void 0 && typeof sidebarRight.openResource === "function") {
|
|
10094
|
+
const address = "dsh-resource://file/session/" + encodeURIComponent(sessionId) + "/" + String(path).replace(/\\/g, "/").split("/").map(encodeURIComponent).join("/");
|
|
10095
|
+
sidebarRight.openResource(address, { params: { line: Number(startLine) || 1 } });
|
|
10096
|
+
} else {
|
|
10097
|
+
const workspaces = ctx.get("workspaces");
|
|
10098
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
10099
|
+
}
|
|
10100
|
+
} catch (_) {
|
|
10101
|
+
try {
|
|
10102
|
+
const workspaces = ctx.get("workspaces");
|
|
10103
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
10104
|
+
} catch (_) {}
|
|
10105
|
+
}
|
|
10106
|
+
selectRange(path, startLine, endLine);
|
|
10107
|
+
}
|
|
10108
|
+
|
|
10109
|
+
|
|
10110
|
+
return install(ctx)
|
|
10111
|
+
}
|
|
10112
|
+
|
|
10113
|
+
|
|
9480
10114
|
/* ─────────────────────── 59-dialog-layer.js ─────────────────────── */
|
|
9481
10115
|
/**
|
|
9482
10116
|
* 59-dialog-layer:外部门户弹窗(DSH 官方设置等)打开时,把古法编程的会话停靠层降到弹窗之下。
|
|
@@ -14700,6 +15334,16 @@ const GUFA_PLUGIN = {
|
|
|
14700
15334
|
console.error('[gufa-code] 安装聊天框文件粘贴失败', error)
|
|
14701
15335
|
}
|
|
14702
15336
|
|
|
15337
|
+
// 文件拖拽 → @引用 / 代码选区复制 → 代码引用 / 引用 chip 点击跳回(原 dsh-drag-ref 能力)
|
|
15338
|
+
try {
|
|
15339
|
+
if (typeof G.installGufaRefBridge === 'function') {
|
|
15340
|
+
const disposeRefBridge = G.installGufaRefBridge(ctx)
|
|
15341
|
+
if (typeof disposeRefBridge === 'function') ctx.effect(() => disposeRefBridge, 'gufa-code: explorer + code reference bridge')
|
|
15342
|
+
}
|
|
15343
|
+
} catch (error) {
|
|
15344
|
+
console.error('[gufa-code] 安装引用桥失败', error)
|
|
15345
|
+
}
|
|
15346
|
+
|
|
14703
15347
|
// 样式(随插件 context 卸载自动移除)
|
|
14704
15348
|
ctx.effect(() => G.injectStyles(), 'gufa-code: styles')
|
|
14705
15349
|
|