| 1: | // ITextSelection.cs | |
| 2: | // Copyright (c) 2001 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.Diagnostics; | |
| 20: | ||
| 21: | using Core.Properties; | |
| 22: | using SharpDevelop.Internal.Undo; | |
| 23: | using SharpDevelop.Gui.Edit; // for the ProgressEventHandler | |
| 24: | ||
| 25: | namespace SharpDevelop.DefaultEditor.Text { | |
| 26: | ||
| 27: | public sealed class TextUtilities | |
| 28: | { | |
| 29: | public static int GetFirstNonWSChar(IDocument document, int offset) | |
| 30: | { | |
| 31: | while (offset < document.TextLength && Char.IsWhiteSpace(document.GetCharAt(offset))) { | |
| 32: | ++offset; | |
| 33: | } | |
| 34: | return offset; | |
| 35: | } | |
| 36: | ||
| 37: | public static int FindWordEnd(IDocument document, int offset) | |
| 38: | { | |
| 39: | LineSegment line = document.GetLineSegmentOfOffset(offset); | |
| 40: | int endPos = line.Offset + line.Length; | |
| 41: | while (offset < endPos && Char.IsLetterOrDigit(document.GetCharAt(offset))) { | |
| 42: | ++offset; | |
| 43: | } | |
| 44: | ||
| 45: | return offset; | |
| 46: | } | |
| 47: | ||
| 48: | public static int FindWordStart(IDocument document, int offset) | |
| 49: | { | |
| 50: | LineSegment line = document.GetLineSegmentOfOffset(offset); | |
| 51: | ||
| 52: | while (offset > line.Offset && !Char.IsLetterOrDigit(document.GetCharAt(offset - 1))) { | |
| 53: | --offset; | |
| 54: | } | |
| 55: | ||
| 56: | return offset; | |
| 57: | } | |
| 58: | ||
| 59: | public static int FindNextWordEnd(IDocument document, int offset) | |
| 60: | { | |
| 61: | LineSegment line = document.GetLineSegmentOfOffset(offset); | |
| 62: | offset = FindWordEnd(document, offset); | |
| 63: | ||
| 64: | int endPos = line.Offset + line.Length; | |
| 65: | while (offset < endPos && !Char.IsLetterOrDigit(document.GetCharAt(offset))) { | |
| 66: | ++offset; | |
| 67: | } | |
| 68: | return offset; | |
| 69: | } | |
| 70: | ||
| 71: | public static int FindPrevWordStart(IDocument document, int offset) | |
| 72: | { | |
| 73: | offset = FindWordStart(document, offset); | |
| 74: | LineSegment line = document.GetLineSegmentOfOffset(offset); | |
| 75: | while (offset > line.Offset && Char.IsLetterOrDigit(document.GetCharAt(offset - 1))) { | |
| 76: | --offset; | |
| 77: | } | |
| 78: | return offset; | |
| 79: | } | |
| 80: | ||
| 81: | public static string GetLineAsString(IDocument document, int lineNumber) | |
| 82: | { | |
| 83: | LineSegment line = document.GetLineSegment(lineNumber); | |
| 84: | return document.GetText(line.Offset, line.Length); | |
| 85: | } | |
| 86: | ||
| 87: | static bool ScanLineComment(IDocument document, int offset) | |
| 88: | { | |
| 89: | while (offset > 0 && offset < document.TextLength) { | |
| 90: | char ch = document.GetCharAt(offset); | |
| 91: | switch (ch) { | |
| 92: | case '\r': | |
| 93: | case '\n': | |
| 94: | return false; | |
| 95: | case '/': | |
| 96: | if (document.GetCharAt(offset + 1) == '/') { | |
| 97: | return true; | |
| 98: | } | |
| 99: | break; | |
| 100: | } | |
| 101: | --offset; | |
| 102: | } | |
| 103: | return false; | |
| 104: | } | |
| 105: | ||
| 106: | public static int SearchBracketBackward(IDocument document, int offset, char openBracket, char closingBracket) | |
| 107: | { | |
| 108: | int brackets = -1; | |
| 109: | ||
| 110: | bool inString = false; | |
| 111: | bool inChar = false; | |
| 112: | ||
| 113: | bool blockComment = false; | |
| 114: | ||
| 115: | while (offset > 0 && offset < document.TextLength) { | |
| 116: | char ch = document.GetCharAt(offset); | |
| 117: | switch (ch) { | |
| 118: | case '/': | |
| 119: | if (blockComment) { | |
| 120: | if (document.GetCharAt(offset + 1) == '*') { | |
| 121: | blockComment = false; | |
| 122: | } | |
| 123: | } | |
| 124: | if (!inString && !inChar && offset + 1 < document.TextLength) { | |
| 125: | if (document.GetCharAt(offset - 1) == '*') { | |
| 126: | blockComment = true; | |
| 127: | } | |
| 128: | } | |
| 129: | break; | |
| 130: | case '"': | |
| 131: | inString = !inString; | |
| 132: | break; | |
| 133: | case '\'': | |
| 134: | inChar = !inChar; | |
| 135: | break; | |
| 136: | default : | |
| 137: | if (ch == closingBracket) { | |
| 138: | if (!(inString || inChar || blockComment) && !ScanLineComment(document, offset)) { | |
| 139: | --brackets; | |
| 140: | } | |
| 141: | } else if (ch == openBracket) { | |
| 142: | if (!(inString || inChar || blockComment) && !ScanLineComment(document, offset)) { | |
| 143: | ++brackets; | |
| 144: | if (brackets == 0) { | |
| 145: | return offset; | |
| 146: | } | |
| 147: | } | |
| 148: | } | |
| 149: | break; | |
| 150: | } | |
| 151: | --offset; | |
| 152: | } | |
| 153: | return - 1; | |
| 154: | } | |
| 155: | ||
| 156: | public static int SearchBracketForward(IDocument document, int offset, char openBracket, char closingBracket) | |
| 157: | { | |
| 158: | int brackets = 1; | |
| 159: | ||
| 160: | bool inString = false; | |
| 161: | bool inChar = false; | |
| 162: | ||
| 163: | bool lineComment = false; | |
| 164: | bool blockComment = false; | |
| 165: | ||
| 166: | if (offset >= 0) { | |
| 167: | while (offset < document.TextLength) { | |
| 168: | char ch = document.GetCharAt(offset); | |
| 169: | switch (ch) { | |
| 170: | case '\r': | |
| 171: | case '\n': | |
| 172: | lineComment = false; | |
| 173: | break; | |
| 174: | case '/': | |
| 175: | if (blockComment) { | |
| 176: | Debug.Assert(offset > 0); | |
| 177: | if (document.GetCharAt(offset - 1) == '*') { | |
| 178: | blockComment = false; | |
| 179: | } | |
| 180: | } | |
| 181: | if (!inString && !inChar && offset + 1 < document.TextLength) { | |
| 182: | if (!blockComment && document.GetCharAt(offset + 1) == '/') { | |
| 183: | lineComment = true; | |
| 184: | } | |
| 185: | if (!lineComment && document.GetCharAt(offset + 1) == '*') { | |
| 186: | blockComment = true; | |
| 187: | } | |
| 188: | } | |
| 189: | break; | |
| 190: | case '"': | |
| 191: | inString = !inString; | |
| 192: | break; | |
| 193: | case '\'': | |
| 194: | inChar = !inChar; | |
| 195: | break; | |
| 196: | default : | |
| 197: | if (ch == openBracket) { | |
| 198: | if (!(inString || inChar || lineComment || blockComment)) { | |
| 199: | ++brackets; | |
| 200: | } | |
| 201: | } else if (ch == closingBracket) { | |
| 202: | if (!(inString || inChar || lineComment || blockComment)) { | |
| 203: | --brackets; | |
| 204: | if (brackets == 0) { | |
| 205: | return offset; | |
| 206: | } | |
| 207: | } | |
| 208: | } | |
| 209: | break; | |
| 210: | } | |
| 211: | ++offset; | |
| 212: | } | |
| 213: | } | |
| 214: | return -1; | |
| 215: | } | |
| 216: | ||
| 217: | /// <summary> | |
| 218: | /// Returns true, if the line lineNumber is empty or filled with whitespaces. | |
| 219: | /// </summary> | |
| 220: | public static bool IsEmptyLine(IDocument document, int lineNumber) | |
| 221: | { | |
| 222: | return IsEmptyLine(document, document.GetLineSegment(lineNumber)); | |
| 223: | } | |
| 224: | ||
| 225: | /// <summary> | |
| 226: | /// Returns true, if the line lineNumber is empty or filled with whitespaces. | |
| 227: | /// </summary> | |
| 228: | public static bool IsEmptyLine(IDocument document, LineSegment line) | |
| 229: | { | |
| 230: | for (int i = line.Offset; i < line.Offset + line.Length; ++i) { | |
| 231: | char ch = document.GetCharAt(i); | |
| 232: | if (!Char.IsWhiteSpace(ch)) { | |
| 233: | return false; | |
| 234: | } | |
| 235: | } | |
| 236: | return true; | |
| 237: | } | |
| 238: | } | |
| 239: | } |
This page was automatically generated by SharpDevelop.