1 11 package org.eclipse.jdt.internal.ui.text; 12 13 import com.ibm.icu.text.BreakIterator; 14 import java.text.CharacterIterator ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 19 20 26 public class JavaWordIterator extends BreakIterator { 27 28 32 private JavaBreakIterator fIterator; 33 34 private int fIndex; 35 36 39 public JavaWordIterator() { 40 fIterator= new JavaBreakIterator(); 41 first(); 42 } 43 44 47 public int first() { 48 fIndex= fIterator.first(); 49 return fIndex; 50 } 51 52 55 public int last() { 56 fIndex= fIterator.last(); 57 return fIndex; 58 } 59 60 63 public int next(int n) { 64 int next= 0; 65 while (--n > 0 && next != DONE) { 66 next= next(); 67 } 68 return next; 69 } 70 71 74 public int next() { 75 fIndex= following(fIndex); 76 return fIndex; 77 } 78 79 82 public int previous() { 83 fIndex= preceding(fIndex); 84 return fIndex; 85 } 86 87 88 91 public int preceding(int offset) { 92 int first= fIterator.preceding(offset); 93 if (isWhitespace(first, offset)) { 94 int second= fIterator.preceding(first); 95 if (second != DONE && !isDelimiter(second, first)) 96 return second; 97 } 98 return first; 99 } 100 101 104 public int following(int offset) { 105 int first= fIterator.following(offset); 106 if (eatFollowingWhitespace(offset, first)) { 107 int second= fIterator.following(first); 108 if (isWhitespace(first, second)) 109 return second; 110 } 111 return first; 112 } 113 114 private boolean eatFollowingWhitespace(int offset, int exclusiveEnd) { 115 if (exclusiveEnd == DONE || offset == DONE) 116 return false; 117 118 if (isWhitespace(offset, exclusiveEnd)) 119 return false; 120 if (isDelimiter(offset, exclusiveEnd)) 121 return false; 122 123 return true; 124 } 125 126 134 private boolean isDelimiter(int offset, int exclusiveEnd) { 135 if (exclusiveEnd == DONE || offset == DONE) 136 return false; 137 138 Assert.isTrue(offset >= 0); 139 Assert.isTrue(exclusiveEnd <= getText().getEndIndex()); 140 Assert.isTrue(exclusiveEnd > offset); 141 142 CharSequence seq= fIterator.fText; 143 144 while (offset < exclusiveEnd) { 145 char ch= seq.charAt(offset); 146 if (ch != '\n' && ch != '\r') 147 return false; 148 offset++; 149 } 150 151 return true; 152 } 153 154 162 private boolean isWhitespace(int offset, int exclusiveEnd) { 163 if (exclusiveEnd == DONE || offset == DONE) 164 return false; 165 166 Assert.isTrue(offset >= 0); 167 Assert.isTrue(exclusiveEnd <= getText().getEndIndex()); 168 Assert.isTrue(exclusiveEnd > offset); 169 170 CharSequence seq= fIterator.fText; 171 172 while (offset < exclusiveEnd) { 173 char ch= seq.charAt(offset); 174 if (!Character.isWhitespace(ch)) 175 return false; 176 if (ch == '\n' || ch == '\r') 177 return false; 178 offset++; 179 } 180 181 return true; 182 } 183 184 187 public int current() { 188 return fIndex; 189 } 190 191 194 public CharacterIterator getText() { 195 return fIterator.getText(); 196 } 197 198 202 public void setText(CharSequence newText) { 203 fIterator.setText(newText); 204 first(); 205 } 206 207 210 public void setText(CharacterIterator newText) { 211 fIterator.setText(newText); 212 first(); 213 } 214 215 218 public void setText(String newText) { 219 setText((CharSequence ) newText); 220 } 221 222 } 223 | Popular Tags |