OSDN Git Service

WatchDogPatternを追加した
[fooeditengine/FooEditEngine.git] / Common / WatchDogPattern.cs
1 /*\r
2  * Copyright (C) 2013 FooProject\r
3  * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by\r
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.\r
5 \r
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of \r
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\r
8 \r
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
10  */\r
11 using System;\r
12 using System.Collections.Generic;\r
13 using System.Text.RegularExpressions;\r
14 \r
15 namespace FooEditEngine\r
16 {\r
17     /// <summary>\r
18     /// WatchDog\83C\83\93\83^\81[\83t\83F\83C\83X\r
19     /// </summary>\r
20     public interface IWatchDog\r
21     {\r
22         IEnumerable<Marker> Mark(int lineHeadIndex,string s);\r
23     }\r
24     /// <summary>\r
25     /// \90³\8bK\95\\8c»\82Å\83p\83^\81[\83\93\83}\83b\83`\82ð\8ds\82¤WatchDog\r
26     /// </summary>\r
27     public sealed class RegexWatchDog : IWatchDog\r
28     {\r
29         Regex regex;\r
30         HilightType type;\r
31         /// <summary>\r
32         /// \83R\83\93\83X\83g\83\89\83N\83^\81[\r
33         /// </summary>\r
34         /// <param name="regex">regex\83I\83u\83W\83F\83N\83g</param>\r
35         /// <param name="type">\83n\83C\83\89\83C\83g\83^\83C\83v</param>\r
36         public RegexWatchDog(Regex regex,HilightType type)\r
37         {\r
38             this.regex = regex;\r
39             this.type = type;\r
40         }\r
41 \r
42         /// <summary>\r
43         /// \83}\81[\83J\81[\82ð\95Ô\82·\r
44         /// </summary>\r
45         /// <param name="lineHeadIndex">\8ds\93ª\82Ö\82Ì\83C\83\93\83f\83b\83N\83X\82ð\95\\82·</param>\r
46         /// <param name="s">\95\8e\9a\97ñ</param>\r
47         /// <returns>Marker\97ñ\8b\93\91Ì\82ð\95Ô\82·</returns>\r
48         public IEnumerable<Marker> Mark(int lineHeadIndex, string s)\r
49         {\r
50             foreach (Match m in this.regex.Matches(s))\r
51             {\r
52                 yield return Marker.Create(lineHeadIndex + m.Index, m.Length, this.type);\r
53             }\r
54         }\r
55     }\r
56     /// <summary>\r
57     /// WatchDog\83p\83^\81[\83\93\83Z\83b\83g\r
58     /// </summary>\r
59     public sealed class WatchDogSet\r
60     {\r
61         MarkerCollection markers;\r
62         Dictionary<int, IWatchDog> set = new Dictionary<int, IWatchDog>();\r
63         internal WatchDogSet(LineToIndexTable lti,MarkerCollection markers)\r
64         {\r
65             lti.CreateingLayout += lti_CreateingLayout;\r
66             this.markers = markers;\r
67         }\r
68 \r
69         void lti_CreateingLayout(object sender, CreateLayoutEventArgs e)\r
70         {\r
71             LineToIndexTable lti = (LineToIndexTable)sender;\r
72             foreach (int id in this.set.Keys)\r
73             {\r
74                 this.markers.RemoveAll(id, e.Index, e.Length);\r
75                 this.markers.AddRange(id, this.set[id].Mark(e.Index,e.Content));\r
76             }\r
77         }\r
78 \r
79         /// <summary>\r
80         /// WatchDog\82ð\92Ç\89Á\82·\82é\r
81         /// </summary>\r
82         /// <param name="id">\83}\81[\83J\81[ID</param>\r
83         /// <param name="dog">IWatchDog\83C\83\93\83^\81[\83t\83F\83C\83X</param>\r
84         public void Add(int id, IWatchDog dog)\r
85         {\r
86             this.set.Add(id, dog);\r
87         }\r
88 \r
89         /// <summary>\r
90         /// WatchDog\82ð\92Ç\89Á\82·\82é\r
91         /// </summary>\r
92         /// <param name="id">\83}\81[\83J\81[ID</param>\r
93         public void Remove(int id)\r
94         {\r
95             this.markers.Clear(id);\r
96             this.set.Remove(id);\r
97         }\r
98 \r
99     }\r
100 }