Ejemplo n.º 1
0
 void OpenTab( string path )
 {
     foreach ( FATabStripItem page in _TabControl1.Items )
         if ( page.Tag is string )
             if ( ( string )page.Tag == path ) {
                 _TabControl1.SelectedItem = page;
                 return;
             }
     FATabStripItem faTabStripItem = new FATabStripItem( );
     faTabStripItem.Title = Path.GetFileName( path );
     FastColoredTextBox RB1 = new FastColoredTextBox( );
     #region TextBox Properties
     RB1.Dock = DockStyle.Fill;
     RB1.CurrentLineColor = Color.FromArgb( 255, 255, 64 );
     RB1.BackColor = Color.FromArgb( 70, 70, 70 );
     RB1.ForeColor = Color.White;
     RB1.PreferredLineWidth = 0;
     RB1.BorderStyle = BorderStyle.FixedSingle;
     RB1.Cursor = Cursors.IBeam;
     RB1.ChangedLineColor = Color.DarkRed;
     RB1.ChangedLineWidth = 2;
     RB1.LeftBracket = '[';
     RB1.LeftBracket2 = '(';
     RB1.LeftBracket3 = '{';
     RB1.RightBracket = ']';
     RB1.RightBracket2 = ')';
     RB1.RightBracket3 = '}';
     RB1.BracketsStyle = RB1.BracketsStyle2 = RB1.BracketsStyle3 = new MarkerStyle( new SolidBrush( Color.FromArgb( 0, 160, 160 ) ) );
     RB1.SelectionStyle.BackgroundBrush = new SolidBrush( preferencesWindow.colors[ 4 ] );
     RB1.Font = preferencesWindow.f;
     RB1.BorderStyle = BorderStyle.None;
     RB1.IndentBackColor = Color.FromArgb( 120, 120, 120 );
     RB1.LineNumberColor = Color.FromArgb( 225, 225, 225 );
     #endregion
     bool iswait = true;
     RB1.TextChanged += new EventHandler<TextChangedEventArgs>( ( object sender, TextChangedEventArgs e ) => {
         new Thread( new ThreadStart( ( ) => {
             if ( !RB1.IsHandleCreated )
                 return;
             faTabStripItem.Saved = false;
             _TabControl1.BeginInvoke( new MethodInvoker( ( ) => {
                 _TabControl1.Invalidate( );
             } ) );
             if ( RB1.BackColor != Color.FromArgb( 50, 50, 50 ) )
                 RB1.BackColor = Color.FromArgb( 50, 50, 50 );
             RB1.BeginInvoke( new MethodInvoker( ( ) => {
                 new Thread( new ThreadStart( ( ) => {
                     try {
                         string txt = RB1.Text;
                         List<Token> lineStream = tokenizer.Tokenize( new Range( RB1, 0,
                             RB1.Selection.Start.iLine == 0 ? 0 : RB1.Selection.Start.iLine - 1,
                             RB1.Lines[ RB1.Selection.Start.iLine == RB1.LinesCount - 1 ? RB1.Selection.Start.iLine : RB1.Selection.Start.iLine + 1 ].Length,
                             RB1.Selection.Start.iLine == RB1.LinesCount - 1 ? RB1.Selection.Start.iLine : RB1.Selection.Start.iLine + 1 ).Text
                             , RB1.Selection.Start.iLine == 0 ? 1 : RB1.Selection.Start.iLine );
                         Lexer.Lexing( ref lineStream );
                         PaintTextBox( RB1, lineStream );
                         try {
                             RB1.TabLength = int.Parse( preferencesWindow.tab_spaces.Text );
                             RB1.TabLength = 4;
                             if ( iswait )
                                 System.Threading.Thread.Sleep( int.Parse( preferencesWindow.paint_delay.Text ) );
                             else
                                 iswait = true;
                         } catch {
                             MessageBox.Show( this, "Fix code editor at preferences window \r\nPath: File->Preferences->Scripts and codes" );
                             return;
                         }
                         if ( RB1.Text != txt )
                             return;
                         RB1.VisibleRange.ClearStyles( false );
                         RB1.VisibleRange.ClearFoldingMarkers( false );
                         List<Token> lt = tokenizer.Tokenize( RB1.VisibleRange.Text );
                         Lexer.Lexing( ref lt );
                         PaintTextBox( RB1, lt );
                     } catch {
                         MessageBox.Show(this, "Exception caught while painting" );
                     }
                 } ) ).Start( );
             } ) );
         } ) ).Start( );
     } );
     RB1.KeyDown += new KeyEventHandler( ( object sender, KeyEventArgs kea ) => {
         RB1.SelectionStyle.BackgroundBrush = new SolidBrush( preferencesWindow.colors[ 4 ] );
         RB1.Font = preferencesWindow.f;
         //runningForm.savedProject = false;
         int speccount = 0;
         bool isstr = false;
         int ind = RB1.Selection.Start.iChar;
         List<Char> txt = RB1[ RB1.Selection.Start.iLine ];
         txt.ForEach( new Action<Char>( ( Char c ) => {
             if ( --ind <= -1 )
                 return;
             if ( c.c == '"' || c.c == '\'' )
                 isstr = isstr == false;
         } ) );
         if ( isstr )
             return;
         string astr = RB1.Text.Substring( 0, RB1.SelectionStart );
         int startline = astr.LastIndexOf( '\n', RB1.SelectionStart - 1 );
         if ( startline == -1 )
             startline = 0;
         string astr2 = astr.Substring( startline );
         if ( kea.KeyValue == 219 && kea.Shift ) {
             kea.Handled = true;
             RB1.InsertText( " {\r\n" + new string( ' ', RB1[ RB1.Selection.Start.iLine ].StartSpacesCount + RB1.TabLength ) );
             iswait = false;
             RB1.OnTextChanged( );
         } else if ( kea.KeyValue == 8 && RB1.SelectionStart > 0 && RB1.SelectionLength == 0 ) {
             astr = RB1.Text;
             startline = astr.LastIndexOf( '\n', RB1.SelectionStart - 1 ) + 1;
             if ( astr.Substring( startline, RB1.SelectionStart - startline ) == new string( ' ', RB1.SelectionStart - startline ) ) {
                 speccount = RB1.SelectionStart - startline;
                 if ( startline != 0 )
                     speccount += 2;
                 RB1.SelectionStart -= speccount;
                 RB1.SelectionLength = speccount;
                 RB1.SelectedText = "";
                 kea.Handled = true;
             }
         } else if ( kea.KeyValue == 13 ) {
             int spaces = RB1[ RB1.Selection.Start.iLine ].StartSpacesCount;
             if ( RB1.Selection.CharAfterStart == '\n' )
                 return;
             RB1.InsertText( "\r\n" + new string( ' ', spaces ) );
             kea.Handled = true;
         } else if ( kea.KeyValue == 221 && kea.Shift ) {
             kea.Handled = true;
             int spaces = RB1[ RB1.Selection.Start.iLine ].StartSpacesCount;
             if ( astr2.Replace( " ", "" ) == "\n" ) {
                 if ( astr2.Length > RB1.TabLength + 1 ) {
                     RB1.SelectionStart -= RB1.TabLength;
                     RB1.SelectionLength = RB1.TabLength;
                     RB1.SelectedText = "}\r\n" + new string( ' ', spaces - RB1.TabLength );
                 } else {
                     RB1.SelectionStart = startline + 1;
                     RB1.SelectionLength = astr2.Length - 1;
                     RB1.SelectedText = "}\r\n";
                 }
             } else {
                 if ( spaces < RB1.TabLength + 1 ) {
                     RB1.InsertText( "\r\n}\r\n" + new string( ' ', spaces ) );
                 } else {
                     RB1.InsertText( "\r\n" + new string( ' ', spaces - RB1.TabLength ) + "}\r\n" + new string( ' ', spaces - RB1.TabLength ) );
                 }
             }
         }
     } );
     RB1.Text = File.ReadAllText( path );
     RB1.Height = RB1.Width = 2500;
     faTabStripItem.Controls.Add( RB1 );
     faTabStripItem.Tag = path;
     _TabControl1.Items.Add( faTabStripItem );
     List<Token> lt2 = tokenizer.Tokenize( RB1.Text );
     Lexer.Lexing( ref lt2 );
     PaintTextBox( RB1, lt2 );
     RB1.OnTextChanged( );
     RB1.ClearLineChanges( );
     faTabStripItem.Saved = true;
     OpenTab( path );
     _TabControl1.Invalidate( );
 }