| 1: | // FormatCommands.cs | |
| 2: | // Copyright (C) 2002 Mike Krueger | |
| 3: | // | |
| 4: | // This program is free software; you can redistribute it and/or modify | |
| 5: | // it under the terms of the GNU General Public License as published by | |
| 6: | // the Free Software Foundation; either version 2 of the License, or | |
| 7: | // (at your option) any later version. | |
| 8: | // | |
| 9: | // This program is distributed in the hope that it will be useful, | |
| 10: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12: | // GNU General Public License for more details. | |
| 13: | // | |
| 14: | // You should have received a copy of the GNU General Public License | |
| 15: | // along with this program; if not, write to the Free Software | |
| 16: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17: | ||
| 18: | using System; | |
| 19: | using System.IO; | |
| 20: | using System.Threading; | |
| 21: | using System.Drawing; | |
| 22: | using System.Drawing.Printing; | |
| 23: | using System.Collections; | |
| 24: | using System.ComponentModel; | |
| 25: | using System.Windows.Forms; | |
| 26: | using System.Diagnostics; | |
| 27: | using System.Text; | |
| 28: | ||
| 29: | using Core.AddIns; | |
| 30: | using Core.Util; | |
| 31: | using Core.Properties; | |
| 32: | using Core.Gui; | |
| 33: | using Core.Gui.Creators; | |
| 34: | ||
| 35: | using SharpDevelop.Gui.Dialogs; | |
| 36: | using SharpDevelop.DefaultEditor.Text; | |
| 37: | using SharpDevelop.DefaultEditor.Gui.Editor; | |
| 38: | using SharpDevelop.Gui; | |
| 39: | ||
| 40: | namespace SharpDevelop.DefaultEditor.Commands { | |
| 41: | ||
| 42: | public class RemoveLeadingWS : AbstractMenuCommand | |
| 43: | { | |
| 44: | void Convert(IDocument document, int y1, int y2) | |
| 45: | { | |
| 46: | int redocounter = 0; // must count how many Delete operations occur | |
| 47: | for (int i = y1; i < y2; ++i) { | |
| 48: | LineSegment line = document.GetLineSegment(i); | |
| 49: | int removeNumber = 0; | |
| 50: | for (int x = line.Offset; x < line.Offset + line.Length && Char.IsWhiteSpace(document.GetCharAt(x)); ++x) { | |
| 51: | ++removeNumber; | |
| 52: | } | |
| 53: | if (removeNumber > 0) { | |
| 54: | document.Remove(line.Offset, removeNumber); | |
| 55: | ++redocounter; // count deletes | |
| 56: | } | |
| 57: | } | |
| 58: | document.UndoStack.UndoLast(redocounter); // redo the whole operation (not the single deletes) | |
| 59: | } | |
| 60: | ||
| 61: | public override void Run() | |
| 62: | { | |
| 63: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 64: | ||
| 65: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 66: | return; | |
| 67: | } | |
| 68: | ||
| 69: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 70: | textarea.BeginUpdate(); | |
| 71: | if (textarea.HasSomethingSelected) { | |
| 72: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 73: | Convert(textarea.Document, selection.StartLine, selection.EndLine); | |
| 74: | } | |
| 75: | } else { | |
| 76: | Convert(textarea.Document, 0, textarea.Document.TotalNumberOfLines); | |
| 77: | } | |
| 78: | textarea.CheckCaretPos(); | |
| 79: | textarea.EndUpdate(); | |
| 80: | textarea.Refresh(); | |
| 81: | } | |
| 82: | } | |
| 83: | ||
| 84: | public class RemoveTrailingWS : AbstractMenuCommand | |
| 85: | { | |
| 86: | void Convert(IDocument document, int y1, int y2) | |
| 87: | { | |
| 88: | int redocounter = 0; // must count how many Delete operations occur | |
| 89: | for (int i = y2 - 1; i >= y1; --i) { | |
| 90: | LineSegment line = document.GetLineSegment(i); | |
| 91: | int removeNumber = 0; | |
| 92: | for (int x = line.Offset + line.Length - 1; x >= line.Offset && Char.IsWhiteSpace(document.GetCharAt(x)); --x) { | |
| 93: | ++removeNumber; | |
| 94: | } | |
| 95: | if (removeNumber > 0) { | |
| 96: | document.Remove(line.Offset + line.Length - removeNumber, removeNumber); | |
| 97: | ++redocounter; // count deletes | |
| 98: | } | |
| 99: | } | |
| 100: | document.UndoStack.UndoLast(redocounter); // redo the whole operation (not the single deletes) | |
| 101: | } | |
| 102: | ||
| 103: | public override void Run() | |
| 104: | { | |
| 105: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 106: | ||
| 107: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 108: | return; | |
| 109: | } | |
| 110: | ||
| 111: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 112: | textarea.BeginUpdate(); | |
| 113: | if (textarea.HasSomethingSelected) { | |
| 114: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 115: | Convert(textarea.Document, selection.StartLine, selection.EndLine); | |
| 116: | } | |
| 117: | } else { | |
| 118: | Convert(textarea.Document, 0, textarea.Document.TotalNumberOfLines); | |
| 119: | } | |
| 120: | textarea.CheckCaretPos(); | |
| 121: | textarea.EndUpdate(); | |
| 122: | textarea.Refresh(); | |
| 123: | } | |
| 124: | } | |
| 125: | ||
| 126: | ||
| 127: | public class ToUpperCase : AbstractMenuCommand | |
| 128: | { | |
| 129: | void Convert(IDocument document, int startOffset, int length) | |
| 130: | { | |
| 131: | string what = document.GetText(startOffset, length).ToUpper(); | |
| 132: | document.Replace(startOffset, length, what); | |
| 133: | } | |
| 134: | ||
| 135: | public override void Run() | |
| 136: | { | |
| 137: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 138: | ||
| 139: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 140: | return; | |
| 141: | } | |
| 142: | ||
| 143: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 144: | textarea.BeginUpdate(); | |
| 145: | if (textarea.HasSomethingSelected) { | |
| 146: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 147: | Convert(textarea.Document, selection.Offset, selection.Length); | |
| 148: | } | |
| 149: | } else { | |
| 150: | Convert(textarea.Document, 0, textarea.Document.TextLength); | |
| 151: | } | |
| 152: | textarea.EndUpdate(); | |
| 153: | textarea.Refresh(); | |
| 154: | } | |
| 155: | } | |
| 156: | ||
| 157: | public class ToLowerCase : AbstractMenuCommand | |
| 158: | { | |
| 159: | void Convert(IDocument document, int startOffset, int length) | |
| 160: | { | |
| 161: | string what = document.GetText(startOffset, length).ToLower(); | |
| 162: | document.Replace(startOffset, length, what); | |
| 163: | } | |
| 164: | ||
| 165: | public override void Run() | |
| 166: | { | |
| 167: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 168: | ||
| 169: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 170: | return; | |
| 171: | } | |
| 172: | ||
| 173: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 174: | textarea.BeginUpdate(); | |
| 175: | if (textarea.HasSomethingSelected) { | |
| 176: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 177: | Convert(textarea.Document, selection.Offset, selection.Length); | |
| 178: | } | |
| 179: | } else { | |
| 180: | Convert(textarea.Document, 0, textarea.Document.TextLength); | |
| 181: | } | |
| 182: | textarea.EndUpdate(); | |
| 183: | textarea.Refresh(); | |
| 184: | } | |
| 185: | } | |
| 186: | ||
| 187: | public class InvertCaseAction : AbstractMenuCommand | |
| 188: | { | |
| 189: | void Convert(IDocument document, int startOffset, int length) | |
| 190: | { | |
| 191: | StringBuilder what = new StringBuilder(document.GetText(startOffset, length)); | |
| 192: | for (int i = 0; i < what.Length; ++i) { | |
| 193: | what[i] = Char.IsUpper(what[i]) ? Char.ToLower(what[i]) : Char.ToUpper(what[i]); | |
| 194: | } | |
| 195: | ||
| 196: | document.Replace(startOffset, length, what.ToString()); | |
| 197: | } | |
| 198: | ||
| 199: | public override void Run() | |
| 200: | { | |
| 201: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 202: | ||
| 203: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 204: | return; | |
| 205: | } | |
| 206: | ||
| 207: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 208: | textarea.BeginUpdate(); | |
| 209: | if (textarea.HasSomethingSelected) { | |
| 210: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 211: | Convert(textarea.Document, selection.Offset, selection.Length); | |
| 212: | } | |
| 213: | } else { | |
| 214: | Convert(textarea.Document, 0, textarea.Document.TextLength); | |
| 215: | } | |
| 216: | textarea.EndUpdate(); | |
| 217: | textarea.Refresh(); | |
| 218: | } | |
| 219: | } | |
| 220: | ||
| 221: | public class CapitalizeAction : AbstractMenuCommand | |
| 222: | { | |
| 223: | void Convert(IDocument document, int startOffset, int length) | |
| 224: | { | |
| 225: | StringBuilder what = new StringBuilder(document.GetText(startOffset, length)); | |
| 226: | for (int i = 0; i < what.Length; ++i) { | |
| 227: | if (!Char.IsLetter(what[i]) && i < what.Length - 1) { | |
| 228: | what[i + 1] = Char.ToUpper(what[i + 1]); | |
| 229: | } | |
| 230: | } | |
| 231: | ||
| 232: | document.Replace(startOffset, length, what.ToString()); | |
| 233: | } | |
| 234: | ||
| 235: | public override void Run() | |
| 236: | { | |
| 237: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 238: | ||
| 239: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 240: | return; | |
| 241: | } | |
| 242: | ||
| 243: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 244: | textarea.BeginUpdate(); | |
| 245: | if (textarea.HasSomethingSelected) { | |
| 246: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 247: | Convert(textarea.Document, selection.Offset, selection.Length); | |
| 248: | } | |
| 249: | } else { | |
| 250: | Convert(textarea.Document, 0, textarea.Document.TextLength); | |
| 251: | } | |
| 252: | textarea.EndUpdate(); | |
| 253: | textarea.Refresh(); | |
| 254: | } | |
| 255: | ||
| 256: | } | |
| 257: | ||
| 258: | public class ConvertTabsToSpaces : AbstractMenuCommand | |
| 259: | { | |
| 260: | void Convert(IDocument document, int startOffset, int length) | |
| 261: | { | |
| 262: | string what = document.GetText(startOffset, length); | |
| 263: | string spaces = ""; | |
| 264: | for (int i = 0; i < document.Properties.GetProperty("TabIndent", 4); ++i) { | |
| 265: | spaces += ' '; | |
| 266: | } | |
| 267: | document.Replace(startOffset, length, what.Replace("\t", spaces)); | |
| 268: | } | |
| 269: | ||
| 270: | public override void Run() | |
| 271: | { | |
| 272: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 273: | ||
| 274: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 275: | return; | |
| 276: | } | |
| 277: | ||
| 278: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 279: | textarea.BeginUpdate(); | |
| 280: | if (textarea.HasSomethingSelected) { | |
| 281: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 282: | Convert(textarea.Document, selection.Offset, selection.Length); | |
| 283: | } | |
| 284: | } else { | |
| 285: | Convert(textarea.Document, 0, textarea.Document.TextLength); | |
| 286: | } | |
| 287: | textarea.CheckCaretPos(); | |
| 288: | textarea.EndUpdate(); | |
| 289: | textarea.Refresh(); | |
| 290: | } | |
| 291: | } | |
| 292: | ||
| 293: | public class ConvertSpacesToTabs : AbstractMenuCommand | |
| 294: | { | |
| 295: | void Convert(IDocument document, int startOffset, int length) | |
| 296: | { | |
| 297: | string what = document.GetText(startOffset, length); | |
| 298: | string spaces = ""; | |
| 299: | for (int i = 0; i < document.Properties.GetProperty("TabIndent", 4); ++i) { | |
| 300: | spaces += ' '; | |
| 301: | } | |
| 302: | document.Replace(startOffset, length, what.Replace(spaces, "\t")); | |
| 303: | } | |
| 304: | ||
| 305: | public override void Run() | |
| 306: | { | |
| 307: | IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow; | |
| 308: | ||
| 309: | if (window == null || !(window.WindowContent.Control is TextAreaControl)) { | |
| 310: | return; | |
| 311: | } | |
| 312: | ||
| 313: | TextAreaControl textarea = (TextAreaControl)window.WindowContent.Control; | |
| 314: | textarea.BeginUpdate(); | |
| 315: | if (textarea.HasSomethingSelected) { | |
| 316: | foreach (ITextSelection selection in textarea.Document.TextSelectionCollection) { | |
| 317: | Convert(textarea.Document, selection.Offset, selection.Length); | |
| 318: | } | |
| 319: | } else { | |
| 320: | Convert(textarea.Document, 0, textarea.Document.TextLength); | |
| 321: | } | |
| 322: | textarea.CheckCaretPos(); | |
| 323: | textarea.EndUpdate(); | |
| 324: | textarea.Refresh(); | |
| 325: | } | |
| 326: | } | |
| 327: | ||
| 328: | public class ConvertLeadingTabsToSpaces : AbstractMenuCommand | |
| 329: | { | |
| 330: | void Convert(IDocument document, int y1, int y2) | |
| 331: | { | |
| 332: | // int redocounter = 0; // must count how many Delete operations occur | |
| 333: | // Point p1 = new Point(); | |
| 334: | // Point p2 = new Point(); | |
| 335: | // | |
| 336: | // string spaces = ""; | |
| 337: | // for (int i = 0; i < textarea.Buffer.BufferOptions.TabIndent; ++i) | |
| 338: | // spaces += ' '; | |
| 339: | // | |
| 340: | // for (int i = y1; i < y2; ++i) { | |
| 341: | // p1.Y = p2.Y = i; | |
| 342: | // for (int j = 0; j < textarea.Buffer[i].Text.Length; ++j) { | |
| 343: | // if (textarea.Buffer[i].Text[j] == '\t') { | |
| 344: | // p1.X = j; | |
| 345: | // p2.X = j + 1; | |
| 346: | // textarea.Buffer.Delete(p1, p2); | |
| 347: | // textarea.Buffer.Insert(p1, spaces); | |
| 348: | // redocounter += 2; | |
| 349: | // j += textarea.Buffer.BufferOptions.TabIndent - 1; // skip inserted characters | |
| 350: | // } else | |
| 351: | // break; | |
| 352: | // } | |
| 353: | // } |