From 977ae1ffc9db7b05c582b5fec4ec15ff32f7c1cd Mon Sep 17 00:00:00 2001 From: test Date: Fri, 1 Jan 2021 17:47:12 +0900 Subject: [PATCH 1/1] =?utf8?q?=E3=82=B3=E3=83=B3=E3=83=88=E3=83=AD?= =?utf8?q?=E3=83=BC=E3=83=A9=E3=83=BC=E3=81=A8=E8=A2=AB=E3=81=A3=E3=81=A6?= =?utf8?q?=E3=81=84=E3=82=8B=E9=83=A8=E5=88=86=E3=81=8C=E3=81=82=E3=81=A3?= =?utf8?q?=E3=81=9F=E3=81=AE=E3=81=A7=E5=85=B1=E9=80=9A=E5=8C=96=E3=81=97?= =?utf8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Core/Automaion/FooTextBoxAutomationPeer.cs | 127 ++++++++++++----------------- Core/Controller.cs | 62 ++++++++++---- 2 files changed, 100 insertions(+), 89 deletions(-) diff --git a/Core/Automaion/FooTextBoxAutomationPeer.cs b/Core/Automaion/FooTextBoxAutomationPeer.cs index 5872981..b8c92da 100644 --- a/Core/Automaion/FooTextBoxAutomationPeer.cs +++ b/Core/Automaion/FooTextBoxAutomationPeer.cs @@ -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(); diff --git a/Core/Controller.cs b/Core/Controller.cs index fa97f54..86c4e90 100644 --- a/Core/Controller.cs +++ b/Core/Controller.cs @@ -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); } + /// + /// 移動後のキャレット位置を求める + /// + /// 起点となるキャレット位置 + /// 移動量 + /// 移動方法 + /// 実際に移動した量 + /// 移動後のキャレット位置 + 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); -- 2.11.0