1 // DefaultFormattingStrategy.cs
3 // This file was derived from a file from #Develop.
5 // Copyright (C) 2001-2007 Mike Krüger <mkrueger@novell.com>
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 using System
.Collections
;
25 using MonoDevelop
.Ide
.Gui
;
26 using MonoDevelop
.Ide
.Gui
.Content
;
28 namespace MonoDevelop
.SourceEditor
.FormattingStrategy
31 /// This class handles the auto and smart indenting in the textbuffer while
34 public class DefaultFormattingStrategy
: IFormattingStrategy
{
36 /// returns the whitespaces which are before a non white space character in the line
39 protected string GetIndentation (TextEditor d
, int lineNumber
)
41 string lineText
= d
.GetLineText (lineNumber
);
42 StringBuilder whitespaces
= new StringBuilder ();
44 foreach (char ch
in lineText
) {
45 if (! Char
.IsWhiteSpace (ch
))
47 whitespaces
.Append (ch
);
50 return whitespaces
.ToString ();
54 /// Could be overwritten to define more complex indenting.
56 protected virtual int AutoIndentLine (TextEditor d
, int lineNumber
, string indentString
)
58 string indentation
= lineNumber
!= 0 ? GetIndentation (d
, lineNumber
- 1) : "";
60 if (indentation
.Length
> 0) {
61 string newLineText
= indentation
+ d
.GetLineText (lineNumber
).Trim ();
62 d
.ReplaceLine (lineNumber
, newLineText
);
65 return indentation
.Length
;
69 /// Could be overwritten to define more complex indenting.
71 protected virtual int SmartIndentLine (TextEditor d
, int line
, string indentString
)
73 return AutoIndentLine (d
, line
, indentString
); // smart = autoindent in normal texts
77 /// This function formats a specific line after <code>ch</code> is pressed.
80 /// the caret delta position the caret will be moved this number
81 /// of bytes (e.g. the number of bytes inserted before the caret, or
82 /// removed, if this number is negative)
84 public virtual int FormatLine (TextEditor d
, int line
, int cursorOffset
, char ch
, string indentString
, bool autoInsertCurlyBracket
)
87 return IndentLine (d
, line
, indentString
);
93 /// This function sets the indentation level in a specific line
96 /// the number of inserted characters.
98 public int IndentLine (TextEditor d
, int line
, string indentString
)
100 switch (TextEditorProperties
.IndentStyle
) {
101 case IndentStyle
.Auto
: return AutoIndentLine (d
, line
, indentString
);
102 case IndentStyle
.Smart
: return SmartIndentLine (d
, line
, indentString
);
103 case IndentStyle
.None
: