Author: fejj
Date: 2007-03-06 17:54:29 -0500 (Tue, 06 Mar 2007)
New Revision: 73855

Added:
   
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngine.cs
   
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngineStack.cs
Modified:
   trunk/monodevelop/Extras/CSharpBinding/CSharpBinding.mdp
   trunk/monodevelop/Extras/CSharpBinding/ChangeLog
   
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpFormattingStrategy.cs
   trunk/monodevelop/Extras/CSharpBinding/Gui/CSharpTextEditorExtension.cs
   trunk/monodevelop/Extras/CSharpBinding/Makefile.am
   
trunk/monodevelop/Extras/CSharpBinding/gtk-gui/CSharpBinding.CodeGenerationPanelWidget.cs
Log:
2007-03-06  Jeffrey Stedfast  <[EMAIL PROTECTED]>

        * FormattingStrategy/CSharpIndentEngine.cs: New class to aid in
        smart indenting.

        * FormattingStrategy/CSharpIndentEngineStack.cs: A broken out
        piece of CSharpIndentEngine.cs to compartamentalize it a bit.

        * FormattingStrategy/CSharpFormattingStrategy.cs
        (SmartIndentLine): Don't do any indenting, Smart Indent is handled
        elsewhere for now.
        (FormatLine): Simplified, because this now only has to worry about '>'

        * Gui/CSharpTextEditorExtension.cs (KeyPress): Implement our hooks
        into Smart Indenting here.



Modified: trunk/monodevelop/Extras/CSharpBinding/CSharpBinding.mdp
===================================================================
--- trunk/monodevelop/Extras/CSharpBinding/CSharpBinding.mdp    2007-03-06 
22:50:04 UTC (rev 73854)
+++ trunk/monodevelop/Extras/CSharpBinding/CSharpBinding.mdp    2007-03-06 
22:54:29 UTC (rev 73855)
@@ -73,6 +73,8 @@
     <File name="./gtk-gui/CSharpBinding.CodeGenerationPanelWidget.cs" 
subtype="Code" buildaction="Compile" />
     <File name="./Gui/CSharpParameterDataProvider.cs" subtype="Code" 
buildaction="Compile" />
     <File name="./Gui/OverrideCompletionData.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="./FormattingStrategy/CSharpIndentEngine.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="./FormattingStrategy/CSharpIndentEngineStack.cs" 
subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Project" localcopy="True" refto="MonoDevelop.Core" 
/>

Modified: trunk/monodevelop/Extras/CSharpBinding/ChangeLog
===================================================================
--- trunk/monodevelop/Extras/CSharpBinding/ChangeLog    2007-03-06 22:50:04 UTC 
(rev 73854)
+++ trunk/monodevelop/Extras/CSharpBinding/ChangeLog    2007-03-06 22:54:29 UTC 
(rev 73855)
@@ -1,3 +1,19 @@
+2007-03-06  Jeffrey Stedfast  <[EMAIL PROTECTED]>
+
+       * FormattingStrategy/CSharpIndentEngine.cs: New class to aid in
+       smart indenting.
+
+       * FormattingStrategy/CSharpIndentEngineStack.cs: A broken out
+       piece of CSharpIndentEngine.cs to compartamentalize it a bit.
+
+       * FormattingStrategy/CSharpFormattingStrategy.cs
+       (SmartIndentLine): Don't do any indenting, Smart Indent is handled
+       elsewhere for now.
+       (FormatLine): Simplified, because this now only has to worry about '>'
+
+       * Gui/CSharpTextEditorExtension.cs (KeyPress): Implement our hooks
+       into Smart Indenting here.
+
 2007-02-27  Lluis Sanchez Gual <[EMAIL PROTECTED]> 
 
        * Gui/OverrideCompletionData.cs, Gui/CSharpParameterDataProvider.cs,

Modified: 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpFormattingStrategy.cs
===================================================================
--- 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpFormattingStrategy.cs
       2007-03-06 22:50:04 UTC (rev 73854)
+++ 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpFormattingStrategy.cs
       2007-03-06 22:54:29 UTC (rev 73855)
