From: konekoneko Date: Sat, 15 Mar 2014 06:16:51 +0000 (+0900) Subject: FoldingItem.Parentを廃止した。これに伴い、FoldingCollectionにいくつかのメソッドを追加した X-Git-Url: http://git.osdn.net/view?p=fooeditengine%2FFooEditEngine.git;a=commitdiff_plain;h=694f028a3b69047dd29e3f321483625afa48d43a FoldingItem.Parentを廃止した。これに伴い、FoldingCollectionにいくつかのメソッドを追加した --- diff --git a/Common/EditView.cs b/Common/EditView.cs index 79c1810..6de6a4c 100644 --- a/Common/EditView.cs +++ b/Common/EditView.cs @@ -281,9 +281,11 @@ namespace FooEditEngine if (foldingData != null) { - if ((foldingData.Parent == null || foldingData.Parent.Expand) && foldingData.IsFirstLine(this.LayoutLines, i)) + if ((!this.LayoutLines.FoldingCollection.IsHasParent(foldingData) || + !this.LayoutLines.FoldingCollection.IsParentHidden(foldingData)) + && foldingData.IsFirstLine(this.LayoutLines, i)) render.DrawFoldingMark(foldingData.Expand, this.PageBound.X + this.GetRealtiveX(AreaType.FoldingArea), pos.Y); - if (foldingData.IsHidden(lineIndex)) + if (this.LayoutLines.FoldingCollection.IsHidden(lineIndex)) continue; } @@ -477,9 +479,7 @@ namespace FooEditEngine lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i); lineLength = this.LayoutLines.GetLengthFromLineNumber(i); - FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength); - - if (foldingData != null && foldingData.IsHidden(lineHeadIndex)) + if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex)) continue; if (y + height > p.Y) @@ -542,8 +542,7 @@ namespace FooEditEngine { int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i); int lineLength = this.LayoutLines.GetLengthFromLineNumber(i); - FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength); - if (foldingData != null && foldingData.IsHidden(lineHeadIndex)) + if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex)) continue; p.Y += this.LayoutLines.GetLayout(i).Height; } @@ -565,9 +564,12 @@ namespace FooEditEngine int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row); int lineLength = this.LayoutLines.GetLengthFromLineNumber(row); FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength); - if (foldingData != null && (!foldingData.IsFirstLine(this.LayoutLines, row) || foldingData.IsParentHidden())) + if(foldingData != null) { - this.LayoutLines.FoldingCollection.Expand(foldingData); + if (this.LayoutLines.FoldingCollection.IsParentHidden(foldingData) || !foldingData.IsFirstLine(this.LayoutLines, row)) + { + this.LayoutLines.FoldingCollection.Expand(foldingData); + } } } @@ -690,9 +692,7 @@ namespace FooEditEngine int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(currentRow); int lineLength = this.LayoutLines.GetLengthFromLineNumber(currentRow); - FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength); - - if (foldingData != null && foldingData.IsHidden(lineHeadIndex)) + if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex)) continue; y += this.LayoutLines.GetLayout(currentRow).Height; @@ -717,8 +717,7 @@ namespace FooEditEngine { int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i); int lineLength = this.LayoutLines.GetLengthFromLineNumber(i); - FoldingItem foldingData = this.LayoutLines.FoldingCollection.GetFarestHiddenFoldingData(lineHeadIndex, lineLength); - if (foldingData != null && foldingData.IsHidden(lineHeadIndex)) + if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex)) continue; if (count <= 0) return i; @@ -846,9 +845,7 @@ namespace FooEditEngine int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row); int lineLength = this.LayoutLines.GetLengthFromLineNumber(row); - FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength); - - if (foldingData != null && foldingData.End < this.Document.Length - 1 && foldingData.IsHidden(lineHeadIndex)) + if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex)) continue; ITextLayout layout = this.LayoutLines.GetLayout(row); diff --git a/Common/FoldingCollection.cs b/Common/FoldingCollection.cs index cc3c1b7..119f5ea 100644 --- a/Common/FoldingCollection.cs +++ b/Common/FoldingCollection.cs @@ -61,36 +61,18 @@ namespace FooEditEngine set; } - internal FoldingItem Parent; - /// /// コンストラクター /// /// 開始インデックス /// 終了インデックス - public FoldingItem(int start, int end) + /// 展開フラグ + public FoldingItem(int start, int end,bool expand = true) { if (start >= end) throw new ArgumentException("start < endである必要があります"); this.Range = new Range(start, end); - this.Expand = true; - } - - internal bool IsParentHidden() - { - if (this.Parent != null && !this.Parent.Expand) - return true; - else - return false; - } - - internal bool IsHidden(int index) - { - if (this.Parent != null && !this.Parent.Expand) - return true; - if (!this.Expand && index > this.Start && index <= this.End) - return true; - return false; + this.Expand = expand; } internal bool IsFirstLine(LineToIndexTable layoutLines, int row) @@ -188,12 +170,6 @@ namespace FooEditEngine { if (item.Start == data.Start && item.End == data.End) return; - if (item.Parent != null && data.Start < item.Parent.Start && data.End >= item.Parent.End) - continue; - else if (item.Start < data.Start && item.End > data.End) - data.Parent = item; - else if (item.Start > data.Start && item.End <= data.End) - item.Parent = data; } this.collection.Add(data); } @@ -275,14 +251,12 @@ namespace FooEditEngine /// 親ノードも含めてすべて展開されます public void Expand(FoldingItem foldingData) { - while (foldingData != null) - { - foldingData.Expand = true; - if (foldingData.Parent == null || foldingData.Parent.Expand) - break; - else - foldingData = foldingData.Parent; - } + if (this.collection.Count == 0) + return; + this.collection.Rebuild(); + List items = this.collection.Query(foldingData.Range); + foreach (FoldingItem item in items) + item.Expand = true; this.StatusChanged(this, new FoldingItemStatusChangedEventArgs(foldingData)); } @@ -305,6 +279,64 @@ namespace FooEditEngine } /// + /// インデックスを含むノードが折りたたまれているかを判定する + /// + /// インデックス + /// 折りたたまれていれば真を返す。そうでない場合・ノードが存在しない場合は偽を返す + public bool IsHidden(int index) + { + this.collection.Rebuild(); + List items = this.collection.Query(index); + if (items.Count == 0) + return false; + int hiddenCount = items.Count((item) =>{ + return !item.Expand && index > item.Start && index <= item.End; + }); + return hiddenCount > 0; + } + + /// + /// 親ノードが隠されているかどうかを判定する + /// + /// 判定したいノード + /// 隠されていれば真を返す + public bool IsParentHidden(FoldingItem foldingItem) + { + if (foldingItem == null) + return false; + this.collection.Rebuild(); + List items = this.collection.Query(foldingItem.Range); + if (items.Count == 0) + return false; + int hiddenCount = items.Count((item) => + { + //自分自身ノードか + if (foldingItem.Range.Equals(item.Range)) + return false; + //ノードが親かつ隠されているかどうか + return !item.Expand && item.Start < foldingItem.Start && item.End > foldingItem.End; + }); + return hiddenCount > 0; + } + + /// + /// 親を持っているか判定する + /// + /// 判定したいノード + /// 親を持っていれば真を返す + public bool IsHasParent(FoldingItem foldingItem) + { + if (foldingItem == null) + return false; + this.collection.Rebuild(); + List items = this.collection.Query(foldingItem.Range); + if (items.Count == 0 || items.Count == 1) + return false; + int parentItemCount = items.Count((item) => item.Start < foldingItem.Start && item.End > foldingItem.End); + return parentItemCount > 0; + } + + /// /// 指定した範囲に属する親ノードを取得する /// /// 開始インデックス @@ -313,17 +345,25 @@ namespace FooEditEngine /// 指定した範囲には属する中で隠された親ノードだけが取得される public FoldingItem GetFarestHiddenFoldingData(int index, int length) { - FoldingItem foldingData = this.Get(index, length); - if (foldingData == null) + if (this.collection.Count == 0) return null; - while (foldingData.Parent != null) + this.collection.Rebuild(); + List items = this.collection.Query(new Range(index, index + length - 1)); + + //もっとも範囲の広いアイテムが親を表す + FoldingItem parentItem = null; + int max = 0; + foreach(FoldingItem item in items) { - if (foldingData.Parent.Expand) - break; - else - foldingData = foldingData.Parent; + int item_length = item.End -item.Start + 1; + if(item_length > max) + { + max = item_length; + parentItem = item; + } } - return foldingData; + + return parentItem; } } } diff --git a/WPF/UnitTest/UnitTest1.cs b/WPF/UnitTest/UnitTest1.cs index 82e9c1b..b15d68c 100644 --- a/WPF/UnitTest/UnitTest1.cs +++ b/WPF/UnitTest/UnitTest1.cs @@ -19,6 +19,40 @@ namespace UnitTest public class FoldingCollectionTest { [TestMethod] + public void HiddenTest() + { + FoldingCollection collection = new FoldingCollection(); + collection.Add(new FoldingItem(0, 10,false)); + collection.Add(new FoldingItem(1, 5)); + Assert.IsTrue(collection.IsHidden(2)); + collection = new FoldingCollection(); + collection.Add(new FoldingItem(0, 10)); + collection.Add(new FoldingItem(1, 5,false)); + Assert.IsTrue(collection.IsHidden(2)); + } + [TestMethod] + public void HiddenParrentTest() + { + FoldingCollection collection = new FoldingCollection(); + FoldingItem item = new FoldingItem(0, 10, false); + collection.Add(item); + FoldingItem item1 = new FoldingItem(1, 5); + collection.Add(item1); + Assert.IsTrue(collection.IsParentHidden(item1)); + Assert.IsTrue(!collection.IsParentHidden(item)); + } + [TestMethod] + public void HasParrentTest() + { + FoldingCollection collection = new FoldingCollection(); + FoldingItem item = new FoldingItem(0, 10, false); + collection.Add(item); + FoldingItem item1 = new FoldingItem(1, 5); + collection.Add(item1); + Assert.IsTrue(collection.IsHasParent(item1)); + Assert.IsTrue(!collection.IsHasParent(item)); + } + [TestMethod] public void GetFoldingItem() { FoldingCollection collection = new FoldingCollection();