@@ -36,6 +36,8 @@ public static class ConsoleController
36
36
public static bool EnableAutoIndent { get ; private set ; } = true ;
37
37
public static bool EnableSuggestions { get ; private set ; } = true ;
38
38
39
+ internal static string ScriptsFolder => Path . Combine ( ExplorerCore . Loader . ExplorerFolder , "Scripts" ) ;
40
+
39
41
internal static readonly string [ ] DefaultUsing = new string [ ]
40
42
{
41
43
"System" ,
@@ -56,6 +58,18 @@ public static void Init()
56
58
ResetConsole ( false ) ;
57
59
// ensure the compiler is supported (if this fails then SRE is probably stubbed)
58
60
Evaluator . Compile ( "0 == 0" ) ;
61
+
62
+ if ( ! Directory . Exists ( ScriptsFolder ) )
63
+ Directory . CreateDirectory ( ScriptsFolder ) ;
64
+
65
+ var startupPath = Path . Combine ( ScriptsFolder , "startup.cs" ) ;
66
+ if ( File . Exists ( startupPath ) )
67
+ {
68
+ ExplorerCore . Log ( $ "Executing startup script from '{ startupPath } '...") ;
69
+ var text = File . ReadAllText ( startupPath ) ;
70
+ Input . Text = text ;
71
+ Evaluate ( ) ;
72
+ }
59
73
}
60
74
catch ( Exception ex )
61
75
{
@@ -69,7 +83,7 @@ public static void Init()
69
83
SetupHelpInteraction ( ) ;
70
84
71
85
Panel . OnInputChanged += OnInputChanged ;
72
- Panel . InputScroll . OnScroll += OnInputScrolled ;
86
+ Panel . InputScroller . OnScroll += OnInputScrolled ;
73
87
Panel . OnCompileClicked += Evaluate ;
74
88
Panel . OnResetClicked += ResetConsole ;
75
89
Panel . OnHelpDropdownChanged += HelpSelected ;
@@ -317,7 +331,7 @@ private static void UpdateCaret(out bool caretMoved)
317
331
var charBot = charTop - CSCONSOLE_LINEHEIGHT ;
318
332
319
333
var viewportMin = Input . Rect . rect . height - Input . Rect . anchoredPosition . y - ( Input . Rect . rect . height * 0.5f ) ;
320
- var viewportMax = viewportMin - Panel . InputScroll . ViewportRect . rect . height ;
334
+ var viewportMax = viewportMin - Panel . InputScroller . ViewportRect . rect . height ;
321
335
322
336
float diff = 0f ;
323
337
if ( charTop > viewportMin )
@@ -337,7 +351,7 @@ private static void SetCaretPosition(int caretPosition)
337
351
{
338
352
settingCaretCoroutine = true ;
339
353
Input . Component . readOnly = true ;
340
- RuntimeProvider . Instance . StartCoroutine ( SetAutocompleteCaretCoro ( caretPosition ) ) ;
354
+ RuntimeProvider . Instance . StartCoroutine ( SetCaretCoroutine ( caretPosition ) ) ;
341
355
}
342
356
343
357
internal static PropertyInfo SelectionGuardProperty => selectionGuardPropInfo ?? GetSelectionGuardPropInfo ( ) ;
@@ -352,7 +366,7 @@ private static PropertyInfo GetSelectionGuardPropInfo()
352
366
353
367
private static PropertyInfo selectionGuardPropInfo ;
354
368
355
- private static IEnumerator SetAutocompleteCaretCoro ( int caretPosition )
369
+ private static IEnumerator SetCaretCoroutine ( int caretPosition )
356
370
{
357
371
var color = Input . Component . selectionColor ;
358
372
color . a = 0f ;
@@ -376,51 +390,90 @@ private static IEnumerator SetAutocompleteCaretCoro(int caretPosition)
376
390
settingCaretCoroutine = false ;
377
391
}
378
392
379
-
380
393
#region Lexer Highlighting
381
394
382
395
/// <summary>
383
396
/// Returns true if caret is inside string or comment, false otherwise
384
397
/// </summary>
385
398
private static bool HighlightVisibleInput ( )
386
399
{
387
- int startIdx = 0 ;
388
- int endIdx = Input . Text . Length - 1 ;
389
- int topLine = 0 ;
390
-
391
- // Calculate visible text if necessary
392
- if ( Input . Rect . rect . height > Panel . InputScroll . ViewportRect . rect . height )
400
+ if ( string . IsNullOrEmpty ( Input . Text ) )
393
401
{
394
- topLine = - 1 ;
395
- int bottomLine = - 1 ;
402
+ Panel . HighlightText . text = "" ;
403
+ Panel . LineNumberText . text = "1" ;
404
+ return false ;
405
+ }
396
406
397
- // the top and bottom position of the viewport in relation to the text height
398
- // they need the half-height adjustment to normalize against the 'line.topY' value.
399
- var viewportMin = Input . Rect . rect . height - Input . Rect . anchoredPosition . y - ( Input . Rect . rect . height * 0.5f ) ;
400
- var viewportMax = viewportMin - Panel . InputScroll . ViewportRect . rect . height ;
407
+ // Calculate the visible lines
401
408
402
- for ( int i = 0 ; i < Input . TextGenerator . lineCount ; i ++ )
403
- {
404
- var line = Input . TextGenerator . lines [ i ] ;
405
- // if not set the top line yet, and top of line is below the viewport top
406
- if ( topLine == - 1 && line . topY <= viewportMin )
407
- topLine = i ;
408
- // if bottom of line is below the viewport bottom
409
- if ( ( line . topY - line . height ) >= viewportMax )
410
- bottomLine = i ;
411
- }
409
+ int topLine = - 1 ;
410
+ int bottomLine = - 1 ;
412
411
413
- topLine = Math . Max ( 0 , topLine - 1 ) ;
414
- bottomLine = Math . Min ( Input . TextGenerator . lineCount - 1 , bottomLine + 1 ) ;
412
+ // the top and bottom position of the viewport in relation to the text height
413
+ // they need the half-height adjustment to normalize against the 'line.topY' value.
414
+ var viewportMin = Input . Rect . rect . height - Input . Rect . anchoredPosition . y - ( Input . Rect . rect . height * 0.5f ) ;
415
+ var viewportMax = viewportMin - Panel . InputScroller . ViewportRect . rect . height ;
415
416
416
- startIdx = Input . TextGenerator . lines [ topLine ] . startCharIdx ;
417
- endIdx = ( bottomLine >= Input . TextGenerator . lineCount - 1 )
418
- ? Input . Text . Length - 1
419
- : ( Input . TextGenerator . lines [ bottomLine + 1 ] . startCharIdx - 1 ) ;
417
+ for ( int i = 0 ; i < Input . TextGenerator . lineCount ; i ++ )
418
+ {
419
+ var line = Input . TextGenerator . lines [ i ] ;
420
+ // if not set the top line yet, and top of line is below the viewport top
421
+ if ( topLine == - 1 && line . topY <= viewportMin )
422
+ topLine = i ;
423
+ // if bottom of line is below the viewport bottom
424
+ if ( ( line . topY - line . height ) >= viewportMax )
425
+ bottomLine = i ;
420
426
}
421
427
428
+ topLine = Math . Max ( 0 , topLine - 1 ) ;
429
+ bottomLine = Math . Min ( Input . TextGenerator . lineCount - 1 , bottomLine + 1 ) ;
430
+
431
+ int startIdx = Input . TextGenerator . lines [ topLine ] . startCharIdx ;
432
+ int endIdx = ( bottomLine >= Input . TextGenerator . lineCount - 1 )
433
+ ? Input . Text . Length - 1
434
+ : ( Input . TextGenerator . lines [ bottomLine + 1 ] . startCharIdx - 1 ) ;
435
+
436
+
422
437
// Highlight the visible text with the LexerBuilder
438
+
423
439
Panel . HighlightText . text = Lexer . BuildHighlightedString ( Input . Text , startIdx , endIdx , topLine , LastCaretPosition , out bool ret ) ;
440
+
441
+ // Set the line numbers
442
+
443
+ // determine true starting line number (not the same as the cached TextGenerator line numbers)
444
+ int realStartLine = 0 ;
445
+ for ( int i = 0 ; i < startIdx ; i ++ )
446
+ {
447
+ if ( LexerBuilder . IsNewLine ( Input . Text [ i ] ) )
448
+ realStartLine ++ ;
449
+ }
450
+ realStartLine ++ ;
451
+ char lastPrev = '\n ' ;
452
+
453
+ var sb = new StringBuilder ( ) ;
454
+
455
+ // append leading new lines for spacing (no point rendering line numbers we cant see)
456
+ for ( int i = 0 ; i < topLine ; i ++ )
457
+ sb . Append ( '\n ' ) ;
458
+
459
+ // append the displayed line numbers
460
+ for ( int i = topLine ; i <= bottomLine ; i ++ )
461
+ {
462
+ if ( i > 0 )
463
+ lastPrev = Input . Text [ Input . TextGenerator . lines [ i ] . startCharIdx - 1 ] ;
464
+
465
+ // previous line ended with a newline character, this is an actual new line.
466
+ if ( LexerBuilder . IsNewLine ( lastPrev ) )
467
+ {
468
+ sb . Append ( realStartLine . ToString ( ) ) ;
469
+ realStartLine ++ ;
470
+ }
471
+
472
+ sb . Append ( '\n ' ) ;
473
+ }
474
+
475
+ Panel . LineNumberText . text = sb . ToString ( ) ;
476
+
424
477
return ret ;
425
478
}
426
479
0 commit comments