@@ -23,177 +23,13 @@
        /// you type.
        /// </summary>
        public class CSharpFormattingStrategy : DefaultFormattingStrategy {
-               
-               /// <summary>
-               /// Define CSharp specific smart indenting for a line :)
-               /// </summary>
+
                protected override int SmartIndentLine (TextEditor d, int 
lineNr, string indentString)
                {
-                       if (lineNr <= 0)
-                               return AutoIndentLine (d, lineNr, indentString);
-                       
-                       string lineAboveText = d.GetLineText (lineNr - 1);
-                       string trimlineAboveText = lineAboveText.Trim ();
-                       string curLineText = d.GetLineText (lineNr);
-                       string trimcurLineText = curLineText.Trim ();
-                       
-                       if ((trimlineAboveText.EndsWith (")") &&
-                            trimcurLineText.StartsWith ("{")) ||       // 
after for, while, etc.
-                           (trimlineAboveText.EndsWith ("else") &&
-                            trimcurLineText.StartsWith ("{")))         // 
after else
-                       {
-                               string indentation = GetIndentation (d, lineNr 
- 1);
-                               d.ReplaceLine (lineNr, indentation + 
trimcurLineText);
-                               return indentation.Length;
-                       }
-                       
-                       // indent closing bracket.
-                       if (trimcurLineText.StartsWith ("}")) {
-                               int openLine;
-                               int closingBracketOffset = 
d.GetClosingBraceForLine (lineNr, out openLine);
-                               
-                               // no closing bracket found -> autoindent
-                               if (closingBracketOffset == -1)
-                                       return AutoIndentLine (d, lineNr, 
indentString);
-                               
-                               string indentation = GetIndentation (d, 
openLine);
-                               
-                               d.ReplaceLine (lineNr, indentation + 
trimcurLineText);
-                               return indentation.Length;
-                       }
-                       
-                       // expression ended, reset to valid indent.
-                       if (lineAboveText.EndsWith (";")) {
-                               int openLine;
-                               int closingBracketOffset = 
d.GetClosingBraceForLine (lineNr, out openLine);
-                               
-                               // no closing bracket found -> autoindent
-                               if (closingBracketOffset == -1)
-                                       return AutoIndentLine (d, lineNr, 
indentString);
-                               
-                               string closingBracketLineText = d.GetLineText 
(openLine).Trim ();
-                               
-                               string indentation = GetIndentation (d, 
openLine);
-                               
-                               // special handling for switch statement 
formatting.
-                               if (closingBracketLineText.StartsWith 
("switch")) {
-                                       if (!(trimlineAboveText.StartsWith 
("break;") ||
-                                             trimlineAboveText.StartsWith 
("goto") ||
-                                             trimlineAboveText.StartsWith 
("return")))
-                                               indentation += indentString;
-                               }
-                               indentation += indentString;
-                               
-                               d.ReplaceLine (lineNr, indentation + 
trimcurLineText);
-                               return indentation.Length;
-                       }
-                       
-                       if (trimlineAboveText.EndsWith ("{") || // indent 
opening bracket.
-                           trimlineAboveText.EndsWith (":") || // indent case 
xyz:
-                           (trimlineAboveText.EndsWith (")") &&  // indent 
single line if, for ... etc
-                            (trimlineAboveText.StartsWith ("if") ||
-                             trimlineAboveText.StartsWith ("while") ||
-                             trimlineAboveText.StartsWith ("for"))) ||
-                           trimlineAboveText.EndsWith ("else")) {
-                               string indentation = GetIndentation (d, lineNr 
- 1) + indentString;
-                               d.ReplaceLine (lineNr, indentation + 
trimcurLineText);
-                               return indentation.Length;
-                       } else {
-                               // try to indent linewrap
-                               ArrayList bracketPos = new ArrayList ();
-                               
-                               // search for a ( bracket that isn't closed
-                               for (int i = 0; i < lineAboveText.Length; ++i) {
-                                       if (lineAboveText [i] == '(')
-                                               bracketPos.Add (i);
-                                       else if (lineAboveText [i] == ')' && 
bracketPos.Count > 0)
-                                               bracketPos.RemoveAt 
(bracketPos.Count - 1);
-                               }
-                               
-                               if (bracketPos.Count > 0) {
-                                       int bracketIndex = (int)bracketPos 
[bracketPos.Count - 1];
-                                       string indentation = GetIndentation (d, 
lineNr - 1);
-                                       
-                                       // insert enough spaces to match brace 
start in the next line
-                                       for (int i = 0; i <= bracketIndex; ++i)
-                                               indentation += " ";
-                                       
-                                       d.ReplaceLine (lineNr, indentation + 
trimcurLineText);
-                                       return indentation.Length;
-                               }
-                       }
-                       
-                       return AutoIndentLine (d, lineNr, indentString);
+                       // Smart indenting is handled elsewhere for c#
+                       return 0;
                }
                
-               bool NeedCurlyBracket (string text)
-               {
-                       int curlyCounter = 0;
-                       
-                       bool inString = false;
-                       bool inChar   = false;
-                       bool verbatim = false;
-                       
-                       bool lineComment  = false;
-                       bool blockComment = false;
-                       
-                       for (int i = 0; i < text.Length; ++i) {
-                               switch (text [i]) {
-                                       case '\r':
-                                       case '\n':
-                                               lineComment = false;
-                                               inChar = false;
-                                               if (!verbatim) inString = false;
-                                               break;
-                                       case '/':
-                                               if (blockComment) {
-                                                       Debug.Assert (i > 0);
-                                                       if (text [i - 1] == '*')
-                                                               blockComment = 
false;
-                                                               
-                                               }
-                                               
-                                               if (!inString && !inChar && i + 
1 < text.Length) {
-                                                       if (!blockComment && 
text [i + 1] == '/')
-                                                               lineComment = 
true;
-                                                       
-                                                       if (!lineComment && 
text [i + 1] == '*')
-                                                               blockComment = 
true;
-                                               }
-                                               break;
-                                       case '"':
-                                               if (!(inChar || lineComment || 
blockComment)) {
-                                                       if (inString && 
verbatim) {
-                                                               ++i; // skip 
escaped quote
-                                                               inString = 
false; // let the string go on
-                                                       }
-                                                       else if (!inString && i 
> 0 && text[i -1] == '@') {
-                                                               verbatim = true;
-                                                       }
-                                                       inString = !inString;
-                                               }
-                                               
-                                               break;
-                                       case '\'':
-                                               if (!(inString || lineComment 
|| blockComment))
-                                                       inChar = !inChar;
-                                               
-                                               break;
-                                       case '{':
-                                               if (!(inString || inChar || 
lineComment || blockComment))
-                                                       ++curlyCounter;
-                                               
-                                               break;
-                                       case '}':
-                                               if (!(inString || inChar || 
lineComment || blockComment))
-                                                       --curlyCounter;
-                                               
-                                               break;
-                               }
-                       }
-                       return curlyCounter > 0;
-               }
-               
                bool IsInsideStringOrComment (TextEditor d, int curLineOffset, 
int cursorOffset)
                {
                        // scan cur line to see if it is inside a string or 
single line comment (//)
@@ -314,60 +150,48 @@
                }
                
                // used for comment tag formater/inserter
-               public override int FormatLine (TextEditor d, int lineNr, int 
cursorOffset, char ch, string indentString, bool autoInsertCurlyBracket)
+               public override int FormatLine (TextEditor d, int lineNr, int 
cursorOffset, char ch, 
+                                               string indentString, bool 
autoInsertCurlyBracket)
                {
                        int curLineOffset = d.GetPositionFromLineColumn 
(lineNr, 1);
                        
-                       if (ch != '\n' && ch != '>' && IsInsideStringOrComment 
(d, curLineOffset, cursorOffset))
+                       if (ch != '>' || !IsInsideDocumentationComment (d, 
curLineOffset, cursorOffset))
                                return 0;
                        
-                       switch (ch) {
-                               case '>':
-                                       if (IsInsideDocumentationComment (d, 
curLineOffset, cursorOffset)) {
-                                               string curLineText  = 
d.GetLineText (lineNr);
-                                               int column = cursorOffset - 
curLineOffset;
-                                               int index = Math.Min (column - 
1, curLineText.Length - 1);
-                                               if (curLineText [index] == '/')
-                                                       break;
-                                               
-                                               while (index >= 0 && 
curLineText [index] != '<')
-                                                       --index;
-                                               
-                                               if (index > 0) {
-                                                       bool skipInsert = false;
-                                                       for (int i = index; i < 
curLineText.Length && i < column; ++i) {
-                                                               if (i < 
curLineText.Length && curLineText [i] == '/' && curLineText [i + 1] == '>')
-                                                                       
skipInsert = true;
-                                                               
-                                                               if (curLineText 
[i] == '>')
-                                                                       break;
-                                                       }
-                                                       
-                                                       if (skipInsert)
-                                                               break;
-                                               
-                                                       StringBuilder 
commentBuilder = new StringBuilder ("");
-                                                       for (int i = index; i < 
curLineText.Length && i < column && !Char.IsWhiteSpace (curLineText [i]); ++i)
-                                                               
commentBuilder.Append (curLineText [i]);
-                                                               
-                                                       string tag = 
commentBuilder.ToString ().Trim ();
-                                                       if (!tag.EndsWith (">"))
-                                                               tag += ">";
-                                                       
-                                                       if (!tag.StartsWith 
("/"))
-                                                               d.InsertText 
(cursorOffset, "</" + tag.Substring (1));
-                                               }
-                                       }
-                                       break;
-                               case '}':
-                               case '{':
-                                       return IndentLine (d, lineNr, 
indentString);
-                               case '\n':
-                                       if (lineNr <= 0)
-                                               return IndentLine (d, lineNr, 
indentString);
+                       string curLineText  = d.GetLineText (lineNr);
+                       int column = cursorOffset - curLineOffset;
+                       int index = Math.Min (column - 1, curLineText.Length - 
1);
+                       if (curLineText [index] == '/')
+                               return 0;
+                       
+                       while (index >= 0 && curLineText [index] != '<')
+                               --index;
+                       
+                       if (index > 0) {
+                               bool skipInsert = false;
+                               for (int i = index; i < curLineText.Length && i 
< column; ++i) {
+                                       if (i < curLineText.Length && 
curLineText [i] == '/' && curLineText [i + 1] == '>')
+                                               skipInsert = true;
                                        
-                                       return IndentLine (d, lineNr - 1, 
indentString);
+                                       if (curLineText [i] == '>')
+                                               break;
+                               }
+                               
+                               if (skipInsert)
+                                       return 0;
+                               
+                               StringBuilder commentBuilder = new 
StringBuilder ("");
+                               for (int i = index; i < curLineText.Length && i 
< column && !Char.IsWhiteSpace (curLineText [i]); ++i)
+                                       commentBuilder.Append (curLineText [i]);
+                               
+                               string tag = commentBuilder.ToString ().Trim ();
+                               if (!tag.EndsWith (">"))
+                                       tag += ">";
+                               
+                               if (!tag.StartsWith ("/"))
+                                       d.InsertText (cursorOffset, "</" + 
tag.Substring (1));
                        }
+                       
                        return 0;
                }
        }

Added: 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngine.cs
===================================================================
--- 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngine.cs 
    2007-03-06 22:50:04 UTC (rev 73854)
+++ 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngine.cs 
    2007-03-06 22:54:29 UTC (rev 73855)
@@ -0,0 +1,783 @@
+using System;
+using System.Text;
+
+using MonoDevelop.SourceEditor.Properties;
+
+
+namespace CSharpBinding.FormattingStrategy {
+       public partial class CSharpIndentEngine : ICloneable {
+               IndentStack stack;
+               
+               StringBuilder linebuf;
+               
+               string keyword;
+               
+               string curIndent;
+               
+               bool needsReindent;
+               bool popVerbatim;
+               bool canBeLabel;
+               bool isEscaped;
+               
+               int commentEndedAt;
+               int firstNonLwsp;
+               int lastNonLwsp;
+               int wordStart;
+               
+               int curLineNr;
+               int cursor;
+               
+               // Constructors
+               public CSharpIndentEngine ()
+               {
+                       stack = new IndentStack ();
+                       linebuf = new StringBuilder ();
+                       Reset ();
+               }
+               
+               // Properties
+               public int Cursor {
+                       get { return cursor; }
+               }
+               
+               public int LineNumber {
+                       get { return curLineNr; }
+               }
+               
+               public int LineOffset {
+                       get { return linebuf.Length; }
+               }
+               
+               public bool NeedsReindent {
+                       get { return needsReindent; }
+               }
+               
+               public bool IsInsideVerbatimString {
+                       get { return stack.PeekInside (0) == 
Inside.VerbatimString; }
+               }
+               
+               public bool IsInsideMultiLineComment {
+                       get { return stack.PeekInside (0) == 
Inside.MultiLineComment; }
+               }
+               
+               string TabsToSpaces (string indent)
+               {
+                       StringBuilder builder;
+                       int width;
+                       
+                       if (indent == String.Empty)
+                               return String.Empty;
+                       
+                       builder = new StringBuilder ();
+                       width = TextEditorProperties.TabIndent;
+                       for (int i = 0; i < indent.Length; i++) {
+                               if (indent[i] == '\t')
+                                       builder.Append (' ', width);
+                               else
+                                       builder.Append (indent[i]);
+                       }
+                       
+                       return builder.ToString ();
+               }
+               
+               public string ThisLineIndent {
+                       get {
+                               if (TextEditorProperties.ConvertTabsToSpaces)
+                                       return TabsToSpaces (curIndent);
+                               
+                               return curIndent;
+                       }
+               }
+               
+               public string NewLineIndent {
+                       get {
+                               if (TextEditorProperties.ConvertTabsToSpaces)
+                                       return TabsToSpaces (stack.PeekIndent 
(0));
+                               
+                               return stack.PeekIndent (0);
+                       }
+               }
+               
+               // Methods
+               
+               // Resets the CSharpIndentEngine state machine
+               public void Reset ()
+               {
+                       stack.Reset ();
+                       
+                       linebuf.Length = 0;
+                       
+                       keyword = String.Empty;
+                       curIndent = String.Empty;
+                       
+                       needsReindent = false;
+                       popVerbatim = false;
+                       canBeLabel = true;
+                       isEscaped = false;
+                       
+                       commentEndedAt = -1;
+                       firstNonLwsp = -1;
+                       lastNonLwsp = -1;
+                       wordStart = 0;
+                       
+                       curLineNr = 1;
+                       cursor = 0;
+               }
+               
+               // Clone the CSharpIndentEngine - useful if a consumer of this 
class wants
+               // to test things w/o changing the real indent engine state
+               public object Clone ()
+               {
+                       CSharpIndentEngine engine = new CSharpIndentEngine ();
+                       
+                       engine.stack = (IndentStack) stack.Clone ();
+                       engine.linebuf = new StringBuilder (linebuf.ToString 
(), linebuf.Capacity);
+                       
+                       engine.keyword = keyword;
+                       engine.curIndent = curIndent;
+                       
+                       engine.needsReindent = needsReindent;
+                       engine.popVerbatim = popVerbatim;
+                       engine.canBeLabel = canBeLabel;
+                       engine.isEscaped = isEscaped;
+                       
+                       engine.commentEndedAt = commentEndedAt;
+                       engine.firstNonLwsp = firstNonLwsp;
+                       engine.lastNonLwsp = lastNonLwsp;
+                       engine.wordStart = wordStart;
+                       
+                       engine.curLineNr = curLineNr;
+                       engine.cursor = cursor;
+                       
+                       return engine;
+               }
+               
+               //int NumLeadingWhiteSpace ()
+               //{
+               //      string line = linebuf.ToString ();
+               //      int n;
+               //      
+               //      for (n = 0; n < line.Length; n++) {
+               //              if (!Char.IsWhiteSpace (line[n]))
+               //                      break;
+               //      }
+               //      
+               //      return n;
+               //}
+               
+               void TrimIndent ()
+               {
+                       switch (stack.PeekInside (0)) {
+                       case Inside.FoldedStatement:
+                       case Inside.Block:
+                               if (curIndent == String.Empty)
+                                       return;
+                               
+                               // chop off the last tab (e.g. unindent 1 level)
+                               curIndent = curIndent.Substring (0, 
curIndent.Length - 1);
+                               break;
+                       default:
+                               curIndent = stack.PeekIndent (0);
+                               break;
+                       }
+               }
+               
+               string IsKeyword ()
+               {
+                       string str = linebuf.ToString (wordStart, 
linebuf.Length - wordStart);
+                       string[] keywords = new string [] {
+                               "namespace",
+                               "struct",
+                               "class",
+                               "enum",
+                               "switch",
+                               "case",
+                               "foreach",
+                               "while",
+                               "for",
+                               "do",
+                               "else",
+                               "if",
+                               "base",
+                               "this"
+                       };
+                       
+                       for (int i = 0; i < keywords.Length; i++) {
+                               if (str == keywords[i])
+                                       return keywords[i];
+                       }
+                       
+                       return null;
+               }
+               
+               bool IsDefault ()
+               {
+                       string str = linebuf.ToString (wordStart, 
linebuf.Length - wordStart).Trim ();
+                       
+                       return str == "default";
+               }
+               
+               void PushFoldedStatement ()
+               {
+                       // don't let folded statements get too nested
+                       if (stack.PeekInside (0) == Inside.FoldedStatement &&
+                               stack.PeekInside (1) == Inside.FoldedStatement)
+                               return;
+                       
+                       stack.Push (Inside.FoldedStatement, keyword, curLineNr, 
0);
+               }
+               
+               // Handlers for specific characters
+               void PushSlash (Inside inside, char pc)
+               {
+                       // ignore these
+                       if ((inside & (Inside.PreProcessor | Inside.LineComment 
| Inside.StringOrChar)) != 0)
+                               return;
+                       
+                       if (inside == Inside.MultiLineComment) {
+                               // check for end of multi-line comment block
+                               if (pc == '*') {
+                                       // restore the keyword and pop the 
multiline comment
+                                       commentEndedAt = linebuf.Length;
+                                       keyword = stack.PeekKeyword (0);
+                                       stack.Pop ();
+                               }
+                       } else {
+                               // FoldedStatement, Block, Attribute or 
ParenList
+                               // check for the start of a single-line comment
+                               if (pc == '/')
+                                       stack.Push (Inside.LineComment, 
keyword, curLineNr, 0);
+                       }
+               }
+               
+               void PushBackSlash (Inside inside, char pc)
+               {
+                       // string and char literals can have \-escapes
+                       if ((inside & (Inside.StringLiteral | 
Inside.CharLiteral)) != 0)
+                               isEscaped = !isEscaped;
+               }
+               
+               void PushStar (Inside inside, char pc)
+               {
+                       int n;
+                       
+                       if (pc != '/')
+                               return;
+                       
+                       // got a "/*" - might start a MultiLineComment
+                       if ((inside & (Inside.StringOrChar | Inside.Comment)) 
!= 0) {
+                               if ((inside & Inside.MultiLineComment) != 0)
+                                       Console.WriteLine ("Watch out! Nested 
/* */ comment detected!");
+                               return;
+                       }
+                       
+                       // push a new multiline comment onto the stack
+                       if (inside != Inside.PreProcessor)
+                               n = linebuf.Length - firstNonLwsp;
+                       else
+                               n = linebuf.Length;
+                       
+                       stack.Push (Inside.MultiLineComment, keyword, 
curLineNr, n);
+               }
+               
+               void PushQuote (Inside inside, char pc)
+               {
+                       Inside type;
+                       
+                       // ignore if in these
+                       if ((inside & (Inside.PreProcessor | Inside.Comment | 
Inside.CharLiteral)) != 0)
+                               return;
+                       
+                       if (inside == Inside.VerbatimString) {
+                               if (popVerbatim) {
+                                       // back in the verbatim-string-literal 
token
+                                       popVerbatim = false;
+                               } else {
+                                       /* need to see the next char before we 
pop the
+                                        * verbatim-string-literal */
+                                       popVerbatim = true;
+                               }
+                       } else if (inside == Inside.StringLiteral) {
+                               // check that it isn't escaped
+                               if (!isEscaped) {
+                                       keyword = stack.PeekKeyword (0);
+                                       stack.Pop ();
+                               }
+                       } else {
+                               // FoldedStatement, Block, Attribute or 
ParenList
+                               if (pc == '@')
+                                       type = Inside.VerbatimString;
+                               else
+                                       type = Inside.StringLiteral;
+                               
+                               // push a new string onto the stack
+                               stack.Push (type, keyword, curLineNr, 0);
+                       }
+               }
+               
+               void PushSQuote (Inside inside, char pc)
+               {
+                       if (inside == Inside.CharLiteral) {
+                               // check that it's not escaped
+                               if (isEscaped)
+                                       return;
+                               
+                               keyword = stack.PeekKeyword (0);
+                               stack.Pop ();
+                               return;
+                       }
+                       
+                       if ((inside & (Inside.PreProcessor | Inside.String | 
Inside.Comment)) != 0) {
+                               // eat it
+                               return;
+                       }
+                       
+                       // push a new char literal onto the stack 
+                       stack.Push (Inside.CharLiteral, keyword, curLineNr, 0);
+               }
+               
+               void PushColon (Inside inside, char pc)
+               {
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) != 0)
+                               return;
+                       
+                       // can't be a case/label if there's no preceeding text
+                       if (firstNonLwsp == -1)
+                               return;
+                       
+                       // goto-label or case statement
+                       if (keyword == "case" || keyword == "default") {
+                               // case (or default) statement
+                               if (stack.PeekKeyword (0) != "switch")
+                                       return;
+                               
+                               TrimIndent ();
+                               needsReindent = true;
+                       } else if (canBeLabel) {
+                               if (inside == Inside.FoldedStatement) {
+                                       // not a label, probably a multi-line 
ternary statement
+                                       return;
+                               }
+                               
+                               // goto label
+                               
+                               /* FIXME: make this configurable...
+                                * options:
+                                * a) left-justified (+1 space)
+                                * b) up 1 level of indent
+                                * c) no change
+                                **/
+                               curIndent = " ";
+                               needsReindent = true;
+                       }
+               }
+               
+               void PushSemicolon (Inside inside, char pc)
+               {
+                       if (inside != Inside.FoldedStatement)
+                               return;
+                       
+                       // chain-pop folded statements
+                       while (stack.PeekInside (0) == Inside.FoldedStatement)
+                               stack.Pop ();
+                       
+                       keyword = String.Empty;
+               }
+               
+               void PushOpenSq (Inside inside, char pc)
+               {
+                       int n = 1;
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) != 0)
+                               return;
+                       
+                       // push a new attribute onto the stack
+                       if (firstNonLwsp != -1)
+                               n += linebuf.Length - firstNonLwsp;
+                       
+                       stack.Push (Inside.Attribute, keyword, curLineNr, n);
+               }
+               
+               void PushCloseSq (Inside inside, char pc)
+               {
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) != 0)
+                               return;
+                       
+                       if (inside != Inside.Attribute) {
+                               Console.WriteLine ("can't pop a '[' if we ain't 
got one?");
+                               return;
+                       }
+                       
+                       // pop this attribute off the stack
+                       keyword = stack.PeekKeyword (0);
+                       stack.Pop ();
+               }
+               
+               void PushOpenParen (Inside inside, char pc)
+               {
+                       int n = 1;
+                       
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) != 0)
+                               return;
+                       
+                       // push a new paren list onto the stack
+                       if (firstNonLwsp != -1)
+                               n += linebuf.Length - firstNonLwsp;
+                       
+                       stack.Push (Inside.ParenList, keyword, curLineNr, n);
+               }
+               
+               void PushCloseParen (Inside inside, char pc)
+               {
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) != 0)
+                               return;
+                       
+                       if (inside != Inside.ParenList) {
+                               Console.WriteLine ("can't pop a '(' if we ain't 
got one?");
+                               return;
+                       }
+                       
+                       // pop this paren list off the stack
+                       keyword = stack.PeekKeyword (0);
+                       stack.Pop ();
+               }
+               
+               void PushOpenBrace (Inside inside, char pc)
+               {
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) != 0)
+                               return;
+                       
+                       // push a new block onto the stack
+                       if (inside == Inside.FoldedStatement) {
+                               string pKeyword = stack.PeekKeyword (0);
+                               
+                               stack.Pop ();
+                               
+                               if ((pKeyword == "base" || pKeyword == "this" 
|| pKeyword == "class") &&
+                                   stack.PeekInside (0) == 
Inside.FoldedStatement) {
+                                       // Folded Constructor or class
+                                       pKeyword = stack.PeekKeyword (0);
+                                       stack.Pop ();
+                               }
+                               
+                               if (firstNonLwsp == -1)
+                                       curIndent = stack.PeekIndent (0);
+                               
+                               stack.Push (Inside.Block, pKeyword, curLineNr, 
0);
+                       } else if (stack.PeekKeyword (0) == "switch" && 
(keyword == "default" || keyword == "case")) {
+                               // this is the Emacs-Way(tm) but we could make 
it configurable
+                               stack.Push (Inside.Block, keyword, curLineNr, 
-1);
+                       } else {
+                               stack.Push (Inside.Block, keyword, curLineNr, 
0);
+                       }
+                       
+                       keyword = String.Empty;
+                       if (firstNonLwsp == -1)
+                               needsReindent = true;
+               }
+               
+               void PushCloseBrace (Inside inside, char pc)
+               {
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) != 0)
+                               return;
+                       
+                       keyword = String.Empty;
+                       
+                       if (inside != Inside.Block) {
+                               Console.WriteLine ("can't pop a '{' if we ain't 
got one?");
+                               return;
+                       }
+                       
+                       // pop this block off the stack
+                       // Note: do not restore the keyword
+                       stack.Pop ();
+                       if (firstNonLwsp == -1) {
+                               TrimIndent ();
+                               needsReindent = true;
+                       }
+               }
+               
+               void PushNewLine (Inside inside, char pc)
+               {
+                       switch (inside) {
+                       case Inside.PreProcessor:
+                               // pop the preprocesor state unless the eoln is 
escaped
+                               if (pc != '\\') {
+                                       keyword = stack.PeekKeyword (0);
+                                       stack.Pop ();
+                               }
+                               break;
+                       case Inside.MultiLineComment:
+                               // nothing to do
+                               break;
+                       case Inside.LineComment:
+                               // pop the line comment
+                               keyword = stack.PeekKeyword (0);
+                               stack.Pop ();
+                               break;
+                       case Inside.VerbatimString:
+                               // nothing to do
+                               break;
+                       case Inside.StringLiteral:
+                               if (isEscaped) {
+                                       /* I don't think c# allows breaking a
+                                        * normal string across lines even
+                                        * when escaping the carriage
+                                        * return... but how else should we
+                                        * handle this? */
+                                       break;
+                               }
+                               
+                               /* not escaped... error!! but what can we do,
+                                * eh? allow folding across multiple lines I
+                                * guess... */
+                               break;
+                       case Inside.CharLiteral:
+                               /* this is an error... what to do? guess we'll
+                                * just pretend it never happened */
+                               break;
+                       case Inside.Attribute:
+                               // nothing to do
+                               break;
+                       case Inside.ParenList:
+                               // nothing to do
+                               break;
+                       default:
+                               // Empty, FoldedStatement, and Block
+                               
+                               // if no text entered on this line, nothing to 
do
+                               if (lastNonLwsp == -1)
+                                       break;
+                               
+                               switch (pc) {
+                               case ':':
+                                       canBeLabel = canBeLabel && inside != 
Inside.FoldedStatement;
+                                       
+                                       if ((keyword == "default" || keyword == 
"case") || canBeLabel)
+                                               break;
+                                       
+                                       PushFoldedStatement ();
+                                       break;
+                               case '[':
+                                       // handled elsewhere
+                                       break;
+                               case ']':
+                                       // handled elsewhere
+                                       break;
+                               case '(':
+                                       // handled elsewhere
+                                       break;
+                               case '{':
+                                       // handled elsewhere
+                                       break;
+                               case '}':
+                                       // handled elsewhere
+                                       break;
+                               case ';':
+                                       // handled elsewhere
+                                       break;
+                               default:
+                                       // case '/':
+                                       if (commentEndedAt == lastNonLwsp)
+                                               break;
+                                       
+                                       if (stack.PeekLineNr (0) == curLineNr)
+                                               break;
+                                       
+                                       if (inside == Inside.Block) {
+                                               if (pc == ',') {
+                                                       // avoid indenting if 
we are in a list
+                                                       /* Note: this may 
negate the need for checking
+                                                        * for "enum" and 
"struct" below */
+                                                       break;
+                                               }
+                                               
+                                               if (stack.PeekKeyword (0) == 
"struct" || stack.PeekKeyword (0) == "enum") {
+                                                       // just variable/value 
declarations
+                                                       break;
+                                               }
+                                       }
+                                       
+                                       PushFoldedStatement ();
+                                       break;
+                               }
+                               
+                               break;
+                       }
+                       
+                       linebuf.Length = 0;
+                       
+                       curIndent = stack.PeekIndent (0);
+                       
+                       canBeLabel = true;
+                       
+                       commentEndedAt = -1;
+                       firstNonLwsp = -1;
+                       lastNonLwsp = -1;
+                       wordStart = 0;
+                       
+                       curLineNr++;
+                       cursor++;
+               }
+               
+               // This is the main logic of this class...
+               public void Push (char c)
+               {
+                       Inside inside;
+                       char pc;
+                       
+                       inside = stack.PeekInside (0);
+                       if (inside == Inside.VerbatimString && popVerbatim && c 
!= '"') {
+                               keyword = stack.PeekKeyword (0);
+                               stack.Pop ();
+                               
+                               inside = stack.PeekInside (0);
+                       }
+                       
+                       needsReindent = false;
+                       
+                       // check the previous char
+                       if (linebuf.Length > 0) {
+                               string buf = linebuf.ToString ();
+                               pc = buf[buf.Length - 1];
+                       } else {
+                               pc = '\0';
+                       }
+                       
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) == 0 
+                               && firstNonLwsp != -1) {
+                               if (Char.IsWhiteSpace (c) || c == '(' || c == 
'{' && linebuf.Length > wordStart) {
+                                       string tmp = IsKeyword ();
+                                       if (tmp != null)
+                                               keyword = tmp;
+                               } else if (c == ':' && linebuf.Length > 
wordStart && IsDefault ()) {
+                                       keyword = "default";
+                               }
+                       }
+                       
+                       // Console.WriteLine ("Pushing '{0}'; keyword = {1}", 
c, keyword);
+                       
+                       switch (c) {
+                               case '/':
+                                       PushSlash (inside, pc);
+                                       break;
+                               case '\\':
+                                       PushBackSlash (inside, pc);
+                                       break;
+                               case '*':
+                                       PushStar (inside, pc);
+                                       break;
+                               case '"':
+                                       PushQuote (inside, pc);
+                                       break;
+                               case '\'':
+                                       PushSQuote (inside, pc);
+                                       break;
+                               case ':':
+                                       PushColon (inside, pc);
+                                       break;
+                               case ';':
+                                       PushSemicolon (inside, pc);
+                                       break;
+                               case '[':
+                                       PushOpenSq (inside, pc);
+                                       break;
+                               case ']':
+                                       PushCloseSq (inside, pc);
+                                       break;
+                               case '(':
+                                       PushOpenParen (inside, pc);
+                                       break;
+                               case ')':
+                                       PushCloseParen (inside, pc);
+                                       break;
+                               case '{':
+                                       PushOpenBrace (inside, pc);
+                                       break;
+                               case '}':
+                                       PushCloseBrace (inside, pc);
+                                       break;
+                               case '\n':
+                                       PushNewLine (inside, pc);
+                                       return;
+                               default:
+                                       break;
+                       }
+                       
+                       inside = stack.PeekInside (0);
+                       if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar | Inside.Comment)) == 0) {
+                               if (!Char.IsWhiteSpace (c)) {
+                                       if (firstNonLwsp == -1)
+                                               firstNonLwsp = linebuf.Length;
+                                       
+                                       if (lastNonLwsp != -1 && c != ':' && 
lastNonLwsp != linebuf.Length - 1) {
+                                               // goto labels must be single 
word tokens
+                                               canBeLabel = false;
+                                       } else if (lastNonLwsp == -1 && 
Char.IsDigit (c)) {
+                                               // labels cannot start with a 
digit
+                                               canBeLabel = false;
+                                       }
+                                       
+                                       lastNonLwsp = linebuf.Length;
+                                       
+                                       if (c == ':')
+                                               wordStart = linebuf.Length + 1;
+                               } else {
+                                       wordStart = linebuf.Length + 1;
+                               }
+                       } else if (c != '\\') {
+                               isEscaped = false;
+                       }
+                       
+                       linebuf.Append (c);
+                       
+                       cursor++;
+               }
+               
+               public void Debug ()
+               {
+                       Console.WriteLine ("\ncurLineNr = {0}\ncursor = 
{1}\nneedsReindent = {2}",
+                                          curLineNr, cursor, needsReindent);
+                       Console.WriteLine ("stack:");
+                       for (int i = 0; i < stack.Count; i++) {
+                               switch (stack.PeekInside (i)) {
+                               case Inside.PreProcessor:
+                                       Console.WriteLine ("\tpreprocessor 
directive");
+                                       break;
+                               case Inside.MultiLineComment:
+                                       Console.WriteLine ("\t/* */ comment 
block");
+                                       break;
+                               case Inside.LineComment:
+                                       Console.WriteLine ("\t// comment");
+                                       break;
+                               case Inside.VerbatimString:
+                                       Console.WriteLine ("\tverbatim string");
+                                       break;
+                               case Inside.StringLiteral:
+                                       Console.WriteLine ("\tstring literal");
+                                       break;
+                               case Inside.CharLiteral:
+                                       Console.WriteLine ("\tchar literal");
+                                       break;
+                               case Inside.Attribute:
+                                       Console.WriteLine ("\t[ ] attribute");
+                                       break;
+                               case Inside.ParenList:
+                                       Console.WriteLine ("\t( ) paren list");
+                                       break;
+                               case Inside.FoldedStatement:
+                                       if (stack.PeekKeyword (i) != 
String.Empty)
+                                               Console.WriteLine 
("\t{0}-statement", stack.PeekKeyword (i));
+                                       else
+                                               Console.WriteLine ("\tfolded 
statement?");
+                                       break;
+                               case Inside.Block:
+                                       if (stack.PeekKeyword (i) != 
String.Empty)
+                                               Console.WriteLine ("\t{0} {1} 
block", stack.PeekKeyword (i), "{ }");
+                                       else
+                                               Console.WriteLine ("\tmethod 
{0} block?", "{ }");
+                                       break;
+                               }
+                       }
+               }
+       }
+}
\ No newline at end of file

Added: 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngineStack.cs
===================================================================
--- 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngineStack.cs
        2007-03-06 22:50:04 UTC (rev 73854)
+++ 
trunk/monodevelop/Extras/CSharpBinding/FormattingStrategy/CSharpIndentEngineStack.cs
        2007-03-06 22:54:29 UTC (rev 73855)
@@ -0,0 +1,209 @@
+using System;
+using System.Text;
+
+namespace CSharpBinding.FormattingStrategy {
+       public partial class CSharpIndentEngine : ICloneable {
+               public enum Inside {
+                       Empty              = 0,
+                       
+                       PreProcessor       = (1 << 0),
+                       
+                       MultiLineComment   = (1 << 1),
+                       LineComment        = (1 << 2),
+                       Comment            = (MultiLineComment | LineComment),
+                       
+                       VerbatimString     = (1 << 3),
+                       StringLiteral      = (1 << 4),
+                       CharLiteral        = (1 << 5),
+                       String             = (VerbatimString | StringLiteral),
+                       StringOrChar       = (String | CharLiteral),
+                       
+                       Attribute          = (1 << 6),
+                       ParenList          = (1 << 7),
+                       
+                       FoldedStatement    = (1 << 8),
+                       Block              = (1 << 9),
+               };
+               
+               private class IndentStack : ICloneable {
+                       readonly static int INITIAL_CAPACITY = 16;
+                       
+                       struct Node {
+                               public Inside inside;
+                               public string keyword;
+                               public string indent;
+                               public int nSpaces;
+                               public int lineNr;
+                       };
+                       
+                       Node[] stack;
+                       int size;
+                       
+                       public IndentStack () : this (INITIAL_CAPACITY)
+                       {
+                       }
+                       
+                       public IndentStack (int capacity)
+                       {
+                               if (capacity < INITIAL_CAPACITY)
+                                       capacity = INITIAL_CAPACITY;
+                               
+                               this.stack = new Node [capacity];
+                               this.size = 0;
+                       }
+                       
+                       public bool IsEmpty {
+                               get { return size == 0; }
+                       }
+                       
+                       public int Count {
+                               get { return size; }
+                       }
+                       
+                       public object Clone ()
+                       {
+                               IndentStack clone = new IndentStack 
(stack.Length);
+                               
+                               clone.stack = (Node[]) stack.Clone ();
+                               clone.size = size;
+                               
+                               return clone;
+                       }
+                       
+                       public void Reset ()
+                       {
+                               for (int i = 0; i < size; i++) {
+                                       stack[i].keyword = null;
+                                       stack[i].indent = null;
+                               }
+                               
+                               size = 0;
+                       }
+                       
+                       public void Push (Inside inside, string keyword, int 
lineNr, int nSpaces)
+                       {
+                               StringBuilder sb;
+                               int sp = size - 1;
+                               Node node;
+                               int n = 0;
+                               
+                               sb = new StringBuilder ();
+                               if ((inside & (Inside.Attribute | 
Inside.ParenList)) != 0) {
+                                       if (size > 0 && stack[sp].inside == 
inside) {
+                                               sb.Append (stack[sp].indent);
+                                               if (stack[sp].lineNr == lineNr)
+                                                       n = stack[sp].nSpaces;
+                                       } else {
+                                               while (sp >= 0) {
+                                                       if ((stack[sp].inside & 
(Inside.FoldedStatement | Inside.Block)) != 0) {
+                                                               sb.Append 
(stack[sp].indent);
+                                                               break;
+                                                       }
+                                                       
+                                                       sp--;
+                                               }
+                                       }
+                                       
+                                       sb.Append (' ', nSpaces - n);
+                               } else if (inside == Inside.MultiLineComment) {
+                                       if (size > 0) {
+                                               sb.Append (stack[sp].indent);
+                                               if (stack[sp].lineNr == lineNr)
+                                                       n = stack[sp].nSpaces;
+                                       }
+                                       
+                                       sb.Append (' ', nSpaces - n);
+                               } else if ((inside & (Inside.FoldedStatement | 
Inside.Block)) != 0) {
+                                       while (sp >= 0) {
+                                               if ((stack[sp].inside & 
(Inside.FoldedStatement | Inside.Block)) != 0) {
+                                                       sb.Append 
(stack[sp].indent);
+                                                       break;
+                                               }
+                                               
+                                               sp--;
+                                       }
+                                       
+                                       if (nSpaces != -1)
+                                               sb.Append ('\t');
+                                       
+                                       nSpaces = 0;
+                               } else if ((inside & (Inside.PreProcessor | 
Inside.StringOrChar)) != 0) {
+                                       // if these fold, do not indent
+                                       nSpaces = 0;
+                               } else if (inside == Inside.LineComment) {
+                                       // can't actually fold, but we can 
still push it onto the stack
+                                       nSpaces = 0;
+                               } else {
+                                       // not a valid argument?
+                                       throw new ArgumentOutOfRangeException 
();
+                               }
+                               
+                               node.indent = sb.ToString ();
+                               node.keyword = keyword;
+                               node.nSpaces = nSpaces;
+                               node.lineNr = lineNr;
+                               node.inside = inside;
+                               
+                               if (size == stack.Length)
+                                       Array.Resize <Node> (ref stack, 2 * 
size);
+                               
+                               stack[size++] = node;
+                       }
+                       
+                       public void Pop ()
+                       {
+                               if (size == 0)
+                                       throw new InvalidOperationException ();
+                               
+                               int sp = size - 1;
+                               stack[sp].keyword = null;
+                               stack[sp].indent = null;
+                               size = sp;
+                       }
+                       
+                       public Inside PeekInside (int up)
+                       {
+                               if (up < 0)
+                                       throw new ArgumentOutOfRangeException 
();
+                               
+                               if (up >= size)
+                                       return Inside.Empty;
+                               
+                               return stack[size - up - 1].inside;
+                       }
+                       
+                       public string PeekKeyword (int up)
+                       {
+                               if (up < 0)
+                                       throw new ArgumentOutOfRangeException 
();
+                               
+                               if (up >= size)
+                                       return String.Empty;
+                               
+                               return stack[size - up - 1].keyword;
+                       }
+                       
+                       public string PeekIndent (int up)
+                       {
+                               if (up < 0)
+                                       throw new ArgumentOutOfRangeException 
();
+                               
+                               if (up >= size)
+                                       return String.Empty;
+                               
+                               return stack[size - up - 1].indent;
+                       }
+                       
+                       public int PeekLineNr (int up)
+                       {
+                               if (up < 0)
+                                       throw new ArgumentOutOfRangeException 
();
+                               
+                               if (up >= size)
+                                       return -1;
+                               
+                               return stack[size - up - 1].lineNr;
+                       }
+               }
+       }
+}

Modified: 
trunk/monodevelop/Extras/CSharpBinding/Gui/CSharpTextEditorExtension.cs
===================================================================
--- trunk/monodevelop/Extras/CSharpBinding/Gui/CSharpTextEditorExtension.cs     
2007-03-06 22:50:04 UTC (rev 73854)
+++ trunk/monodevelop/Extras/CSharpBinding/Gui/CSharpTextEditorExtension.cs     
2007-03-06 22:54:29 UTC (rev 73855)
@@ -6,7 +6,11 @@
 using MonoDevelop.Ide.Gui.Content;
 using MonoDevelop.Projects.Parser;
 using MonoDevelop.Projects.Gui.Completion;
+using MonoDevelop.SourceEditor;
+using MonoDevelop.SourceEditor.Properties;
+using MonoDevelop.SourceEditor.FormattingStrategy;
 using CSharpBinding.Parser;
+using CSharpBinding.FormattingStrategy;
 using MonoDevelop.Projects.Ambience;
 using Ambience_ = MonoDevelop.Projects.Ambience.Ambience;
 
@@ -14,26 +18,155 @@
 {
        public class CSharpTextEditorExtension: TextEditorExtension
        {
+               CSharpIndentEngine engine;
+               
+               public CSharpTextEditorExtension () : base ()
+               {
+                       engine = new CSharpIndentEngine ();
+               }
+               
                public override bool ExtendsEditor (Document doc, 
IEditableTextBuffer editor)
                {
                        return System.IO.Path.GetExtension (doc.Title) == ".cs";
                }
-
+               
                public override bool KeyPress (Gdk.Key key, Gdk.ModifierType 
modifier)
                {
-                       if (key == Gdk.Key.numbersign) {
-                               // Remove all indenting
-                               int i = Editor.CursorPosition;
-                               string s = Editor.GetText (i-1, i);
-                               while (s.Length > 0 && (s[0] == ' ' || s[0] == 
'\t')) {
-                                       i--;
-                                       s = Editor.GetText (i-1, i);
+                       bool reindent = false, insert = true;
+                       CSharpIndentEngine ctx;
+                       string indent;
+                       int cursor;
+                       char c;
+                       
+                       // This code is for Smart Indent, if the user has 
chosen poorly... don't help him out ;-)
+                       if (TextEditorProperties.IndentStyle != 
IndentStyle.Smart)
+                               return base.KeyPress (key, modifier);
+                       
+                       cursor = Editor.CursorPosition;
+                       
+                       if (cursor < engine.Cursor) {
+                               // FIXME: this may be faster if we use 
GetText() for the region
+                               /* Note: however, it would be best to fix 
TextEditor.cs to make
+                                * GetCharAt() faster because the overhead of 
grabbing a
+                                * potentially huge string would suck. */
+                               engine.Reset ();
+                               for (int i = 0; i < cursor; i++) {
+                                       char ch = Editor.GetCharAt (i);
+                                       if (ch == 0)
+                                               break;
+                                       engine.Push (ch);
                                }
-                               if (s.Length == 0 || s[0] == '\n') {
-                                       Editor.DeleteText (i, 
Editor.CursorPosition - i);
+                       } else if (cursor > engine.Cursor) {
+                               // FIXME: this may be faster if we use 
GetText() for the region
+                               /* Note: however, it would be best to fix 
TextEditor.cs to make
+                                * GetCharAt() faster because the overhead of 
grabbing a
+                                * potentially huge string would suck. */
+                               for (int i = engine.Cursor; i < cursor; i++) {
+                                       char ch = Editor.GetCharAt (i);
+                                       if (ch == 0)
+                                               break;
+                                       engine.Push (ch);
                                }
                        }
-                       return base.KeyPress (key, modifier);
+                       
+                       switch (key) {
+                       case Gdk.Key.Tab:
+                               if (Editor.SelectionEndPosition > 
Editor.SelectionStartPosition)
+                                       return base.KeyPress (key, modifier);
+                               
+                               if (!engine.IsInsideVerbatimString) {
+                                       reindent = true;
+                                       insert = false;
+                               }
+                               c = '\t';
+                               break;
+                       case Gdk.Key.Return:
+                       case Gdk.Key.KP_Enter:
+                               reindent = true;
+                               c = '\n';
+                               break;
+                       default:
+                               if ((c = (char) Gdk.Keyval.ToUnicode ((uint) 
key)) == 0)
+                                       return base.KeyPress (key, modifier);
+                               break;
+                       }
+                       
+                       if (insert)
+                               engine.Push (c);
+                       
+                       // engine.Debug ();
+                       
+                       if (!(reindent || engine.NeedsReindent)) {
+                               if (insert)
+                                       return base.KeyPress (key, modifier);
+                               
+                               return true;
+                       }
+                       
+                       if (c == '\n') {
+                               if (Editor.SelectionEndPosition > 
Editor.SelectionStartPosition)
+                                       return base.KeyPress (key, modifier);
+                               
+                               // Pass off to base.KeyPress() so the '\n' gets 
added to the Undo stack, etc
+                               base.KeyPress (key, modifier);
+                               insert = false;
+                               cursor++;
+                               
+                               if (Editor.CursorPosition > cursor) {
+                                       // FIXME: gotta figure out what is 
doing this...
+                                       System.Console.WriteLine ("seems 
base.KeyPress() indented for us? delete that shiz");
+                                       Editor.DeleteText (cursor, 
Editor.CursorPosition - cursor);
+                               }
+                               
+                               char ch = Editor.GetCharAt (cursor);
+                               if (ch == 0 || ch == '\n') {
+                                       // the simple case
+                                       indent = engine.NewLineIndent;
+                                       Editor.InsertText (cursor, indent);
+                                       for (int i = 0; i < indent.Length; i++)
+                                               engine.Push (indent[i]);
+                                       cursor += indent.Length;
+                                       
+                                       if (engine.IsInsideMultiLineComment) {
+                                               Editor.InsertText (cursor, "* 
");
+                                               engine.Push ('*');
+                                               engine.Push (' ');
+                                       }
+                                       
+                                       // we handled the Return
+                                       return true;
+                               }
+                               
+                               // need more context... fall thru
+                       }
+                       
+                       // Get more context but w/o changing our IndentEngine 
state
+                       ctx = (CSharpIndentEngine) engine.Clone ();
+                       string line = Editor.GetLineText (ctx.LineNumber);
+                       for (int i = ctx.LineOffset; i < line.Length; i++) {
+                               ctx.Push (line[i]);
+                               if (ctx.NeedsReindent)
+                                       break;
+                       }
+                       
+                       // ok, we should have enough context now
+                       
+                       // Delete leading LWSP
+                       int nlwsp = 0;
+                       while (nlwsp < line.Length && Char.IsWhiteSpace 
(line[nlwsp]))
+                               nlwsp++;
+                       
+                       int pos = Editor.GetPositionFromLineColumn 
(ctx.LineNumber, 1);
+                       Editor.DeleteText (pos, nlwsp);
+                       
+                       // Insert our new indent text
+                       indent = ctx.ThisLineIndent;
+                       Editor.InsertText (pos, indent);
+                       
+                       if (insert)
+                               return base.KeyPress (key, modifier);
+                       
+                       return true;
                }
                
                public override IParameterDataProvider 
HandleParameterCompletion (ICodeCompletionContext completionContext, char 
completionChar)

Modified: trunk/monodevelop/Extras/CSharpBinding/Makefile.am
===================================================================
--- trunk/monodevelop/Extras/CSharpBinding/Makefile.am  2007-03-06 22:50:04 UTC 
(rev 73854)
+++ trunk/monodevelop/Extras/CSharpBinding/Makefile.am  2007-03-06 22:54:29 UTC 
(rev 73855)
@@ -44,6 +44,8 @@
 Project/CSharpCompilerParameters.cs \
 CSharpLanguageBinding.cs \
 FormattingStrategy/CSharpFormattingStrategy.cs \
+FormattingStrategy/CSharpIndentEngineStack.cs \
+FormattingStrategy/CSharpIndentEngine.cs \
 CSharpBindingCompilerManager.cs \
 CSharpEnhancedCodeProvider.cs \
 gtk-gui/generated.cs \

Modified: 
trunk/monodevelop/Extras/CSharpBinding/gtk-gui/CSharpBinding.CodeGenerationPanelWidget.cs
===================================================================
--- 
trunk/monodevelop/Extras/CSharpBinding/gtk-gui/CSharpBinding.CodeGenerationPanelWidget.cs
   2007-03-06 22:50:04 UTC (rev 73854)
+++ 
trunk/monodevelop/Extras/CSharpBinding/gtk-gui/CSharpBinding.CodeGenerationPanelWidget.cs
   2007-03-06 22:54:29 UTC (rev 73855)
@@ -79,6 +79,7 @@
             Stetic.Gui.Initialize();
             // Widget CSharpBinding.CodeGenerationPanelWidget
             Stetic.BinContainer.Attach(this);
+            this.Events = ((Gdk.EventMask)(256));
             this.Name = "CSharpBinding.CodeGenerationPanelWidget";
             // Container child 
CSharpBinding.CodeGenerationPanelWidget.Gtk.Container+ContainerChild
             this.vbox62 = new Gtk.VBox();

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to