OSDN Git Service

コンパイルエラーを修正した
[fooeditengine/FooEditEngine.git] / Core / FoldingGenerator.cs
1 using System;
2 using System.Linq;
3
4 namespace FooEditEngine
5 {
6     class FoldingGenerator : ILineInfoGenerator
7     {
8         public FoldingCollection FoldingCollection = new FoldingCollection();
9         const long AllowCallTicks = 1000 * 10000;   //see.DateTime.Ticks プロパティ
10         long lastUpdateTicks = DateTime.Now.Ticks;
11         IFoldingStrategy _folding;
12         bool _IsSync = true;
13
14         public IFoldingStrategy FoldingStrategy
15         {
16             get
17             {
18                 return this._folding;
19             }
20             set
21             {
22                 this._folding = value;
23                 if (value == null)
24                     this.FoldingCollection.Clear();
25             }
26         }
27
28         public void Clear(LineToIndexTable lti)
29         {
30             this.FoldingCollection.Clear();
31             this._IsSync = false;
32         }
33
34         public bool Generate(Document doc,LineToIndexTable lti, bool force = true)
35         {
36             if (doc.Length == 0)
37                 return false;
38             long nowTick = DateTime.Now.Ticks;
39             bool sync = force || !this._IsSync;
40             if (sync && Math.Abs(nowTick - this.lastUpdateTicks) >= AllowCallTicks)
41             {
42                 this.GenerateFolding(doc, lti, 0, doc.Length - 1);
43                 this.lastUpdateTicks = nowTick;
44                 this._IsSync = true;
45                 return true;
46             }
47             return false;
48         }
49
50         void GenerateFolding(Document doc, LineToIndexTable lti,int start, int end)
51         {
52             if (start > end)
53                 throw new ArgumentException("start <= endである必要があります");
54             if (this.FoldingStrategy != null)
55             {
56                 //再生成するとすべて展開状態になってしまうので、閉じてるやつだけを保存しておく
57                 FoldingItem[] closed_items = this.FoldingCollection.Where((e) => { return !e.Expand; }).ToArray();
58
59                 this.FoldingCollection.Clear();
60
61                 var items = this.FoldingStrategy.AnalyzeDocument(doc, start, end)
62                     .Where((item) =>
63                     {
64                         int startRow = lti.GetLineNumberFromIndex(item.Start);
65                         int endRow = lti.GetLineNumberFromIndex(item.End);
66                         return startRow != endRow;
67                     })
68                     .Select((item) => item);
69                 this.FoldingCollection.AddRange(items);
70
71                 this.FoldingCollection.ApplyExpandStatus(closed_items);
72             }
73         }
74
75         public void Update(Document doc, int startIndex, int insertLength, int removeLength)
76         {
77             this.FoldingCollection.UpdateData(doc, startIndex, insertLength, removeLength);
78             this._IsSync = false;
79             this.lastUpdateTicks = DateTime.Now.Ticks;
80         }
81     }
82 }