Code Coverage Statistics for Source File
c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Actions\FormatActions.cs
|
Sequence Point Coverage
N/A
0 of 0
|
Branch Coverage
N/A
0 of 0
|
Lines
213
|
Highlight:
Uncovered Code
Covered Code
| L | V | Source |
|---|---|---|
1 |
// <file> |
|
2 |
// <copyright see="prj:///doc/copyright.txt"/> |
|
3 |
// <license see="prj:///doc/license.txt"/> |
|
4 |
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> |
|
5 |
// <version>$Revision: 2640 $</version> |
|
6 |
// </file> |
|
7 |
||
8 |
using System; |
|
9 |
using System.Text; |
|
10 |
using ICSharpCode.TextEditor.Document; |
|
11 |
||
12 |
namespace ICSharpCode.TextEditor.Actions |
|
13 |
{ |
|
14 |
public abstract class AbstractLineFormatAction : AbstractEditAction |
|
15 |
{ |
|
16 |
protected TextArea textArea; |
|
17 |
abstract protected void Convert(IDocument document, int startLine, int endLine); |
|
18 |
||
19 |
public override void Execute(TextArea textArea) |
|
20 |
{ |
|
21 |
this.textArea = textArea; |
|
22 |
textArea.BeginUpdate(); |
|
23 |
textArea.Document.UndoStack.StartUndoGroup(); |
|
24 |
if (textArea.SelectionManager.HasSomethingSelected) { |
|
25 |
foreach (ISelection selection in textArea.SelectionManager.SelectionCollection) { |
|
26 |
Convert(textArea.Document, selection.StartPosition.Y, selection.EndPosition.Y); |
|
27 |
} |
|
28 |
} else { |
|
29 |
Convert(textArea.Document, 0, textArea.Document.TotalNumberOfLines - 1); |
|
30 |
} |
|
31 |
textArea.Document.UndoStack.EndUndoGroup(); |
|
32 |
textArea.Caret.ValidateCaretPos(); |
|
33 |
textArea.EndUpdate(); |
|
34 |
textArea.Refresh(); |
|
35 |
} |
|
36 |
} |
|
37 |
||
38 |
public abstract class AbstractSelectionFormatAction : AbstractEditAction |
|
39 |
{ |
|
40 |
protected TextArea textArea; |
|
41 |
abstract protected void Convert(IDocument document, int offset, int length); |
|
42 |
||
43 |
public override void Execute(TextArea textArea) |
|
44 |
{ |
|
45 |
this.textArea = textArea; |
|
46 |
textArea.BeginUpdate(); |
|
47 |
if (textArea.SelectionManager.HasSomethingSelected) { |
|
48 |
foreach (ISelection selection in textArea.SelectionManager.SelectionCollection) { |
|
49 |
Convert(textArea.Document, selection.Offset, selection.Length); |
|
50 |
} |
|
51 |
} else { |
|
52 |
Convert(textArea.Document, 0, textArea.Document.TextLength); |
|
53 |
} |
|
54 |
textArea.Caret.ValidateCaretPos(); |
|
55 |
textArea.EndUpdate(); |
|
56 |
textArea.Refresh(); |
|
57 |
} |
|
58 |
} |
|
59 |
||
60 |
public class RemoveLeadingWS : AbstractLineFormatAction |
|
61 |
{ |
|
62 |
protected override void Convert(IDocument document, int y1, int y2) |
|
63 |
{ |
|
64 |
for (int i = y1; i < y2; ++i) { |
|
65 |
LineSegment line = document.GetLineSegment(i); |
|
66 |
int removeNumber = 0; |
|
67 |
for (int x = line.Offset; x < line.Offset + line.Length && Char.IsWhiteSpace(document.GetCharAt(x)); ++x) { |
|
68 |
++removeNumber; |
|
69 |
} |
|
70 |
if (removeNumber > 0) { |
|
71 |
document.Remove(line.Offset, removeNumber); |
|
72 |
} |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
public class RemoveTrailingWS : AbstractLineFormatAction |
|
78 |
{ |
|
79 |
protected override void Convert(IDocument document, int y1, int y2) |
|
80 |
{ |
|
81 |
for (int i = y2 - 1; i >= y1; --i) { |
|
82 |
LineSegment line = document.GetLineSegment(i); |
|
83 |
int removeNumber = 0; |
|
84 |
for (int x = line.Offset + line.Length - 1; x >= line.Offset && Char.IsWhiteSpace(document.GetCharAt(x)); --x) { |
|
85 |
++removeNumber; |
|
86 |
} |
|
87 |
if (removeNumber > 0) { |
|
88 |
document.Remove(line.Offset + line.Length - removeNumber, removeNumber); |
|
89 |
} |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
||
95 |
public class ToUpperCase : AbstractSelectionFormatAction |
|
96 |
{ |
|
97 |
protected override void Convert(IDocument document, int startOffset, int length) |
|
98 |
{ |
|
99 |
string what = document.GetText(startOffset, length).ToUpper(); |
|
100 |
document.Replace(startOffset, length, what); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
public class ToLowerCase : AbstractSelectionFormatAction |
|
105 |
{ |
|
106 |
protected override void Convert(IDocument document, int startOffset, int length) |
|
107 |
{ |
|
108 |
string what = document.GetText(startOffset, length).ToLower(); |
|
109 |
document.Replace(startOffset, length, what); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
public class InvertCaseAction : AbstractSelectionFormatAction |
|
114 |
{ |
|
115 |
protected override void Convert(IDocument document, int startOffset, int length) |
|
116 |
{ |
|
117 |
StringBuilder what = new StringBuilder(document.GetText(startOffset, length)); |
|
118 |
||
119 |
for (int i = 0; i < what.Length; ++i) { |
|
120 |
what[i] = Char.IsUpper(what[i]) ? Char.ToLower(what[i]) : Char.ToUpper(what[i]); |
|
121 |
} |
|
122 |
||
123 |
document.Replace(startOffset, length, what.ToString()); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
public class CapitalizeAction : AbstractSelectionFormatAction |
|
128 |
{ |
|
129 |
protected override void Convert(IDocument document, int startOffset, int length) |
|
130 |
{ |
|
131 |
StringBuilder what = new StringBuilder(document.GetText(startOffset, length)); |
|
132 |
||
133 |
for (int i = 0; i < what.Length; ++i) { |
|
134 |
if (!Char.IsLetter(what[i]) && i < what.Length - 1) { |
|
135 |
what[i + 1] = Char.ToUpper(what[i + 1]); |
|
136 |
} |
|
137 |
} |
|
138 |
document.Replace(startOffset, length, what.ToString()); |
|
139 |
} |
|
140 |
||
141 |
} |
|
142 |
||
143 |
public class ConvertTabsToSpaces : AbstractSelectionFormatAction |
|
144 |
{ |
|
145 |
protected override void Convert(IDocument document, int startOffset, int length) |
|
146 |
{ |
|
147 |
string what = document.GetText(startOffset, length); |
|
148 |
string spaces = new string(' ', document.TextEditorProperties.TabIndent); |
|
149 |
document.Replace(startOffset, length, what.Replace("\t", spaces)); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
public class ConvertSpacesToTabs : AbstractSelectionFormatAction |
|
154 |
{ |
|
155 |
protected override void Convert(IDocument document, int startOffset, int length) |
|
156 |
{ |
|
157 |
string what = document.GetText(startOffset, length); |
|
158 |
string spaces = new string(' ', document.TextEditorProperties.TabIndent); |
|
159 |
document.Replace(startOffset, length, what.Replace(spaces, "\t")); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
public class ConvertLeadingTabsToSpaces : AbstractLineFormatAction |
|
164 |
{ |
|
165 |
protected override void Convert(IDocument document, int y1, int y2) |
|
166 |
{ |
|
167 |
for (int i = y2; i >= y1; --i) { |
|
168 |
LineSegment line = document.GetLineSegment(i); |
|
169 |
||
170 |
if(line.Length > 0) { |
|
171 |
// count how many whitespace characters there are at the start |
|
172 |
int whiteSpace = 0; |
|
173 |
for(whiteSpace = 0; whiteSpace < line.Length && Char.IsWhiteSpace(document.GetCharAt(line.Offset + whiteSpace)); whiteSpace++) { |
|
174 |
// deliberately empty |
|
175 |
} |
|
176 |
if(whiteSpace > 0) { |
|
177 |
string newLine = document.GetText(line.Offset,whiteSpace); |
|
178 |
string newPrefix = newLine.Replace("\t",new string(' ', document.TextEditorProperties.TabIndent)); |
|
179 |
document.Replace(line.Offset,whiteSpace,newPrefix); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
public class ConvertLeadingSpacesToTabs : AbstractLineFormatAction |
|
187 |
{ |
|
188 |
protected override void Convert(IDocument document, int y1, int y2) |
|
189 |
{ |
|
190 |
for (int i = y2; i >= y1; --i) { |
|
191 |
LineSegment line = document.GetLineSegment(i); |
|
192 |
if(line.Length > 0) { |
|
193 |
// note: some users may prefer a more radical ConvertLeadingSpacesToTabs that |
|
194 |
// means there can be no spaces before the first character even if the spaces |
|
195 |
// didn't add up to a whole number of tabs |
|
196 |
string newLine = TextUtilities.LeadingWhiteSpaceToTabs(document.GetText(line.Offset,line.Length), document.TextEditorProperties.TabIndent); |
|
197 |
document.Replace(line.Offset,line.Length,newLine); |
|
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
/// <summary> |
|
204 |
/// This is a sample editaction plugin, it indents the selected area. |
|
205 |
/// </summary> |
|
206 |
public class FormatBuffer : AbstractLineFormatAction |
|
207 |
{ |
|
208 |
protected override void Convert(IDocument document, int startLine, int endLine) |
|
209 |
{ |
|
210 |
document.FormattingStrategy.IndentLines(textArea, startLine, endLine); |
|
211 |
} |
|
212 |
} |
|
213 |
} |