Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.editor.model;
  
 
 import  org.eclipse.core.runtime.Assert;
 
This class was copied from org.eclipse.jdt.internal.ui.text.JavaBreakIterator.
 
 /*
  * @see org.eclipse.jdt.internal.ui.text.JavaBreakIterator
  */
 public class CommonBreakIterator extends BreakIterator {

A run of common characters.
 
 	protected static abstract class Run {
The length of this run.
 
 		protected int length;
 
 		public Run() {
 			init();
 		}

Returns true if this run consumes ch, false otherwise. If true is returned, the length of the receiver is adjusted accordingly.

Parameters:
ch the character to test
Returns:
true if ch was consumed
 
 		protected boolean consume(char ch) {
 			if (isValid(ch)) {
 				++;
 				return true;
 			}
 			return false;
 		}

Whether this run accepts that character; does not update state. Called from the default implementation of consume.

Parameters:
ch the character to test
Returns:
true if ch is accepted
 
 		protected abstract boolean isValid(char ch);

Resets this run to the initial state.
 
 		protected void init() {
 			 = 0;
 		}
 	}
 
 	protected static class Whitespace extends Run {
 		protected boolean isValid(char ch) {
 			return Character.isWhitespace(ch) && ch != '\n' && ch != '\r';
 		}
 	}
 
 	protected static class LineDelimiter extends Run {
State: INIT -> delimiter -> EXIT.
 
 		private char fState;
 		private static final char INIT = '\0';
 		private static final char EXIT = '\1';
 
 		/*
 		 * @see org.eclipse.xtext.ui.editor.model.CommonBreakIterator.Run#init()
 		 */
 		protected void init() {
 			super.init();
 			 = ;
 		}
 
 		/*
 		 * @see org.eclipse.xtext.ui.editor.model.CommonBreakIterator.Run#consume(char)
 		 */
 		protected boolean consume(char ch) {
 			if (!isValid(ch) ||  == )
 				return false;
 
 			if ( == ) {
				 = ch;
				return true;
else if ( != ch) {
				return true;
else {
				return false;
			}
		}
		protected boolean isValid(char ch) {
			return ch == '\n' || ch == '\r';
		}
	}
	protected static class Identifier extends Run {
		/*
		 * @see org.eclipse.xtext.ui.editor.model.CommonBreakIterator.Run#isValid(char)
		 */
		protected boolean isValid(char ch) {
			return Character.isJavaIdentifierPart(ch);
		}
	}
	protected static class CamelCaseIdentifier extends Run {
		/* states */
		private static final int S_INIT = 0;
		private static final int S_LOWER = 1;
		private static final int S_ONE_CAP = 2;
		private static final int S_ALL_CAPS = 3;
		private static final int S_EXIT = 4;
		private static final int S_EXIT_MINUS_ONE = 5;
		/* character types */
		private static final int K_INVALID = 0;
		private static final int K_LOWER = 1;
		private static final int K_UPPER = 2;
		private static final int K_OTHER = 3;
		private int fState;
		private final static int[][] MATRIX = new int[][] {
				// K_INVALID, K_LOWER,           K_UPPER,    K_OTHER
 }, // S_INIT
 }, // S_LOWER
 }, // S_ONE_CAP
 }, // S_ALL_CAPS
		};
		/*
		 * @see org.eclipse.xtext.ui.editor.model.CommonBreakIterator.Run#init()
		 */
		protected void init() {
			super.init();
		}
		/*
		 * @see org.eclipse.xtext.ui.editor.model.CommonBreakIterator.Run#consumes(char)
		 */
		protected boolean consume(char ch) {
			int kind = getKind(ch);
			 = [][kind];
			switch () {
				case :
				case :
					return true;
				case :
					return false;
					return false;
				default:
					Assert.isTrue(false);
					return false;
			}
		}

Determines the kind of a character.

Parameters:
ch the character to test
		protected int getKind(char ch) {
			if (Character.isUpperCase(ch))
				return ;
			if (Character.isLowerCase(ch))
				return ;
			if (Character.isJavaIdentifierPart(ch)) // _, digits...
				return ;
			return ;
		}
		/*
		 * @see org.eclipse.xtext.ui.editor.model.CommonBreakIterator.Run#isValid(char)
		 */
		protected boolean isValid(char ch) {
			return Character.isJavaIdentifierPart(ch);
		}
	}
	protected static class Other extends Run {
		/*
		 * @see org.eclipse.xtext.ui.editor.model.CommonBreakIterator.Run#isValid(char)
		 */
		protected boolean isValid(char ch) {
			return !Character.isWhitespace(ch) && !Character.isJavaIdentifierPart(ch);
		}
	}

A CharSequence based implementation of CharacterIterator. Copied from org.eclipse.jdt.internal.ui.text.SequenceCharacterIterator.
	/*
	 * @see org.eclipse.jdt.internal.ui.text.SequenceCharacterIterator
	 */
	protected class SequenceCharacterIterator implements CharacterIterator {
		private int fIndex = -1;
		private final CharSequence fSequence;
		private final int fFirst;
		private final int fLast;
		private void invariant() {
			Assert.isTrue( >= );
			Assert.isTrue( <= );
		}

Creates an iterator for the entire sequence.

Parameters:
sequence the sequence backing this iterator
			this(sequence, 0);
		}

Creates an iterator.

