]> O.S.I.I.S - jp/crowedit.git/commitdiff
handle buffer events triggering parsing in parser
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 5 Sep 2017 08:21:00 +0000 (10:21 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 5 Sep 2017 08:21:00 +0000 (10:21 +0200)
src/Parser.cs
src/SourceEditor.cs

index 5d0a92dd4be54698d95d4f2ae81e07a41d62a186..a35b6498a3b84e8ef3e9dab3cfc1b47cdb19e4e9 100644 (file)
@@ -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<TokenList> ();
                        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<TokenList> foldings = new Stack<TokenList>();
+                       //                      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;
index 7e8cafc5c1be3b82880a41cdfde719e79bb9d973..c7a3fdeba202bf59e1e8f52d3b18d27c24266708 100644 (file)
@@ -96,65 +96,6 @@ namespace Crow.Coding
                protected TextExtents te;
                #endregion
 
-               void updateFolding () {
-//                     Stack<TokenList> foldings = new Stack<TokenList>();
-//                     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;