From: Jean-Philippe Bruyère Date: Wed, 30 Aug 2017 22:22:09 +0000 (+0200) Subject: code clean and formatting X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=dd79f2b862d6d56d8ad517eb9462f0b0063c763d;p=jp%2Fcrowedit.git code clean and formatting --- diff --git a/CodeBufferEventArgs.cs b/CodeBufferEventArgs.cs new file mode 100644 index 0000000..07dd25b --- /dev/null +++ b/CodeBufferEventArgs.cs @@ -0,0 +1,20 @@ +using System; + +namespace Crow.Coding +{ + public class CodeBufferEventArgs : EventArgs { + public int LineStart; + public int LineCount; + + public CodeBufferEventArgs(int lineNumber) { + LineStart = lineNumber; + LineCount = 1; + } + public CodeBufferEventArgs(int lineStart, int lineCount) { + LineStart = lineStart; + LineCount = lineCount; + } + } + +} + diff --git a/CrowEdit.csproj b/CrowEdit.csproj index ea89cf2..d3877ab 100644 --- a/CrowEdit.csproj +++ b/CrowEdit.csproj @@ -81,6 +81,9 @@ + + + diff --git a/ParsingException.cs b/ParsingException.cs new file mode 100644 index 0000000..88c51c9 --- /dev/null +++ b/ParsingException.cs @@ -0,0 +1,22 @@ +using System; + +namespace Crow.Coding +{ + public class ParsingException : Exception + { + public int Line; + public int Column; + public ParsingException(Parser parser, string txt) + : base(string.Format("Parser exception ({0},{1}): {2}", parser.currentLine, parser.currentColumn, txt)) + { + Line = parser.currentLine; + Column = parser.currentColumn; + } + public ParsingException(Parser parser, string txt, Exception innerException) + : base(txt, innerException) + { + txt = string.Format("Parser exception ({0},{1}): {2}", parser.currentLine, parser.currentColumn, txt); + } + } +} + diff --git a/src/CSharpParser.cs b/src/CSharpParser.cs index f9d7cba..472d004 100644 --- a/src/CSharpParser.cs +++ b/src/CSharpParser.cs @@ -28,7 +28,7 @@ namespace Crow.Coding Preprocessor, } - public CSharpParser (CodeTextBuffer _buffer) : base(_buffer) + public CSharpParser (CodeBuffer _buffer) : base(_buffer) { } diff --git a/src/CodeBuffer.cs b/src/CodeBuffer.cs index ab764f7..104ddb4 100644 --- a/src/CodeBuffer.cs +++ b/src/CodeBuffer.cs @@ -25,21 +25,7 @@ using System.Text.RegularExpressions; namespace Crow.Coding { - public class CodeBufferEventArgs : EventArgs { - public int LineStart; - public int LineCount; - - public CodeBufferEventArgs(int lineNumber) { - LineStart = lineNumber; - LineCount = 1; - } - public CodeBufferEventArgs(int lineStart, int lineCount) { - LineStart = lineStart; - LineCount = lineCount; - } - } - - public class CodeTextBuffer + public class CodeBuffer { #region Events public event EventHandler LineUpadateEvent; @@ -49,11 +35,13 @@ namespace Crow.Coding #endregion #region CTOR - public CodeTextBuffer () : base() {} + public CodeBuffer () : base() {} #endregion - + string lineBreak = Interface.LineBreak; List lines = new List(); + public int longestLineIdx = 0; + public int longestLineCharCount = 0; public int Length { get { return lines.Count;}} @@ -86,7 +74,6 @@ namespace Crow.Coding BufferCleared.Raise (this, null); } - public void Load(string rawSource) { this.Clear(); @@ -98,10 +85,6 @@ namespace Crow.Coding lineBreak = detectLineBreakKind (rawSource); findLongestLine (); } - string lineBreak = Interface.LineBreak; - - public int longestLineIdx = 0; - public int longestLineCharCount = 0; void findLongestLine(){ longestLineCharCount = 0; diff --git a/src/Parser.cs b/src/Parser.cs index 4d5b0ce..f5d91c6 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -30,19 +30,6 @@ namespace Crow.Coding Preprocessor, } - public class ParsingException : Exception - { - public ParsingException(Parser parser, string txt) - : base(string.Format("Parser exception ({0},{1}): {2}", parser.currentLine, parser.currentColumn, txt)) - { - } - public ParsingException(Parser parser, string txt, Exception innerException) - : base(txt, innerException) - { - txt = string.Format("Parser exception ({0},{1}): {2}", parser.currentLine, parser.currentColumn, txt); - } - } - #region CTOR public Parser (CodeBuffer _buffer) { @@ -54,30 +41,29 @@ namespace Crow.Coding #endregion - protected int currentLine = 0; - protected int currentColumn = 0; + CodeBuffer buffer; + + internal int currentLine = 0; + internal int currentColumn = 0; protected Token currentTok; protected bool eof = true; - CodeBuffer buffer; - public List Tokens; - protected TokenList TokensLine; public Point CurrentPosition { get { return new Point (currentLine, currentColumn); } } - public virtual void SetLineInError(int lineNumber) { + public abstract void Parse(int line); + public virtual void SetLineInError(ParsingException ex) { currentTok = default(Token); - Tokens [lineNumber] = new TokenList () {new Token () { Content = buffer [lineNumber] }}; + Tokens [ex.Line] = new TokenList () {new Token () { Content = buffer [ex.Line] }}; } - public abstract void Parse(int line); + #region low level parsing protected void readToCurrTok(bool startOfTok = false){ if (startOfTok) currentTok.Start = CurrentPosition; currentTok += Read(); } - protected void readAndResetCurrentTok(System.Enum type, bool startToc = false) { readToCurrTok (); saveAndResetCurrentTok (type); @@ -86,11 +72,9 @@ namespace Crow.Coding protected void saveAndResetCurrentTok(System.Enum type) { currentTok.Type = (TokenType)type; currentTok.End = CurrentPosition; - TokensLine.Add (currentTok); - + Tokens[currentLine].Add (currentTok); currentTok = default(Token); } - protected virtual char Peek() { if (eof) throw new ParsingException (this, "Unexpected End of File"); @@ -116,7 +100,6 @@ namespace Crow.Coding currentColumn++; return c; } - protected virtual string ReadUntil (string endExp){ string tmp = ""; @@ -134,7 +117,6 @@ namespace Crow.Coding } throw new ParsingException (this, string.Format("Expectign '{0}'", endExp)); } - protected void SkipWhiteSpaces () { if (currentTok.Type != TokenType.Unknown) throw new ParsingException (this, "current token should be reset to unknown (0) before skiping white spaces"); @@ -147,5 +129,6 @@ namespace Crow.Coding if (currentTok.Type != TokenType.Unknown) saveAndResetCurrentTok (); } + #endregion } } \ No newline at end of file diff --git a/src/SourceEditor.cs b/src/SourceEditor.cs index f2304f9..7b73819 100644 --- a/src/SourceEditor.cs +++ b/src/SourceEditor.cs @@ -38,15 +38,6 @@ using CrowEdit; namespace Crow.Coding { - public struct TextFormating { - public Color Foreground; - public Color Background; - - public TextFormating(Color fg, Color bg){ - Foreground = fg; - Background = bg; - } - } /// /// Scrolling text box optimized for monospace fonts, for coding /// @@ -55,17 +46,17 @@ namespace Crow.Coding #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)); + 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)); - 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)); + 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)); - buffer = new CodeTextBuffer (); + buffer = new CodeBuffer (); buffer.LineUpadateEvent += Buffer_LineUpadateEvent; buffer.LineAdditionEvent += Buffer_LineAdditionEvent;; buffer.LineRemoveEvent += Buffer_LineRemoveEvent; @@ -75,7 +66,6 @@ namespace Crow.Coding } #endregion - Dictionary formating = new Dictionary(); public event EventHandler TextChanged; @@ -87,7 +77,7 @@ namespace Crow.Coding #region private and protected fields int visibleLines = 1; int visibleColumns = 1; - CodeTextBuffer buffer; + CodeBuffer buffer; Parser parser; Color selBackground; Color selForeground; @@ -96,37 +86,13 @@ namespace Crow.Coding Point _selBegin = -1; //selection start (row,column) Point _selRelease = -1; //selection end (row,column) + Dictionary formatting = new Dictionary(); + protected Rectangle rText; protected FontExtents fe; protected TextExtents te; #endregion - [XmlAttributeAttribute][DefaultValue("label")] - public string Text - { - get { - return buffer == null ? "" : buffer.FullText; - } - set - { - if (string.Equals (value, buffer?.FullText, StringComparison.Ordinal)) - return; - - buffer.Load (value); - - MaxScrollY = Math.Max (0, buffer.Length - visibleLines); - MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns); - - OnTextChanged (this, null); - RegisterForGraphicUpdate (); - } - } - - void Buffer_BufferCleared (object sender, EventArgs e) - { - parser = new XMLParser (buffer); - RegisterForGraphicUpdate (); - } void reparseSource () { for (int i = 0; i < parser.Tokens.Count; i++) { if (parser.Tokens[i].Dirty) @@ -136,12 +102,18 @@ namespace Crow.Coding void tryParseBufferLine(int lPtr) { try { parser.Parse (lPtr); - } catch (Exception ex) { + } catch (ParsingException ex) { Debug.WriteLine (ex.ToString ()); - parser.SetLineInError (lPtr); + parser.SetLineInError (ex); } RegisterForGraphicUpdate (); } + #region Buffer events handlers + void Buffer_BufferCleared (object sender, EventArgs e) + { + parser = new XMLParser (buffer); + RegisterForGraphicUpdate (); + } void Buffer_LineAdditionEvent (object sender, CodeBufferEventArgs e) { for (int i = 0; i < e.LineCount; i++) { @@ -167,7 +139,29 @@ namespace Crow.Coding reparseSource (); RegisterForGraphicUpdate (); } + #endregion + + #region Public Crow Properties + [XmlAttributeAttribute][DefaultValue("label")] + public string Text + { + get { + return buffer == null ? "" : buffer.FullText; + } + set + { + if (string.Equals (value, buffer?.FullText, StringComparison.Ordinal)) + return; + buffer.Load (value); + + MaxScrollY = Math.Max (0, buffer.Length - visibleLines); + MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns); + + OnTextChanged (this, null); + RegisterForGraphicUpdate (); + } + } [XmlAttributeAttribute][DefaultValue("BlueGray")] public virtual Color SelectionBackground { get { return selBackground; } @@ -317,7 +311,9 @@ namespace Crow.Coding } [XmlIgnore]public bool selectionIsEmpty { get { return SelRelease == SelBegin; } } + #endregion + #region Editing and moving cursor /// /// Moves cursor one char to the left. /// @@ -406,42 +402,39 @@ namespace Crow.Coding } OnTextChanged (this, null); } - - #region GraphicObject overrides - public override Font Font { - get { return base.Font; } - set { - base.Font = value; - - using (ImageSurface img = new ImageSurface (Format.Argb32, 1, 1)) { - using (Context gr = new Context (img)) { - gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight); - gr.SetFontSize (Font.Size); - - fe = gr.FontExtents; - } - } - MaxScrollY = 0; - RegisterForGraphicUpdate (); - } - } - protected override int measureRawSize(LayoutingType lt) + /// + /// Insert new string at caret position, should be sure no line break is inside. + /// + /// String. + protected void Insert(string str) { - if (lt == LayoutingType.Height) - return (int)Math.Ceiling(fe.Height * buffer.Length) + Margin * 2; - - return (int)(fe.MaxXAdvance * buffer.longestLineCharCount) + Margin * 2; + if (!selectionIsEmpty) + this.DeleteChar (); + string[] strLines = Regex.Split (str, "\r\n|\r|\n|" + @"\\n").ToArray(); + buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[0]); + CurrentColumn += strLines[0].Length; + for (int i = 1; i < strLines.Length; i++) { + InsertLineBreak (); + buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[i]); + CurrentColumn += strLines[i].Length; + } + OnTextChanged (this, null); + RegisterForGraphicUpdate(); } - public override void OnLayoutChanges (LayoutingType layoutType) + /// + /// Insert a line break. + /// + protected void InsertLineBreak() { - base.OnLayoutChanges (layoutType); - - if (layoutType == LayoutingType.Height) - updateVisibleLines (); - else if (layoutType == LayoutingType.Width) - updateVisibleColumns (); + buffer.Insert(CurrentLine + 1, buffer[CurrentLine].Substring(CurrentColumn)); + buffer [CurrentLine] = buffer [CurrentLine].Substring (0, CurrentColumn); + CurrentLine++; + CurrentColumn = 0; + OnTextChanged (this, null); } + #endregion + #region Drawing void draw(Context gr){ gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize (Font.Size); @@ -559,8 +552,8 @@ namespace Crow.Coding Color selbg = this.SelectionBackground; Color selfg = this.SelectionForeground; - if (formating.ContainsKey ((int)tokens [t].Type)) { - TextFormating tf = formating [(int)tokens [t].Type]; + if (formatting.ContainsKey ((int)tokens [t].Type)) { + TextFormatting tf = formatting [(int)tokens [t].Type]; bg = tf.Background; fg = tf.Foreground; } @@ -606,6 +599,43 @@ namespace Crow.Coding lPtr += lstr.Length; } } + #endregion + + #region GraphicObject overrides + public override Font Font { + get { return base.Font; } + set { + base.Font = value; + + using (ImageSurface img = new ImageSurface (Format.Argb32, 1, 1)) { + using (Context gr = new Context (img)) { + gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight); + gr.SetFontSize (Font.Size); + + fe = gr.FontExtents; + } + } + MaxScrollY = 0; + RegisterForGraphicUpdate (); + } + } + protected override int measureRawSize(LayoutingType lt) + { + if (lt == LayoutingType.Height) + return (int)Math.Ceiling(fe.Height * buffer.Length) + Margin * 2; + + return (int)(fe.MaxXAdvance * buffer.longestLineCharCount) + Margin * 2; + } + public override void OnLayoutChanges (LayoutingType layoutType) + { + base.OnLayoutChanges (layoutType); + + if (layoutType == LayoutingType.Height) + updateVisibleLines (); + else if (layoutType == LayoutingType.Width) + updateVisibleColumns (); + } + protected override void onDraw (Context gr) { base.onDraw (gr); @@ -910,39 +940,5 @@ namespace Crow.Coding System.Diagnostics.Debug.WriteLine ("update visible columns: " + visibleColumns); System.Diagnostics.Debug.WriteLine ("update MaxScrollX: " + MaxScrollX); } - - - - /// - /// Insert new string at caret position, should be sure no line break is inside. - /// - /// String. - protected void Insert(string str) - { - if (!selectionIsEmpty) - this.DeleteChar (); - string[] strLines = Regex.Split (str, "\r\n|\r|\n|" + @"\\n").ToArray(); - buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[0]); - CurrentColumn += strLines[0].Length; - for (int i = 1; i < strLines.Length; i++) { - InsertLineBreak (); - buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[i]); - CurrentColumn += strLines[i].Length; - } - OnTextChanged (this, null); - RegisterForGraphicUpdate(); - } - - /// - /// Insert a line break. - /// - protected void InsertLineBreak() - { - buffer.Insert(CurrentLine + 1, buffer[CurrentLine].Substring(CurrentColumn)); - buffer [CurrentLine] = buffer [CurrentLine].Substring (0, CurrentColumn); - CurrentLine++; - CurrentColumn = 0; - OnTextChanged (this, null); - } } } \ No newline at end of file diff --git a/src/TextFormatting.cs b/src/TextFormatting.cs new file mode 100644 index 0000000..64e7aea --- /dev/null +++ b/src/TextFormatting.cs @@ -0,0 +1,15 @@ +using System; + +namespace Crow.Coding +{ + public struct TextFormatting { + public Color Foreground; + public Color Background; + + public TextFormatting(Color fg, Color bg){ + Foreground = fg; + Background = bg; + } + } +} + diff --git a/src/XMLParser.cs b/src/XMLParser.cs index 6e4a3ea..0118571 100644 --- a/src/XMLParser.cs +++ b/src/XMLParser.cs @@ -8,11 +8,6 @@ namespace Crow.Coding { public class XMLParser : Parser { - - public XMLParser (CodeTextBuffer _buffer) : base(_buffer) - { - } - public new enum TokenType { Unknown = Parser.TokenType.Unknown, WhiteSpace = Parser.TokenType.WhiteSpace, @@ -30,6 +25,7 @@ namespace Crow.Coding AttributeValueClosing = Parser.TokenType.StringLitteralClosing, AttributeValue = Parser.TokenType.StringLitteral, } + public enum States { init, //first statement of prolog, xmldecl should only apear in this state @@ -38,12 +34,16 @@ namespace Crow.Coding ExternalSubsetInit, ExternalSubset, DTDEnd,//doctype finished - XML, - StartTag, - Content, - EndTag, - XMLEnd + XML,//normal xml + StartTag,//inside start tag + Content,//after start tag with no closing slash + EndTag } + + #region CTOR + public XMLParser (CodeBuffer _buffer) : base(_buffer) {} + #endregion + enum Keywords { DOCTYPE, @@ -97,10 +97,10 @@ namespace Crow.Coding // } #endregion - public override void SetLineInError (int lineNumber) + public override void SetLineInError (ParsingException ex) { - base.SetLineInError (lineNumber); - Tokens[lineNumber].EndingState = (int)States.init; + base.SetLineInError (ex); + Tokens[ex.Line].EndingState = (int)States.init; } public override void Parse (int line) @@ -112,16 +112,14 @@ namespace Crow.Coding eof = false; bool eol = false; + //retrieve current parser state from previous line if (line > 0) curState = (States)Tokens [line - 1].EndingState; else curState = States.init; - TokensLine = Tokens [line]; - - States previousEndingState = (States)TokensLine.EndingState; - - TokensLine.Clear (); + States previousEndingState = (States)Tokens[line].EndingState; + Tokens[line].Clear (); @@ -244,10 +242,8 @@ namespace Crow.Coding } } - TokensLine.EndingState = (int)curState; - TokensLine.Dirty = false; - - Debug.WriteLine ("\tState:{0}->{1}", previousEndingState, curState); + Tokens[line].EndingState = (int)curState; + Tokens [line].Dirty = false; if (previousEndingState != curState && line < Tokens.Count - 1) Tokens [line + 1].Dirty = true;