-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcontextAwareHinter.js
More file actions
200 lines (165 loc) · 5.13 KB
/
contextAwareHinter.js
File metadata and controls
200 lines (165 loc) · 5.13 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import getContext from './getContext';
import p5CodeAstAnalyzer from './p5CodeAstAnalyzer';
import classMap from './p5-instance-methods-and-creators.json';
import * as hints from './p5-hinter';
const scopeMap = require('./p5-scope-function-access-map.json');
function getExpressionBeforeCursor(cm) {
const cursor = cm.getCursor();
const line = cm.getLine(cursor.line);
const uptoCursor = line.slice(0, cursor.ch);
const match = uptoCursor.match(
/([a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*)\.(?:[a-zA-Z_$][\w$]*)?$/
);
return match ? match[1] : null;
}
export default function contextAwareHinter(cm, options = {}) {
const {
variableToP5ClassMap = {},
scopeToDeclaredVarsMap = {},
userDefinedFunctionMetadata = {},
userDefinedClassMetadata = {}
} = p5CodeAstAnalyzer(cm) || {};
const { hinter } = options;
if (!hinter || typeof hinter.search !== 'function') {
return [];
}
const baseExpression = getExpressionBeforeCursor(cm);
if (baseExpression) {
const className = variableToP5ClassMap[baseExpression];
const userClassEntry = Object.values(userDefinedClassMetadata).find(
(cls) => cls.initializer === baseExpression
);
let methods = [];
if (userClassEntry?.methods) {
const { methods: userMethods } = userClassEntry;
methods = userMethods;
} else if (className && classMap[className]?.methods) {
const { methods: classMethods } = classMap[className];
methods = classMethods;
} else {
return [];
}
const cursor = cm.getCursor();
const lineText = cm.getLine(cursor.line);
const dotMatch = lineText
.slice(0, cursor.ch)
.match(/\.([a-zA-Z_$][\w$]*)?$/);
let from = cursor;
if (dotMatch) {
const fullMatch = dotMatch[0];
const methodStart = cursor.ch - fullMatch.length + 1;
from = { line: cursor.line, ch: methodStart };
} else {
from = cursor;
}
const to = { line: cursor.line, ch: cursor.ch };
const typed = dotMatch?.[1]?.toLowerCase() || '';
const methodHints = methods
.filter((method) => method.toLowerCase().startsWith(typed))
.map((method) => ({
item: {
text: method,
type: 'fun',
isMethod: true
},
displayText: method,
from,
to
}));
return methodHints;
}
const { line, ch } = cm.getCursor();
const { string } = cm.getTokenAt({ line, ch });
const currentWord = string.trim();
const currentContext = getContext(cm);
let allHints;
if (!currentWord) {
allHints = hints.p5Hinter;
allHints = allHints.map((h) => ({
item: h
}));
} else {
allHints = hinter.search(currentWord);
}
// const whitelist = scopeMap[currentContext]?.whitelist || [];
const blacklist = scopeMap[currentContext]?.blacklist || [];
const lowerCurrentWord = currentWord.toLowerCase();
function isInScope(varName) {
return Object.entries(scopeToDeclaredVarsMap).some(
([scope, vars]) =>
varName in vars && (scope === 'global' || scope === currentContext)
);
}
const allVarNames = Array.from(
new Set(
Object.values(scopeToDeclaredVarsMap)
.map((s) => Object.keys(s))
.flat()
.filter((name) => typeof name === 'string')
)
);
const varHints = allVarNames
.filter(
(varName) =>
varName.toLowerCase().startsWith(lowerCurrentWord) && isInScope(varName)
)
.map((varName) => {
const isFunc =
scopeToDeclaredVarsMap[currentContext]?.[varName] === 'fun' ||
(!scopeToDeclaredVarsMap[currentContext]?.[varName] &&
scopeToDeclaredVarsMap.global?.[varName] === 'fun');
const baseItem = isFunc
? { ...userDefinedFunctionMetadata[varName] }
: {
text: varName,
type: 'var',
params: [],
p5: false
};
return {
item: baseItem,
isBlacklisted: blacklist.includes(varName)
};
});
const filteredHints = allHints
.filter(
(h) =>
h &&
h.item &&
typeof h.item.text === 'string' &&
h.item.text.toLowerCase().startsWith(lowerCurrentWord)
)
.map((hint) => {
const name = hint.item?.text || '';
const isBlacklisted = blacklist.includes(name);
return {
...hint,
isBlacklisted
};
});
const combinedHints = [...varHints, ...filteredHints];
const typePriority = {
fun: 0,
var: 1,
keyword: 2,
other: 3
};
const sorted = combinedHints.sort((a, b) => {
const nameA = a.item?.text || '';
const nameB = b.item?.text || '';
const typeA = a.item?.type || 'other';
const typeB = b.item?.type || 'other';
const isBlacklistedA = a.isBlacklisted ? 1 : 0;
const isBlacklistedB = b.isBlacklisted ? 1 : 0;
const typeScoreA = typePriority[typeA] ?? typePriority.other;
const typeScoreB = typePriority[typeB] ?? typePriority.other;
if (isBlacklistedA !== isBlacklistedB) {
return isBlacklistedA - isBlacklistedB;
}
if (typeScoreA !== typeScoreB) {
return typeScoreA - typeScoreB;
}
return nameA.localeCompare(nameB);
});
return sorted;
}