From aaf8bab9bc9c45697a08e7aff432fcefc7afed71 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 31 Aug 2017 15:20:31 +0200 Subject: [PATCH] debug tab, bold and italic formatting --- src/Parser.cs | 18 +++++++++--------- src/SourceEditor.cs | 31 +++++++++++++++++++++---------- src/TextFormatting.cs | 6 +++++- src/XMLParser.cs | 20 ++++++++++---------- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/Parser.cs b/src/Parser.cs index f5d91c6..a72746d 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -49,6 +49,7 @@ namespace Crow.Coding protected bool eof = true; public List Tokens; + protected TokenList TokensLine; public Point CurrentPosition { get { return new Point (currentLine, currentColumn); } } @@ -64,6 +65,10 @@ namespace Crow.Coding currentTok.Start = CurrentPosition; currentTok += Read(); } + protected void readToCurrTok(int length) { + for (int i = 0; i < length; i++) + currentTok += Read (); + } protected void readAndResetCurrentTok(System.Enum type, bool startToc = false) { readToCurrTok (); saveAndResetCurrentTok (type); @@ -72,7 +77,7 @@ namespace Crow.Coding protected void saveAndResetCurrentTok(System.Enum type) { currentTok.Type = (TokenType)type; currentTok.End = CurrentPosition; - Tokens[currentLine].Add (currentTok); + TokensLine.Add (currentTok); currentTok = default(Token); } protected virtual char Peek() { @@ -100,17 +105,12 @@ namespace Crow.Coding currentColumn++; return c; } - protected virtual string ReadUntil (string endExp){ + protected virtual string ReadLineUntil (string endExp){ string tmp = ""; while (!eof) { - if (buffer [currentLine].Length - currentColumn - endExp.Length < 0) { - currentLine++; - if (currentLine >= buffer.Length) - eof = true; - currentColumn = 0; - continue; - } + if (buffer [currentLine].Length - currentColumn - endExp.Length < 0) + break; if (string.Equals (Peek (endExp.Length), endExp)) return tmp; tmp += Read(); diff --git a/src/SourceEditor.cs b/src/SourceEditor.cs index 237a5d8..ea5b32a 100644 --- a/src/SourceEditor.cs +++ b/src/SourceEditor.cs @@ -46,15 +46,17 @@ namespace Crow.Coding #region CTOR public SourceEditor ():base() { - formatting.Add ((int)XMLParser.TokenType.AttributeName, new TextFormatting (Color.DarkBlue, Color.Transparent)); - formatting.Add ((int)XMLParser.TokenType.ElementName, new TextFormatting (Color.DarkRed, Color.Transparent)); - formatting.Add ((int)XMLParser.TokenType.ElementStart, new TextFormatting (Color.Red, Color.Transparent)); - formatting.Add ((int)XMLParser.TokenType.ElementEnd, new TextFormatting (Color.Red, Color.Transparent)); - formatting.Add ((int)XMLParser.TokenType.ElementClosing, new TextFormatting (Color.Red, Color.Transparent)); - - formatting.Add ((int)XMLParser.TokenType.AttributeValueOpening, new TextFormatting (Color.DarkPink, Color.Transparent)); - formatting.Add ((int)XMLParser.TokenType.AttributeValueClosing, new TextFormatting (Color.DarkPink, Color.Transparent)); - formatting.Add ((int)XMLParser.TokenType.AttributeValue, new TextFormatting (Color.DarkPink, Color.Transparent)); + formatting.Add ((int)XMLParser.TokenType.AttributeName, new TextFormatting (Color.UnitedNationsBlue, Color.Transparent)); + formatting.Add ((int)XMLParser.TokenType.ElementName, new TextFormatting (Color.DarkBlue, Color.Transparent, true)); + formatting.Add ((int)XMLParser.TokenType.ElementStart, new TextFormatting (Color.Red, Color.Transparent,true)); + formatting.Add ((int)XMLParser.TokenType.ElementEnd, new TextFormatting (Color.Red, Color.Transparent,true)); + formatting.Add ((int)XMLParser.TokenType.ElementClosing, new TextFormatting (Color.Red, Color.Transparent, true)); + formatting.Add ((int)XMLParser.TokenType.Affectation, new TextFormatting (Color.Red, Color.Transparent, true)); + + formatting.Add ((int)XMLParser.TokenType.AttributeValueOpening, new TextFormatting (Color.DarkPink, Color.Transparent,true)); + formatting.Add ((int)XMLParser.TokenType.AttributeValueClosing, new TextFormatting (Color.DarkPink, Color.Transparent,true)); + formatting.Add ((int)XMLParser.TokenType.AttributeValue, new TextFormatting (Color.DarkPink, Color.Transparent, false, true)); + formatting.Add ((int)XMLParser.TokenType.XMLDecl, new TextFormatting (Color.SeaGreen, Color.Transparent, true)); buffer = new CodeBuffer (); buffer.LineUpadateEvent += Buffer_LineUpadateEvent; @@ -86,7 +88,7 @@ namespace Crow.Coding Point _selBegin = -1; //selection start (row,column) Point _selRelease = -1; //selection end (row,column) - Dictionary formatting = new Dictionary(); + Dictionary formatting = new Dictionary(); protected Rectangle rText; protected FontExtents fe; @@ -505,13 +507,20 @@ namespace Crow.Coding Color fg = this.Foreground; Color selbg = this.SelectionBackground; Color selfg = this.SelectionForeground; + FontSlant fts = FontSlant.Normal; + FontWeight ftw = FontWeight.Normal; if (formatting.ContainsKey ((int)tokens [t].Type)) { TextFormatting tf = formatting [(int)tokens [t].Type]; bg = tf.Background; fg = tf.Foreground; + if (tf.Bold) + ftw = FontWeight.Bold; + if (tf.Italic) + fts = FontSlant.Italic; } + gr.SelectFontFace (Font.Name, fts, ftw); gr.SetSourceColor (fg); int x = cb.X + (int)((lPtr - ScrollX) * fe.MaxXAdvance); @@ -614,6 +623,8 @@ namespace Crow.Coding CurrentLine--; else CurrentLine = ScrollY + (int)Math.Floor (mouseLocalPos.Y / fe.Height); + + CurrentPosition = buffer.VisualPosition; //for rounding if in middle of tabs } public override void onMouseEnter (object sender, MouseMoveEventArgs e) { diff --git a/src/TextFormatting.cs b/src/TextFormatting.cs index 64e7aea..f7b2e51 100644 --- a/src/TextFormatting.cs +++ b/src/TextFormatting.cs @@ -5,10 +5,14 @@ namespace Crow.Coding public struct TextFormatting { public Color Foreground; public Color Background; + public bool Bold; + public bool Italic; - public TextFormatting(Color fg, Color bg){ + public TextFormatting(Color fg, Color bg, bool bold = false, bool italic = false){ Foreground = fg; Background = bg; + Bold = bold; + Italic = italic; } } } diff --git a/src/XMLParser.cs b/src/XMLParser.cs index 0118571..29f57b3 100644 --- a/src/XMLParser.cs +++ b/src/XMLParser.cs @@ -111,6 +111,7 @@ namespace Crow.Coding currentColumn = 0; eof = false; bool eol = false; + TokensLine = Tokens [line]; //retrieve current parser state from previous line if (line > 0) @@ -118,10 +119,8 @@ namespace Crow.Coding else curState = States.init; - States previousEndingState = (States)Tokens[line].EndingState; - Tokens[line].Clear (); - - + States previousEndingState = (States)TokensLine.EndingState; + TokensLine.Clear (); while (! (eof||eol)) { SkipWhiteSpaces (); @@ -143,7 +142,8 @@ namespace Crow.Coding if (curState != States.init) throw new ParsingException (this, "prolog may appear only on first line"); readToCurrTok (); - currentTok += ReadUntil ("?>"); + currentTok += ReadLineUntil ("?>"); + readToCurrTok (2); saveAndResetCurrentTok (TokenType.XMLDecl); curState = States.prolog; break; @@ -154,7 +154,7 @@ namespace Crow.Coding readToCurrTok (); if (Peek () != '-') throw new ParsingException (this, "Expecting comment start tag"); - currentTok += ReadUntil ("--"); + currentTok += ReadLineUntil ("--"); if (Peek () != '>') throw new ParsingException (this, "Expecting comment closing tag"); readAndResetCurrentTok (TokenType.BlockComment); @@ -164,7 +164,7 @@ namespace Crow.Coding } break; default: - if (!(curState == States.Content || curState == States.XML || curState == States.init)) + if (!(curState == States.Content || curState == States.XML || curState == States.init || curState == States.prolog)) throw new ParsingException (this, "Unexpected char: '<'"); if (Peek () == '/') { curState = States.EndTag; @@ -228,7 +228,7 @@ namespace Crow.Coding readAndResetCurrentTok (TokenType.AttributeValueOpening, true); currentTok.Start = CurrentPosition; - currentTok.Content = ReadUntil (new string (new char[]{ openAttVal })); + currentTok.Content = ReadLineUntil (new string (new char[]{ openAttVal })); saveAndResetCurrentTok (TokenType.AttributeValue); if (Peek () != openAttVal) @@ -242,8 +242,8 @@ namespace Crow.Coding } } - Tokens[line].EndingState = (int)curState; - Tokens [line].Dirty = false; + TokensLine.EndingState = (int)curState; + TokensLine.Dirty = false; if (previousEndingState != curState && line < Tokens.Count - 1) Tokens [line + 1].Dirty = true; -- 2.47.3