forked from damien122/Autoit-Visual-Studio-Extension
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathai_signature.js
262 lines (231 loc) · 7.94 KB
/
ai_signature.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import {
languages,
SignatureHelp,
SignatureInformation,
ParameterInformation,
MarkdownString,
Hover,
} from 'vscode';
import {
includePattern,
findFilepath,
libraryIncludePattern,
getIncludeData,
AUTOIT_MODE,
buildFunctionSignature,
functionDefinitionRegex,
} from './util';
import defaultSigs from './signatures';
import DEFAULT_UDFS from './constants';
let currentIncludeFiles = [];
let includes = {};
/**
* Reduces a partial line of code to the current Function for parsing
* @param {string} code The line of code
*/
function getParsableCode(code) {
const reducedCode = code
.replace(/\w+\([^()]*\)/g, '')
.replace(/"[^"]*"/g, '')
.replace(/'[^']*'/g, '') // Remove double/single quote sets
.replace(/"[^"]*(?=$)/g, '') // Remove double quote and text at end of line
.replace(/'[^']*(?=$)/g, '') // Remove single quote and text at end of line
.replace(/\([^()]*\)/g, '') // Remove paren sets
.replace(/\({2,}/g, '('); // Reduce multiple open parens
return reducedCode;
}
/**
* Parses the current function calling for SignatureHelp from a string of AutoIt code.
* Gets the second-to-last part (right before the last open parenthesis)
* and extracts the function name
*
* @param {string} code - The AutoIt code to parse.
* @returns {?string} - The name of the current function, or null if it couldn't be parsed.
*/
function getCurrentFunction(code) {
const functionCallParts = code.split('(');
if (functionCallParts.length <= 1) {
return null;
}
const functionCallPart = functionCallParts[functionCallParts.length - 2].match(/(.*)\b(\w+)/);
if (functionCallPart) {
return functionCallPart[2];
}
return null;
}
function countCommas(code) {
// Find the position of the closest/last open paren
const openParen = code.lastIndexOf('(');
// Count non-string commas in text following open paren
let commas = code.slice(openParen).match(/(?!\B["'][^"']*),(?![^"']*['"]\B)/g);
if (commas === null) {
commas = 0;
} else {
commas = commas.length;
}
return commas;
}
function getCallInfo(doc, pos) {
// Acquire the text up the point where the current cursor/paren/comma is at
const codeAtPosition = doc.lineAt(pos.line).text.substring(0, pos.character);
const cleanCode = getParsableCode(codeAtPosition);
return {
func: getCurrentFunction(cleanCode),
commas: countCommas(cleanCode),
};
}
function arraysMatch(arr1, arr2) {
if (arr1.length === arr2.length && arr1.some(v => arr2.indexOf(v) <= 0)) {
return true;
}
return false;
}
/**
* Retrieves the includes from the given text.
*
* @param {string} text - The text to search for includes.
* @returns {string[]} An array of includes found in the text.
*/
function getIncludesFromText(text) {
const includesCheck = [];
let pattern = includePattern.exec(text);
while (pattern) {
includesCheck.push(pattern[1]);
pattern = includePattern.exec(text);
}
return includesCheck;
}
/**
* Retrieves the library includes from the given text.
*
* @param {string} text - The text to search for library includes.
* @returns {RegExpMatchArray[]} An array of library includes found in the text.
*/
function getLibraryIncludesFromText(text) {
const libraryIncludes = [];
let pattern = libraryIncludePattern.exec(text);
while (pattern) {
libraryIncludes.push(pattern);
pattern = libraryIncludePattern.exec(text);
}
return libraryIncludes;
}
/**
* Retrieves the includes and library includes from the given document.
* Determines whether includes should be re-parsed or not.
*
* @param {Object} doc - The document to search for includes.
* @returns {Object} An object containing the includes found in the document.
*/
function getIncludedFunctionSignatures(doc) {
const text = doc.getText();
const includesCheck = getIncludesFromText(text);
const libraryIncludes = getLibraryIncludesFromText(text);
if (!arraysMatch(includesCheck, currentIncludeFiles)) {
includes = {};
includesCheck.forEach(script => {
const newIncludes = getIncludeData(script, doc);
Object.assign(includes, newIncludes);
});
currentIncludeFiles = includesCheck;
}
libraryIncludes.forEach(pattern => {
const filename = pattern[1].replace('.au3', '');
if (DEFAULT_UDFS.indexOf(filename) === -1) {
const fullPath = findFilepath(pattern[1]);
if (fullPath) {
const newData = getIncludeData(fullPath, doc);
Object.assign(includes, newData);
}
}
});
return includes;
}
/**
* Returns an object of AutoIt functions found within the current AutoIt script
* @param {vscode.TextDocument} doc The TextDocument object representing the AutoIt script
* @returns {Object} Object containing SignatureInformation objects
*/
function getLocalFunctionSignatures(doc) {
const text = doc.getText();
const functions = {};
let functionMatch = functionDefinitionRegex.exec(text);
while (functionMatch) {
const functionData = buildFunctionSignature(functionMatch, text, doc.fileName);
functions[functionData.functionName] = functionData.functionObject;
functionMatch = functionDefinitionRegex.exec(text);
}
return functions;
}
/**
* Creates a SignatureInformation object from a given signature.
* @param {Object} foundSig - The signature to create the SignatureInformation object from.
* @returns {SignatureInformation} The created SignatureInformation object.
*/
function createSignatureInfo(foundSig) {
const signatureInfo = new SignatureInformation(
foundSig.label,
new MarkdownString(`##### ${foundSig.documentation}`),
);
signatureInfo.parameters = Object.keys(foundSig.params).map(
key =>
new ParameterInformation(
foundSig.params[key].label,
new MarkdownString(foundSig.params[key].documentation),
),
);
return signatureInfo;
}
/**
* Creates a Hover object for the user created functions.
*
* @param {TextDocument} document - The TextDocument object representing the AutoIt script
* @param {Position} position - The position of the cursor when the function was called
* @returns {Hover | null} - A Hover object containing the hover info, or null if no info found.
*/
export const signatureHoverProvider = languages.registerHoverProvider(AUTOIT_MODE, {
provideHover(document, position) {
const hoveredPosition = document.getWordRangeAtPosition(position);
if (!hoveredPosition) return null;
const hoveredWord = document.getText(hoveredPosition);
const allSignatures = {
...getIncludedFunctionSignatures(document),
...getLocalFunctionSignatures(document),
};
const matchedSignature = allSignatures[hoveredWord];
if (!matchedSignature || !matchedSignature.label) return null;
const documentationLines = matchedSignature.documentation.split('\r');
documentationLines[1] = `##### ${documentationLines[1]}`;
const hoverText = [...documentationLines, `\`\`\`\r${matchedSignature.label}\r\`\`\``];
return new Hover(hoverText);
},
});
export default languages.registerSignatureHelpProvider(
AUTOIT_MODE,
{
/**
* Provides signature help for a given document and position.
* @param {TextDocument} document - The document to provide signature help for.
* @param {Position} position - The position in the document to provide signature help for.
* @returns {SignatureHelp | null} The signature help or null if no help is available.
*/
provideSignatureHelp(document, position) {
const caller = getCallInfo(document, position);
if (!caller.func) return null;
const allSignatures = {
...defaultSigs,
...getIncludedFunctionSignatures(document),
...getLocalFunctionSignatures(document),
};
const matchedSignature = allSignatures[caller.func];
if (!matchedSignature) return null;
const result = new SignatureHelp();
result.signatures = [createSignatureInfo(matchedSignature)];
result.activeSignature = 0;
result.activeParameter = caller.commas;
return result;
},
},
'(',
',',
);