From: Jean-Philippe Bruyère Date: Tue, 5 Sep 2017 08:21:00 +0000 (+0200) Subject: handle buffer events triggering parsing in parser X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=bce97af1a0ed3e9410bc8f8a3e913aaca56bc0e3;p=jp%2Fcrowedit.git handle buffer events triggering parsing in parser --- diff --git a/src/Parser.cs b/src/Parser.cs index 5d0a92d..a35b649 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -41,6 +41,12 @@ namespace Crow.Coding public Parser (CodeBuffer _buffer) { buffer = _buffer; + + buffer.LineUpadateEvent += Buffer_LineUpadateEvent; + buffer.LineAdditionEvent += Buffer_LineAdditionEvent;; + buffer.LineRemoveEvent += Buffer_LineRemoveEvent; + buffer.BufferCleared += Buffer_BufferCleared; + Tokens = new List (); if (buffer.LineCount > 0) eof = false; @@ -48,6 +54,91 @@ namespace Crow.Coding #endregion + #region Buffer events handlers + void Buffer_BufferCleared (object sender, EventArgs e) + { + Tokens.Clear (); + } + void Buffer_LineAdditionEvent (object sender, CodeBufferEventArgs e) + { + for (int i = 0; i < e.LineCount; i++) { + int lptr = e.LineStart + i; + Tokens.Insert (lptr, new TokenList ()); + tryParseBufferLine (e.LineStart + i); + } + reparseSource (); + } + + void Buffer_LineRemoveEvent (object sender, CodeBufferEventArgs e) + { + for (int i = 0; i < e.LineCount; i++) + Tokens.RemoveAt (e.LineStart + i); + reparseSource (); + } + + void Buffer_LineUpadateEvent (object sender, CodeBufferEventArgs e) + { + for (int i = 0; i < e.LineCount; i++) + tryParseBufferLine (e.LineStart + i); + reparseSource (); + } + #endregion + + void updateFolding () { + // Stack foldings = new Stack(); + // bool inStartTag = false; + // + // for (int i = 0; i < parser.Tokens.Count; i++) { + // TokenList tl = parser.Tokens [i]; + // tl.foldingTo = null; + // int fstTK = tl.FirstNonBlankTokenIndex; + // if (fstTK > 0 && fstTK < tl.Count - 1) { + // if (tl [fstTK + 1] != XMLParser.TokenType.ElementName) + // continue; + // if (tl [fstTK] == XMLParser.TokenType.ElementStart) { + // //search closing tag + // int tkPtr = fstTK+2; + // while (tkPtr < tl.Count) { + // if (tl [tkPtr] == XMLParser.TokenType.ElementClosing) + // + // tkPtr++; + // } + // if (tl.EndingState == (int)XMLParser.States.Content) + // foldings.Push (tl); + // else if (tl.EndingState == (int)XMLParser.States.StartTag) + // inStartTag = true; + // continue; + // } + // if (tl [fstTK] == XMLParser.TokenType.ElementEnd) { + // TokenList tls = foldings.Pop (); + // int fstTKs = tls.FirstNonBlankTokenIndex; + // if (tls [fstTK + 1].Content == tl [fstTK + 1].Content) { + // tl.foldingTo = tls; + // continue; + // } + // parser.CurrentPosition = tls [fstTK + 1].Start; + // parser.SetLineInError(new ParsingException(parser, "closing tag not corresponding")); + // } + // + // } + // } + } + void reparseSource () { + for (int i = 0; i < Tokens.Count; i++) { + if (Tokens[i].Dirty) + tryParseBufferLine (i); + } + updateFolding (); + } + void tryParseBufferLine(int lPtr) { + try { + Parse (lPtr); + } catch (ParsingException ex) { + Debug.WriteLine (ex.ToString ()); + SetLineInError (ex); + } + } + CodeBuffer buffer; internal int currentLine = 0; diff --git a/src/SourceEditor.cs b/src/SourceEditor.cs index 7e8cafc..c7a3fde 100644 --- a/src/SourceEditor.cs +++ b/src/SourceEditor.cs @@ -96,65 +96,6 @@ namespace Crow.Coding protected TextExtents te; #endregion - void updateFolding () { -// Stack foldings = new Stack(); -// bool inStartTag = false; -// -// for (int i = 0; i < parser.Tokens.Count; i++) { -// TokenList tl = parser.Tokens [i]; -// tl.foldingTo = null; -// int fstTK = tl.FirstNonBlankTokenIndex; -// if (fstTK > 0 && fstTK < tl.Count - 1) { -// if (tl [fstTK + 1] != XMLParser.TokenType.ElementName) -// continue; -// if (tl [fstTK] == XMLParser.TokenType.ElementStart) { -// //search closing tag -// int tkPtr = fstTK+2; -// while (tkPtr < tl.Count) { -// if (tl [tkPtr] == XMLParser.TokenType.ElementClosing) -// -// tkPtr++; -// } -// if (tl.EndingState == (int)XMLParser.States.Content) -// foldings.Push (tl); -// else if (tl.EndingState == (int)XMLParser.States.StartTag) -// inStartTag = true; -// continue; -// } -// if (tl [fstTK] == XMLParser.TokenType.ElementEnd) { -// TokenList tls = foldings.Pop (); -// int fstTKs = tls.FirstNonBlankTokenIndex; -// if (tls [fstTK + 1].Content == tl [fstTK + 1].Content) { -// tl.foldingTo = tls; -// continue; -// } -// parser.CurrentPosition = tls [fstTK + 1].Start; -// parser.SetLineInError(new ParsingException(parser, "closing tag not corresponding")); -// } -// -// } -// } - } - void reparseSource () { - if (parser == null) - return; - for (int i = 0; i < parser.Tokens.Count; i++) { - if (parser.Tokens[i].Dirty) - tryParseBufferLine (i); - } - updateFolding (); - } - void tryParseBufferLine(int lPtr) { - if (parser == null) - return; - try { - parser.Parse (lPtr); - } catch (ParsingException ex) { - Debug.WriteLine (ex.ToString ()); - parser.SetLineInError (ex); - } - RegisterForGraphicUpdate (); - } void measureLeftMargin () { leftMargin = 0; if (PrintLineNumbers) @@ -188,8 +129,6 @@ namespace Crow.Coding #region Buffer events handlers void Buffer_BufferCleared (object sender, EventArgs e) { - if (parser != null) - parser.Tokens.Clear (); buffer.longestLineCharCount = 0; buffer.longestLineIdx = 0; measureLeftMargin (); @@ -206,14 +145,8 @@ namespace Crow.Coding buffer.longestLineCharCount = charCount; }else if (lptr <= buffer.longestLineIdx) buffer.longestLineIdx++; - if (parser != null) { - parser.Tokens.Insert (lptr, new TokenList ()); - tryParseBufferLine (e.LineStart + i); - } } measureLeftMargin (); - if (parser != null) - reparseSource (); RegisterForGraphicUpdate (); } @@ -224,14 +157,10 @@ namespace Crow.Coding int lptr = e.LineStart + i; if (lptr <= buffer.longestLineIdx) trigFindLongestLine = true; - if (parser != null) - parser.Tokens.RemoveAt (lptr); } if (trigFindLongestLine) findLongestLineAndUpdateMaxScrollX (); measureLeftMargin (); - if (parser != null) - reparseSource (); RegisterForGraphicUpdate (); } @@ -246,9 +175,7 @@ namespace Crow.Coding buffer.longestLineCharCount = buffer.GetPrintableLine (lptr).Length; buffer.longestLineIdx = lptr; } - tryParseBufferLine (lptr); } - reparseSource (); if (trigFindLongestLine) findLongestLineAndUpdateMaxScrollX (); RegisterForGraphicUpdate (); @@ -1065,8 +992,8 @@ namespace Crow.Coding this.Insert ("\t"); break; case Key.F8: - if (parser != null) - reparseSource (); +// if (parser != null) +// reparseSource (); break; default: break;