OSDN Git Service

コントローラーと被っている部分があったので共通化した
authortest <test@yahoo.co.jp>
Fri, 1 Jan 2021 08:47:12 +0000 (17:47 +0900)
committertest <test@yahoo.co.jp>
Fri, 1 Jan 2021 08:47:12 +0000 (17:47 +0900)
Core/Automaion/FooTextBoxAutomationPeer.cs
Core/Controller.cs

index 5872981..b8c92da 100644 (file)
@@ -356,14 +356,21 @@ namespace FooEditEngine
             Document doc = this.textbox.Document;
             if (unit == TextUnit.Character)
             {
-                if (this.start == this.end)
+                return;
+            }
+            if (unit == TextUnit.Format || unit == TextUnit.Word)
+            {
+                var t = doc.GetSepartor(this.start, (c) => Util.IsWordSeparator(c));
+                if (t == null)
+                    this.start = this.end = 0;
+                else
                 {
-                    if (this.start < this.textbox.Document.Length)
-                        this.end += 1;
+                    this.start = t.Item1;
+                    this.end = t.Item2;
                 }
                 return;
             }
-            if (unit == TextUnit.Format || unit == TextUnit.Word || unit == TextUnit.Line || unit == TextUnit.Paragraph)
+            if (unit == TextUnit.Line || unit == TextUnit.Paragraph)
             {
                 var t = doc.GetSepartor(this.start, (c) => c == Document.NewLine);
                 if (t == null)
@@ -494,31 +501,11 @@ namespace FooEditEngine
         {
             if (count == 0)
                 return 0;
-            switch (unit)
-            {
-                case TextUnit.Character:
-                    {
-                        int moved;
-                        this.start = this.MoveEndpointByCharacter(this.start,count,out moved);
-                        this.end = this.start + 1;
-                        return moved;
-                    }
-                case TextUnit.Format:
-                case TextUnit.Word:
-                case TextUnit.Line:
-                    {
-                        return this.MoveEndPointByLine(this.start, count, out this.start, out this.end);
-                    }
-                case TextUnit.Paragraph:
-                case TextUnit.Page:
-                case TextUnit.Document:
-                    {
-                        this.start = 0;
-                        this.end = this.textbox.Document.Length;
-                        return 1;
-                    }
-            }
-            throw new NotImplementedException();
+            Controller ctrl = textbox.Controller;
+            LineToIndexTable layoutLine = textbox.LayoutLineCollection;
+            int moved = this.MoveEndpointByUnit(TextPatternRangeEndpoint.Start, unit, count);
+            this.ExpandToEnclosingUnit(unit);
+            return moved;
         }
 
         public void MoveEndpointByRange(TextPatternRangeEndpoint endpoint, ITextRangeProvider targetRange, TextPatternRangeEndpoint targetEndpoint)
@@ -553,61 +540,53 @@ namespace FooEditEngine
         {
             if (count == 0)
                 return 0;
+
             int moved = 0;
-            if (unit == TextUnit.Character)
+            TextPoint caret = TextPoint.Null,newCaret = TextPoint.Null;
+            Controller ctrl = textbox.Controller;
+            LineToIndexTable layoutLine = textbox.LayoutLineCollection;
+
+            if (endpoint == TextPatternRangeEndpoint.Start)
+                caret = layoutLine.GetTextPointFromIndex(this.start);
+            else if (endpoint == TextPatternRangeEndpoint.End)
+                caret = layoutLine.GetTextPointFromIndex(this.end);
+
+            switch(unit)
             {
-                if (endpoint == TextPatternRangeEndpoint.Start)
-                {
-                    this.start = this.MoveEndpointByCharacter(this.start, count, out moved);
-                    if(this.start > this.end)
-                        this.end = this.start;
-                }
-                else if (endpoint == TextPatternRangeEndpoint.End)
-                {
-                    this.end = this.MoveEndpointByCharacter(this.end, count, out moved);
-                    if (this.end < this.start)
-                        this.start = this.end;
-                }
+                case TextUnit.Character:
+                    newCaret = ctrl.GetNextCaret(caret, count, MoveFlow.Character, out moved);
+                    break;
+                case TextUnit.Format:
+                case TextUnit.Word:
+                    newCaret = ctrl.GetNextCaret(caret, count, MoveFlow.Word, out moved);
+                    break;
+                case TextUnit.Line:
+                case TextUnit.Paragraph:
+                    newCaret = ctrl.GetNextCaret(caret, count, MoveFlow.Paragraph, out moved);
+                    break;
+                case TextUnit.Page:
+                case TextUnit.Document:
+                    this.start = 0;
+                    this.end = this.textbox.Document.Length - 1;
+                    moved = 1;
+                    break;
             }
-            if (unit == TextUnit.Format || unit == TextUnit.Word || unit == TextUnit.Line)
+
+            if (endpoint == TextPatternRangeEndpoint.Start)
             {
-                if (endpoint == TextPatternRangeEndpoint.Start)
-                    return this.MoveEndPointByLine(this.start, count, out this.start, out this.end);
-                else if (endpoint == TextPatternRangeEndpoint.End)
-                    return this.MoveEndPointByLine(this.end, count, out this.start, out this.end);
+                this.start = layoutLine.GetIndexFromTextPoint(newCaret);
+                if (this.start > this.end)
+                    this.end = this.start;
             }
-            if (unit == TextUnit.Paragraph || unit == TextUnit.Page || unit == TextUnit.Document)
+            else if (endpoint == TextPatternRangeEndpoint.End)
             {
-                this.start = 0;
-                this.end = this.textbox.Document.Length - 1;
-                moved = 1;
+                this.end = layoutLine.GetIndexFromTextPoint(newCaret);
+                if (this.end < this.start)
+                    this.start = this.end;
             }
             return moved;
         }
 
-        int MoveEndpointByCharacter(int index, int count,out int moved)
-        {
-            int newIndex = index + count - 1;
-            if (newIndex > this.textbox.Document.Length)
-                newIndex = this.textbox.Document.Length;
-            if (newIndex < 0)
-                newIndex = 0;
-            moved = newIndex - index;
-            return newIndex;
-        }
-
-        int MoveEndPointByLine(int index, int count,out int newStartInex,out int newEndInex)
-        {
-            LineToIndexTable layoutLineCollection = this.textbox.LayoutLineCollection;
-            Controller controller = this.textbox.Controller;
-            TextPoint currentPoint = layoutLineCollection.GetTextPointFromIndex(index);
-            TextPoint newPoint = controller.GetTextPointAfterMoveLine(count, currentPoint);
-            int lineLength = layoutLineCollection.GetLengthFromLineNumber(newPoint.row);
-            newStartInex = layoutLineCollection.GetIndexFromLineNumber(newPoint.row);
-            newEndInex = newStartInex + lineLength;
-            return newPoint.row - currentPoint.row;
-        }
-
         public void RemoveFromSelection()
         {
             throw new InvalidOperationException();
index fa97f54..86c4e90 100644 (file)
@@ -20,10 +20,12 @@ using System.Drawing;
 
 namespace FooEditEngine
 {
-    internal enum MoveFlow
+    public enum MoveFlow
     {
-        Horizontical,
-        Vertical,
+        Character,
+        Word,
+        Line,
+        Paragraph
     }
     internal enum ScrollDirection
     {
@@ -519,21 +521,51 @@ namespace FooEditEngine
         public void MoveCaretHorizontical(int realLength, bool isSelected,bool alignWord = false)
         {
             TextPoint caret = this.Document.CaretPostion;
-            for (int i = Math.Abs(realLength); i > 0; i--)
-            {
-                bool MoveFlow = realLength > 0;
-                if (this.Document.RightToLeft)
-                    MoveFlow = !MoveFlow;
-                caret = this.MoveCaretHorizontical(caret,MoveFlow);
-
-                if (alignWord)
-                    caret = this.AlignNearestWord(caret, MoveFlow);
-            }
+            int moved;
+            caret = GetNextCaret(caret, realLength, alignWord ? MoveFlow.Word : MoveFlow.Character,out moved);
             this.View.JumpCaret(caret.row, caret.col, false);
             this.View.AdjustCaretAndSrc(AdjustFlow.Both);
             this.SelectWithMoveCaret(isSelected);
         }
 
+        /// <summary>
+        /// 移動後のキャレット位置を求める
+        /// </summary>
+        /// <param name="caret">起点となるキャレット位置</param>
+        /// <param name="count">移動量</param>
+        /// <param name="method">移動方法</param>
+        /// <param name="moved">実際に移動した量</param>
+        /// <returns>移動後のキャレット位置</returns>
+        public TextPoint GetNextCaret(TextPoint caret, int count,MoveFlow method,out int moved)
+        {
+            moved = 0;
+            if(method == MoveFlow.Character || method == MoveFlow.Word)
+            {
+                for (int i = Math.Abs(count); i > 0; i--)
+                {
+                    bool moveFlow = count > 0;
+                    if (this.Document.RightToLeft)
+                        moveFlow = !moveFlow;
+                    caret = this.MoveCaretHorizontical(caret, moveFlow);
+
+                    if (method == FooEditEngine.MoveFlow.Word)
+                        caret = this.AlignNearestWord(caret, moveFlow);
+                    moved++;
+                }
+            }
+            if(method == MoveFlow.Line || method == MoveFlow.Paragraph)
+            {
+                for (int i = Math.Abs(count); i > 0; i--)
+                {
+                    caret = this.MoveCaretVertical(caret, count > 0);
+                    moved++;
+                }
+            }
+            if (count < 0)
+                moved = -moved;
+            return caret;
+        }
+
         TextPoint AlignNearestWord(TextPoint caret,bool MoveFlow)
         {
             string str = this.View.LayoutLines[caret.row];
@@ -564,8 +596,8 @@ namespace FooEditEngine
         public void MoveCaretVertical(int deltarow,bool isSelected)
         {
             TextPoint caret = this.Document.CaretPostion;
-            for (int i = Math.Abs(deltarow); i > 0; i--)
-                caret = this.MoveCaretVertical(caret, deltarow > 0);
+            int moved;
+            caret = this.GetNextCaret(caret, deltarow, MoveFlow.Line,out moved);
             this.View.JumpCaret(caret.row, caret.col, true);
             this.View.AdjustCaretAndSrc(AdjustFlow.Both);
             this.SelectWithMoveCaret(isSelected);