OSDN Git Service

バージョンナンバーを増やした
[fooeditengine/FooEditEngine.git] / Core / WatchDogPattern.cs
1 /*
2  * Copyright (C) 2013 FooProject
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
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
5
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
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/>.
10  */
11 using System;
12 using System.Collections.Generic;
13 using System.Text.RegularExpressions;
14
15 namespace FooEditEngine
16 {
17     /// <summary>
18     /// IMarkerPattern\83C\83\93\83^\81[\83t\83F\83C\83X
19     /// </summary>
20     public interface IMarkerPattern
21     {
22         /// <summary>
23         /// \83}\81[\83J\81[\82ð\95Ô\82·
24         /// </summary>
25         /// <param name="lineHeadIndex">\8ds\93ª\82Ö\82Ì\83C\83\93\83f\83b\83N\83X\82ð\95\\82·</param>
26         /// <param name="s">\95\8e\9a\97ñ</param>
27         /// <returns>Marker\97ñ\8b\93\91Ì\82ð\95Ô\82·</returns>
28         IEnumerable<Marker> GetMarker(int lineHeadIndex, string s);
29     }
30     /// <summary>
31     /// \90³\8bK\95\\8c»\82Å\83}\81[\83J\81[\82Ì\8eæ\93¾\82ð\8ds\82¤\83N\83\89\83X
32     /// </summary>
33     public sealed class RegexMarkerPattern : IMarkerPattern
34     {
35         Regex regex;
36         HilightType type;
37         Color color;
38         /// <summary>
39         /// \83R\83\93\83X\83g\83\89\83N\83^\81[
40         /// </summary>
41         /// <param name="regex">regex\83I\83u\83W\83F\83N\83g</param>
42         /// <param name="type">\83n\83C\83\89\83C\83g\83^\83C\83v</param>
43         /// <param name="color">\90F</param>
44         public RegexMarkerPattern(Regex regex,HilightType type,Color color)
45         {
46             this.regex = regex;
47             this.type = type;
48             this.color = color;
49         }
50
51         /// <summary>
52         /// \83}\81[\83J\81[\82ð\95Ô\82·
53         /// </summary>
54         /// <param name="lineHeadIndex">\8ds\93ª\82Ö\82Ì\83C\83\93\83f\83b\83N\83X\82ð\95\\82·</param>
55         /// <param name="s">\95\8e\9a\97ñ</param>
56         /// <returns>Marker\97ñ\8b\93\91Ì\82ð\95Ô\82·</returns>
57         public IEnumerable<Marker> GetMarker(int lineHeadIndex, string s)
58         {
59             foreach (Match m in this.regex.Matches(s))
60             {
61                 yield return Marker.Create(lineHeadIndex + m.Index, m.Length, this.type,this.color);
62             }
63         }
64     }
65     /// <summary>
66     /// MarkerPattern\83Z\83b\83g
67     /// </summary>
68     public sealed class MarkerPatternSet
69     {
70         MarkerCollection markers;
71         Dictionary<int, IMarkerPattern> watchDogSet = new Dictionary<int, IMarkerPattern>();
72
73         internal MarkerPatternSet(LineToIndexTable lti,MarkerCollection markers)
74         {
75             this.markers = markers;
76         }
77
78         internal IEnumerable<Marker> GetMarkers(CreateLayoutEventArgs e)
79         {
80             foreach (int id in this.watchDogSet.Keys)
81             {
82                 foreach (Marker m in this.watchDogSet[id].GetMarker(e.Index, e.Content))
83                     yield return m;
84             }
85         }
86
87         internal event EventHandler Updated;
88
89         /// <summary>
90         /// WatchDog\82ð\92Ç\89Á\82·\82é
91         /// </summary>
92         /// <param name="id">\83}\81[\83J\81[ID</param>
93         /// <param name="dog">IMarkerPattern\83C\83\93\83^\81[\83t\83F\83C\83X</param>
94         public void Add(int id, IMarkerPattern dog)
95         {
96             this.watchDogSet.Add(id, dog);
97             this.Updated(this, null);
98         }
99
100         /// <summary>
101         /// \83}\81[\83J\81[ID\82ª\8aÜ\82Ü\82ê\82Ä\82¢\82é\82©\82Ç\82¤\82©\82ð\92²\82×\82é
102         /// </summary>
103         /// <param name="id">\83}\81[\83J\81[ID</param>
104         /// <returns>\8aÜ\82Ü\82ê\82Ä\82¢\82ê\82Î\90^\81B\82»\82¤\82Å\82È\82¯\82ê\82Î\8bU</returns>
105         public bool Contains(int id)
106         {
107             return this.watchDogSet.ContainsKey(id);
108         }
109
110         /// <summary>
111         /// WatchDog\82ð\92Ç\89Á\82·\82é
112         /// </summary>
113         /// <param name="id">\83}\81[\83J\81[ID</param>
114         public void Remove(int id)
115         {
116             this.markers.Clear(id);
117             this.watchDogSet.Remove(id);
118             this.Updated(this, null);
119         }
120     }
121 }