dsh-gufa-code 1.3.90 → 1.3.92
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 +2 -0
- package/client.js +570 -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 +9 -0
- package/package.json +1 -1
- package/src/client/20-editor.js +14 -0
- package/src/client/58b-drag-ref.js +542 -0
- package/src/client/99-main.js +10 -0
package/README.md
CHANGED
|
@@ -44,8 +44,10 @@ dsh plugin add dsh-gufa-code
|
|
|
44
44
|
- 保存走磁盘版本冲突检测 + 原子写;未保存标记;
|
|
45
45
|
- **⌘/Ctrl + 点击跳转定义 / 方法实现**:Python 走 **Pyright 语言服务(LSP)**,JS/TS 走 Monaco 内置 TypeScript 语言服务(点击前会按需把当前文件的 import 依赖加载进 Monaco,跨文件变量/函数由 TS 语言服务解析);其他语言暂用项目级定义搜索兜底;优先走 Monaco 语言服务;目标文件没在 Monaco 里打开、或语言没有内置语言服务时,自动用项目级定义搜索兜底(函数 / 类 / 方法 / const / type 等声明)。兜底会解析 import/require 直接定位来源文件,并跳过压缩产物/大文件;同一个文件里的声明、import 命中的定义、目录距离近的定义直接跳,只有多个真正同名的声明才弹「跳转到定义」列表,点同名属性/变量时不会拿远端无关的 const 来凑数;按住 ⌘/Ctrl 悬停时,可跳转的符号会显示下划线 + 手型光标;
|
|
46
46
|
- **全局搜索 / 替换**(左栏放大镜 / `⌘⇧F`):整个项目的文本搜索,支持区分大小写、正则、包含/排除文件(glob);结果按文件分组、高亮匹配,点击跳转行列;替换栏支持保留大小写(AB)、全部替换(需确认)、按文件替换(悬停文件头铅笔)、按单行匹配替换与忽略(悬停结果行);替换前会自动保存未落盘的编辑器内容;Git 项目用 `git ls-files` 枚举文件(尊重 `.gitignore`、含未跟踪文件),非 Git 项目遍历目录并自动跳过 `node_modules` 等依赖/缓存目录;摸鱼悬浮窗里同样可用;
|
|
47
|
+
- 资源管理器文件/文件夹可直接**拖进右侧输入框**变成 @引用;
|
|
47
48
|
- 资源管理器右键:新建 / 重命名 / 删除 / 复制 / 粘贴 / 打开所在文件夹 / 两文件对比;
|
|
48
49
|
- **从 Finder 粘贴/拖入文件**:系统剪贴板里复制的文件可直接 ⌘V 粘进资源管理器(写入选中的目录,重名自动加「副本」),也可从 Finder 直接拖进文件树;
|
|
50
|
+
- **复制代码选区 / 拖动文件到输入框 = @代码引用**(原 dsh-drag-ref 能力已并入本插件,不再需要独立插件):复制跨行选区(首尾半行也算)或完整单行时生成 `@文件 Lx-Ly` 代码引用,粘贴进输入框变成引用 chip;不足一行的选区按普通文本复制;成功复制/插入不再弹提示;
|
|
49
51
|
- **粘贴文件到聊天框 = @引用**:在 Finder 复制文件后在右侧对话输入框 ⌘V,会自动匹配项目内同名同内容文件并插入 `@相对路径` 引用(图片仍按 DSH 默认附件处理);
|
|
50
52
|
- 支持 Ctrl/Cmd 多选、软删除与 `⌘Z` 撤销;
|
|
51
53
|
- **Git diff**:Monaco diff 视图,红绿强对比、自动定位第一处改动,工作区一侧可编辑并自动保存;
|
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.92'
|
|
20
20
|
G.PANEL_ID = 'gufa-code'
|
|
21
21
|
/** 从哪个工作区点进来的(工作区菜单/节点按钮写入,App 启动时消费)。 */
|
|
22
22
|
G.pendingEntryWorkspace = null
|
|
@@ -2883,6 +2883,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
2883
2883
|
const viewStatesRef = useRef(new Map()) /* path -> editor view state(滚动/光标) */
|
|
2884
2884
|
const lastPropContentRef = useRef(new Map()) /* path -> 最近一次同步过的 props content */
|
|
2885
2885
|
const modelsRootRef = useRef(root) /* model 所属项目根:root 变了必须重建 */
|
|
2886
|
+
const tsPreloadRef = useRef(null) /* 后台预加载 import 依赖(JS/TS) */
|
|
2886
2887
|
const modelPathRef = useRef(null) /* 当前挂在 editor 上的 tab path */
|
|
2887
2888
|
const suppressChangeRef = useRef(false) /* 程序化 setValue 时抑制 onChange 回环 */
|
|
2888
2889
|
const propsRef = useRef(props)
|
|
@@ -3170,6 +3171,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
3170
3171
|
}
|
|
3171
3172
|
}
|
|
3172
3173
|
}
|
|
3174
|
+
tsPreloadRef.current = preloadTsDependencies
|
|
3173
3175
|
const queryDefinitions = (position, word, model, path, root) => {
|
|
3174
3176
|
const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
|
|
3175
3177
|
if (language === 'python') {
|
|
@@ -3802,6 +3804,18 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
3802
3804
|
GUFA_LSP_WARMED.add(root)
|
|
3803
3805
|
void G.api('lsp.warm', { root }).catch(() => { GUFA_LSP_WARMED.delete(root) })
|
|
3804
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
|
+
}
|
|
3805
3819
|
/* 打开/切换文件不抢焦点:只有用户点击编辑区时才进入键盘编辑 */
|
|
3806
3820
|
}
|
|
3807
3821
|
} else if (targetTab !== null) {
|
|
@@ -9561,6 +9575,551 @@ G.installChatFilePaste = function installChatFilePaste(ctx) {
|
|
|
9561
9575
|
}
|
|
9562
9576
|
|
|
9563
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 absolute = decodeURIComponent(rawPath.replace(/^\//, ""));
|
|
9797
|
+
if (absolute === "") return null;
|
|
9798
|
+
/* gufa 的 model URI 是项目绝对路径;引用 chip 需要项目相对路径 */
|
|
9799
|
+
let path = absolute;
|
|
9800
|
+
try {
|
|
9801
|
+
const root = typeof G.projectRoot === "string" ? String(G.projectRoot).replace(/\/+$/, "") : "";
|
|
9802
|
+
const rootNoSlash = root.replace(/^\//, "");
|
|
9803
|
+
if (root !== "" && absolute.startsWith(root + "/")) path = absolute.slice(root.length + 1);
|
|
9804
|
+
else if (rootNoSlash !== "" && absolute.startsWith(rootNoSlash + "/")) path = absolute.slice(rootNoSlash.length + 1);
|
|
9805
|
+
} catch (_) {}
|
|
9806
|
+
if (path === "") return null;
|
|
9807
|
+
const startLine = Math.min(selection.startLineNumber, selection.endLineNumber);
|
|
9808
|
+
const endLine = Math.max(selection.startLineNumber, selection.endLineNumber);
|
|
9809
|
+
const startColumn = Math.min(selection.startColumn, selection.endColumn);
|
|
9810
|
+
const endColumn = Math.max(selection.startColumn, selection.endColumn);
|
|
9811
|
+
/* 单行:从行首选到行尾才算引用;多行:任意列都算引用。 */
|
|
9812
|
+
const fullLines = startLine !== endLine
|
|
9813
|
+
? true
|
|
9814
|
+
: startColumn === 1 && endColumn === model.getLineMaxColumn(endLine);
|
|
9815
|
+
return { path, startLine, endLine, fullLines, text };
|
|
9816
|
+
} catch (_) {
|
|
9817
|
+
return null;
|
|
9818
|
+
}
|
|
9819
|
+
}
|
|
9820
|
+
|
|
9821
|
+
function install(ctx) {
|
|
9822
|
+
loadRefs();
|
|
9823
|
+
const onDragStart = (event) => {
|
|
9824
|
+
const target = event.target;
|
|
9825
|
+
const row = target instanceof Element ? target.closest(ROW) : null;
|
|
9826
|
+
if (row === null || event.dataTransfer === null) return;
|
|
9827
|
+
const path = row.getAttribute("data-edrv-path");
|
|
9828
|
+
if (path === null || path === "") return;
|
|
9829
|
+
const chevron = row.querySelector(".edrv-tree-chev");
|
|
9830
|
+
const isDir = chevron !== null && (chevron.textContent || "").trim() !== "";
|
|
9831
|
+
event.dataTransfer.effectAllowed = "copy";
|
|
9832
|
+
event.dataTransfer.setData(DRAG_TYPE, JSON.stringify({ path, isDir }));
|
|
9833
|
+
event.dataTransfer.setData("text/plain", path);
|
|
9834
|
+
};
|
|
9835
|
+
const onDragOver = (event) => {
|
|
9836
|
+
if (!isOurs(event.dataTransfer) || dropZoneOf(event.target) === null) return;
|
|
9837
|
+
event.preventDefault();
|
|
9838
|
+
if (event.dataTransfer !== null) event.dataTransfer.dropEffect = "copy";
|
|
9839
|
+
};
|
|
9840
|
+
const onDrop = (event) => {
|
|
9841
|
+
if (!isOurs(event.dataTransfer)) return;
|
|
9842
|
+
if (dropZoneOf(event.target) === null) {
|
|
9843
|
+
toast("松手位置不在输入框内,未插入", "err");
|
|
9844
|
+
return;
|
|
9845
|
+
}
|
|
9846
|
+
event.preventDefault();
|
|
9847
|
+
event.stopPropagation();
|
|
9848
|
+
const payload = payloadOf(event.dataTransfer);
|
|
9849
|
+
if (payload === null) {
|
|
9850
|
+
return;
|
|
9851
|
+
}
|
|
9852
|
+
const result = insertRef(ctx, { path: payload.path, isDir: payload.isDir === true });
|
|
9853
|
+
if (result.ok) {
|
|
9854
|
+
insertedRefs.set(labelOf(payload.path), { path: payload.path, startLine: void 0, endLine: void 0 });
|
|
9855
|
+
saveRefs();
|
|
9856
|
+
}
|
|
9857
|
+
if (result.ok) toast("已插入引用:" + labelOf(payload.path), "ok");
|
|
9858
|
+
else toast("插入失败:" + result.reason, "err");
|
|
9859
|
+
};
|
|
9860
|
+
|
|
9861
|
+
/** Copying a selection inside Monaco records the path + line range. */
|
|
9862
|
+
const onCopy = (event) => {
|
|
9863
|
+
const target = event.target;
|
|
9864
|
+
if (!(target instanceof Element) || target.closest(".monaco-editor") === null) return;
|
|
9865
|
+
const editor = focusedEditor();
|
|
9866
|
+
if (editor === null) return;
|
|
9867
|
+
const info = editorSelectionOf(editor);
|
|
9868
|
+
if (info === null) return;
|
|
9869
|
+
/* 引用判定:跨行选区一律算引用(哪怕首行/末行只选中半行);
|
|
9870
|
+
单行必须完整整行选中,否则按普通文本复制。 */
|
|
9871
|
+
const multiLine = info.startLine !== info.endLine;
|
|
9872
|
+
if (!multiLine && info.fullLines !== true) return;
|
|
9873
|
+
try {
|
|
9874
|
+
event.preventDefault();
|
|
9875
|
+
event.stopPropagation();
|
|
9876
|
+
if (event.clipboardData !== null) {
|
|
9877
|
+
event.clipboardData.setData("text/plain", info.text);
|
|
9878
|
+
event.clipboardData.setData(CODE_REF_TYPE, JSON.stringify(info));
|
|
9879
|
+
}
|
|
9880
|
+
} catch (_) {}
|
|
9881
|
+
};
|
|
9882
|
+
|
|
9883
|
+
const onPaste = (event) => {
|
|
9884
|
+
if (!hasCodeRef(event.clipboardData)) return;
|
|
9885
|
+
if (dropZoneOf(event.target) === null) return;
|
|
9886
|
+
const info = codeRefOf(event.clipboardData);
|
|
9887
|
+
if (info === null) return;
|
|
9888
|
+
event.preventDefault();
|
|
9889
|
+
event.stopPropagation();
|
|
9890
|
+
const result = insertRef(ctx, { path: info.path, startLine: info.startLine, endLine: info.endLine, text: info.text });
|
|
9891
|
+
if (result.ok) {
|
|
9892
|
+
insertedRefs.set(labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), { path: info.path, startLine: info.startLine, endLine: info.endLine });
|
|
9893
|
+
saveRefs();
|
|
9894
|
+
}
|
|
9895
|
+
if (result.ok) toast("已插入代码引用:" + labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), "ok");
|
|
9896
|
+
else toast("插入失败:" + result.reason, "err");
|
|
9897
|
+
};
|
|
9898
|
+
|
|
9899
|
+
/**
|
|
9900
|
+
* Clicking a reference chip re-opens the target and selects its line range.
|
|
9901
|
+
* Only real chips are considered: a title chip, a Lexical decorator inside
|
|
9902
|
+
* the composer, or an inline leaf whose text matches a known chip label.
|
|
9903
|
+
* Message containers and the editable root never match.
|
|
9904
|
+
*/
|
|
9905
|
+
const onReferenceClick = (event) => {
|
|
9906
|
+
const target = event.target;
|
|
9907
|
+
if (!(target instanceof Element)) return;
|
|
9908
|
+
let info = null;
|
|
9909
|
+
|
|
9910
|
+
/* 1) chip carrying a title (`@path L12-34` or `@path`). */
|
|
9911
|
+
const titleChip = target.closest("[title^=\"@\"]");
|
|
9912
|
+
if (titleChip !== null) {
|
|
9913
|
+
const chipText = (titleChip.textContent || "").trim();
|
|
9914
|
+
if (chipText.length > 80) return;
|
|
9915
|
+
const title = (titleChip.getAttribute("title") || "").trim();
|
|
9916
|
+
if (/^@(?:"[^"\n]+"|\S+)(?:\s+L\d+(?:-\d+)?)?$/u.test(title)) info = parseRefText(title);
|
|
9917
|
+
if (info === null && chipText !== "") info = insertedRefs.get(chipText) || null;
|
|
9918
|
+
if (info !== null && (info.startLine === void 0 || info.endLine === void 0)) {
|
|
9919
|
+
const sibling = titleChip.nextSibling;
|
|
9920
|
+
const tail = sibling !== null && sibling.nodeType === 3 ? sibling.textContent : "";
|
|
9921
|
+
const range = /^\s*L(\d+)(?:-(\d+))?/u.exec(String(tail || ""));
|
|
9922
|
+
if (range) {
|
|
9923
|
+
info.startLine = Number(range[1]);
|
|
9924
|
+
info.endLine = range[2] === void 0 ? info.startLine : Number(range[2]);
|
|
9925
|
+
}
|
|
9926
|
+
}
|
|
9927
|
+
} else {
|
|
9928
|
+
/* 2) Lexical decorator chip inside the composer (no title attribute). */
|
|
9929
|
+
const decorator = target.closest("[data-lexical-decorator=\"true\"], [contenteditable=\"false\"]");
|
|
9930
|
+
if (decorator !== null && decorator.closest("[data-conversation-region=\"composer\"]") !== null) {
|
|
9931
|
+
const label = (decorator.textContent || "").trim();
|
|
9932
|
+
if (label === "" || label.length > 80) return;
|
|
9933
|
+
info = insertedRefs.get(label) || null;
|
|
9934
|
+
} else {
|
|
9935
|
+
/* 3) inline leaf fallback (never the editable root or a block container). */
|
|
9936
|
+
if (target.getAttribute && target.getAttribute("contenteditable") === "true") return;
|
|
9937
|
+
try {
|
|
9938
|
+
const display = getComputedStyle(target).display;
|
|
9939
|
+
if (display !== "inline" && display !== "inline-flex" && display !== "inline-block") return;
|
|
9940
|
+
} catch (_) { return; }
|
|
9941
|
+
if (target.childElementCount > 0) return;
|
|
9942
|
+
const text = (target.textContent || "").trim();
|
|
9943
|
+
if (text === "" || text.length > 80) return;
|
|
9944
|
+
info = insertedRefs.get(text) || null;
|
|
9945
|
+
}
|
|
9946
|
+
}
|
|
9947
|
+
|
|
9948
|
+
if (info === null || info.path === void 0 || info.path === "") return;
|
|
9949
|
+
if (info.startLine === void 0 || info.endLine === void 0) return;
|
|
9950
|
+
event.preventDefault();
|
|
9951
|
+
event.stopPropagation();
|
|
9952
|
+
openAtRange(ctx, info.path, info.startLine, info.endLine);
|
|
9953
|
+
};
|
|
9954
|
+
|
|
9955
|
+
const mark = (root) => {
|
|
9956
|
+
const rows = root && typeof root.querySelectorAll === "function" ? root.querySelectorAll(ROW) : [];
|
|
9957
|
+
for (const row of rows) {
|
|
9958
|
+
if (row.getAttribute("draggable") !== "true") row.setAttribute("draggable", "true");
|
|
9959
|
+
}
|
|
9960
|
+
};
|
|
9961
|
+
|
|
9962
|
+
document.addEventListener("dragstart", onDragStart, true);
|
|
9963
|
+
document.addEventListener("dragover", onDragOver, true);
|
|
9964
|
+
document.addEventListener("drop", onDrop, true);
|
|
9965
|
+
document.addEventListener("click", onReferenceClick, true);
|
|
9966
|
+
document.addEventListener("copy", onCopy, true);
|
|
9967
|
+
document.addEventListener("paste", onPaste, true);
|
|
9968
|
+
mark(document);
|
|
9969
|
+
const observer = new MutationObserver((mutations) => {
|
|
9970
|
+
for (const mutation of mutations) {
|
|
9971
|
+
for (const node of mutation.addedNodes) {
|
|
9972
|
+
if (!(node instanceof Element)) continue;
|
|
9973
|
+
if (node.matches(ROW)) node.setAttribute("draggable", "true");
|
|
9974
|
+
mark(node);
|
|
9975
|
+
}
|
|
9976
|
+
}
|
|
9977
|
+
});
|
|
9978
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
9979
|
+
|
|
9980
|
+
return () => {
|
|
9981
|
+
document.removeEventListener("dragstart", onDragStart, true);
|
|
9982
|
+
document.removeEventListener("dragover", onDragOver, true);
|
|
9983
|
+
document.removeEventListener("drop", onDrop, true);
|
|
9984
|
+
document.removeEventListener("click", onReferenceClick, true);
|
|
9985
|
+
document.removeEventListener("copy", onCopy, true);
|
|
9986
|
+
document.removeEventListener("paste", onPaste, true);
|
|
9987
|
+
observer.disconnect();
|
|
9988
|
+
};
|
|
9989
|
+
}
|
|
9990
|
+
|
|
9991
|
+
/** Inserted references by chip label, for chips that carry no title attribute. */
|
|
9992
|
+
const insertedRefs = new Map();
|
|
9993
|
+
const REF_STORE_KEY = "gufa-code.refs.v1";
|
|
9994
|
+
|
|
9995
|
+
/** Restore label → {path,startLine,endLine} from localStorage (drafts survive restarts). */
|
|
9996
|
+
function loadRefs() {
|
|
9997
|
+
try {
|
|
9998
|
+
const raw = localStorage.getItem(REF_STORE_KEY);
|
|
9999
|
+
if (raw === null) return;
|
|
10000
|
+
const parsed = JSON.parse(raw);
|
|
10001
|
+
if (parsed === null || typeof parsed !== "object") return;
|
|
10002
|
+
for (const label of Object.keys(parsed)) {
|
|
10003
|
+
const info = parsed[label];
|
|
10004
|
+
if (info && typeof info.path === "string" && info.path !== "") insertedRefs.set(label, info);
|
|
10005
|
+
}
|
|
10006
|
+
} catch (_) {}
|
|
10007
|
+
}
|
|
10008
|
+
|
|
10009
|
+
/** Persist the label map so chips restored from a draft still resolve after a restart. */
|
|
10010
|
+
function saveRefs() {
|
|
10011
|
+
try {
|
|
10012
|
+
const out = {};
|
|
10013
|
+
let count = 0;
|
|
10014
|
+
for (const [label, info] of insertedRefs) {
|
|
10015
|
+
out[label] = info;
|
|
10016
|
+
if (++count >= 300) break;
|
|
10017
|
+
}
|
|
10018
|
+
localStorage.setItem(REF_STORE_KEY, JSON.stringify(out));
|
|
10019
|
+
} catch (_) {}
|
|
10020
|
+
}
|
|
10021
|
+
|
|
10022
|
+
/** Parse `@path L12-34` / `@"a b" L12` into { path, startLine, endLine }. */
|
|
10023
|
+
function parseRefText(text) {
|
|
10024
|
+
const raw = String(text == null ? "" : text).trim();
|
|
10025
|
+
if (!raw.startsWith("@")) return null;
|
|
10026
|
+
let body = raw.slice(1);
|
|
10027
|
+
let path = "";
|
|
10028
|
+
if (body.startsWith('"')) {
|
|
10029
|
+
const end = body.indexOf('"', 1);
|
|
10030
|
+
if (end < 0) return null;
|
|
10031
|
+
path = body.slice(1, end);
|
|
10032
|
+
body = body.slice(end + 1);
|
|
10033
|
+
} else {
|
|
10034
|
+
const space = body.indexOf(" ");
|
|
10035
|
+
path = space < 0 ? body : body.slice(0, space);
|
|
10036
|
+
body = space < 0 ? "" : body.slice(space);
|
|
10037
|
+
}
|
|
10038
|
+
if (path === "") return null;
|
|
10039
|
+
const range = /^\s*L(\d+)(?:-(\d+))?\s*$/u.exec(body);
|
|
10040
|
+
if (!range) return { path, startLine: void 0, endLine: void 0 };
|
|
10041
|
+
const start = Number(range[1]);
|
|
10042
|
+
const end = range[2] === void 0 ? start : Number(range[2]);
|
|
10043
|
+
return { path, startLine: start, endLine: end };
|
|
10044
|
+
}
|
|
10045
|
+
|
|
10046
|
+
/** Find the Monaco model for one workspace-relative path. */
|
|
10047
|
+
function modelFor(path) {
|
|
10048
|
+
const monaco = window.monaco;
|
|
10049
|
+
if (!monaco || !monaco.editor || typeof monaco.editor.getModels !== "function") return null;
|
|
10050
|
+
let models = [];
|
|
10051
|
+
try { models = monaco.editor.getModels() || []; } catch (_) { return null; }
|
|
10052
|
+
const wanted = String(path).replace(/\\/g, "/").replace(/^\.\//, "");
|
|
10053
|
+
for (const model of models) {
|
|
10054
|
+
const raw = model && model.uri && model.uri.path ? String(model.uri.path) : "";
|
|
10055
|
+
const candidate = decodeURIComponent(raw.replace(/^\//, ""));
|
|
10056
|
+
if (candidate === wanted || candidate.endsWith("/" + wanted) || wanted.endsWith("/" + candidate)) return model;
|
|
10057
|
+
}
|
|
10058
|
+
return null;
|
|
10059
|
+
}
|
|
10060
|
+
|
|
10061
|
+
/** Select the line range once the editor for that file is mounted. */
|
|
10062
|
+
function selectRange(path, startLine, endLine) {
|
|
10063
|
+
const start = Number(startLine);
|
|
10064
|
+
const end = Number(endLine) || start;
|
|
10065
|
+
if (!Number.isFinite(start) || start <= 0) return;
|
|
10066
|
+
const deadline = Date.now() + 4000;
|
|
10067
|
+
const tick = () => {
|
|
10068
|
+
const monaco = window.monaco;
|
|
10069
|
+
const model = modelFor(path);
|
|
10070
|
+
if (monaco && model) {
|
|
10071
|
+
let editors = [];
|
|
10072
|
+
try { editors = monaco.editor.getEditors() || []; } catch (_) {}
|
|
10073
|
+
const editor = editors.find((item) => item.getModel && item.getModel() === model);
|
|
10074
|
+
if (editor) {
|
|
10075
|
+
try {
|
|
10076
|
+
const range = new monaco.Range(start, 1, end, 1);
|
|
10077
|
+
if (typeof editor.setSelection === "function") editor.setSelection(range);
|
|
10078
|
+
if (typeof editor.revealRangeInCenter === "function") editor.revealRangeInCenter(range);
|
|
10079
|
+
else if (typeof editor.revealLineInCenter === "function") editor.revealLineInCenter(start);
|
|
10080
|
+
if (typeof editor.focus === "function") editor.focus();
|
|
10081
|
+
} catch (_) {}
|
|
10082
|
+
return;
|
|
10083
|
+
}
|
|
10084
|
+
}
|
|
10085
|
+
if (Date.now() < deadline) setTimeout(tick, 120);
|
|
10086
|
+
};
|
|
10087
|
+
tick();
|
|
10088
|
+
}
|
|
10089
|
+
|
|
10090
|
+
/** Open a file through the official resource route (falls back to workspaces.openPath). */
|
|
10091
|
+
function openAtRange(ctx, path, startLine, endLine) {
|
|
10092
|
+
try {
|
|
10093
|
+
if (G.bus !== null && G.bus !== undefined && typeof G.bus.emit === "function") {
|
|
10094
|
+
G.bus.emit("editor:open-at", { path, line: Number(startLine) || 1, column: 1 });
|
|
10095
|
+
selectRange(path, startLine, endLine);
|
|
10096
|
+
return;
|
|
10097
|
+
}
|
|
10098
|
+
} catch (_) {}
|
|
10099
|
+
try {
|
|
10100
|
+
const sessionId = activeSessionId();
|
|
10101
|
+
const sidebarRight = ctx.get("sidebarRight");
|
|
10102
|
+
if (sessionId !== void 0 && sidebarRight !== void 0 && typeof sidebarRight.openResource === "function") {
|
|
10103
|
+
const address = "dsh-resource://file/session/" + encodeURIComponent(sessionId) + "/" + String(path).replace(/\\/g, "/").split("/").map(encodeURIComponent).join("/");
|
|
10104
|
+
sidebarRight.openResource(address, { params: { line: Number(startLine) || 1 } });
|
|
10105
|
+
} else {
|
|
10106
|
+
const workspaces = ctx.get("workspaces");
|
|
10107
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
10108
|
+
}
|
|
10109
|
+
} catch (_) {
|
|
10110
|
+
try {
|
|
10111
|
+
const workspaces = ctx.get("workspaces");
|
|
10112
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
10113
|
+
} catch (_) {}
|
|
10114
|
+
}
|
|
10115
|
+
selectRange(path, startLine, endLine);
|
|
10116
|
+
}
|
|
10117
|
+
|
|
10118
|
+
|
|
10119
|
+
return install(ctx)
|
|
10120
|
+
}
|
|
10121
|
+
|
|
10122
|
+
|
|
9564
10123
|
/* ─────────────────────── 59-dialog-layer.js ─────────────────────── */
|
|
9565
10124
|
/**
|
|
9566
10125
|
* 59-dialog-layer:外部门户弹窗(DSH 官方设置等)打开时,把古法编程的会话停靠层降到弹窗之下。
|
|
@@ -14784,6 +15343,16 @@ const GUFA_PLUGIN = {
|
|
|
14784
15343
|
console.error('[gufa-code] 安装聊天框文件粘贴失败', error)
|
|
14785
15344
|
}
|
|
14786
15345
|
|
|
15346
|
+
// 文件拖拽 → @引用 / 代码选区复制 → 代码引用 / 引用 chip 点击跳回(原 dsh-drag-ref 能力)
|
|
15347
|
+
try {
|
|
15348
|
+
if (typeof G.installGufaRefBridge === 'function') {
|
|
15349
|
+
const disposeRefBridge = G.installGufaRefBridge(ctx)
|
|
15350
|
+
if (typeof disposeRefBridge === 'function') ctx.effect(() => disposeRefBridge, 'gufa-code: explorer + code reference bridge')
|
|
15351
|
+
}
|
|
15352
|
+
} catch (error) {
|
|
15353
|
+
console.error('[gufa-code] 安装引用桥失败', error)
|
|
15354
|
+
}
|
|
15355
|
+
|
|
14787
15356
|
// 样式(随插件 context 卸载自动移除)
|
|
14788
15357
|
ctx.effect(() => G.injectStyles(), 'gufa-code: styles')
|
|
14789
15358
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -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.
|
|
11
|
-
<key>CFBundleVersion</key><string>1.3.
|
|
10
|
+
<key>CFBundleShortVersionString</key><string>1.3.92</string>
|
|
11
|
+
<key>CFBundleVersion</key><string>1.3.92</string>
|
|
12
12
|
<key>LSUIElement</key><true/>
|
|
13
13
|
<key>NSHighResolutionCapable</key><true/>
|
|
14
14
|
<key>NSPrincipalClass</key><string>NSApplication</string>
|
|
Binary file
|
package/docs/KNOWN-PITFALLS.md
CHANGED
|
@@ -30,6 +30,15 @@
|
|
|
30
30
|
- 必须做防串扰:`provideInlineCompletions` 里 260ms 防抖 + 全局 500ms 限流 + `token.isCancellationRequested` 取消 + 按 `path/version/position` 缓存;否则每敲一个字符都打模型、又慢又费 token。
|
|
31
31
|
- Host prompt 只带 `prefix`(末尾 3200 字)和 `suffix`(800 字),`maxTokens 160`、`temperature 0.2`;返回前剥掉 markdown 围栏。上下文太大不仅慢,而且容易让模型续写整段而不是补一行。
|
|
32
32
|
|
|
33
|
+
## 0.8 代码引用/拖拽引用(原 dsh-drag-ref 已并入)
|
|
34
|
+
|
|
35
|
+
- 原先独立插件 dsh-drag-ref 的复制代码引用、拖文件到输入框、chip 点击跳回能力已并入 `src/client/58b-drag-ref.js`,profile 里不再装独立插件;两边同时装会重复 `copy` 监听。
|
|
36
|
+
- 复制语义:**跨行选区一律算引用**(首行/末行只选半行也可以,行范围按实际首尾行);**单行必须整行选中**才生成引用,否则不拦截、按普通文本复制。
|
|
37
|
+
- 成功复制/插入/拖动/跳转一律不弹 toast(用户明确讨厌成功提示),只有失败才提示;toast 复用 `G.refToast`。
|
|
38
|
+
- 古法 IDE 内点击引用 chip:优先 `G.bus.emit('editor:open-at')` 打开自己的 Monaco 标签,再 `selectRange` 选中行范围;不要依赖 DSH 侧边栏资源路由。
|
|
39
|
+
- 行范围引用格式 `@path Lx-Ly`;chip 标签里存路径+行号用于点击定位,`localStorage` key 使用 `gufa-code.refs.v1`。
|
|
40
|
+
- **model URI 是项目绝对路径,引用 chip 必须转成项目相对路径**(`G.projectRoot` 前缀剥离),否则点击 chip 会走 `fs.read` 绝对路径并报「路径不存在」;这个坑 1.3.92 已修。
|
|
41
|
+
|
|
33
42
|
## 1. 弹窗/菜单层级:只有一个正确做法(反复踩过多次,最终定案)
|
|
34
43
|
|
|
35
44
|
**现象**:弹窗被右侧「古法 AI」聊天栏压住、只显示一半、关闭按钮点不到;或者反过来把官方设置/会话弹窗盖住。
|
package/package.json
CHANGED
package/src/client/20-editor.js
CHANGED
|
@@ -533,6 +533,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
533
533
|
const viewStatesRef = useRef(new Map()) /* path -> editor view state(滚动/光标) */
|
|
534
534
|
const lastPropContentRef = useRef(new Map()) /* path -> 最近一次同步过的 props content */
|
|
535
535
|
const modelsRootRef = useRef(root) /* model 所属项目根:root 变了必须重建 */
|
|
536
|
+
const tsPreloadRef = useRef(null) /* 后台预加载 import 依赖(JS/TS) */
|
|
536
537
|
const modelPathRef = useRef(null) /* 当前挂在 editor 上的 tab path */
|
|
537
538
|
const suppressChangeRef = useRef(false) /* 程序化 setValue 时抑制 onChange 回环 */
|
|
538
539
|
const propsRef = useRef(props)
|
|
@@ -820,6 +821,7 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
820
821
|
}
|
|
821
822
|
}
|
|
822
823
|
}
|
|
824
|
+
tsPreloadRef.current = preloadTsDependencies
|
|
823
825
|
const queryDefinitions = (position, word, model, path, root) => {
|
|
824
826
|
const language = typeof model.getLanguageId === 'function' ? model.getLanguageId() : ''
|
|
825
827
|
if (language === 'python') {
|
|
@@ -1452,6 +1454,18 @@ const GUFA_DEFINITION_STRONG_KINDS = new Set(['function', 'class', 'method', 'de
|
|
|
1452
1454
|
GUFA_LSP_WARMED.add(root)
|
|
1453
1455
|
void G.api('lsp.warm', { root }).catch(() => { GUFA_LSP_WARMED.delete(root) })
|
|
1454
1456
|
}
|
|
1457
|
+
/* JS/TS:切到文件后后台预加载 import 依赖,点 Cmd+点击时 Monaco 已有跨文件 model,少等网络 */
|
|
1458
|
+
if (targetTab.language === 'typescript' || targetTab.language === 'javascript') {
|
|
1459
|
+
const modelForPreload = targetModel
|
|
1460
|
+
const pathForPreload = targetTab.path
|
|
1461
|
+
window.setTimeout(() => {
|
|
1462
|
+
try {
|
|
1463
|
+
if (tsPreloadRef.current !== null) void tsPreloadRef.current(modelForPreload, pathForPreload, root)
|
|
1464
|
+
} catch {
|
|
1465
|
+
/* ignore */
|
|
1466
|
+
}
|
|
1467
|
+
}, 200)
|
|
1468
|
+
}
|
|
1455
1469
|
/* 打开/切换文件不抢焦点:只有用户点击编辑区时才进入键盘编辑 */
|
|
1456
1470
|
}
|
|
1457
1471
|
} else if (targetTab !== null) {
|
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 58b-drag-ref:把 dsh-drag-ref 的「拖动文件到输入框 → @引用」「复制代码选区 →
|
|
3
|
+
* 代码引用」「粘贴引用 chip」「点击 chip 跳回代码」整合进古法编程客户端。
|
|
4
|
+
*/
|
|
5
|
+
G.installGufaRefBridge = function installGufaRefBridge(ctx) {
|
|
6
|
+
if (typeof document === "undefined") return () => {}
|
|
7
|
+
const DRAG_TYPE = "application/x-dsh-explorer-path";
|
|
8
|
+
const CODE_REF_TYPE = "application/x-dsh-code-ref";
|
|
9
|
+
const ROW = "[data-edrv-path]";
|
|
10
|
+
const COMPOSER = "[data-conversation-region=\"composer\"]";
|
|
11
|
+
const CHAT = "[data-conversation-content]";
|
|
12
|
+
|
|
13
|
+
function toast(text, kind) {
|
|
14
|
+
/* 只保留错误提示;复制/插入成功不再打扰用户 */
|
|
15
|
+
if (kind !== "err") return;
|
|
16
|
+
try { G.refToast(String(text), "err") } catch (_) {}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function activeSessionId() {
|
|
20
|
+
const el = document.querySelector("[data-conversation-session]");
|
|
21
|
+
const id = el === null ? null : el.getAttribute("data-conversation-session");
|
|
22
|
+
return typeof id === "string" && id !== "" ? id : void 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function mentionOf(path) {
|
|
26
|
+
const rel = String(path).replace(/\\/g, "/");
|
|
27
|
+
if (/[\u0000-\u001f\u007f-\u009f"]/u.test(rel)) return "@" + path;
|
|
28
|
+
return /\s/u.test(rel) ? `@"${rel}"` : "@" + rel;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function labelOf(path) {
|
|
32
|
+
const parts = String(path).split(/[\\/]/);
|
|
33
|
+
return parts[parts.length - 1] || String(path);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function rangeText(startLine, endLine) {
|
|
37
|
+
if (!Number.isFinite(startLine) || !Number.isFinite(endLine)) return "";
|
|
38
|
+
const a = Math.min(startLine, endLine);
|
|
39
|
+
const b = Math.max(startLine, endLine);
|
|
40
|
+
return a === b ? `${a} 行` : `${a}-${b} 行`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function detectEnd(snapshot) {
|
|
44
|
+
const total = String(snapshot && snapshot.draft !== void 0 ? snapshot.draft : "").length;
|
|
45
|
+
const occurrences = snapshot && snapshot.occurrences;
|
|
46
|
+
if (!Array.isArray(occurrences) || occurrences.length === 0) return total;
|
|
47
|
+
let extra = 0;
|
|
48
|
+
for (const item of occurrences) {
|
|
49
|
+
const len = Number(item && item.length);
|
|
50
|
+
if (Number.isFinite(len) && len > 1) extra += len - 1;
|
|
51
|
+
}
|
|
52
|
+
return Math.max(0, total - extra);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function caretOffset(input, fallback) {
|
|
56
|
+
try {
|
|
57
|
+
if (typeof input.caretSpan !== "function") return fallback;
|
|
58
|
+
const span = input.caretSpan();
|
|
59
|
+
const end = span ? Number(span.end) : NaN;
|
|
60
|
+
if (!Number.isFinite(end)) return fallback;
|
|
61
|
+
return Math.min(Math.max(0, end), fallback);
|
|
62
|
+
} catch (_) {
|
|
63
|
+
return fallback;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function typeIntoComposer(text) {
|
|
68
|
+
const editor = document.querySelector(COMPOSER + " [contenteditable=\"true\"]") || document.querySelector(COMPOSER);
|
|
69
|
+
if (editor === null) return false;
|
|
70
|
+
try {
|
|
71
|
+
editor.focus();
|
|
72
|
+
if (document.execCommand("insertText", false, text)) return true;
|
|
73
|
+
} catch (_) {}
|
|
74
|
+
try {
|
|
75
|
+
const data = new DataTransfer();
|
|
76
|
+
data.setData("text/plain", text);
|
|
77
|
+
editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true }));
|
|
78
|
+
return true;
|
|
79
|
+
} catch (_) {}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Insert a file/folder reference (optionally with a line range). */
|
|
84
|
+
function insertRef(ctx, info) {
|
|
85
|
+
const isDir = info.isDir === true;
|
|
86
|
+
const mention = mentionOf(info.path);
|
|
87
|
+
const hasRange = !isDir && Number.isFinite(info.startLine) && Number.isFinite(info.endLine);
|
|
88
|
+
const ref = hasRange
|
|
89
|
+
? mention + (info.startLine === info.endLine ? ` L${info.startLine}` : ` L${Math.min(info.startLine, info.endLine)}-${Math.max(info.startLine, info.endLine)}`)
|
|
90
|
+
: mention;
|
|
91
|
+
const label = hasRange
|
|
92
|
+
? labelOf(info.path) + " " + rangeText(info.startLine, info.endLine)
|
|
93
|
+
: labelOf(info.path);
|
|
94
|
+
const reference = {
|
|
95
|
+
source: "reference",
|
|
96
|
+
ref,
|
|
97
|
+
label,
|
|
98
|
+
appearance: isDir ? "folder" : "file",
|
|
99
|
+
clipboardText: mention
|
|
100
|
+
};
|
|
101
|
+
let input = null;
|
|
102
|
+
let reason = "";
|
|
103
|
+
try {
|
|
104
|
+
const sessions = ctx.get("sessions");
|
|
105
|
+
const conversation = ctx.get("conversation");
|
|
106
|
+
const sessionId = activeSessionId();
|
|
107
|
+
if (sessionId === void 0) reason = "找不到当前会话 id";
|
|
108
|
+
else if (sessions === void 0) reason = "sessions 服务不可用";
|
|
109
|
+
else if (conversation === void 0) reason = "conversation 服务不可用";
|
|
110
|
+
else {
|
|
111
|
+
const actx = typeof sessions.scope === "function" ? sessions.scope(sessionId) : void 0;
|
|
112
|
+
if (actx === void 0) reason = "sessions.scope(" + sessionId.slice(0, 12) + "…) 为空";
|
|
113
|
+
else {
|
|
114
|
+
input = conversation.input.for(actx);
|
|
115
|
+
if (input === void 0) reason = "conversation.input.for(...) 为空";
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} catch (error) {
|
|
119
|
+
reason = "门面解析异常:" + String(error && error.message || error);
|
|
120
|
+
}
|
|
121
|
+
if (input !== null && typeof input.state?.getSnapshot === "function") {
|
|
122
|
+
try {
|
|
123
|
+
const snapshot = input.state.getSnapshot();
|
|
124
|
+
const offset = caretOffset(input, detectEnd(snapshot));
|
|
125
|
+
const span = { start: offset, end: offset, draftRev: snapshot && snapshot.draftRev };
|
|
126
|
+
if (typeof input.insertReference === "function" && input.insertReference(reference, span) === true) return { ok: true, via: "insertReference" };
|
|
127
|
+
if (typeof input.insertText === "function" && input.insertText(mention + " ", span) === true) return { ok: true, via: "insertText" };
|
|
128
|
+
reason = reason || "insertReference/insertText 都返回 false";
|
|
129
|
+
} catch (error) {
|
|
130
|
+
reason = "插入异常:" + String(error && error.message || error);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (info.text && typeIntoComposer(info.text)) return { ok: true, via: "composer-text" };
|
|
134
|
+
if (typeIntoComposer(mention + " ")) return { ok: true, via: "composer" };
|
|
135
|
+
return { ok: false, reason: reason || "输入框不可用" };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function isOurs(transfer) {
|
|
139
|
+
if (transfer === null || transfer === void 0) return false;
|
|
140
|
+
try {
|
|
141
|
+
return Array.prototype.indexOf.call(transfer.types, DRAG_TYPE) !== -1;
|
|
142
|
+
} catch (_) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function payloadOf(transfer) {
|
|
148
|
+
if (transfer === null || transfer === void 0) return null;
|
|
149
|
+
try {
|
|
150
|
+
const raw = transfer.getData(DRAG_TYPE);
|
|
151
|
+
if (raw !== "") {
|
|
152
|
+
const parsed = JSON.parse(raw);
|
|
153
|
+
if (parsed && typeof parsed.path === "string" && parsed.path !== "") return parsed;
|
|
154
|
+
}
|
|
155
|
+
} catch (_) {}
|
|
156
|
+
try {
|
|
157
|
+
const text = transfer.getData("text/plain");
|
|
158
|
+
if (text !== "") return { path: text, isDir: false };
|
|
159
|
+
} catch (_) {}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function hasCodeRef(clipboard) {
|
|
164
|
+
if (clipboard === null || clipboard === void 0) return false;
|
|
165
|
+
try {
|
|
166
|
+
return Array.prototype.indexOf.call(clipboard.types, CODE_REF_TYPE) !== -1;
|
|
167
|
+
} catch (_) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function codeRefOf(clipboard) {
|
|
173
|
+
try {
|
|
174
|
+
const raw = clipboard.getData(CODE_REF_TYPE);
|
|
175
|
+
if (raw === "") return null;
|
|
176
|
+
const parsed = JSON.parse(raw);
|
|
177
|
+
if (!parsed || typeof parsed.path !== "string" || parsed.path === "") return null;
|
|
178
|
+
return parsed;
|
|
179
|
+
} catch (_) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function dropZoneOf(node) {
|
|
185
|
+
if (!(node instanceof Element)) return null;
|
|
186
|
+
return node.closest(COMPOSER) || node.closest(CHAT);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The focused Monaco editor (copy source). */
|
|
190
|
+
function focusedEditor() {
|
|
191
|
+
const monaco = window.monaco;
|
|
192
|
+
if (!monaco || !monaco.editor || typeof monaco.editor.getEditors !== "function") return null;
|
|
193
|
+
let editors = [];
|
|
194
|
+
try {
|
|
195
|
+
editors = monaco.editor.getEditors() || [];
|
|
196
|
+
} catch (_) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
for (const editor of editors) {
|
|
200
|
+
try {
|
|
201
|
+
if (editor && typeof editor.hasTextFocus === "function" && editor.hasTextFocus()) return editor;
|
|
202
|
+
} catch (_) {}
|
|
203
|
+
}
|
|
204
|
+
return editors.length === 1 ? editors[0] : null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** File path + selected line range + selected text from one Monaco editor.
|
|
208
|
+
* `fullLines` 只在选区完整覆盖整行时为真;不足一行或首尾带半行的选区
|
|
209
|
+
* 按普通文本复制,不生成代码引用。 */
|
|
210
|
+
function editorSelectionOf(editor) {
|
|
211
|
+
try {
|
|
212
|
+
const model = editor.getModel();
|
|
213
|
+
const selection = editor.getSelection();
|
|
214
|
+
if (!model || !selection) return null;
|
|
215
|
+
const text = model.getValueInRange(selection);
|
|
216
|
+
if (!text) return null;
|
|
217
|
+
const rawPath = model.uri && model.uri.path ? String(model.uri.path) : "";
|
|
218
|
+
const absolute = decodeURIComponent(rawPath.replace(/^\//, ""));
|
|
219
|
+
if (absolute === "") return null;
|
|
220
|
+
/* gufa 的 model URI 是项目绝对路径;引用 chip 需要项目相对路径 */
|
|
221
|
+
let path = absolute;
|
|
222
|
+
try {
|
|
223
|
+
const root = typeof G.projectRoot === "string" ? String(G.projectRoot).replace(/\/+$/, "") : "";
|
|
224
|
+
const rootNoSlash = root.replace(/^\//, "");
|
|
225
|
+
if (root !== "" && absolute.startsWith(root + "/")) path = absolute.slice(root.length + 1);
|
|
226
|
+
else if (rootNoSlash !== "" && absolute.startsWith(rootNoSlash + "/")) path = absolute.slice(rootNoSlash.length + 1);
|
|
227
|
+
} catch (_) {}
|
|
228
|
+
if (path === "") return null;
|
|
229
|
+
const startLine = Math.min(selection.startLineNumber, selection.endLineNumber);
|
|
230
|
+
const endLine = Math.max(selection.startLineNumber, selection.endLineNumber);
|
|
231
|
+
const startColumn = Math.min(selection.startColumn, selection.endColumn);
|
|
232
|
+
const endColumn = Math.max(selection.startColumn, selection.endColumn);
|
|
233
|
+
/* 单行:从行首选到行尾才算引用;多行:任意列都算引用。 */
|
|
234
|
+
const fullLines = startLine !== endLine
|
|
235
|
+
? true
|
|
236
|
+
: startColumn === 1 && endColumn === model.getLineMaxColumn(endLine);
|
|
237
|
+
return { path, startLine, endLine, fullLines, text };
|
|
238
|
+
} catch (_) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function install(ctx) {
|
|
244
|
+
loadRefs();
|
|
245
|
+
const onDragStart = (event) => {
|
|
246
|
+
const target = event.target;
|
|
247
|
+
const row = target instanceof Element ? target.closest(ROW) : null;
|
|
248
|
+
if (row === null || event.dataTransfer === null) return;
|
|
249
|
+
const path = row.getAttribute("data-edrv-path");
|
|
250
|
+
if (path === null || path === "") return;
|
|
251
|
+
const chevron = row.querySelector(".edrv-tree-chev");
|
|
252
|
+
const isDir = chevron !== null && (chevron.textContent || "").trim() !== "";
|
|
253
|
+
event.dataTransfer.effectAllowed = "copy";
|
|
254
|
+
event.dataTransfer.setData(DRAG_TYPE, JSON.stringify({ path, isDir }));
|
|
255
|
+
event.dataTransfer.setData("text/plain", path);
|
|
256
|
+
};
|
|
257
|
+
const onDragOver = (event) => {
|
|
258
|
+
if (!isOurs(event.dataTransfer) || dropZoneOf(event.target) === null) return;
|
|
259
|
+
event.preventDefault();
|
|
260
|
+
if (event.dataTransfer !== null) event.dataTransfer.dropEffect = "copy";
|
|
261
|
+
};
|
|
262
|
+
const onDrop = (event) => {
|
|
263
|
+
if (!isOurs(event.dataTransfer)) return;
|
|
264
|
+
if (dropZoneOf(event.target) === null) {
|
|
265
|
+
toast("松手位置不在输入框内,未插入", "err");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
event.preventDefault();
|
|
269
|
+
event.stopPropagation();
|
|
270
|
+
const payload = payloadOf(event.dataTransfer);
|
|
271
|
+
if (payload === null) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const result = insertRef(ctx, { path: payload.path, isDir: payload.isDir === true });
|
|
275
|
+
if (result.ok) {
|
|
276
|
+
insertedRefs.set(labelOf(payload.path), { path: payload.path, startLine: void 0, endLine: void 0 });
|
|
277
|
+
saveRefs();
|
|
278
|
+
}
|
|
279
|
+
if (result.ok) toast("已插入引用:" + labelOf(payload.path), "ok");
|
|
280
|
+
else toast("插入失败:" + result.reason, "err");
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
/** Copying a selection inside Monaco records the path + line range. */
|
|
284
|
+
const onCopy = (event) => {
|
|
285
|
+
const target = event.target;
|
|
286
|
+
if (!(target instanceof Element) || target.closest(".monaco-editor") === null) return;
|
|
287
|
+
const editor = focusedEditor();
|
|
288
|
+
if (editor === null) return;
|
|
289
|
+
const info = editorSelectionOf(editor);
|
|
290
|
+
if (info === null) return;
|
|
291
|
+
/* 引用判定:跨行选区一律算引用(哪怕首行/末行只选中半行);
|
|
292
|
+
单行必须完整整行选中,否则按普通文本复制。 */
|
|
293
|
+
const multiLine = info.startLine !== info.endLine;
|
|
294
|
+
if (!multiLine && info.fullLines !== true) return;
|
|
295
|
+
try {
|
|
296
|
+
event.preventDefault();
|
|
297
|
+
event.stopPropagation();
|
|
298
|
+
if (event.clipboardData !== null) {
|
|
299
|
+
event.clipboardData.setData("text/plain", info.text);
|
|
300
|
+
event.clipboardData.setData(CODE_REF_TYPE, JSON.stringify(info));
|
|
301
|
+
}
|
|
302
|
+
} catch (_) {}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const onPaste = (event) => {
|
|
306
|
+
if (!hasCodeRef(event.clipboardData)) return;
|
|
307
|
+
if (dropZoneOf(event.target) === null) return;
|
|
308
|
+
const info = codeRefOf(event.clipboardData);
|
|
309
|
+
if (info === null) return;
|
|
310
|
+
event.preventDefault();
|
|
311
|
+
event.stopPropagation();
|
|
312
|
+
const result = insertRef(ctx, { path: info.path, startLine: info.startLine, endLine: info.endLine, text: info.text });
|
|
313
|
+
if (result.ok) {
|
|
314
|
+
insertedRefs.set(labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), { path: info.path, startLine: info.startLine, endLine: info.endLine });
|
|
315
|
+
saveRefs();
|
|
316
|
+
}
|
|
317
|
+
if (result.ok) toast("已插入代码引用:" + labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), "ok");
|
|
318
|
+
else toast("插入失败:" + result.reason, "err");
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Clicking a reference chip re-opens the target and selects its line range.
|
|
323
|
+
* Only real chips are considered: a title chip, a Lexical decorator inside
|
|
324
|
+
* the composer, or an inline leaf whose text matches a known chip label.
|
|
325
|
+
* Message containers and the editable root never match.
|
|
326
|
+
*/
|
|
327
|
+
const onReferenceClick = (event) => {
|
|
328
|
+
const target = event.target;
|
|
329
|
+
if (!(target instanceof Element)) return;
|
|
330
|
+
let info = null;
|
|
331
|
+
|
|
332
|
+
/* 1) chip carrying a title (`@path L12-34` or `@path`). */
|
|
333
|
+
const titleChip = target.closest("[title^=\"@\"]");
|
|
334
|
+
if (titleChip !== null) {
|
|
335
|
+
const chipText = (titleChip.textContent || "").trim();
|
|
336
|
+
if (chipText.length > 80) return;
|
|
337
|
+
const title = (titleChip.getAttribute("title") || "").trim();
|
|
338
|
+
if (/^@(?:"[^"\n]+"|\S+)(?:\s+L\d+(?:-\d+)?)?$/u.test(title)) info = parseRefText(title);
|
|
339
|
+
if (info === null && chipText !== "") info = insertedRefs.get(chipText) || null;
|
|
340
|
+
if (info !== null && (info.startLine === void 0 || info.endLine === void 0)) {
|
|
341
|
+
const sibling = titleChip.nextSibling;
|
|
342
|
+
const tail = sibling !== null && sibling.nodeType === 3 ? sibling.textContent : "";
|
|
343
|
+
const range = /^\s*L(\d+)(?:-(\d+))?/u.exec(String(tail || ""));
|
|
344
|
+
if (range) {
|
|
345
|
+
info.startLine = Number(range[1]);
|
|
346
|
+
info.endLine = range[2] === void 0 ? info.startLine : Number(range[2]);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
} else {
|
|
350
|
+
/* 2) Lexical decorator chip inside the composer (no title attribute). */
|
|
351
|
+
const decorator = target.closest("[data-lexical-decorator=\"true\"], [contenteditable=\"false\"]");
|
|
352
|
+
if (decorator !== null && decorator.closest("[data-conversation-region=\"composer\"]") !== null) {
|
|
353
|
+
const label = (decorator.textContent || "").trim();
|
|
354
|
+
if (label === "" || label.length > 80) return;
|
|
355
|
+
info = insertedRefs.get(label) || null;
|
|
356
|
+
} else {
|
|
357
|
+
/* 3) inline leaf fallback (never the editable root or a block container). */
|
|
358
|
+
if (target.getAttribute && target.getAttribute("contenteditable") === "true") return;
|
|
359
|
+
try {
|
|
360
|
+
const display = getComputedStyle(target).display;
|
|
361
|
+
if (display !== "inline" && display !== "inline-flex" && display !== "inline-block") return;
|
|
362
|
+
} catch (_) { return; }
|
|
363
|
+
if (target.childElementCount > 0) return;
|
|
364
|
+
const text = (target.textContent || "").trim();
|
|
365
|
+
if (text === "" || text.length > 80) return;
|
|
366
|
+
info = insertedRefs.get(text) || null;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (info === null || info.path === void 0 || info.path === "") return;
|
|
371
|
+
if (info.startLine === void 0 || info.endLine === void 0) return;
|
|
372
|
+
event.preventDefault();
|
|
373
|
+
event.stopPropagation();
|
|
374
|
+
openAtRange(ctx, info.path, info.startLine, info.endLine);
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const mark = (root) => {
|
|
378
|
+
const rows = root && typeof root.querySelectorAll === "function" ? root.querySelectorAll(ROW) : [];
|
|
379
|
+
for (const row of rows) {
|
|
380
|
+
if (row.getAttribute("draggable") !== "true") row.setAttribute("draggable", "true");
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
document.addEventListener("dragstart", onDragStart, true);
|
|
385
|
+
document.addEventListener("dragover", onDragOver, true);
|
|
386
|
+
document.addEventListener("drop", onDrop, true);
|
|
387
|
+
document.addEventListener("click", onReferenceClick, true);
|
|
388
|
+
document.addEventListener("copy", onCopy, true);
|
|
389
|
+
document.addEventListener("paste", onPaste, true);
|
|
390
|
+
mark(document);
|
|
391
|
+
const observer = new MutationObserver((mutations) => {
|
|
392
|
+
for (const mutation of mutations) {
|
|
393
|
+
for (const node of mutation.addedNodes) {
|
|
394
|
+
if (!(node instanceof Element)) continue;
|
|
395
|
+
if (node.matches(ROW)) node.setAttribute("draggable", "true");
|
|
396
|
+
mark(node);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
401
|
+
|
|
402
|
+
return () => {
|
|
403
|
+
document.removeEventListener("dragstart", onDragStart, true);
|
|
404
|
+
document.removeEventListener("dragover", onDragOver, true);
|
|
405
|
+
document.removeEventListener("drop", onDrop, true);
|
|
406
|
+
document.removeEventListener("click", onReferenceClick, true);
|
|
407
|
+
document.removeEventListener("copy", onCopy, true);
|
|
408
|
+
document.removeEventListener("paste", onPaste, true);
|
|
409
|
+
observer.disconnect();
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Inserted references by chip label, for chips that carry no title attribute. */
|
|
414
|
+
const insertedRefs = new Map();
|
|
415
|
+
const REF_STORE_KEY = "gufa-code.refs.v1";
|
|
416
|
+
|
|
417
|
+
/** Restore label → {path,startLine,endLine} from localStorage (drafts survive restarts). */
|
|
418
|
+
function loadRefs() {
|
|
419
|
+
try {
|
|
420
|
+
const raw = localStorage.getItem(REF_STORE_KEY);
|
|
421
|
+
if (raw === null) return;
|
|
422
|
+
const parsed = JSON.parse(raw);
|
|
423
|
+
if (parsed === null || typeof parsed !== "object") return;
|
|
424
|
+
for (const label of Object.keys(parsed)) {
|
|
425
|
+
const info = parsed[label];
|
|
426
|
+
if (info && typeof info.path === "string" && info.path !== "") insertedRefs.set(label, info);
|
|
427
|
+
}
|
|
428
|
+
} catch (_) {}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** Persist the label map so chips restored from a draft still resolve after a restart. */
|
|
432
|
+
function saveRefs() {
|
|
433
|
+
try {
|
|
434
|
+
const out = {};
|
|
435
|
+
let count = 0;
|
|
436
|
+
for (const [label, info] of insertedRefs) {
|
|
437
|
+
out[label] = info;
|
|
438
|
+
if (++count >= 300) break;
|
|
439
|
+
}
|
|
440
|
+
localStorage.setItem(REF_STORE_KEY, JSON.stringify(out));
|
|
441
|
+
} catch (_) {}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Parse `@path L12-34` / `@"a b" L12` into { path, startLine, endLine }. */
|
|
445
|
+
function parseRefText(text) {
|
|
446
|
+
const raw = String(text == null ? "" : text).trim();
|
|
447
|
+
if (!raw.startsWith("@")) return null;
|
|
448
|
+
let body = raw.slice(1);
|
|
449
|
+
let path = "";
|
|
450
|
+
if (body.startsWith('"')) {
|
|
451
|
+
const end = body.indexOf('"', 1);
|
|
452
|
+
if (end < 0) return null;
|
|
453
|
+
path = body.slice(1, end);
|
|
454
|
+
body = body.slice(end + 1);
|
|
455
|
+
} else {
|
|
456
|
+
const space = body.indexOf(" ");
|
|
457
|
+
path = space < 0 ? body : body.slice(0, space);
|
|
458
|
+
body = space < 0 ? "" : body.slice(space);
|
|
459
|
+
}
|
|
460
|
+
if (path === "") return null;
|
|
461
|
+
const range = /^\s*L(\d+)(?:-(\d+))?\s*$/u.exec(body);
|
|
462
|
+
if (!range) return { path, startLine: void 0, endLine: void 0 };
|
|
463
|
+
const start = Number(range[1]);
|
|
464
|
+
const end = range[2] === void 0 ? start : Number(range[2]);
|
|
465
|
+
return { path, startLine: start, endLine: end };
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Find the Monaco model for one workspace-relative path. */
|
|
469
|
+
function modelFor(path) {
|
|
470
|
+
const monaco = window.monaco;
|
|
471
|
+
if (!monaco || !monaco.editor || typeof monaco.editor.getModels !== "function") return null;
|
|
472
|
+
let models = [];
|
|
473
|
+
try { models = monaco.editor.getModels() || []; } catch (_) { return null; }
|
|
474
|
+
const wanted = String(path).replace(/\\/g, "/").replace(/^\.\//, "");
|
|
475
|
+
for (const model of models) {
|
|
476
|
+
const raw = model && model.uri && model.uri.path ? String(model.uri.path) : "";
|
|
477
|
+
const candidate = decodeURIComponent(raw.replace(/^\//, ""));
|
|
478
|
+
if (candidate === wanted || candidate.endsWith("/" + wanted) || wanted.endsWith("/" + candidate)) return model;
|
|
479
|
+
}
|
|
480
|
+
return null;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Select the line range once the editor for that file is mounted. */
|
|
484
|
+
function selectRange(path, startLine, endLine) {
|
|
485
|
+
const start = Number(startLine);
|
|
486
|
+
const end = Number(endLine) || start;
|
|
487
|
+
if (!Number.isFinite(start) || start <= 0) return;
|
|
488
|
+
const deadline = Date.now() + 4000;
|
|
489
|
+
const tick = () => {
|
|
490
|
+
const monaco = window.monaco;
|
|
491
|
+
const model = modelFor(path);
|
|
492
|
+
if (monaco && model) {
|
|
493
|
+
let editors = [];
|
|
494
|
+
try { editors = monaco.editor.getEditors() || []; } catch (_) {}
|
|
495
|
+
const editor = editors.find((item) => item.getModel && item.getModel() === model);
|
|
496
|
+
if (editor) {
|
|
497
|
+
try {
|
|
498
|
+
const range = new monaco.Range(start, 1, end, 1);
|
|
499
|
+
if (typeof editor.setSelection === "function") editor.setSelection(range);
|
|
500
|
+
if (typeof editor.revealRangeInCenter === "function") editor.revealRangeInCenter(range);
|
|
501
|
+
else if (typeof editor.revealLineInCenter === "function") editor.revealLineInCenter(start);
|
|
502
|
+
if (typeof editor.focus === "function") editor.focus();
|
|
503
|
+
} catch (_) {}
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (Date.now() < deadline) setTimeout(tick, 120);
|
|
508
|
+
};
|
|
509
|
+
tick();
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** Open a file through the official resource route (falls back to workspaces.openPath). */
|
|
513
|
+
function openAtRange(ctx, path, startLine, endLine) {
|
|
514
|
+
try {
|
|
515
|
+
if (G.bus !== null && G.bus !== undefined && typeof G.bus.emit === "function") {
|
|
516
|
+
G.bus.emit("editor:open-at", { path, line: Number(startLine) || 1, column: 1 });
|
|
517
|
+
selectRange(path, startLine, endLine);
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
} catch (_) {}
|
|
521
|
+
try {
|
|
522
|
+
const sessionId = activeSessionId();
|
|
523
|
+
const sidebarRight = ctx.get("sidebarRight");
|
|
524
|
+
if (sessionId !== void 0 && sidebarRight !== void 0 && typeof sidebarRight.openResource === "function") {
|
|
525
|
+
const address = "dsh-resource://file/session/" + encodeURIComponent(sessionId) + "/" + String(path).replace(/\\/g, "/").split("/").map(encodeURIComponent).join("/");
|
|
526
|
+
sidebarRight.openResource(address, { params: { line: Number(startLine) || 1 } });
|
|
527
|
+
} else {
|
|
528
|
+
const workspaces = ctx.get("workspaces");
|
|
529
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
530
|
+
}
|
|
531
|
+
} catch (_) {
|
|
532
|
+
try {
|
|
533
|
+
const workspaces = ctx.get("workspaces");
|
|
534
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
535
|
+
} catch (_) {}
|
|
536
|
+
}
|
|
537
|
+
selectRange(path, startLine, endLine);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
return install(ctx)
|
|
542
|
+
}
|
package/src/client/99-main.js
CHANGED
|
@@ -487,6 +487,16 @@ const GUFA_PLUGIN = {
|
|
|
487
487
|
console.error('[gufa-code] 安装聊天框文件粘贴失败', error)
|
|
488
488
|
}
|
|
489
489
|
|
|
490
|
+
// 文件拖拽 → @引用 / 代码选区复制 → 代码引用 / 引用 chip 点击跳回(原 dsh-drag-ref 能力)
|
|
491
|
+
try {
|
|
492
|
+
if (typeof G.installGufaRefBridge === 'function') {
|
|
493
|
+
const disposeRefBridge = G.installGufaRefBridge(ctx)
|
|
494
|
+
if (typeof disposeRefBridge === 'function') ctx.effect(() => disposeRefBridge, 'gufa-code: explorer + code reference bridge')
|
|
495
|
+
}
|
|
496
|
+
} catch (error) {
|
|
497
|
+
console.error('[gufa-code] 安装引用桥失败', error)
|
|
498
|
+
}
|
|
499
|
+
|
|
490
500
|
// 样式(随插件 context 卸载自动移除)
|
|
491
501
|
ctx.effect(() => G.injectStyles(), 'gufa-code: styles')
|
|
492
502
|
|