OSDN Git Service

GapBufferが更新されていた
[fooeditengine/FooEditEngine.git] / Core / UndoCommands.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.Text;
13 using System.Text.RegularExpressions;
14 using Slusser.Collections.Generic;
15
16 namespace FooEditEngine
17 {
18     sealed class ReplaceCommand : ICommand
19     {
20         StringBuffer Buffer;
21         TextRange ReplacementRange, ReplacedRange;
22         GapBuffer<char> replacement, replaced;   //置き換え後の文字列、置き換え前の文字列
23
24         public ReplaceCommand(StringBuffer buf, int start, int length, string str)
25         {
26             this.Buffer = buf;
27             this.ReplacementRange = new TextRange(start,str.Length);
28             this.replacement = new GapBuffer<char>();
29             this.replacement.AddRange(Util.GetEnumrator(str));
30             this.ReplacedRange = new TextRange(start,length);
31             this.replaced = new GapBuffer<char>();
32             this.replaced.AddRange(this.Buffer.GetEnumerator(start, length));
33         }
34
35         #region ICommand メンバー
36
37         public void undo()
38         {
39             this.Buffer.Replace(this.ReplacementRange.Index, this.replacement.Count,this.replaced,this.replaced.Count);
40         }
41
42         public void redo()
43         {
44             this.Buffer.Replace(this.ReplacedRange.Index, this.replaced.Count, this.replacement, this.replacement.Count);
45         }
46
47         public bool marge(ICommand a)
48         {
49             ReplaceCommand cmd = a as ReplaceCommand;
50             if (cmd == null)
51                 return false;
52             
53             if (this.ReplacedRange.Length == 0 &&
54                 cmd.ReplacementRange.Index >= this.ReplacementRange.Index &&
55                 cmd.ReplacementRange.Index + cmd.ReplacementRange.Length <= this.ReplacementRange.Index + this.ReplacementRange.Length)
56             {
57                 int bufferIndex = cmd.ReplacedRange.Index - this.ReplacementRange.Index;
58                 if(bufferIndex < this.replacement.Count)
59                     this.replacement.RemoveRange(bufferIndex, cmd.ReplacedRange.Length);
60                 this.replacement.InsertRange(bufferIndex, cmd.replacement);
61                 return true;
62             }
63
64             if (this.ReplacedRange.Index + this.ReplacementRange.Length == cmd.ReplacedRange.Index && 
65                 this.ReplacedRange.Index == this.ReplacementRange.Index)
66             {
67                 this.replaced.AddRange(cmd.replaced);
68                 this.replacement.AddRange(cmd.replacement);
69                 this.ReplacedRange.Length += cmd.ReplacedRange.Length;
70                 this.ReplacementRange.Length += cmd.ReplacementRange.Length;
71                 return true;
72             }
73             return false;
74         }
75
76         public bool isempty()
77         {
78             return this.replaced.Count == 0 && this.replacement.Count == 0;
79         }
80         #endregion
81
82     }
83
84     sealed class ReplaceAllCommand : ICommand
85     {
86         StringBuffer buffer;
87         Regex regex;
88         StringBuffer oldBuffer;
89         string replacePattern;
90         bool groupReplace;
91         LineToIndexTable layoutLines;
92         public ReplaceAllCommand(StringBuffer buffer, LineToIndexTable layoutlines, Regex regex, string replacePattern,bool groupReplace)
93         {
94             this.buffer = buffer;
95             this.regex = regex;
96             this.replacePattern = replacePattern;
97             this.groupReplace = groupReplace;
98             this.layoutLines = layoutlines;
99         }
100
101         public void undo()
102         {
103             this.buffer.Replace(this.oldBuffer);
104         }
105
106         public void redo()
107         {
108             this.oldBuffer = new StringBuffer(this.buffer);
109             this.buffer.ReplaceRegexAll(this.layoutLines, this.regex, this.replacePattern, this.groupReplace);
110         }
111
112         public bool marge(ICommand a)
113         {
114             return false;
115         }
116
117         public bool isempty()
118         {
119             return false;
120         }
121     }
122     sealed class FastReplaceAllCommand : ICommand
123     {
124         StringBuffer buffer;
125         StringBuffer oldBuffer;
126         string targetPattern;
127         string replacePattern;
128         bool caseInsensitve;
129         LineToIndexTable layoutLines;
130         public FastReplaceAllCommand(StringBuffer buffer,LineToIndexTable layoutlines,string targetPattern, string replacePattern,bool ci)
131         {
132             this.buffer = buffer;
133             this.replacePattern = replacePattern;
134             this.targetPattern = targetPattern;
135             this.caseInsensitve = ci;
136             this.layoutLines = layoutlines;
137         }
138
139         public void undo()
140         {
141             this.buffer.Replace(this.oldBuffer);
142         }
143
144         public void redo()
145         {
146             this.oldBuffer = new StringBuffer(this.buffer);
147             this.buffer.ReplaceAll(this.layoutLines, this.targetPattern, this.replacePattern,this.caseInsensitve);
148         }
149
150         public bool marge(ICommand a)
151         {
152             return false;
153         }
154
155         public bool isempty()
156         {
157             return false;
158         }
159     }
160 }