Skip to content

Commit 4e731a6

Browse files
authored
Merge pull request #14 from WyattSL/expanded_intellisense
Expanded intellisense
2 parents 116c1e4 + d1473ce commit 4e731a6

File tree

1 file changed

+164
-22
lines changed

1 file changed

+164
-22
lines changed

extension.js

Lines changed: 164 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,11 @@ function activate(context) {
177177

178178
// Add arguments if its a function/method
179179
if(cmdType == 2 || cmdType == 1){
180-
docs.title += "(" + ArgData[type][cmd].map(d => d.name + (d.optional ? "?" : "") + ": " + d.type + (d.type == "Map" || d.type == "List" ? `[${d.subType}]` : "")).join(", ") + ")";
180+
docs.title += "(" + (ArgData[type][cmd] || []).map(d => d.name + (d.optional ? "?" : "") + ": " + d.type + (d.type == "Map" || d.type == "List" ? `[${d.subType}]` : "")).join(", ") + ")";
181181
}
182182

183-
// Add result if set
184-
let resultTypes = ReturnData[type][cmd];
185-
if (resultTypes) docs.title += ": " + resultTypes.map(d => d.type + (d.type == "Map" || d.type == "List" ? `[${d.subType}]` : "")).join(" or ");
183+
// Add result
184+
docs.title += ": " + (ReturnData[type][cmd] || []).map(d => d.type + (d.type == "Map" || d.type == "List" ? `[${d.subType}]` : "")).join(" or ");
186185

187186
// Add info/hover text
188187
docs.description = HoverData[type][cmd] || "";
@@ -191,12 +190,7 @@ function activate(context) {
191190
if (Encryption.includes(c)) docs.description += "\n\n\**This function cannot be used in encryption.*";
192191

193192
// Add examples
194-
let Ex = Examples[type] ? Examples[type][cmd] : null;
195-
let codeExamples = [];
196-
197-
if (Ex) {
198-
codeExamples = Ex;
199-
}
193+
let codeExamples = Examples[type][cmd] || [];
200194

201195
// Return normal text
202196
if(!asMarkdown) return docs.title + "\n\n\n" + docs.description.replace(/<[^>]*>?/gm, '') + "\n\n" + codeExamples.join("\n\n\n");
@@ -317,28 +311,69 @@ function activate(context) {
317311
return CompData;
318312
}
319313
}
320-
return undefined;
321314
}
322315

