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         bool _IsSync = true;
12
13         /// <summary>
14         /// シンタックスハイライター
15         /// </summary>
16         internal IHilighter Hilighter { get; set; }
17
18         public void Clear(LineToIndexTable lti)
19         {
20             for (int i = 0; i < lti.Count; i++)
21                 lti.GetRaw(i).Syntax = null;
22             lti.ClearLayoutCache();
23             this._IsSync = false;
24         }
25
26         public bool Generate(Document doc, LineToIndexTable lti, bool force = true)
27         {
28             if (this.Hilighter == null)
29                 return false;
30
31             long nowTick = DateTime.Now.Ticks;
32             bool sync = force || !this._IsSync;
33             if (sync || Math.Abs(nowTick - this.lastUpdateTicks) >= AllowCallTicks)
34             {
35                 for (int i = 0; i < lti.Count; i++)
36                     this.HilightLine(lti, i);
37
38                 this.Hilighter.Reset();
39                 lti.ClearLayoutCache();
40
41                 this.lastUpdateTicks = nowTick;
42
43                 this._IsSync = true;
44
45                 return true;
46             }
47             return false;
48         }
49
50         private void HilightLine(LineToIndexTable lti, int row)
51         {
52             //シンタックスハイライトを行う
53             List<SyntaxInfo> syntax = new List<SyntaxInfo>();
54             string str = lti[row];
55             int level = this.Hilighter.DoHilight(str, str.Length, (s) =>
56             {
57                 if (s.type == TokenType.None || s.type == TokenType.Control)
58                     return;
59                 if (str[s.index + s.length - 1] == Document.NewLine)
60                     s.length--;
61                 syntax.Add(new SyntaxInfo(s.index, s.length, s.type));
62             });
63
64             LineToIndexTableData lineData = lti.GetRaw(row);
65             lineData.Syntax = syntax.ToArray();
66
67         }
68
69         public void Update(Document doc, int startIndex, int insertLength, int removeLength)
70         {
71         }
72     }
73 }