[MonoDevelop] Patch for Indent

Nick D ndrochak@gol.com
Mon, 12 Apr 2004 08:25:37 +0900


--=-FjETPMRO133Z18xgv3OU
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi.

Here's a patch to make code template insertion do some reasonable
indenting.

OK to commit this?

Nick D.

--=-FjETPMRO133Z18xgv3OU
Content-Disposition: attachment; filename=indent.patch
Content-Type: text/x-patch; name=indent.patch; charset=
Content-Transfer-Encoding: 7bit

Index: Gui/SourceEditorView.cs
===================================================================
--- Gui/SourceEditorView.cs	(revision 1436)
+++ Gui/SourceEditorView.cs	(working copy)
@@ -293,15 +293,27 @@
 			return start;
 		}
 
-
+		public string GetLeadingWhiteSpace (int line) {
+			string lineText = ((IFormattableDocument)this).GetLineAsString (line);
+			int index = 0;
+			while (index < lineText.Length && Char.IsWhiteSpace (lineText[index])) {
+    				index++;
+    			}
+ 	   		return index > 0 ? lineText.Substring (0, index) : "";
+		}
+		
 		public void InsertTemplate (CodeTemplate template)
 		{
-			int newCaretOffset = buf.GetIterAtMark (buf.InsertMark).Offset;
+			TextIter iter = buf.GetIterAtMark (buf.InsertMark);
+			int newCaretOffset = iter.Offset;
 			string word = GetWordBeforeCaret ().Trim ();
-			
+			int beginLine = iter.Line;
+			int endLine = beginLine;
 			if (word.Length > 0)
 				newCaretOffset = DeleteWordBeforeCaret ();
 			
+			string leadingWhiteSpace = GetLeadingWhiteSpace (beginLine);
+
 			int finalCaretOffset = newCaretOffset;
 			
 			for (int i =0; i < template.Text.Length; ++i) {
@@ -318,6 +330,7 @@
 					case '\n':
 						buf.InsertAtCursor ('\n'.ToString ());
 						newCaretOffset++;
+						endLine++;
 						break;
 					default:
 						buf.InsertAtCursor (template.Text[i].ToString ());
@@ -326,6 +339,10 @@
 				}
 			}
 			
+			if (endLine > beginLine) {
+				IndentLines (beginLine+1, endLine, leadingWhiteSpace);
+			}
+			
 			buf.PlaceCursor (buf.GetIterAtOffset (finalCaretOffset));
 		}
 
@@ -369,8 +386,11 @@
 		
 		void IndentLines (int y0, int y1)
 		{
-			string indent = InsertSpacesInsteadOfTabs ? new string (' ', (int) TabsWidth) : "\t";
-			
+			IndentLines (y0, y1, InsertSpacesInsteadOfTabs ? "\t" : new string (' ', (int) TabsWidth));
+		}
+
+		void IndentLines (int y0, int y1, string indent)
+		{
 			for (int l = y0; l <= y1; l ++)
 				Buffer.Insert (Buffer.GetIterAtLine (l), indent);
 		}
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 1436)
+++ ChangeLog	(working copy)
@@ -1,3 +1,8 @@
+2004-04-12  Nick Drochak <ndrochak@gol.com>
+
+	* Gui/SourceEditorView.cs: Use white space already on the line to
+	indent the 2nd and following lines of an inserted template.
+
 2004-04-10  Todd Berman  <tberman@sevenl.net>
 
 	* Gui/SourceEditorView.cs: fix logic bug.

--=-FjETPMRO133Z18xgv3OU--