forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsGlobalCompletion.ts
More file actions
67 lines (58 loc) · 1.88 KB
/
jsGlobalCompletion.ts
File metadata and controls
67 lines (58 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { syntaxTree } from '@codemirror/language';
function getAllProperties(obj:Object) {
const props = new Set();
let current = obj;
while (current && current !== Object.prototype) {
// Get all properties from current level
Object.getOwnPropertyNames(current).forEach(prop => {
props.add(prop);
});
// Move up the prototype chain
current = Object.getPrototypeOf(current);
}
return Array.from(props);
}
// Usage
const allDocumentProps = getAllProperties(document);
const completePropertyAfter = ['PropertyName', '.', '?.'];
const dontCompleteIn = [
'TemplateString',
'LineComment',
'BlockComment',
'VariableDefinition',
'PropertyDefinition',
];
export default function jsCompletionsFromGlobalScope(context: any) {
let nodeBefore = syntaxTree(context.state).resolveInner(context.pos, -1);
if (
completePropertyAfter.includes(nodeBefore.name) &&
nodeBefore.parent?.name == 'MemberExpression'
) {
let object = nodeBefore.parent.getChild('Expression');
if (object?.name == 'VariableName') {
let from = /\./.test(nodeBefore.name) ? nodeBefore.to : nodeBefore.from;
let variableName = context.state.sliceDoc(object.from, object.to);
if (typeof window[variableName] == 'object')
return completeProperties(from, window[variableName]);
}
} else if (nodeBefore.name == 'VariableName') {
return completeProperties(nodeBefore.from, window);
} else if (context.explicit && !dontCompleteIn.includes(nodeBefore.name)) {
return completeProperties(context.pos, window);
}
return null;
}
function completeProperties(from: any, object: any) {
let options = [];
for (let name of getAllProperties(object)) {
options.push({
label: name,
type: typeof object[name as string] == 'function' ? 'function' : 'variable',
});
}
return {
from,
options,
validFor: /^[\w$]*$/,
};
}