OSDN Git Service

シンタックスハイライターと折り畳み関係を別のクラスに移した
[fooeditengine/FooEditEngine.git] / Core / SyntaxHilightGenerator.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace FooEditEngine
6 {
7     class SyntaxHilightGenerator : ILineInfoGenerator
8     {
9         const long AllowCallTicks = 1000 * 10000;   //see.DateTime.Ticks プロパティ
10         long lastUpdateTicks = DateTime.Now.Ticks;
11         IFoldingStrategy _folding;
12         bool _IsSync = true;
13
14         /// <summary>
15         /// シンタックスハイライター
16         /// </summary>
17         internal IHilighter Hilighter { get; set; }
18
19         public void Clear(LineToIndexTable lti)
20         {
21             for (int i = 0; i < lti.Count; i++)
22                 lti.GetRaw(i).Syntax = null;
23             lti.ClearLayoutCache();
24             this._IsSync = false;
25         }
26
27         public bool Generate(Document doc, LineToIndexTable lti, bool force = true)
28         {
29             if (this.Hilighter == null || doc.IsLocked)
30                 return false;
31
32             long nowTick = DateTime.Now.Ticks;
33             bool sync = force || !this._IsSync;
34             if (sync || Math.Abs(nowTick - this.lastUpdateTicks) >= AllowCallTicks)
35             {
36                 for (int i = 0; i < lti.Count; i++)
37                     this.HilightLine(lti, i);
38
39                 this.Hilighter.Reset();
40                 lti.ClearLayoutCache();
41
42                 this.lastUpdateTicks = nowTick;
43
44                 this._IsSync = true;
45
46                 return true;
47             }
48             return false;
49         }
50
51         private void HilightLine(LineToIndexTable lti, int row)
52         {
53             //シンタックスハイライトを行う
54             List<SyntaxInfo> syntax = new List<SyntaxInfo>();
55             string str = lti[row];
56             int level = this.Hilighter.DoHilight(str, str.Length, (s) =>
57             {
58                 if (s.type == TokenType.None || s.type == TokenType.Control)
59                     return;
60                 if (str[s.index + s.length - 1] == Document.NewLine)
61                     s.length--;
62                 syntax.Add(new SyntaxInfo(s.index, s.length, s.type));
63             });
64
65             LineToIndexTableData lineData = lti.GetRaw(row);
66             lineData.Syntax = syntax.ToArray();
67
68         }
69
70         public void Update(Document doc, int startIndex, int insertLength, int removeLength)
71         {
72         }
73     }
74 }