@@ -177,12 +177,11 @@ function activate(context) {
177
177
178
178
// Add arguments if its a function/method
179
179
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 ( ", " ) + ")" ;
181
181
}
182
182
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 " ) ;
186
185
187
186
// Add info/hover text
188
187
docs . description = HoverData [ type ] [ cmd ] || "" ;
@@ -191,12 +190,7 @@ function activate(context) {
191
190
if ( Encryption . includes ( c ) ) docs . description += "\n\n\**This function cannot be used in encryption.*" ;
192
191
193
192
// 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 ] || [ ] ;
200
194
201
195
// Return normal text
202
196
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) {
317
311
return CompData ;
318
312
}
319
313
}
320
- return undefined ;
321
314
}
322
315
323
-
324
-
325
316
let compD = vscode . languages . registerCompletionItemProvider ( 'greyscript' , {
326
317
provideCompletionItems ( document , position , token , ccontext ) {
327
318
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
+
329
324
// Get typed word
330
325
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
+
331
348
let word = document . getText ( range ) ;
349
+ if ( ! word ) return ;
332
350
//console.log(word);
333
351
334
- // Set default options (THIS IS ALSO THE FALLBACK)
335
- let options = { "General" : CompData [ "General" ] } ;
352
+ let variableOptions = [ ] ;
336
353
337
354
// If there is a . in front of the text check what the previous item accesses
338
355
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 ) ) ) == "." ) {
339
356
let res = getOptionsBasedOfPriorCommand ( document , range ) ;
340
357
if ( res ) options = res ;
341
358
}
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
+ }
342
377
343
378
let output = { } ;
344
379
@@ -347,13 +382,19 @@ function activate(context) {
347
382
let keyOutput = options [ key ] . filter ( cmd => cmd . includes ( word ) ) ;
348
383
if ( keyOutput . length > 0 ) output [ key ] = keyOutput ;
349
384
}
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
+
350
392
//console.log(output);
351
393
352
394
// Instantiate result array
353
- let out = [ ] ;
354
395
355
396
// Instantiate sort text index
356
- var a = 0 ;
397
+ // var a = 0;
357
398
358
399
// Go through filtered results
359
400
for ( key in output ) {
@@ -374,10 +415,15 @@ function activate(context) {
374
415
out . push ( t ) ;
375
416
376
417
// Increment sort text index
377
- a ++
418
+ // a++
378
419
}
379
420
}
380
421
422
+ // Go through filtered variables
423
+ for ( variable of variablesOutput ) {
424
+ out . push ( new vscode . CompletionItem ( variable . name , variable . type ) ) ;
425
+ }
426
+
381
427
//console.log("AutoCompletion result:");
382
428
//console.log(out);
383
429
@@ -386,7 +432,103 @@ function activate(context) {
386
432
}
387
433
} ) ;
388
434
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
+ }
390
532
391
533
function hexToRgb ( hex ) {
392
534
var result = / ^ # ? ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) ? $ / i. exec ( hex ) ;
@@ -397,7 +539,7 @@ function activate(context) {
397
539
} : null ;
398
540
}
399
541
400
- function RGBToHex ( r , g , b ) {
542
+ function rgbToHex ( r , g , b ) {
401
543
r = ( r * 255 ) . toString ( 16 ) ;
402
544
g = ( g * 255 ) . toString ( 16 ) ;
403
545
b = ( b * 255 ) . toString ( 16 ) ;
@@ -484,7 +626,7 @@ function activate(context) {
484
626
return out ;
485
627
} ,
486
628
provideColorPresentations ( color , ctx , token ) {
487
- let hex = RGBToHex ( color . red , color . green , color . blue ) ;
629
+ let hex = rgbToHex ( color . red , color . green , color . blue ) ;
488
630
ctx . range = new vscode . Range ( ctx . range . start , new vscode . Position ( ctx . range . end . line , ctx . range . start . character + hex . length ) )
489
631
return [ vscode . ColorPresentation ( hex ) ]
490
632
}
0 commit comments