forked from damien122/Autoit-Visual-Studio-Extension
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathai_definition.js
91 lines (74 loc) · 3.38 KB
/
ai_definition.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
import { languages, Location, Position, Uri } from 'vscode';
import { AUTOIT_MODE, getIncludePath, getIncludeText, getIncludeScripts } from './util';
const AutoItDefinitionProvider = {
/**
* Finds the definition of a word in a document and returns its location.
* @param {TextDocument} document - The document in which to search for the word definition.
* @param {Position} position - The position of the word for which to find the definition.
* @returns {Location|null} - The location of the word definition, or null if not found.
*/
provideDefinition(document, position) {
const lookupRange = document.getWordRangeAtPosition(position);
const lookupText = document.getText(lookupRange);
const documentText = document.getText();
const definitionRegex = this.determineRegex(lookupText);
let match = documentText.match(definitionRegex);
if (match) {
return new Location(document.uri, document.positionAt(match.index + (match[1] || '').length));
}
// If nothing was found, search include files
match = this.findDefinitionInIncludeFiles(documentText, definitionRegex, document);
if (match && match.found) {
const { scriptPath } = match;
const scriptContentBeforeMatch = match.scriptContent
.slice(0, match.found.index + (match[1] || '').length)
.split('\n');
const matchLine = scriptContentBeforeMatch.length - 1;
const matchCharacterIndex =
scriptContentBeforeMatch[scriptContentBeforeMatch.length - 1].length;
return new Location(Uri.file(scriptPath), new Position(matchLine, matchCharacterIndex));
}
return null;
},
/**
* Determines the regex for a given lookup string.
* @param {string} lookup - The lookup string.
* @returns {RegExp} The regex for the lookup string.
*/
determineRegex(lookup) {
const variableRegex = /(?<![;].*)(?<!(?:#cs|#comments-start).*)((?:Local|Global|Const)\s*)?@(?:\[[\w\d\\$]+\])?\s*=?.*(?![^#]*(#ce|#comments-end))/;
if (lookup.startsWith('$')) {
return new RegExp(variableRegex.source.replace('@', `\\${lookup}\\b`), 'i');
}
return new RegExp(
`(?<![;].*)(?<!(?:#cs|#comments-start).*)(Func\\s+)${lookup}\\s*\\((?![^#]*(#ce|#comments-end))`,
);
},
/**
* Searches the included scripts in a document for a definition matching a regular expression.
* @param {string} docText - The text of the document.
* @param {RegExp} defRegex - The regular expression to search for.
* @param {TextDocument} document - The document being searched.
* @returns {object|null} - An object containing information about the found definition, or null if not found.
*/
findDefinitionInIncludeFiles(docText, defRegex, document) {
const scriptsToSearch = [];
let returnObject = false;
getIncludeScripts(document, docText, scriptsToSearch);
const searchScript = (script) => {
const scriptPath = getIncludePath(script, document);
const scriptContent = getIncludeText(scriptPath);
const found = scriptContent.match(defRegex);
if (returnObject) return { scriptPath, scriptContent, found };
return found;
};
const match = scriptsToSearch.find(searchScript);
if (match) {
returnObject = true;
return searchScript(match);
}
return null;
},
};
const defProvider = languages.registerDefinitionProvider(AUTOIT_MODE, AutoItDefinitionProvider);
export default defProvider;