Code Coverage Statistics for Source File

c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Document\FormattingStrategy\DefaultFormattingStrategy.cs

Sequence Point Coverage
N/A
0 of 0
Branch Coverage
N/A
0 of 0
Lines
172
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: 2643 $</version>
6
// </file>
7
8
using System;
9
using System.Text;
10
11
namespace ICSharpCode.TextEditor.Document
12
{
13
	/// <summary>
14
	/// This class handles the auto and smart indenting in the textbuffer while
15
	/// you type.
16
	/// </summary>
17
	public class DefaultFormattingStrategy : IFormattingStrategy
18
	{
19
		/// <summary>
20
		/// Creates a new instance off <see cref="DefaultFormattingStrategy"/>
21
		/// </summary>
22
		public DefaultFormattingStrategy()
23
		{
24
		}
25
		
26
		/// <summary>
27
		/// returns the whitespaces which are before a non white space character in the line line
28
		/// as a string.
29
		/// </summary>
30
		protected string GetIndentation(TextArea textArea, int lineNumber)
31
		{
32
			if (lineNumber < 0 || lineNumber > textArea.Document.TotalNumberOfLines) {
33
				throw new ArgumentOutOfRangeException("lineNumber");
34
			}
35
			
36
			string lineText = TextUtilities.GetLineAsString(textArea.Document, lineNumber);
37
			StringBuilder whitespaces = new StringBuilder();
38
			
39
			foreach (char ch in lineText) {
40
				if (Char.IsWhiteSpace(ch)) {
41
					whitespaces.Append(ch);
42
				} else {
43
					break;
44
				}
45
			}
46
			return whitespaces.ToString();
47
		}
48
		
49
		/// <summary>
50
		/// Could be overwritten to define more complex indenting.
51
		/// </summary>
52
		protected virtual int AutoIndentLine(TextArea textArea, int lineNumber)
53
		{
54
			string indentation = lineNumber != 0 ? GetIndentation(textArea, lineNumber - 1) : "";
55
			if(indentation.Length > 0) {
56
				string newLineText = indentation + TextUtilities.GetLineAsString(textArea.Document, lineNumber).Trim();
57
				LineSegment oldLine  = textArea.Document.GetLineSegment(lineNumber);
58
				textArea.Document.Replace(oldLine.Offset, oldLine.Length, newLineText);
59
			}
60
			return indentation.Length;
61
		}
62
		
63
		/// <summary>
64
		/// Could be overwritten to define more complex indenting.
65
		/// </summary>
66
		protected virtual int SmartIndentLine(TextArea textArea, int line)
67
		{
68
			return AutoIndentLine(textArea, line); // smart = autoindent in normal texts
69
		}
70
		
71
		/// <summary>
72
		/// This function formats a specific line after <code>ch</code> is pressed.
73
		/// </summary>
74
		/// <returns>
75
		/// the caret delta position the caret will be moved this number
76
		/// of bytes (e.g. the number of bytes inserted before the caret, or
77
		/// removed, if this number is negative)
78
		/// </returns>
79
		public virtual void FormatLine(TextArea textArea, int line, int cursorOffset, char ch)
80
		{
81
			if (ch == '\n') {
82
				textArea.Caret.Column = IndentLine(textArea, line);
83
			}
84
		}
85
		
86
		/// <summary>
87
		/// This function sets the indentation level in a specific line
88
		/// </summary>
89
		/// <returns>
90
		/// the number of inserted characters.
91
		/// </returns>
92
		public int IndentLine(TextArea textArea, int line)
93
		{
94
			textArea.Document.UndoStack.StartUndoGroup();
95
			int result;
96
			switch (textArea.Document.TextEditorProperties.IndentStyle) {
97
				case IndentStyle.None:
98
					result = 0;
99
					break;
100
				case IndentStyle.Auto:
101
					result = AutoIndentLine(textArea, line);
102
					break;
103
				case IndentStyle.Smart:
104
					result = SmartIndentLine(textArea, line);
105
					break;
106
				default:
107
					throw new NotSupportedException("Unsupported value for IndentStyle: " + textArea.Document.TextEditorProperties.IndentStyle);
108
			}
109
			textArea.Document.UndoStack.EndUndoGroup();
110
			return result;
111
		}
112
		
113
		/// <summary>
114
		/// This function sets the indentlevel in a range of lines.
115
		/// </summary>
116
		public virtual void IndentLines(TextArea textArea, int begin, int end)
117
		{
118
			textArea.Document.UndoStack.StartUndoGroup();
119
			for (int i = begin; i <= end; ++i) {
120
				IndentLine(textArea, i);
121
			}
122
			textArea.Document.UndoStack.EndUndoGroup();
123
		}
124
		
125
		public virtual int SearchBracketBackward(IDocument document, int offset, char openBracket, char closingBracket)
126
		{
127
			int brackets = -1;
128
			// first try "quick find" - find the matching bracket if there is no string/comment in the way
129
			for (int i = offset; i >= 0; --i) {
130
				char ch = document.GetCharAt(i);
131
				if (ch == openBracket) {
132
					++brackets;
133
					if (brackets == 0) return i;
134
				} else if (ch == closingBracket) {
135
					--brackets;
136
				} else if (ch == '"') {
137
					break;
138
				} else if (ch == '\'') {
139
					break;
140
				} else if (ch == '/' && i > 0) {
141
					if (document.GetCharAt(i - 1) == '/') break;
142
					if (document.GetCharAt(i - 1) == '*') break;
143
				}
144
			}
145
			return -1;
146
		}
147
		
148
		public virtual int SearchBracketForward(IDocument document, int offset, char openBracket, char closingBracket)
149
		{
150
			int brackets = 1;
151
			// try "quick find" - find the matching bracket if there is no string/comment in the way
152
			for (int i = offset; i < document.TextLength; ++i) {
153
				char ch = document.GetCharAt(i);
154
				if (ch == openBracket) {
155
					++brackets;
156
				} else if (ch == closingBracket) {
157
					--brackets;
158
					if (brackets == 0) return i;
159
				} else if (ch == '"') {
160
					break;
161
				} else if (ch == '\'') {
162
					break;
163
				} else if (ch == '/' && i > 0) {
164
					if (document.GetCharAt(i - 1) == '/') break;
165
				} else if (ch == '*' && i > 0) {
166
					if (document.GetCharAt(i - 1) == '/') break;
167
				}
168
			}
169
			return -1;
170
		}
171
	}
172
}