Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 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.jface.text.BadLocationException;
 import  org.eclipse.jface.text.IDocument;
 import  org.eclipse.jface.text.IRegion;
 import  org.eclipse.jface.text.ITypedRegion;
 import  org.eclipse.jface.text.Region;

Author(s):
Sven Efftinge - Initial contribution and API
 
 public class DocumentUtil {

searches backwards for the given string within the same partition type

Returns:
the region of the match or null if no match were found
 
 	public IRegion searchBackwardsInSamePartition(String toFind, IDocument documentint endOffsetthrows BadLocationException {
 		return searchBackwardsInSamePartition(toFinddocument.get(), documentendOffset);
 	}

searches backwards for the given string within the same partition type

Returns:
the region of the match or null if no match were found
Since:
2.4
 
 	public IRegion searchBackwardsInSamePartition(String toFindString documentText, IDocument documentint endOffsetthrows BadLocationException {
 		if (endOffset < 0) {
 			return null;
 		}
 		int length = toFind.length();
 		String text = preProcessSearchString(documentText);
 		ITypedRegion partition = document.getPartition(endOffset);
 		int indexOf = text.lastIndexOf(toFindendOffset - length);
 		while (indexOf >= 0) {
 			ITypedRegion partition2 = document.getPartition(indexOf);
 			if (partition2.getType().equals(partition.getType())) {
 				return new Region(indexOflength);
 			}
 			indexOf = text.lastIndexOf(toFindpartition2.getOffset() - length);
 		}
 		String trimmed = toFind.trim();
 		if (trimmed.length() > 0 && trimmed.length() != length) {
 			return searchBackwardsInSamePartition(trimmeddocumentTextdocumentendOffset);
 		}
 		return null;
 	}
 	
 	protected String preProcessSearchString(String string) {
 		return string;
 	}

searches for the given string within the same partition type

Returns:
the region of the match or null if no match were found
 
 	public IRegion searchInSamePartition(String toFind, IDocument documentint startOffsetthrows BadLocationException {
 		return searchInSamePartition(toFinddocument.get(), documentstartOffset);
 	}

searches for the given string within the same partition type

Returns:
the region of the match or null if no match were found
Since:
2.4
 
 	public IRegion searchInSamePartition(String toFindString documentText, IDocument documentint startOffset)
 			throws BadLocationException {
 		if (startOffset >= document.getLength()) {
 			return null;
 		}
 		String text = preProcessSearchString(documentText);
 		ITypedRegion partition = document.getPartition(startOffset);
 		
 		int indexOf = text.indexOf(toFindgetOffset(toFindstartOffset));
 		while (indexOf >= 0 && indexOf < document.getLength()) {
 			ITypedRegion partition2 = document.getPartition(indexOf);
 			if (partition2.getType().equals(partition.getType())) {
 				return new Region(indexOftoFind.length());
 			}
 			indexOf = text.indexOf(toFindpartition2.getOffset() + partition2.getLength());
 		}
 		String trimmed = toFind.trim();
 		if (trimmed.length() > 0 && trimmed.length() != toFind.length()) {
 			return searchInSamePartition(trimmeddocumentTextdocumentstartOffset);
 		}
 		return null;
 	}
 
 	private int getOffset(String toFindint startOffset) {
 		int whitespacesCount = getWhitespacesCount(toFind);
 		return Math.max(startOffset - whitespacesCount, 0);
	}
	private int getWhitespacesCount(String toFind) {
		int whitespacesCount = 0;
		while (toFind.length() > whitespacesCount && Character.isWhitespace(toFind.charAt(whitespacesCount))) {
			whitespacesCount++;
		}
		return whitespacesCount;
	}
	public boolean isSameLine(IDocument docint offsetint offset2throws BadLocationException {
		return doc.getLineOfOffset(offset)==doc.getLineOfOffset(offset2);
	}
	public int findNextOffSetInPartition(IDocument docint partitionOffSetint minIndexthrows BadLocationException {
		ITypedRegion partition = doc.getPartition(partitionOffSet);
		ITypedRegion partition2 = doc.getPartition(minIndex);
		if (partition.getType().equals(partition2.getType()) || partition2.getLength() == 0) {
			return minIndex;
else {
			return findNextOffSetInPartition(docpartitionOffSetminIndex + partition2.getLength());
		}
	}
New to GrepCode? Check out our FAQ X