1 30 31 package org.syntax.jedit; 32 33 import javax.swing.text.*; 34 35 40 public class TextUtilities 41 { 42 51 public static int findMatchingBracket(Document doc, int offset) 52 throws BadLocationException 53 { 54 if(doc.getLength() == 0) 55 return -1; 56 char c = doc.getText(offset,1).charAt(0); 57 char cprime; boolean direction; 60 switch(c) 61 { 62 case '(': cprime = ')'; direction = false; break; 63 case ')': cprime = '('; direction = true; break; 64 case '[': cprime = ']'; direction = false; break; 65 case ']': cprime = '['; direction = true; break; 66 case '{': cprime = '}'; direction = false; break; 67 case '}': cprime = '{'; direction = true; break; 68 default: return -1; 69 } 70 71 int count; 72 73 76 if(direction) 78 { 79 count = 1; 82 83 String text = doc.getText(0,offset); 85 86 for(int i = offset - 1; i >= 0; i--) 88 { 89 char x = text.charAt(i); 94 if(x == c) 95 count++; 96 97 else if(x == cprime) 101 { 102 if(--count == 0) 103 return i; 104 } 105 } 106 } 107 else 108 { 109 count = 1; 112 113 offset++; 115 116 int len = doc.getLength() - offset; 118 119 String text = doc.getText(offset,len); 121 122 for(int i = 0; i < len; i++) 124 { 125 char x = text.charAt(i); 130 131 if(x == c) 132 count++; 133 134 else if(x == cprime) 138 { 139 if(--count == 0) 140 return i + offset; 141 } 142 } 143 } 144 145 return -1; 147 } 148 149 154 public static int findWordStart(String line, int pos, String noWordSep) 155 { 156 char ch = line.charAt(pos - 1); 157 158 if(noWordSep == null) 159 noWordSep = ""; 160 boolean selectNoLetter = (!Character.isLetterOrDigit(ch) 161 && noWordSep.indexOf(ch) == -1); 162 163 int wordStart = 0; 164 for(int i = pos - 1; i >= 0; i--) 165 { 166 ch = line.charAt(i); 167 if(selectNoLetter ^ (!Character.isLetterOrDigit(ch) && 168 noWordSep.indexOf(ch) == -1)) 169 { 170 wordStart = i + 1; 171 break; 172 } 173 } 174 175 return wordStart; 176 } 177 178 183 public static int findWordEnd(String line, int pos, String noWordSep) 184 { 185 char ch = line.charAt(pos); 186 187 if(noWordSep == null) 188 noWordSep = ""; 189 boolean selectNoLetter = (!Character.isLetterOrDigit(ch) 190 && noWordSep.indexOf(ch) == -1); 191 192 int wordEnd = line.length(); 193 for(int i = pos; i < line.length(); i++) 194 { 195 ch = line.charAt(i); 196 if(selectNoLetter ^ (!Character.isLetterOrDigit(ch) && 197 noWordSep.indexOf(ch) == -1)) 198 { 199 wordEnd = i; 200 break; 201 } 202 } 203 return wordEnd; 204 } 205 } 206 | Popular Tags |