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 * A java break iterator. It returns all breaks, including before and after
21 * whitespace, and it returns all camelcase breaks.
23 * A line break may be any of "\n", "\r", "\r\n", "\n\r".
28 public class JavaBreakIterator extends BreakIterator {
31 * A run of common characters.
33 protected static abstract class Run {
34 /** The length of this run. */
42 * Returns <code>true</code> if this run consumes <code>ch</code>,
43 * <code>false</code> otherwise. If <code>true</code> is returned,
44 * the length of the receiver is adjusted accordingly.
46 * @param ch the character to test
47 * @return <code>true</code> if <code>ch</code> was consumed
49 protected boolean consume(char ch) {
58 * Whether this run accepts that character; does not update state. Called
59 * from the default implementation of <code>consume</code>.
61 * @param ch the character to test
62 * @return <code>true</code> if <code>ch</code> is accepted
64 protected abstract boolean isValid(char ch);
67 * Resets this run to the initial state.
69 protected void init() {
74 static final class Whitespace extends Run {
75 protected boolean isValid(char ch) {
76 return Character.isWhitespace(ch) && ch != '\n' && ch != '\r';
80 static final class LineDelimiter extends Run {
81 /** State: INIT -> delimiter -> EXIT. */
83 private static final char INIT= '\0';
84 private static final char EXIT= '\1';
87 * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator.Run#init()
89 protected void init() {
95 * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator.Run#consume(char)
97 protected boolean consume(char ch) {
98 if (!isValid(ch) || fState == EXIT)
101 if (fState == INIT) {
105 } else if (fState != ch) {
114 protected boolean isValid(char ch) {
115 return ch == '\n' || ch == '\r';
119 static final class Identifier extends Run {
121 * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator.Run#isValid(char)
123 protected boolean isValid(char ch) {
124 return Character.isJavaIdentifierPart(ch);
128 static final class CamelCaseIdentifier extends Run {
130 private static final int S_INIT= 0;
131 private static final int S_LOWER= 1;
132 private static final int S_ONE_CAP= 2;
133 private static final int S_ALL_CAPS= 3;
134 private static final int S_EXIT= 4;
135 private static final int S_EXIT_MINUS_ONE= 5;
137 /* character types */
138 private static final int K_INVALID= 0;
139 private static final int K_LOWER= 1;
140 private static final int K_UPPER= 2;
141 private static final int K_OTHER= 3;
145 private final static int[][] MATRIX= new int[][] {
146 // K_INVALID, K_LOWER, K_UPPER, K_OTHER
147 { S_EXIT, S_LOWER, S_ONE_CAP, S_LOWER }, // S_INIT
148 { S_EXIT, S_LOWER, S_EXIT, S_LOWER }, // S_LOWER
149 { S_EXIT, S_LOWER, S_ALL_CAPS, S_LOWER }, // S_ONE_CAP
150 { S_EXIT, S_EXIT_MINUS_ONE, S_ALL_CAPS, S_LOWER }, // S_ALL_CAPS
154 * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator.Run#init()
156 protected void init() {
162 * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator.Run#consumes(char)
164 protected boolean consume(char ch) {
165 int kind= getKind(ch);
166 fState= MATRIX[fState][kind];
175 case S_EXIT_MINUS_ONE:
179 Assert.isTrue(false);
185 * Determines the kind of a character.
187 * @param ch the character to test
189 private int getKind(char ch) {
190 if (Character.isUpperCase(ch))
192 if (Character.isLowerCase(ch))
194 if (Character.isJavaIdentifierPart(ch)) // _, digits...
200 * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator.Run#isValid(char)
202 protected boolean isValid(char ch) {
203 return Character.isJavaIdentifierPart(ch);
207 static final class Other extends Run {
209 * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator.Run#isValid(char)
211 protected boolean isValid(char ch) {
212 return !Character.isWhitespace(ch) && !Character.isJavaIdentifierPart(ch);
216 private static final Run WHITESPACE= new Whitespace();
217 private static final Run DELIMITER= new LineDelimiter();
218 private static final Run CAMELCASE= new CamelCaseIdentifier(); // new Identifier();
219 private static final Run OTHER= new Other();
221 /** The platform break iterator (word instance) used as a base. */
222 protected final BreakIterator fIterator;
223 /** The text we operate on. */
224 protected CharSequence fText;
225 /** our current position for the stateful methods. */
230 * Creates a new break iterator.
232 public JavaBreakIterator() {
233 fIterator= BreakIterator.getWordInstance();
234 fIndex= fIterator.current();
238 * @see java.text.BreakIterator#current()
240 public int current() {
245 * @see java.text.BreakIterator#first()
248 fIndex= fIterator.first();
253 * @see java.text.BreakIterator#following(int)
255 public int following(int offset) {
256 // work around too eager IAEs in standard impl
257 if (offset == getText().getEndIndex())
260 int next= fIterator.following(offset);
264 // TODO deal with complex script word boundaries
265 // Math.min(offset + run.length, next) does not work
266 // since wordinstance considers _ as boundaries
267 // seems to work fine, however
268 Run run= consumeRun(offset);
269 return offset + run.length;
274 * Consumes a run of characters at the limits of which we introduce a break.
275 * @param offset the offset to start at
276 * @return the run that was consumed
278 private Run consumeRun(int offset) {
279 // assert offset < length
281 char ch= fText.charAt(offset);
282 int length= fText.length();
284 while (run.consume(ch) && offset < length - 1) {
286 ch= fText.charAt(offset);
293 * Retunrs a run based on a character.
295 * @param ch the character to test
296 * @return the correct character given <code>ch</code>
298 private Run getRun(char ch) {
300 if (WHITESPACE.isValid(ch))
302 else if (DELIMITER.isValid(ch))
304 else if (CAMELCASE.isValid(ch))
306 else if (OTHER.isValid(ch))
309 Assert.isTrue(false);
318 * @see java.text.BreakIterator#getText()
320 public CharacterIterator getText() {
321 return fIterator.getText();
325 * @see java.text.BreakIterator#isBoundary(int)
327 public boolean isBoundary(int offset) {
328 if (offset == getText().getBeginIndex())
331 return following(offset - 1) == offset;
335 * @see java.text.BreakIterator#last()
338 fIndex= fIterator.last();
343 * @see java.text.BreakIterator#next()
346 fIndex= following(fIndex);
351 * @see java.text.BreakIterator#next(int)
353 public int next(int n) {
354 return fIterator.next(n);
358 * @see java.text.BreakIterator#preceding(int)
360 public int preceding(int offset) {
361 if (offset == getText().getBeginIndex())
364 if (isBoundary(offset - 1))
367 int previous= offset - 1;
369 previous= fIterator.preceding(previous);
370 } while (!isBoundary(previous));
373 while (previous < offset) {
375 previous= following(previous);
382 * @see java.text.BreakIterator#previous()
384 public int previous() {
385 fIndex= preceding(fIndex);
390 * @see java.text.BreakIterator#setText(java.lang.String)
392 public void setText(String newText) {
393 setText((CharSequence) newText);
397 * Creates a break iterator given a char sequence.
398 * @param newText the new text
400 public void setText(CharSequence newText) {
402 fIterator.setText(new SequenceCharacterIterator(newText));
407 * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
409 public void setText(CharacterIterator newText) {
410 if (newText instanceof CharSequence) {
411 fText= (CharSequence) newText;
412 fIterator.setText(newText);
415 throw new UnsupportedOperationException("CharacterIterator not supported"); //$NON-NLS-1$