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
|
@@ -0,0 +1,533 @@
|
|
|
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 path = decodeURIComponent(rawPath.replace(/^\//, ""));
|
|
219
|
+
if (path === "") return null;
|
|
220
|
+
const startLine = Math.min(selection.startLineNumber, selection.endLineNumber);
|
|
221
|
+
const endLine = Math.max(selection.startLineNumber, selection.endLineNumber);
|
|
222
|
+
const startColumn = Math.min(selection.startColumn, selection.endColumn);
|
|
223
|
+
const endColumn = Math.max(selection.startColumn, selection.endColumn);
|
|
224
|
+
/* 单行:从行首选到行尾才算引用;多行:任意列都算引用。 */
|
|
225
|
+
const fullLines = startLine !== endLine
|
|
226
|
+
? true
|
|
227
|
+
: startColumn === 1 && endColumn === model.getLineMaxColumn(endLine);
|
|
228
|
+
return { path, startLine, endLine, fullLines, text };
|
|
229
|
+
} catch (_) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function install(ctx) {
|
|
235
|
+
loadRefs();
|
|
236
|
+
const onDragStart = (event) => {
|
|
237
|
+
const target = event.target;
|
|
238
|
+
const row = target instanceof Element ? target.closest(ROW) : null;
|
|
239
|
+
if (row === null || event.dataTransfer === null) return;
|
|
240
|
+
const path = row.getAttribute("data-edrv-path");
|
|
241
|
+
if (path === null || path === "") return;
|
|
242
|
+
const chevron = row.querySelector(".edrv-tree-chev");
|
|
243
|
+
const isDir = chevron !== null && (chevron.textContent || "").trim() !== "";
|
|
244
|
+
event.dataTransfer.effectAllowed = "copy";
|
|
245
|
+
event.dataTransfer.setData(DRAG_TYPE, JSON.stringify({ path, isDir }));
|
|
246
|
+
event.dataTransfer.setData("text/plain", path);
|
|
247
|
+
};
|
|
248
|
+
const onDragOver = (event) => {
|
|
249
|
+
if (!isOurs(event.dataTransfer) || dropZoneOf(event.target) === null) return;
|
|
250
|
+
event.preventDefault();
|
|
251
|
+
if (event.dataTransfer !== null) event.dataTransfer.dropEffect = "copy";
|
|
252
|
+
};
|
|
253
|
+
const onDrop = (event) => {
|
|
254
|
+
if (!isOurs(event.dataTransfer)) return;
|
|
255
|
+
if (dropZoneOf(event.target) === null) {
|
|
256
|
+
toast("松手位置不在输入框内,未插入", "err");
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
event.preventDefault();
|
|
260
|
+
event.stopPropagation();
|
|
261
|
+
const payload = payloadOf(event.dataTransfer);
|
|
262
|
+
if (payload === null) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const result = insertRef(ctx, { path: payload.path, isDir: payload.isDir === true });
|
|
266
|
+
if (result.ok) {
|
|
267
|
+
insertedRefs.set(labelOf(payload.path), { path: payload.path, startLine: void 0, endLine: void 0 });
|
|
268
|
+
saveRefs();
|
|
269
|
+
}
|
|
270
|
+
if (result.ok) toast("已插入引用:" + labelOf(payload.path), "ok");
|
|
271
|
+
else toast("插入失败:" + result.reason, "err");
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
/** Copying a selection inside Monaco records the path + line range. */
|
|
275
|
+
const onCopy = (event) => {
|
|
276
|
+
const target = event.target;
|
|
277
|
+
if (!(target instanceof Element) || target.closest(".monaco-editor") === null) return;
|
|
278
|
+
const editor = focusedEditor();
|
|
279
|
+
if (editor === null) return;
|
|
280
|
+
const info = editorSelectionOf(editor);
|
|
281
|
+
if (info === null) return;
|
|
282
|
+
/* 引用判定:跨行选区一律算引用(哪怕首行/末行只选中半行);
|
|
283
|
+
单行必须完整整行选中,否则按普通文本复制。 */
|
|
284
|
+
const multiLine = info.startLine !== info.endLine;
|
|
285
|
+
if (!multiLine && info.fullLines !== true) return;
|
|
286
|
+
try {
|
|
287
|
+
event.preventDefault();
|
|
288
|
+
event.stopPropagation();
|
|
289
|
+
if (event.clipboardData !== null) {
|
|
290
|
+
event.clipboardData.setData("text/plain", info.text);
|
|
291
|
+
event.clipboardData.setData(CODE_REF_TYPE, JSON.stringify(info));
|
|
292
|
+
}
|
|
293
|
+
} catch (_) {}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const onPaste = (event) => {
|
|
297
|
+
if (!hasCodeRef(event.clipboardData)) return;
|
|
298
|
+
if (dropZoneOf(event.target) === null) return;
|
|
299
|
+
const info = codeRefOf(event.clipboardData);
|
|
300
|
+
if (info === null) return;
|
|
301
|
+
event.preventDefault();
|
|
302
|
+
event.stopPropagation();
|
|
303
|
+
const result = insertRef(ctx, { path: info.path, startLine: info.startLine, endLine: info.endLine, text: info.text });
|
|
304
|
+
if (result.ok) {
|
|
305
|
+
insertedRefs.set(labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), { path: info.path, startLine: info.startLine, endLine: info.endLine });
|
|
306
|
+
saveRefs();
|
|
307
|
+
}
|
|
308
|
+
if (result.ok) toast("已插入代码引用:" + labelOf(info.path) + " " + rangeText(info.startLine, info.endLine), "ok");
|
|
309
|
+
else toast("插入失败:" + result.reason, "err");
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Clicking a reference chip re-opens the target and selects its line range.
|
|
314
|
+
* Only real chips are considered: a title chip, a Lexical decorator inside
|
|
315
|
+
* the composer, or an inline leaf whose text matches a known chip label.
|
|
316
|
+
* Message containers and the editable root never match.
|
|
317
|
+
*/
|
|
318
|
+
const onReferenceClick = (event) => {
|
|
319
|
+
const target = event.target;
|
|
320
|
+
if (!(target instanceof Element)) return;
|
|
321
|
+
let info = null;
|
|
322
|
+
|
|
323
|
+
/* 1) chip carrying a title (`@path L12-34` or `@path`). */
|
|
324
|
+
const titleChip = target.closest("[title^=\"@\"]");
|
|
325
|
+
if (titleChip !== null) {
|
|
326
|
+
const chipText = (titleChip.textContent || "").trim();
|
|
327
|
+
if (chipText.length > 80) return;
|
|
328
|
+
const title = (titleChip.getAttribute("title") || "").trim();
|
|
329
|
+
if (/^@(?:"[^"\n]+"|\S+)(?:\s+L\d+(?:-\d+)?)?$/u.test(title)) info = parseRefText(title);
|
|
330
|
+
if (info === null && chipText !== "") info = insertedRefs.get(chipText) || null;
|
|
331
|
+
if (info !== null && (info.startLine === void 0 || info.endLine === void 0)) {
|
|
332
|
+
const sibling = titleChip.nextSibling;
|
|
333
|
+
const tail = sibling !== null && sibling.nodeType === 3 ? sibling.textContent : "";
|
|
334
|
+
const range = /^\s*L(\d+)(?:-(\d+))?/u.exec(String(tail || ""));
|
|
335
|
+
if (range) {
|
|
336
|
+
info.startLine = Number(range[1]);
|
|
337
|
+
info.endLine = range[2] === void 0 ? info.startLine : Number(range[2]);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
} else {
|
|
341
|
+
/* 2) Lexical decorator chip inside the composer (no title attribute). */
|
|
342
|
+
const decorator = target.closest("[data-lexical-decorator=\"true\"], [contenteditable=\"false\"]");
|
|
343
|
+
if (decorator !== null && decorator.closest("[data-conversation-region=\"composer\"]") !== null) {
|
|
344
|
+
const label = (decorator.textContent || "").trim();
|
|
345
|
+
if (label === "" || label.length > 80) return;
|
|
346
|
+
info = insertedRefs.get(label) || null;
|
|
347
|
+
} else {
|
|
348
|
+
/* 3) inline leaf fallback (never the editable root or a block container). */
|
|
349
|
+
if (target.getAttribute && target.getAttribute("contenteditable") === "true") return;
|
|
350
|
+
try {
|
|
351
|
+
const display = getComputedStyle(target).display;
|
|
352
|
+
if (display !== "inline" && display !== "inline-flex" && display !== "inline-block") return;
|
|
353
|
+
} catch (_) { return; }
|
|
354
|
+
if (target.childElementCount > 0) return;
|
|
355
|
+
const text = (target.textContent || "").trim();
|
|
356
|
+
if (text === "" || text.length > 80) return;
|
|
357
|
+
info = insertedRefs.get(text) || null;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (info === null || info.path === void 0 || info.path === "") return;
|
|
362
|
+
if (info.startLine === void 0 || info.endLine === void 0) return;
|
|
363
|
+
event.preventDefault();
|
|
364
|
+
event.stopPropagation();
|
|
365
|
+
openAtRange(ctx, info.path, info.startLine, info.endLine);
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const mark = (root) => {
|
|
369
|
+
const rows = root && typeof root.querySelectorAll === "function" ? root.querySelectorAll(ROW) : [];
|
|
370
|
+
for (const row of rows) {
|
|
371
|
+
if (row.getAttribute("draggable") !== "true") row.setAttribute("draggable", "true");
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
document.addEventListener("dragstart", onDragStart, true);
|
|
376
|
+
document.addEventListener("dragover", onDragOver, true);
|
|
377
|
+
document.addEventListener("drop", onDrop, true);
|
|
378
|
+
document.addEventListener("click", onReferenceClick, true);
|
|
379
|
+
document.addEventListener("copy", onCopy, true);
|
|
380
|
+
document.addEventListener("paste", onPaste, true);
|
|
381
|
+
mark(document);
|
|
382
|
+
const observer = new MutationObserver((mutations) => {
|
|
383
|
+
for (const mutation of mutations) {
|
|
384
|
+
for (const node of mutation.addedNodes) {
|
|
385
|
+
if (!(node instanceof Element)) continue;
|
|
386
|
+
if (node.matches(ROW)) node.setAttribute("draggable", "true");
|
|
387
|
+
mark(node);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
392
|
+
|
|
393
|
+
return () => {
|
|
394
|
+
document.removeEventListener("dragstart", onDragStart, true);
|
|
395
|
+
document.removeEventListener("dragover", onDragOver, true);
|
|
396
|
+
document.removeEventListener("drop", onDrop, true);
|
|
397
|
+
document.removeEventListener("click", onReferenceClick, true);
|
|
398
|
+
document.removeEventListener("copy", onCopy, true);
|
|
399
|
+
document.removeEventListener("paste", onPaste, true);
|
|
400
|
+
observer.disconnect();
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Inserted references by chip label, for chips that carry no title attribute. */
|
|
405
|
+
const insertedRefs = new Map();
|
|
406
|
+
const REF_STORE_KEY = "gufa-code.refs.v1";
|
|
407
|
+
|
|
408
|
+
/** Restore label → {path,startLine,endLine} from localStorage (drafts survive restarts). */
|
|
409
|
+
function loadRefs() {
|
|
410
|
+
try {
|
|
411
|
+
const raw = localStorage.getItem(REF_STORE_KEY);
|
|
412
|
+
if (raw === null) return;
|
|
413
|
+
const parsed = JSON.parse(raw);
|
|
414
|
+
if (parsed === null || typeof parsed !== "object") return;
|
|
415
|
+
for (const label of Object.keys(parsed)) {
|
|
416
|
+
const info = parsed[label];
|
|
417
|
+
if (info && typeof info.path === "string" && info.path !== "") insertedRefs.set(label, info);
|
|
418
|
+
}
|
|
419
|
+
} catch (_) {}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Persist the label map so chips restored from a draft still resolve after a restart. */
|
|
423
|
+
function saveRefs() {
|
|
424
|
+
try {
|
|
425
|
+
const out = {};
|
|
426
|
+
let count = 0;
|
|
427
|
+
for (const [label, info] of insertedRefs) {
|
|
428
|
+
out[label] = info;
|
|
429
|
+
if (++count >= 300) break;
|
|
430
|
+
}
|
|
431
|
+
localStorage.setItem(REF_STORE_KEY, JSON.stringify(out));
|
|
432
|
+
} catch (_) {}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Parse `@path L12-34` / `@"a b" L12` into { path, startLine, endLine }. */
|
|
436
|
+
function parseRefText(text) {
|
|
437
|
+
const raw = String(text == null ? "" : text).trim();
|
|
438
|
+
if (!raw.startsWith("@")) return null;
|
|
439
|
+
let body = raw.slice(1);
|
|
440
|
+
let path = "";
|
|
441
|
+
if (body.startsWith('"')) {
|
|
442
|
+
const end = body.indexOf('"', 1);
|
|
443
|
+
if (end < 0) return null;
|
|
444
|
+
path = body.slice(1, end);
|
|
445
|
+
body = body.slice(end + 1);
|
|
446
|
+
} else {
|
|
447
|
+
const space = body.indexOf(" ");
|
|
448
|
+
path = space < 0 ? body : body.slice(0, space);
|
|
449
|
+
body = space < 0 ? "" : body.slice(space);
|
|
450
|
+
}
|
|
451
|
+
if (path === "") return null;
|
|
452
|
+
const range = /^\s*L(\d+)(?:-(\d+))?\s*$/u.exec(body);
|
|
453
|
+
if (!range) return { path, startLine: void 0, endLine: void 0 };
|
|
454
|
+
const start = Number(range[1]);
|
|
455
|
+
const end = range[2] === void 0 ? start : Number(range[2]);
|
|
456
|
+
return { path, startLine: start, endLine: end };
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** Find the Monaco model for one workspace-relative path. */
|
|
460
|
+
function modelFor(path) {
|
|
461
|
+
const monaco = window.monaco;
|
|
462
|
+
if (!monaco || !monaco.editor || typeof monaco.editor.getModels !== "function") return null;
|
|
463
|
+
let models = [];
|
|
464
|
+
try { models = monaco.editor.getModels() || []; } catch (_) { return null; }
|
|
465
|
+
const wanted = String(path).replace(/\\/g, "/").replace(/^\.\//, "");
|
|
466
|
+
for (const model of models) {
|
|
467
|
+
const raw = model && model.uri && model.uri.path ? String(model.uri.path) : "";
|
|
468
|
+
const candidate = decodeURIComponent(raw.replace(/^\//, ""));
|
|
469
|
+
if (candidate === wanted || candidate.endsWith("/" + wanted) || wanted.endsWith("/" + candidate)) return model;
|
|
470
|
+
}
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Select the line range once the editor for that file is mounted. */
|
|
475
|
+
function selectRange(path, startLine, endLine) {
|
|
476
|
+
const start = Number(startLine);
|
|
477
|
+
const end = Number(endLine) || start;
|
|
478
|
+
if (!Number.isFinite(start) || start <= 0) return;
|
|
479
|
+
const deadline = Date.now() + 4000;
|
|
480
|
+
const tick = () => {
|
|
481
|
+
const monaco = window.monaco;
|
|
482
|
+
const model = modelFor(path);
|
|
483
|
+
if (monaco && model) {
|
|
484
|
+
let editors = [];
|
|
485
|
+
try { editors = monaco.editor.getEditors() || []; } catch (_) {}
|
|
486
|
+
const editor = editors.find((item) => item.getModel && item.getModel() === model);
|
|
487
|
+
if (editor) {
|
|
488
|
+
try {
|
|
489
|
+
const range = new monaco.Range(start, 1, end, 1);
|
|
490
|
+
if (typeof editor.setSelection === "function") editor.setSelection(range);
|
|
491
|
+
if (typeof editor.revealRangeInCenter === "function") editor.revealRangeInCenter(range);
|
|
492
|
+
else if (typeof editor.revealLineInCenter === "function") editor.revealLineInCenter(start);
|
|
493
|
+
if (typeof editor.focus === "function") editor.focus();
|
|
494
|
+
} catch (_) {}
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (Date.now() < deadline) setTimeout(tick, 120);
|
|
499
|
+
};
|
|
500
|
+
tick();
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** Open a file through the official resource route (falls back to workspaces.openPath). */
|
|
504
|
+
function openAtRange(ctx, path, startLine, endLine) {
|
|
505
|
+
try {
|
|
506
|
+
if (G.bus !== null && G.bus !== undefined && typeof G.bus.emit === "function") {
|
|
507
|
+
G.bus.emit("editor:open-at", { path, line: Number(startLine) || 1, column: 1 });
|
|
508
|
+
selectRange(path, startLine, endLine);
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
} catch (_) {}
|
|
512
|
+
try {
|
|
513
|
+
const sessionId = activeSessionId();
|
|
514
|
+
const sidebarRight = ctx.get("sidebarRight");
|
|
515
|
+
if (sessionId !== void 0 && sidebarRight !== void 0 && typeof sidebarRight.openResource === "function") {
|
|
516
|
+
const address = "dsh-resource://file/session/" + encodeURIComponent(sessionId) + "/" + String(path).replace(/\\/g, "/").split("/").map(encodeURIComponent).join("/");
|
|
517
|
+
sidebarRight.openResource(address, { params: { line: Number(startLine) || 1 } });
|
|
518
|
+
} else {
|
|
519
|
+
const workspaces = ctx.get("workspaces");
|
|
520
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
521
|
+
}
|
|
522
|
+
} catch (_) {
|
|
523
|
+
try {
|
|
524
|
+
const workspaces = ctx.get("workspaces");
|
|
525
|
+
if (workspaces !== void 0 && typeof workspaces.openPath === "function") workspaces.openPath(path);
|
|
526
|
+
} catch (_) {}
|
|
527
|
+
}
|
|
528
|
+
selectRange(path, startLine, endLine);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
return install(ctx)
|
|
533
|
+
}
|
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
|
|