Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/editor/tools/yaml/yaml.js
12923 views
1
(() => {
2
// yaml-intelligence.js
3
var mainPath = "";
4
function setMainPath(path) {
5
mainPath = path;
6
}
7
function getLocalPath(filename) {
8
const result = new URL(mainPath);
9
result.pathname = [...result.pathname.split("/").slice(0, -1), filename].join(
10
"/"
11
);
12
return result.toString();
13
}
14
var InternalError = class extends Error {
15
constructor(message, printName = true, printStack = true) {
16
super(message);
17
this.name = "Internal Error";
18
this.printName = printName;
19
this.printStack = printStack;
20
}
21
printName;
22
printStack;
23
};
24
function clientStubs(calls, worker) {
25
let callId = 0;
26
const nextId = () => callId++;
27
const result = {};
28
const promises = {};
29
for (const callName of calls) {
30
result[callName] = (...args) => {
31
const thisId = nextId();
32
worker.postMessage({
33
callName,
34
// The IDE sends some arrays with funky functions in the
35
// prototype, so the web worker API tries to clone those and
36
// fails. We strip them in a potentially slow way, so we
37
// should watch out for performance here.
38
args: JSON.parse(JSON.stringify(args)),
39
id: thisId
40
});
41
return new Promise((resolve, reject) => {
42
promises[thisId] = { resolve, reject };
43
});
44
};
45
}
46
worker.onmessage = function(e) {
47
const { result: result2, exception, id } = e.data;
48
if (promises[id] === void 0) {
49
throw new InternalError(`bad call id ${id} in web worker RPC`);
50
}
51
const { resolve, reject } = promises[id];
52
delete promises[id];
53
if (result2) {
54
resolve(result2);
55
} else if (exception) {
56
reject(exception);
57
}
58
};
59
return result;
60
}
61
var stubs;
62
function ensureStubs(path) {
63
if (stubs) {
64
return;
65
}
66
setMainPath(path);
67
const worker = new Worker(getLocalPath("web-worker.js"));
68
stubs = clientStubs(["getCompletions", "getLint"], worker);
69
}
70
var QuartoYamlEditorTools = {
71
// helpers to facilitate repro'ing in the browser
72
// getAutomation: function (
73
// params: { context: YamlIntelligenceContext; kind: AutomationKind },
74
// ) {
75
// const {
76
// context,
77
// kind,
78
// } = params;
79
// return getAutomation(kind, context);
80
// },
81
// exportSmokeTest,
82
// entry points required by the IDE
83
getCompletions: async function(context, path) {
84
ensureStubs(path);
85
return await stubs["getCompletions"](context, path);
86
},
87
getLint: async function(context, path) {
88
ensureStubs(path);
89
return await stubs["getLint"](context, path);
90
}
91
};
92
93
// automation.js
94
window.QuartoYamlEditorTools = QuartoYamlEditorTools;
95
})();
96
97