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;
17 //import org.eclipse.jface.text.Assert;
18 import org.eclipse.core.runtime.Assert;
21 * Breaks java text into word starts, also stops at line start and end. No
22 * direction dependency.
26 public class JavaWordIterator extends BreakIterator {
29 * The underlying java break iterator. It returns all breaks, including
30 * before and after every whitespace.
32 private JavaBreakIterator fIterator;
34 /** The current index for the stateful operations. */
38 * Creates a new word iterator.
40 public JavaWordIterator() {
41 fIterator = new JavaBreakIterator();
46 * @see java.text.BreakIterator#first()
49 fIndex = fIterator.first();
54 * @see java.text.BreakIterator#last()
57 fIndex = fIterator.last();
62 * @see java.text.BreakIterator#next(int)
64 public int next(int n) {
66 while (--n > 0 && next != DONE) {
73 * @see java.text.BreakIterator#next()
76 fIndex = following(fIndex);
81 * @see java.text.BreakIterator#previous()
83 public int previous() {
84 fIndex = preceding(fIndex);
89 * @see java.text.BreakIterator#preceding(int)
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))
102 * @see java.text.BreakIterator#following(int)
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))
114 private boolean eatFollowingWhitespace(int offset, int exclusiveEnd) {
115 if (exclusiveEnd == DONE || offset == DONE)
118 if (isWhitespace(offset, exclusiveEnd))
120 if (isDelimiter(offset, exclusiveEnd))
127 * Returns <code>true</code> if the given sequence into the underlying
128 * text represents a delimiter, <code>false</code> otherwise.
132 * @param exclusiveEnd
134 * @return <code>true</code> if the given range is a delimiter
136 private boolean isDelimiter(int offset, int exclusiveEnd) {
137 if (exclusiveEnd == DONE || offset == DONE)
140 Assert.isTrue(offset >= 0);
141 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
142 Assert.isTrue(exclusiveEnd > offset);
144 CharSequence seq = fIterator.fText;
146 while (offset < exclusiveEnd) {
147 char ch = seq.charAt(offset);
148 if (ch != '\n' && ch != '\r')
157 * Returns <code>true</code> if the given sequence into the underlying
158 * text represents whitespace, but not a delimiter, <code>false</code>
163 * @param exclusiveEnd
165 * @return <code>true</code> if the given range is whitespace
167 private boolean isWhitespace(int offset, int exclusiveEnd) {
168 if (exclusiveEnd == DONE || offset == DONE)
171 Assert.isTrue(offset >= 0);
172 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
173 Assert.isTrue(exclusiveEnd > offset);
175 CharSequence seq = fIterator.fText;
177 while (offset < exclusiveEnd) {
178 char ch = seq.charAt(offset);
179 if (!Character.isWhitespace(ch))
181 if (ch == '\n' || ch == '\r')
190 * @see java.text.BreakIterator#current()
192 public int current() {
197 * @see java.text.BreakIterator#getText()
199 public CharacterIterator getText() {
200 return fIterator.getText();
204 * Sets the text as <code>CharSequence</code>.
209 public void setText(CharSequence newText) {
210 fIterator.setText(newText);
215 * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
217 public void setText(CharacterIterator newText) {
218 fIterator.setText(newText);
223 * @see java.text.BreakIterator#setText(java.lang.String)
225 public void setText(String newText) {
226 setText((CharSequence) newText);