]> O.S.I.I.S - jp/crowedit.git/commitdiff
basic formating test
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 30 Aug 2017 05:48:25 +0000 (07:48 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 30 Aug 2017 05:48:25 +0000 (07:48 +0200)
src/Parser.cs
src/SourceEditor.cs
src/XMLParser.cs

index 8c153e59f713bcfb88f5b1f831d97a2915b8b8bd..945f843e3b0f9acbf872f1d7499cf1896383ee43 100644 (file)
@@ -58,6 +58,7 @@ namespace CrowEdit
                CodeTextBuffer buffer;
 
                public List<List<Token>> Tokens;
+               protected List<Token> TokensLine;
 
                public bool Parsed { get { return parsed; }}
                public Point CurrentPosition { get { return new Point (currentLine, currentColumn); } }
@@ -78,7 +79,7 @@ namespace CrowEdit
                protected void saveAndResetCurrentTok(System.Enum type) {
                        currentTok.Type = (TokenType)type;
                        currentTok.End = CurrentPosition;
-                       Tokens.Add (currentTok);
+                       TokensLine.Add (currentTok);
 
                        currentTok = default(Token);
                }
index e2c6f5b1f5c6ae6fab42eefb9db0a853da5ab590..8d904aff0400f88e42b8173e2da97ab643db6f56 100644 (file)
@@ -34,9 +34,19 @@ using System.Collections.Generic;
 using System.Text.RegularExpressions;
 using System.Linq;
 using System.Diagnostics;
+using CrowEdit;
 
 namespace Crow
 {
+       public struct TextFormating {
+               public Color Foreground;
+               public Color Background;
+
+               public TextFormating(Color fg, Color bg){
+                       Foreground = fg;
+                       Background = bg;
+               }
+       }
        /// <summary>
        /// Scrolling text box optimized for monospace fonts, for coding
        /// </summary>
@@ -44,12 +54,21 @@ namespace Crow
        {
                #region CTOR
                public SourceEditor ():base()
-               {
-
+               {                       
+                       formating.Add ((int)XMLParser.TokenType.AttributeName, new TextFormating (Color.DarkBlue, Color.Transparent));
+                       formating.Add ((int)XMLParser.TokenType.ElementName, new TextFormating (Color.DarkRed, Color.Transparent));
+                       formating.Add ((int)XMLParser.TokenType.ElementStart, new TextFormating (Color.Red, Color.Transparent));
+                       formating.Add ((int)XMLParser.TokenType.ElementEnd, new TextFormating (Color.Red, Color.Transparent));
+                       formating.Add ((int)XMLParser.TokenType.ElementClosing, new TextFormating (Color.Red, Color.Transparent));
 
+                       formating.Add ((int)XMLParser.TokenType.AttributeValueOpening, new TextFormating (Color.DarkPink, Color.Transparent));
+                       formating.Add ((int)XMLParser.TokenType.AttributeValueClosing, new TextFormating (Color.DarkPink, Color.Transparent));
+                       formating.Add ((int)XMLParser.TokenType.AttributeValue, new TextFormating (Color.DarkPink, Color.Transparent));
                }
                #endregion
 
+               Dictionary<int,TextFormating> formating = new Dictionary<int, TextFormating>();
+
                public event EventHandler TextChanged;
 
                public virtual void OnTextChanged(Object sender, EventArgs e)
@@ -61,6 +80,7 @@ namespace Crow
                int visibleLines = 1;
                int visibleColumns = 1;
                CodeTextBuffer buffer;
+               Parser parser;
                Color selBackground;
                Color selForeground;
                int _currentCol;        //0 based cursor position in string
@@ -367,10 +387,8 @@ namespace Crow
                        else if (layoutType == LayoutingType.Width)
                                updateVisibleColumns ();
                }
-               protected override void onDraw (Context gr)
-               {
-                       base.onDraw (gr);
 
+               void draw(Context gr){
                        gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
                        gr.SetFontSize (Font.Size);
                        gr.FontOptions = Interface.FontRenderingOptions;
@@ -378,8 +396,6 @@ namespace Crow
 
                        Rectangle cb = ClientRectangle;
 
-                       Foreground.SetAsSource (gr);
-
                        bool selectionInProgress = false;
 
                        Foreground.SetAsSource (gr);
@@ -439,6 +455,107 @@ namespace Crow
                                }
                        }
                }
+               void drawParsed(Context gr){
+                       gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
+                       gr.SetFontSize (Font.Size);
+                       gr.FontOptions = Interface.FontRenderingOptions;
+                       gr.Antialias = Interface.Antialias;
+
+                       Rectangle cb = ClientRectangle;
+
+                       bool selectionInProgress = false;
+
+                       Foreground.SetAsSource (gr);
+
+                       #region draw text cursor
+                       if (SelBegin != SelRelease)
+                               selectionInProgress = true;
+                       else if (HasFocus){
+                               gr.LineWidth = 1.0;
+                               double cursorX = cb.X + (CurrentColumn - ScrollX) * fe.MaxXAdvance;
+                               gr.MoveTo (0.5 + cursorX, cb.Y + (CurrentLine - ScrollY) * fe.Height);
+                               gr.LineTo (0.5 + cursorX, cb.Y + (CurrentLine + 1 - ScrollY) * fe.Height);
+                               gr.Stroke();
+                       }
+                       #endregion
+
+                       for (int i = 0; i < visibleLines; i++) {
+                               int curL = i + ScrollY;
+                               if (curL >= parser.Tokens.Count)
+                                       break;
+                               List<Token> tokens = parser.Tokens[curL];
+                               int lPtr = 0;
+
+                               for (int t = 0; t < tokens.Count; t++) {
+                                       string lstr = tokens [t].Content;
+                                       if (lPtr < ScrollX) {
+                                               if (lPtr - ScrollX + lstr.Length <= 0) {
+                                                       lPtr += lstr.Length;
+                                                       continue;
+                                               }
+                                               lstr = lstr.Substring (ScrollX - lPtr);
+                                               lPtr += ScrollX - lPtr;
+                                       }
+                                       Color bg = this.Background;
+                                       Color fg = this.Foreground;
+
+                                       if (formating.ContainsKey ((int)tokens [t].Type)) {
+                                               TextFormating tf = formating [(int)tokens [t].Type];
+                                               bg = tf.Background;
+                                               fg = tf.Foreground;
+                                       }
+
+                                       gr.SetSourceColor (fg);
+
+                                       int x = cb.X + (int)((lPtr - ScrollX) * fe.MaxXAdvance);
+
+                                       gr.MoveTo (x, cb.Y + fe.Ascent + fe.Height * i);
+                                       gr.ShowText (lstr);
+                                       gr.Fill ();
+
+                                       lPtr += lstr.Length;
+                               }
+
+
+//                             if (selectionInProgress && curL >= selectionStart.Y && curL <= selectionEnd.Y) {
+//
+//                                     double rLineX = cb.X,
+//                                     rLineY = cb.Y + i * fe.Height,
+//                                     rLineW = lstr.Length * fe.MaxXAdvance;
+//
+//                                     System.Diagnostics.Debug.WriteLine ("sel start: " + selectionStart + " sel end: " + selectionEnd);
+//                                     if (curL == selectionStart.Y) {
+//                                             rLineX += (selectionStart.X - ScrollX) * fe.MaxXAdvance;
+//                                             rLineW -= selectionStart.X * fe.MaxXAdvance;
+//                                     }
+//                                     if (curL == selectionEnd.Y)
+//                                             rLineW -= (lstr.Length - selectionEnd.X) * fe.MaxXAdvance;
+//
+//                                     gr.Save ();
+//                                     gr.Operator = Operator.Source;
+//                                     gr.Rectangle (rLineX, rLineY, rLineW, fe.Height);
+//                                     gr.SetSourceColor (SelectionBackground);
+//                                     gr.FillPreserve ();
+//                                     gr.Clip ();
+//                                     gr.Operator = Operator.Over;
+//                                     gr.SetSourceColor (SelectionForeground);
+//                                     gr.MoveTo (cb.X, cb.Y + fe.Ascent + fe.Height * i);
+//                                     gr.ShowText (lstr);
+//                                     gr.Fill ();
+//                                     gr.Restore ();
+//                             }
+                       }
+               }
+               protected override void onDraw (Context gr)
+               {
+                       base.onDraw (gr);
+
+                       if (parser?.Parsed == true)
+                               drawParsed (gr);
+                       else
+                               draw(gr);
+                               
+               }
                #endregion
 
                #region Mouse handling
@@ -686,12 +803,16 @@ namespace Crow
                                break;
                        case Key.F8:
                                try {
-                                       CrowEdit.XMLParser parser = new CrowEdit.XMLParser (buffer);
+                                       parser = new CrowEdit.XMLParser (buffer);
                                        parser.Parse ();
-                               }catch(Exception ee){
+                               } catch (Exception ee) {
                                        Debug.WriteLine (ee.ToString ());
+                                       parser = null;
                                }
                                break;
+                       case Key.F9:
+                               parser = null;
+                               break;
                        default:
                                break;
                        }
index 8129fac72ed3cf74866fefe2b5b83ae3515b95e9..99ff5cb6749269a79653fb05c57974fca3c57edd 100644 (file)
@@ -99,7 +99,8 @@ namespace CrowEdit
                public override void Parse ()
                {
                        parsed = false;
-                       Tokens = new List<Token> ();
+                       Tokens = new List<List<Token>> ();
+                       TokensLine = new List<Token> ();
                        currentLine = currentColumn = 0;
                        currentTok = default(Token);
                        curState = States.init;
@@ -116,7 +117,9 @@ namespace CrowEdit
                                case '\n':
                                        if (currentTok != TokenType.Unknown)
                                                throw new ParsingException (this, "Unexpected end of line");
-                                       readAndResetCurrentTok (TokenType.NewLine, true);
+                                       Read ();
+                                       Tokens.Add (TokensLine);
+                                       TokensLine = new List<Token> ();
                                        break;
                                case '<':
                                        readToCurrTok (true);
@@ -223,6 +226,8 @@ namespace CrowEdit
                                        break;
                                }
                        }
+                       if (TokensLine.Count > 0)
+                               Tokens.Add (TokensLine);
 
                        parsed = true;
                }