From 5206215d9b539a1380e44a536d1a287f95ceac8a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 7 Oct 2021 14:22:20 +0000 Subject: [PATCH] debug, clean, unfold current loc --- CrowEditBase/src/Compiler/SyntaxNode.cs | 19 +-- CrowEditBase/src/Editor.cs | 26 +-- CrowEditBase/src/SourceEditor.cs | 161 +++++++++--------- .../CEXmlPlugin/src/Parsing/XmlTokenizer.cs | 1 + 4 files changed, 96 insertions(+), 111 deletions(-) diff --git a/CrowEditBase/src/Compiler/SyntaxNode.cs b/CrowEditBase/src/Compiler/SyntaxNode.cs index bee3622..c0080ee 100644 --- a/CrowEditBase/src/Compiler/SyntaxNode.cs +++ b/CrowEditBase/src/Compiler/SyntaxNode.cs @@ -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 children = new List (); public IEnumerable 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 FoldableNodes { diff --git a/CrowEditBase/src/Editor.cs b/CrowEditBase/src/Editor.cs index 9d3e258..d799121 100644 --- a/CrowEditBase/src/Editor.cs +++ b/CrowEditBase/src/Editor.cs @@ -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) diff --git a/CrowEditBase/src/SourceEditor.cs b/CrowEditBase/src/SourceEditor.cs index edca959..41d5b7d 100644 --- a/CrowEditBase/src/SourceEditor.cs +++ b/CrowEditBase/src/SourceEditor.cs @@ -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 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 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 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 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 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 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 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 (); diff --git a/plugins/CEXmlPlugin/src/Parsing/XmlTokenizer.cs b/plugins/CEXmlPlugin/src/Parsing/XmlTokenizer.cs index f4cff61..a349a7f 100644 --- a/plugins/CEXmlPlugin/src/Parsing/XmlTokenizer.cs +++ b/plugins/CEXmlPlugin/src/Parsing/XmlTokenizer.cs @@ -80,6 +80,7 @@ namespace CrowEdit.Xml addTok (ref reader, XmlTokenType.BlockComment); reader.Advance (3); addTok (ref reader, XmlTokenType.BlockCommentEnd); + break; } else reader.Read (); } -- 2.47.3