1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.text.BreakIterator;
14 import java.text.CharacterIterator;
16 import org.eclipse.jface.text.Assert;
19 * Breaks java text into word starts, also stops at line start and end. No
20 * direction dependency.
24 public class JavaWordIterator extends BreakIterator {
27 * The underlying java break iterator. It returns all breaks, including
28 * before and after every whitespace.
30 private JavaBreakIterator fIterator;
32 /** The current index for the stateful operations. */
36 * Creates a new word iterator.
38 public JavaWordIterator() {
39 fIterator = new JavaBreakIterator();
44 * @see java.text.BreakIterator#first()
47 fIndex = fIterator.first();
52 * @see java.text.BreakIterator#last()
55 fIndex = fIterator.last();
60 * @see java.text.BreakIterator#next(int)
62 public int next(int n) {
64 while (--n > 0 && next != DONE) {
71 * @see java.text.BreakIterator#next()
74 fIndex = following(fIndex);
79 * @see java.text.BreakIterator#previous()
81 public int previous() {
82 fIndex = preceding(fIndex);
87 * @see java.text.BreakIterator#preceding(int)
89 public int preceding(int offset) {
90 int first = fIterator.preceding(offset);
91 if (isWhitespace(first, offset)) {
92 int second = fIterator.preceding(first);
93 if (second != DONE && !isDelimiter(second, first))
100 * @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
126 * text represents a delimiter, <code>false</code> otherwise.
130 * @param exclusiveEnd
132 * @return <code>true</code> if the given range is a delimiter
134 private boolean isDelimiter(int offset, int exclusiveEnd) {
135 if (exclusiveEnd == DONE || offset == DONE)
138 Assert.isTrue(offset >= 0);
139 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
140 Assert.isTrue(exclusiveEnd > offset);
142 CharSequence seq = fIterator.fText;
144 while (offset < exclusiveEnd) {
145 char ch = seq.charAt(offset);
146 if (ch != '\n' && ch != '\r')
155 * Returns <code>true</code> if the given sequence into the underlying
156 * text represents whitespace, but not a delimiter, <code>false</code>
161 * @param exclusiveEnd
163 * @return <code>true</code> if the given range is whitespace
165 private boolean isWhitespace(int offset, int exclusiveEnd) {
166 if (exclusiveEnd == DONE || offset == DONE)
169 Assert.isTrue(offset >= 0);
170 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
171 Assert.isTrue(exclusiveEnd > offset);
173 CharSequence seq = fIterator.fText;
175 while (offset < exclusiveEnd) {
176 char ch = seq.charAt(offset);
177 if (!Character.isWhitespace(ch))
179 if (ch == '\n' || ch == '\r')
188 * @see java.text.BreakIterator#current()
190 public int current() {
195 * @see java.text.BreakIterator#getText()
197 public CharacterIterator getText() {
198 return fIterator.getText();
202 * Sets the text as <code>CharSequence</code>.
207 public void setText(CharSequence newText) {
208 fIterator.setText(newText);
213 * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
215 public void setText(CharacterIterator newText) {
216 fIterator.setText(newText);
221 * @see java.text.BreakIterator#setText(java.lang.String)
223 public void setText(String newText) {
224 setText((CharSequence) newText);