* Makefile.am:
[monodevelop.git] / extras / GtkSourceViewEditor / MonoDevelop.SourceEditor.FormattingStrategy / DefaultFormattingStrategy.cs
blobd4ed9889df337e98e1c9dcc505c803c04c9e4cf3
1 // DefaultFormattingStrategy.cs
2 //
3 // This file was derived from a file from #Develop.
4 //
5 // Copyright (C) 2001-2007 Mike Krüger <mkrueger@novell.com>
6 //
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.
11 //
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.
16 //
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
21 using System;
22 using System.Text;
23 using System.Collections;
25 using MonoDevelop.Ide.Gui;
26 using MonoDevelop.Ide.Gui.Content;
28 namespace MonoDevelop.SourceEditor.FormattingStrategy
30 /// <summary>
31 /// This class handles the auto and smart indenting in the textbuffer while
32 /// you type.
33 /// </summary>
34 public class DefaultFormattingStrategy : IFormattingStrategy {
35 /// <summary>
36 /// returns the whitespaces which are before a non white space character in the line
37 /// as a string.
38 /// </summary>
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))
46 break;
47 whitespaces.Append (ch);
50 return whitespaces.ToString ();
53 /// <summary>
54 /// Could be overwritten to define more complex indenting.
55 /// </summary>
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;
68 /// <summary>
69 /// Could be overwritten to define more complex indenting.
70 /// </summary>
71 protected virtual int SmartIndentLine (TextEditor d, int line, string indentString)
73 return AutoIndentLine (d, line, indentString); // smart = autoindent in normal texts
76 /// <summary>
77 /// This function formats a specific line after <code>ch</code> is pressed.
78 /// </summary>
79 /// <returns>
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)
83 /// </returns>
84 public virtual int FormatLine (TextEditor d, int line, int cursorOffset, char ch, string indentString, bool autoInsertCurlyBracket)
86 if (ch == '\n')
87 return IndentLine (d, line, indentString);
89 return 0;
92 /// <summary>
93 /// This function sets the indentation level in a specific line
94 /// </summary>
95 /// <returns>
96 /// the number of inserted characters.
97 /// </returns>
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 :
104 default : return 0;