]> O.S.I.I.S - jp/crowedit.git/commitdiff
debug, clean, unfold current loc foldings
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 7 Oct 2021 14:22:20 +0000 (14:22 +0000)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 7 Oct 2021 14:22:20 +0000 (14:22 +0000)
CrowEditBase/src/Compiler/SyntaxNode.cs
CrowEditBase/src/Editor.cs
CrowEditBase/src/SourceEditor.cs
plugins/CEXmlPlugin/src/Parsing/XmlTokenizer.cs

index bee36228936f5369b28287e9209db9e57f511d30..c0080ee1078ef4417ffe1bb3f59008f6bf424991 100644 (file)
@@ -17,6 +17,7 @@ namespace CrowEditBase
                public override SyntaxRootNode Root => this;
                public override bool IsFoldable => false;
                public override SyntaxNode NextSiblingOrParentsNextSibling => null;
+               public override void UnfoldToTheTop() {}
        }
        public class SyntaxNode {
                public SyntaxNode Parent { get; private set; }
@@ -25,7 +26,10 @@ namespace CrowEditBase
                public virtual bool IsComplete => EndToken.HasValue;
                public virtual bool IsFoldable => Parent.StartLine != StartLine && lineCount > 1;
                public virtual SyntaxRootNode Root => Parent.Root;
-
+               public virtual void UnfoldToTheTop () {
+                       isFolded = false;
+                       Parent.UnfoldToTheTop ();
+               }
 
                List<SyntaxNode> children = new List<SyntaxNode> ();
                public IEnumerable<SyntaxNode> Children => children;
@@ -50,19 +54,6 @@ namespace CrowEditBase
                                return null;
                        }
                }
-               /*public virtual int lineCount2 {
-                       get {
-                               SyntaxNode ns = NextSibling;
-                               if (ns == null)
-                                       return Parent.StartLine + Parent.lineCount2 - StartLine;
-                               return ns.StartLine - StartLine;
-                       }
-               }*/
-               /*public virtual int FoldedLineCount {
-                       get {
-
-                       }
-               }*/
                public virtual SyntaxNode NextSiblingOrParentsNextSibling
                        => NextSibling ?? Parent.NextSiblingOrParentsNextSibling;
                public IEnumerable<SyntaxNode> FoldableNodes {
index 9d3e25816acf5a5f6af7f6e4bd36a9208b5ea1bf..d7991219e47fb6ec40db2f75d0a6bf6518220ae6 100644 (file)
@@ -43,17 +43,6 @@ namespace Crow
                        ContextCommands = new CommandGroup (CMDCut, CMDCopy, CMDPaste);
                }
 
-               /*protected override CharLocation? SelectionStart {
-                       get => base.SelectionStart;
-                       set {
-                               if (SelectionStart == value)
-                                       return;
-                               base.SelectionStart = value;
-                               CMDCopy.CanExecute = CMDCut.CanExecute = !SelectionIsEmpty;
-                       }
-               }*/
-
-
                public TextDocument Document {
                        get => document;
                        set {
@@ -107,7 +96,7 @@ namespace Crow
                                CMDCopy.CanExecute = CMDCut.CanExecute = !SelectionIsEmpty;
                        }
                }
-               public virtual int CurrentLine {
+               public int CurrentLine {
                        get => currentLoc.HasValue ? currentLoc.Value.Line : 0;
                        set {
                                if (currentLoc?.Line == value)
@@ -118,7 +107,7 @@ namespace Crow
                                CMDCopy.CanExecute = CMDCut.CanExecute = !SelectionIsEmpty;
                        }
                }
-               public virtual int CurrentColumn {
+               public int CurrentColumn {
                        get => currentLoc.HasValue ? currentLoc.Value.Column < 0 ? 0 : currentLoc.Value.Column : 0;
                        set {
                                if (CurrentColumn == value)
@@ -475,13 +464,14 @@ namespace Crow
 
                        gr.Translate (ScrollX, ScrollY);
                }
-               protected int getLineIndex (Point mouseLocalPos) =>
+               protected int getLineIndexFromMousePosition (Point mouseLocalPos) =>
                        (int)Math.Min (Math.Max (0, Math.Floor ((mouseLocalPos.Y + ScrollY)/ lineHeight)), visualLineCount - 1);
                protected int getVisualLineIndex (Point mouseLocalPos) =>
                        (int)Math.Min (Math.Max (0, Math.Floor (mouseLocalPos.Y / lineHeight)), visibleLines - 1);
+               protected virtual int visualCurrentLine => CurrentLoc.HasValue ? CurrentLoc.Value.Line : 0;
 
                protected virtual void updateHoverLocation (Point mouseLocalPos) {
-                       int hoverLine = getLineIndex (mouseLocalPos);
+                       int hoverLine = getLineIndexFromMousePosition (mouseLocalPos);
                        NotifyValueChanged("MouseY", mouseLocalPos.Y + ScrollY);
                        NotifyValueChanged("ScrollY", ScrollY);
                        NotifyValueChanged("VisibleLines", visibleLines);
@@ -494,7 +484,6 @@ namespace Crow
                }
                protected virtual bool cancelLinePrint (double lineHeght, double y, int clientHeight) => false;
                RectangleD? textCursor = null;
-               protected virtual int visualCurrentLine => CurrentLoc.HasValue ? CurrentLoc.Value.Line : 0;
 
                public virtual bool DrawCursor (Context ctx, out Rectangle rect) {
                        if (CurrentLoc == null) {
@@ -659,11 +648,6 @@ namespace Crow
 
                        (IFace as CrowEditBase.CrowEditBase).CurrentEditor = this;
                }
-               /*protected override void onUnfocused (object sender, EventArgs e)
-               {
-                       base.onUnfocused (sender, e);
-                       RegisterForRedraw ();
-               }*/
                public override void onMouseEnter (object sender, MouseMoveEventArgs e) {
                        base.onMouseEnter (sender, e);
                        if (!Focusable)
index edca959636ee9776231c6c98717c858a26fac3e4..41d5b7d3769cffd4c25b6113e1e28aa108383b35 100644 (file)
@@ -128,6 +128,20 @@ namespace Crow
                        leftMargin += leftMarginRightGap;
                        //updateVisibleColumns ();
                }
+
+               protected override CharLocation? CurrentLoc {
+                       get => currentLoc;
+                       set {
+                               if (currentLoc == value)
+                                       return;
+                               currentLoc = value;
+                               if (currentLoc.HasValue)
+                                       getFoldContainingLine (currentLoc.Value.Line)?.UnfoldToTheTop();
+                               NotifyValueChanged ("CurrentLine", CurrentLine);
+                               NotifyValueChanged ("CurrentColumn", CurrentColumn);
+                               CMDCopy.CanExecute = CMDCut.CanExecute = !SelectionIsEmpty;
+                       }
+               }
                public override int measureRawSize(LayoutingType lt)
                {
                        DbgLogger.StartEvent(DbgEvtType.GOMeasure, this, lt);
@@ -152,7 +166,7 @@ namespace Crow
                        hideOverlay ();
                        if (mouseIsInMargin) {
                                if (e.Button == MouseButton.Left && mouseIsInFoldRect) {
-                                       SyntaxNode curNode = getFoldFromLine (hoverLoc.Value.Line);
+                                       SyntaxNode curNode = getFoldStartingAt (hoverLoc.Value.Line);
                                        if (curNode != null) {
                                                curNode.isFolded = !curNode.isFolded;
                                                textMeasureIsUpToDate = false;
@@ -165,14 +179,6 @@ namespace Crow
 
                        base.onMouseDown (sender, e);
                }
-               SyntaxNode getFoldFromLine (int line) {
-                       if (!(Document is SourceDocument doc))
-                               return null;
-                       IEnumerable<SyntaxNode> folds = doc.SyntaxRootNode.FoldableNodes;
-                       if (folds == null)
-                               return null;
-                       return folds.FirstOrDefault (n => n.StartLine == line);
-               }
                protected override void mouseMove (MouseEventArgs e) {
                        Point mLoc = ScreenPointToLocal (e.Position);
                        if (mLoc.X < leftMargin - leftMarginRightGap) {
@@ -203,9 +209,8 @@ namespace Crow
                                RegisterForRedraw ();
                        }
                }
-               //int hoverVisualLine, visualLine;
                protected override void updateHoverLocation (Point mouseLocalPos) {
-                       int hoverVisualLine = getLineIndex (mouseLocalPos);
+                       int hoverVisualLine = getLineIndexFromMousePosition (mouseLocalPos);
                        int hoverLine = hoverVisualLine + countFoldedLinesUntil (hoverVisualLine);
                        NotifyValueChanged("MouseY", mouseLocalPos.Y + ScrollY);
                        NotifyValueChanged("ScrollY", ScrollY);
@@ -224,57 +229,6 @@ namespace Crow
                                updateLocation (gr, ClientRectangle.Width, ref hoverLoc);
                        }
                }
-               int getVisualLine (int absoluteLine) {
-                       if (!(Document is SourceDocument doc))
-                               return absoluteLine;
-                       int foldedLines = 0;
-                       IEnumerator<SyntaxNode> foldsEnum = doc.SyntaxRootNode.FoldableNodes.GetEnumerator();
-                       bool notEndOfFolds = foldsEnum.MoveNext();
-                       while (notEndOfFolds && foldsEnum.Current.StartLine < absoluteLine) {
-                               if (foldsEnum.Current.isFolded) {
-                                       foldedLines += foldsEnum.Current.LineCount - 1;
-                                       SyntaxNode nextNode = foldsEnum.Current.NextSiblingOrParentsNextSibling;
-                                       if (nextNode == null)
-                                               break;
-                                       notEndOfFolds = foldsEnum.MoveNext();
-                                       while (notEndOfFolds && foldsEnum.Current.StartLine < nextNode.StartLine)
-                                               notEndOfFolds = foldsEnum.MoveNext();
-                               } else
-                                       notEndOfFolds = foldsEnum.MoveNext();
-                       }
-                       return absoluteLine - foldedLines;
-               }
-               int countFoldedLinesUntil (int visualLine) {
-                       if (!(Document is SourceDocument doc))
-                               return 0;
-                       int foldedLines = 0;
-                       IEnumerator<SyntaxNode> nodeEnum = doc.SyntaxRootNode.FoldableNodes.GetEnumerator ();
-                       if (!nodeEnum.MoveNext())
-                               return 0;
-
-                       int l = 0;
-                       while (l < visualLine + foldedLines) {
-                               if (nodeEnum.Current.StartLine == l) {
-                                       if (nodeEnum.Current.isFolded) {
-                                               foldedLines += nodeEnum.Current.lineCount - 1;
-                                               SyntaxNode nextNode = nodeEnum.Current.NextSiblingOrParentsNextSibling;
-                                               if (nextNode == null || !nodeEnum.MoveNext())
-                                                       return foldedLines;
-
-                                               while (nodeEnum.Current.StartLine < nextNode.StartLine) {
-                                                       if (!nodeEnum.MoveNext())
-                                                               return foldedLines;
-                                               }
-
-                                       } else if (!nodeEnum.MoveNext())
-                                               return foldedLines;
-                               }
-                               l ++;
-                       }
-                       //Console.WriteLine ($"visualLine: {visualLine} foldedLines: {foldedLines}");
-                       return foldedLines;
-               }
-
                public override void onKeyDown(object sender, KeyEventArgs e)
                {
                        TextSpan selection = Selection;
@@ -345,17 +299,79 @@ namespace Crow
                        base.onKeyDown(sender, e);
                }
 
+
+               SyntaxNode getFoldStartingAt (int line) {
+                       if (!(Document is SourceDocument doc))
+                               return null;
+                       IEnumerable<SyntaxNode> folds = doc.SyntaxRootNode.FoldableNodes;
+                       if (folds == null)
+                               return null;
+                       return folds.FirstOrDefault (n => n.StartLine == line);
+               }
+               SyntaxNode getFoldContainingLine (int line) {
+                       if (!(Document is SourceDocument doc))
+                               return null;
+                       IEnumerable<SyntaxNode> folds = doc.SyntaxRootNode.FoldableNodes;
+                       if (folds == null)
+                               return null;
+                       return folds.LastOrDefault (n => n.StartLine <= line && n.EndLine >= line);
+               }
+
+               int getVisualLine (int absoluteLine) {
+                       if (!(Document is SourceDocument doc))
+                               return absoluteLine;
+                       int foldedLines = 0;
+                       IEnumerator<SyntaxNode> foldsEnum = doc.SyntaxRootNode.FoldableNodes.GetEnumerator();
+                       bool notEndOfFolds = foldsEnum.MoveNext();
+                       while (notEndOfFolds && foldsEnum.Current.StartLine < absoluteLine) {
+                               if (foldsEnum.Current.isFolded) {
+                                       foldedLines += foldsEnum.Current.LineCount - 1;
+                                       SyntaxNode nextNode = foldsEnum.Current.NextSiblingOrParentsNextSibling;
+                                       if (nextNode == null)
+                                               break;
+                                       notEndOfFolds = foldsEnum.MoveNext();
+                                       while (notEndOfFolds && foldsEnum.Current.StartLine < nextNode.StartLine)
+                                               notEndOfFolds = foldsEnum.MoveNext();
+                               } else
+                                       notEndOfFolds = foldsEnum.MoveNext();
+                       }
+                       return absoluteLine - foldedLines;
+               }
+               int countFoldedLinesUntil (int visualLine) {
+                       if (!(Document is SourceDocument doc))
+                               return 0;
+                       int foldedLines = 0;
+                       IEnumerator<SyntaxNode> nodeEnum = doc.SyntaxRootNode.FoldableNodes.GetEnumerator ();
+                       if (!nodeEnum.MoveNext())
+                               return 0;
+
+                       int l = 0;
+                       while (l < visualLine + foldedLines) {
+                               if (nodeEnum.Current.StartLine == l) {
+                                       if (nodeEnum.Current.isFolded) {
+                                               foldedLines += nodeEnum.Current.lineCount - 1;
+                                               SyntaxNode nextNode = nodeEnum.Current.NextSiblingOrParentsNextSibling;
+                                               if (nextNode == null || !nodeEnum.MoveNext())
+                                                       return foldedLines;
+
+                                               while (nodeEnum.Current.StartLine < nextNode.StartLine) {
+                                                       if (!nodeEnum.MoveNext())
+                                                               return foldedLines;
+                                               }
+
+                                       } else if (!nodeEnum.MoveNext())
+                                               return foldedLines;
+                               }
+                               l ++;
+                       }
+                       //Console.WriteLine ($"visualLine: {visualLine} foldedLines: {foldedLines}");
+                       return foldedLines;
+               }
+
                protected override int getAbsoluteLineIndexFromVisualLineMove (int startLine, int visualLineDiff) {
                        int newVl = Math.Min (Math.Max (0, getVisualLine (startLine) + visualLineDiff), visualLineCount - 1);
                        return newVl + countFoldedLinesUntil (newVl);
                }
-               /*public override bool LineMove (int lineDiff) {
-                       CharLocation loc = CurrentLoc.Value;
-                       int newLine = getAbsoluteLineIndexFromVisualLineMove (loc.Line, lineDiff);
-                       if (newLine == loc.Line)
-                               return false;
-                       return base.LineMove (newLine - loc.Line);
-               }*/
 
                protected override int visualLineCount
                {
@@ -397,13 +413,6 @@ namespace Crow
                                        return;
                                }
 
-                               setFontForContext (gr);
-
-                               if (!textMeasureIsUpToDate) {
-                                       lock (linesMutex)
-                                               measureTextBounds (gr);
-                               }
-
                                double lineHeight = fe.Ascent + fe.Descent;
                                updateMargin ();
 
index f4cff61596fbc6dff3b3fa54502a42b725ee358b..a349a7f8af698be5cc2a665008b1526cf6265536 100644 (file)
@@ -80,6 +80,7 @@ namespace CrowEdit.Xml
                                                                        addTok (ref reader, XmlTokenType.BlockComment);
                                                                        reader.Advance (3);
                                                                        addTok (ref reader, XmlTokenType.BlockCommentEnd);
+                                                                       break;
                                                                } else
                                                                        reader.Read ();
                                                        }