323-
324-
325316
let compD = vscode.languages.registerCompletionItemProvider('greyscript', {
326317
provideCompletionItems(document,position,token,ccontext) {
327318
if (!vscode.workspace.getConfiguration("greyscript").get("autocomplete")) return;
328-
319+
let out = [];
320+
321+
// Set default options (THIS IS ALSO THE FALLBACK)
322+
let options = {"General": CompData["General"]};
323+
329324
// Get typed word
330325
let range = document.getWordRangeAtPosition(position);
326+
if(!range) {
327+
for (key in options) {
328+
for(c of options[key]){
329+
//console.log("Processing result: " + c);
330+
331+
// Get type of completion item
332+
let type = CompTypes[c] || CompTypes["default"];
333+
334+
// Create completion item
335+
let t = new vscode.CompletionItem(c,type)
336+
337+
// Add hover data to completion item
338+
t.documentation = getHoverData(key, c);
339+
t.commitCharacters = [".", ";"]
340+
341+
// Push completion item to result array
342+
out.push(t);
343+
}
344+
}
345+
return new vscode.CompletionList(out,true);
346+
}
347+
331348
let word = document.getText(range);
349+
if(!word) return;
332350
//console.log(word);
333351

334-
// Set default options (THIS IS ALSO THE FALLBACK)
335-
let options = {"General": CompData["General"]};
352+
let variableOptions = [];
336353

337354
// If there is a . in front of the text check what the previous item accesses
338355
if(range && range.start.character - 2 >= 0 && document.getText(new vscode.Range(new vscode.Position(range.start.line, range.start.character - 1), new vscode.Position(range.start.line, range.start.character))) == "."){
339356
let res = getOptionsBasedOfPriorCommand(document, range);
340357
if(res) options = res;
341358
}
359+
else {
360+
// Get All user defined variables
361+
let linesTillLine = document.getText().split("\n").splice(0, range.start.line)
362+
363+
for(line of linesTillLine.reverse()){
364+
matches = line.match(/\w+(\s|)=/g);
365+
if(matches){
366+
for(match of matches){
367+
variableName = match.replace(/(\s|)=/, "");
368+
if(!variableOptions.some(m => m.name === variableName)) {
369+
let assignment = line.substring(match.index)
370+
assignment = assignment.substring(assignment.indexOf("=") + 1).trim();
371+
variableOptions.push({"name": variableName, "type": (assignment.startsWith("function") ? 2 : 5)});
372+
}
373+
}
374+
}
375+
}
376+
}
342377

343378
let output = {};
344379

@@ -347,13 +382,19 @@ function activate(context) {
347382
let keyOutput = options[key].filter(cmd => cmd.includes(word));
348383
if(keyOutput.length > 0) output[key] = keyOutput;
349384
}
385+
386+
let variablesOutput = [];
387+
// Get autocompletion of variableNames
388+
for(variable of variableOptions){
389+
if(variable.name.includes(word)) variablesOutput.push(variable);
390+
}
391+
350392
//console.log(output);
351393

352394
// Instantiate result array
353-
let out = [];
354395

355396
// Instantiate sort text index
356-
var a = 0;
397+
//var a = 0;
357398

358399
// Go through filtered results
359400
for (key in output) {
@@ -374,10 +415,15 @@ function activate(context) {
374415
out.push(t);
375416

376417
// Increment sort text index
377-
a++
418+
//a++
378419
}
379420
}
380421

422+
// Go through filtered variables
423+
for(variable of variablesOutput){
424+
out.push(new vscode.CompletionItem(variable.name, variable.type));
425+
}
426+
381427
//console.log("AutoCompletion result:");
382428
//console.log(out);
383429

@@ -386,7 +432,103 @@ function activate(context) {
386432
}
387433
});
388434

389-
if (vscode.workspace.getConfiguration("greyscript").get("autocomplete")) context.subscriptions.push(compD)
435+
function processFunctionParameter(p) {
436+
if(p.length == 0) return "";
437+
438+
// Parse the user defined function parameters
439+
optionalParam = p.match(/\w+(\s|)=(\s|)/);
440+
if(optionalParam){
441+
let value = p.substring(optionalParam[0].length);
442+
let name = optionalParam[0].replace(/(\s|)=(\s|)/, "");
443+
444+
if(value == "true" || value == "false") return name + ": Bool";
445+
else if(!isNaN(value)) return name + ": Number";
446+
else if(value.startsWith("\"")) return name + ": String";
447+
else if(value.startsWith("[")) return name + ": List";
448+
else if(value.startsWith("{")) return name + ": Map";
449+
else return name + ": any";
450+
}
451+
else return p.trim() + ": any"
452+
}
453+
454+
if (vscode.workspace.getConfiguration("greyscript").get("autocomplete")) {
455+
context.subscriptions.push(compD)
456+
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider("greyscript", {
457+
provideSignatureHelp(document, position, token, ctx) {
458+
// Check if current line is not a function creation
459+
let re = RegExp("(\\s|)=(\\s|)function");
460+
let curLine = document.lineAt(position.line);
461+
if((ctx.triggerCharacter == "(" && curLine.text.match(re)) || curLine.text.lastIndexOf("(") < 1) return;
462+
463+
// Get the function being called
464+
let range = document.getWordRangeAtPosition(new vscode.Position(position.line, curLine.text.lastIndexOf("(") - 1));
465+
let word = document.getText(range);
466+
467+
// Create default signature help
468+
let t = new vscode.SignatureHelp();
469+
if(curLine.text.match(/(?<=\()(.*?)(?=\))/)) t.activeParameter = curLine.text.match(/(?<=\()(.*?)(?=\))/)[0].split(",").length - 1;
470+
t.signatures = [];
471+
t.activeSignature = 0;
472+
473+
// Get all the possible signatures from comp data
474+
for(key in CompData){
475+
if(CompData[key].includes(word)) {
476+
let cmdType = CompTypes[word] || CompTypes["default"];
477+
if(cmdType != 2 && cmdType != 1) continue;
478+
479+
args = (ArgData[key][word] || []).map(d => d.name + (d.optional ? "?" : "") + ": " + d.type + (d.type == "Map" || d.type == "List" ? `[${d.subType}]` : "")).join(", ")
480+
results = ": " + (ReturnData[key][word] || []).map(d => d.type + (d.type == "Map" || d.type == "List" ? `[${d.subType}]` : "")).join(" or ")
481+
482+
let info = new vscode.SignatureInformation(key + "." + word + "(" + args + ")" + results);
483+
info.parameters = [];
484+
485+
for(param of args.split(",")){
486+
let p = new vscode.ParameterInformation(param.trim(), "");
487+
if(param.trim().length > 0) info.parameters.push(p);
488+
}
489+
490+
t.signatures.push(info);
491+
}
492+
}
493+
494+
// Get all lines till this line
495+
let linesTillLine = document.getText().split("\n").splice(0, range.start.line)
496+
re = RegExp(word + "(\\s|)=(\\s|)function");
497+
498+
// Get last defined user function using this word
499+
let func = null;
500+
for(line of linesTillLine.reverse()){
501+
matches = line.match(re);
502+
if(matches){
503+
func = line;
504+
break;
505+
}
506+
}
507+
508+
// If no user defined function is found return the current signatures
509+
if(!func) return t;
510+
511+
// Parse the signature information
512+
let params = func.match(/(?<=\()(.*?)(?=\))/)[0];
513+
let info = new vscode.SignatureInformation(word + "(" + params.split(",").map(p => processFunctionParameter(p)).join(", ") + "): any");
514+
info.parameters = [];
515+
516+
// Go through all parameters and register them
517+
for(param of params.split(",")){
518+
let p = param.trim();
519+
let processed = processFunctionParameter(p)
520+
let pInfo = new vscode.ParameterInformation(processed);
521+
if(p.length > 0) info.parameters.push(pInfo);
522+
}
523+
524+
// Push the user defined function as a signature
525+
t.signatures.push(info);
526+
527+
// Return all found signatures
528+
return t;
529+
}
530+
}, [",", "("]))
531+
}
390532

391533
function hexToRgb(hex) {
392534
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(hex);
@@ -397,7 +539,7 @@ function activate(context) {
397539
} : null;
398540
}
399541

400-
function RGBToHex(r,g,b) {
542+
function rgbToHex(r,g,b) {
401543
r = (r*255).toString(16);
402544
g = (g*255).toString(16);
403545
b = (b*255).toString(16);
@@ -484,7 +626,7 @@ function activate(context) {
484626
return out;
485627
},
486628
provideColorPresentations(color, ctx, token){
487-
let hex = RGBToHex(color.red, color.green, color.blue);
629+
let hex = rgbToHex(color.red, color.green, color.blue);
488630
ctx.range = new vscode.Range(ctx.range.start, new vscode.Position(ctx.range.end.line, ctx.range.start.character + hex.length))
489631
return [vscode.ColorPresentation(hex)]
490632
}

0 commit comments

Comments
 (0)