1 package org.simantics.scl.ui.editor2.iterator;
3 import java.text.CharacterIterator;
5 import com.ibm.icu.text.BreakIterator;
7 import org.eclipse.core.runtime.Assert;
12 * Breaks java text into word starts, also stops at line start and end. No
13 * direction dependency.
17 public class JavaWordIterator extends BreakIterator {
20 * The underlying java break iterator. It returns all breaks, including
21 * before and after every whitespace.
23 private JavaBreakIterator fIterator;
24 /** The current index for the stateful operations. */
28 * Creates a new word iterator.
30 public JavaWordIterator() {
31 fIterator= new JavaBreakIterator();
36 * @see java.text.BreakIterator#first()
40 fIndex= fIterator.first();
45 * @see java.text.BreakIterator#last()
49 fIndex= fIterator.last();
54 * @see java.text.BreakIterator#next(int)
57 public int next(int n) {
59 while (--n > 0 && next != DONE) {
66 * @see java.text.BreakIterator#next()
70 fIndex= following(fIndex);
75 * @see java.text.BreakIterator#previous()
78 public int previous() {
79 fIndex= preceding(fIndex);
85 * @see java.text.BreakIterator#preceding(int)
88 public int preceding(int offset) {
89 int first= fIterator.preceding(offset);
90 if (isWhitespace(first, offset)) {
91 int second= fIterator.preceding(first);
92 if (second != DONE && !isDelimiter(second, first))
99 * @see java.text.BreakIterator#following(int)
102 public int following(int offset) {
103 int first= fIterator.following(offset);
104 if (eatFollowingWhitespace(offset, first)) {
105 int second= fIterator.following(first);
106 if (isWhitespace(first, second))
112 private boolean eatFollowingWhitespace(int offset, int exclusiveEnd) {
113 if (exclusiveEnd == DONE || offset == DONE)
116 if (isWhitespace(offset, exclusiveEnd))
118 if (isDelimiter(offset, exclusiveEnd))
125 * Returns <code>true</code> if the given sequence into the underlying text
126 * represents a delimiter, <code>false</code> otherwise.
128 * @param offset the offset
129 * @param exclusiveEnd the end offset
130 * @return <code>true</code> if the given range is a delimiter
132 private boolean isDelimiter(int offset, int exclusiveEnd) {
133 if (exclusiveEnd == DONE || offset == DONE)
136 Assert.isTrue(offset >= 0);
137 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
138 Assert.isTrue(exclusiveEnd > offset);
140 CharSequence seq= fIterator.fText;
142 while (offset < exclusiveEnd) {
143 char ch= seq.charAt(offset);
144 if (ch != '\n' && ch != '\r')
153 * Returns <code>true</code> if the given sequence into the underlying text
154 * represents whitespace, but not a delimiter, <code>false</code> otherwise.
156 * @param offset the offset
157 * @param exclusiveEnd the end offset
158 * @return <code>true</code> if the given range is whitespace
160 private boolean isWhitespace(int offset, int exclusiveEnd) {
161 if (exclusiveEnd == DONE || offset == DONE)
164 Assert.isTrue(offset >= 0);
165 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
166 Assert.isTrue(exclusiveEnd > offset);
168 CharSequence seq= fIterator.fText;
170 while (offset < exclusiveEnd) {
171 char ch= seq.charAt(offset);
172 if (!Character.isWhitespace(ch))
174 if (ch == '\n' || ch == '\r')
183 * @see java.text.BreakIterator#current()
186 public int current() {
191 * @see java.text.BreakIterator#getText()
194 public CharacterIterator getText() {
195 return fIterator.getText();
199 * Sets the text as <code>CharSequence</code>.
200 * @param newText the new text
202 public void setText(CharSequence newText) {
203 fIterator.setText(newText);
208 * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
211 public void setText(CharacterIterator newText) {
212 fIterator.setText(newText);
217 * @see java.text.BreakIterator#setText(java.lang.String)
220 public void setText(String newText) {
221 setText((CharSequence) newText);