Parameters:
sequence the sequence backing this iterator
first the first character to consider
Throws:
IllegalArgumentException if the indices are out of bounds
		public SequenceCharacterIterator(CharSequence sequenceint firstthrows IllegalArgumentException {
			this(sequencefirstsequence.length());
		}

Creates an iterator.

Parameters:
sequence the sequence backing this iterator
first the first character to consider
last the last character index to consider
Throws:
IllegalArgumentException if the indices are out of bounds
		public SequenceCharacterIterator(CharSequence sequenceint firstint lastthrows IllegalArgumentException {
			if (sequence == null)
				throw new NullPointerException();
			if (first < 0 || first > last)
			if (last > sequence.length())
			 = sequence;
			 = first;
			 = last;
			 = first;
		}
		/*
		 * @see java.text.CharacterIterator#first()
		 */
		public char first() {
		}
		/*
		 * @see java.text.CharacterIterator#last()
		 */
		public char last() {
			if ( == )
			else
				return setIndex(getEndIndex() - 1);
		}
		/*
		 * @see java.text.CharacterIterator#current()
		 */
		public char current() {
			if ( >=  &&  < )
			else
				return ;
		}
		/*
		 * @see java.text.CharacterIterator#next()
		 */
		public char next() {
			return setIndex(Math.min( + 1, getEndIndex()));
		}
		/*
		 * @see java.text.CharacterIterator#previous()
		 */
		public char previous() {
			if ( > getBeginIndex()) {
				return setIndex( - 1);
else {
				return ;
			}
		}
		/*
		 * @see java.text.CharacterIterator#setIndex(int)
		 */
		public char setIndex(int position) {
			if (position >= getBeginIndex() && position <= getEndIndex())
				 = position;
			else
			return current();
		}
		/*
		 * @see java.text.CharacterIterator#getBeginIndex()
		 */
		public int getBeginIndex() {
			return ;
		}
		/*
		 * @see java.text.CharacterIterator#getEndIndex()
		 */
		public int getEndIndex() {
			return ;
		}
		/*
		 * @see java.text.CharacterIterator#getIndex()
		 */
		public int getIndex() {
			return ;
		}
		/*
		 * @see java.text.CharacterIterator#clone()
		 */
		public Object clone() {
			try {
				return super.clone();
				throw new InternalError();
			}
		}
	}
	protected Run whitespace;
	protected Run delimiter;
	protected Run identifier;
	protected Run other;

The platform break iterator (word instance) used as a base.
	protected final BreakIterator fIterator;
The text we operate on.
	protected CharSequence fText;
our current position for the stateful methods.
	private int fIndex;

Creates a new break iterator.
	public CommonBreakIterator(boolean camelCase) {
		 = BreakIterator.getWordInstance();
		if (camelCase)
		else
		 = new Other();
	}
	/*
	 * @see java.text.BreakIterator#current()
	 */
	public int current() {
		return ;
	}
	/*
	 * @see java.text.BreakIterator#first()
	 */
	public int first() {
		return ;
	}
	/*
	 * @see java.text.BreakIterator#following(int)
	 */
	public int following(int offset) {
		// work around too eager IAEs in standard implementation
		if (offset == getText().getEndIndex())
			return ;
		int next = .following(offset);
		if (next == )
			return ;
		// TODO deal with complex script word boundaries
		// Math.min(offset + run.length, next) does not work
		// since BreakIterator.getWordInstance considers _ as boundaries
		// seems to work fine, however
		Run run = consumeRun(offset);
		return offset + run.length;
	}

Consumes a run of characters at the limits of which we introduce a break.

Parameters:
offset the offset to start at
Returns:
the run that was consumed
	protected Run consumeRun(int offset) {
		// assert offset < length
		char ch = .charAt(offset);
		int length = .length();
		Run run = getRun(ch);
		while (run.consume(ch) && offset < length - 1) {
			offset++;
			ch = .charAt(offset);
		}
		return run;
	}

Returns a run based on a character.

Parameters:
ch the character to test
Returns:
the correct character given ch
	protected Run getRun(char ch) {
		Run run;
			run = ;
		else if (.isValid(ch))
			run = ;
		else if (.isValid(ch))
			run = ;
		else if (.isValid(ch))
			run = ;
		else {
			Assert.isTrue(false);
			return null;
		}
		run.init();
		return run;
	}
	/*
	 * @see java.text.BreakIterator#getText()
	 */
		return .getText();
	}
	/*
	 * @see java.text.BreakIterator#isBoundary(int)
	 */
	public boolean isBoundary(int offset) {
		if (offset == getText().getBeginIndex())
			return true;
		else
			return following(offset - 1) == offset;
	}
	/*
	 * @see java.text.BreakIterator#last()
	 */
	public int last() {
		return ;
	}
	/*
	 * @see java.text.BreakIterator#next()
	 */
	public int next() {
		return ;
	}
	/*
	 * @see java.text.BreakIterator#next(int)
	 */
	public int next(int n) {
		return .next(n);
	}
	/*
	 * @see java.text.BreakIterator#preceding(int)
	 */
	public int preceding(int offset) {
		if (offset == getText().getBeginIndex())
			return ;
		if (isBoundary(offset - 1))
			return offset - 1;
		int previous = offset - 1;
		do {
			previous = .preceding(previous);
while (!isBoundary(previous));
		int last = ;
		while (previous < offset) {
			last = previous;
			previous = following(previous);
		}
		return last;
	}
	/*
	 * @see java.text.BreakIterator#previous()
	 */
	public int previous() {
		return ;
	}
	/*
	 * @see java.text.BreakIterator#setText(java.lang.String)
	 */
	public void setText(String newText) {
		setText((CharSequencenewText);
	}

Creates a break iterator given a char sequence.

Parameters:
newText the new text
	public void setText(CharSequence newText) {
		 = newText;
	}
	/*
	 * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
	 */
	public void setText(CharacterIterator newText) {
		if (newText instanceof CharSequence) {
			 = (CharSequencenewText;
else {
			throw new UnsupportedOperationException("CharacterIterator not supported"); //$NON-NLS-1$
		}
	}
New to GrepCode? Check out our FAQ X