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;
20 * Breaks java text into word starts, also stops at line start and end. No
21 * direction dependency.
25 public class JavaWordIterator extends BreakIterator {
28 * The underlying java break iterator. It returns all breaks, including
29 * before and after every whitespace.
31 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);
88 * @see java.text.BreakIterator#preceding(int)
90 public int preceding(int offset) {
91 int first= fIterator.preceding(offset);
92 if (isWhitespace(first, offset)) {
93 int second= fIterator.preceding(first);
94 if (second != DONE && !isDelimiter(second, first))
101 * @see java.text.BreakIterator#following(int)
103 public int following(int offset) {
104 int first= fIterator.following(offset);
105 if (eatFollowingWhitespace(offset, first)) {
106 int second= fIterator.following(first);
107 if (isWhitespace(first, second))
113 private boolean eatFollowingWhitespace(int offset, int exclusiveEnd) {
114 if (exclusiveEnd == DONE || offset == DONE)
117 if (isWhitespace(offset, exclusiveEnd))
119 if (isDelimiter(offset, exclusiveEnd))
126 * Returns <code>true</code> if the given sequence into the underlying text
127 * represents a delimiter, <code>false</code> otherwise.
129 * @param offset the offset
130 * @param exclusiveEnd the end offset
131 * @return <code>true</code> if the given range is a delimiter
133 private boolean isDelimiter(int offset, int exclusiveEnd) {
134 if (exclusiveEnd == DONE || offset == DONE)
137 Assert.isTrue(offset >= 0);
138 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
139 Assert.isTrue(exclusiveEnd > offset);
141 CharSequence seq= fIterator.fText;
143 while (offset < exclusiveEnd) {
144 char ch= seq.charAt(offset);
145 if (ch != '\n' && ch != '\r')
154 * Returns <code>true</code> if the given sequence into the underlying text
155 * represents whitespace, but not a delimiter, <code>false</code> otherwise.
157 * @param offset the offset
158 * @param exclusiveEnd the end offset
159 * @return <code>true</code> if the given range is whitespace
161 private boolean isWhitespace(int offset, int exclusiveEnd) {
162 if (exclusiveEnd == DONE || offset == DONE)
165 Assert.isTrue(offset >= 0);
166 Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
167 Assert.isTrue(exclusiveEnd > offset);
169 CharSequence seq= fIterator.fText;
171 while (offset < exclusiveEnd) {
172 char ch= seq.charAt(offset);
173 if (!Character.isWhitespace(ch))
175 if (ch == '\n' || ch == '\r')
184 * @see java.text.BreakIterator#current()
186 public int current() {
191 * @see java.text.BreakIterator#getText()
193 public CharacterIterator getText() {
194 return fIterator.getText();
198 * Sets the text as <code>CharSequence</code>.
199 * @param newText the new text
201 public void setText(CharSequence newText) {
202 fIterator.setText(newText);
207 * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
209 public void setText(CharacterIterator newText) {
210 fIterator.setText(newText);
215 * @see java.text.BreakIterator#setText(java.lang.String)
217 public void setText(String newText) {
218 setText((CharSequence) newText);