/* * Copyright (C) 2013 FooProject * * 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 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace FooEditEngine { /// /// IMarkerPatternインターフェイス /// public interface IMarkerPattern { /// /// マーカーを返す /// /// 行頭へのインデックスを表す /// 文字列 /// Marker列挙体を返す IEnumerable GetMarker(int lineHeadIndex, string s); } /// /// 正規表現でマーカーの取得を行うクラス /// public sealed class RegexMarkerPattern : IMarkerPattern { Regex regex; HilightType type; Color color; /// /// コンストラクター /// /// regexオブジェクト /// ハイライトタイプ /// 色 public RegexMarkerPattern(Regex regex,HilightType type,Color color) { this.regex = regex; this.type = type; this.color = color; } /// /// マーカーを返す /// /// 行頭へのインデックスを表す /// 文字列 /// Marker列挙体を返す public IEnumerable GetMarker(int lineHeadIndex, string s) { foreach (Match m in this.regex.Matches(s)) { yield return Marker.Create(lineHeadIndex + m.Index, m.Length, this.type,this.color); } } } /// /// MarkerPatternセット /// public sealed class MarkerPatternSet { MarkerCollection markers; Dictionary set = new Dictionary(); internal MarkerPatternSet(LineToIndexTable lti,MarkerCollection markers) { lti.CreateingLayout += lti_CreateingLayout; this.markers = markers; } void lti_CreateingLayout(object sender, CreateLayoutEventArgs e) { LineToIndexTable lti = (LineToIndexTable)sender; foreach (int id in this.set.Keys) { this.markers.RemoveAll(id, e.Index, e.Length); this.markers.AddRange(id, this.set[id].GetMarker(e.Index,e.Content)); } } internal event EventHandler Updated; /// /// WatchDogを追加する /// /// マーカーID /// IMarkerPatternインターフェイス public void Add(int id, IMarkerPattern dog) { this.set.Add(id, dog); this.Updated(this, null); } /// /// マーカーIDが含まれているかどうかを調べる /// /// マーカーID /// 含まれていれば真。そうでなければ偽 public bool Contains(int id) { return this.set.ContainsKey(id); } /// /// WatchDogを追加する /// /// マーカーID public void Remove(int id) { this.markers.Clear(id); this.set.Remove(id); this.Updated(this, null); } } }