1 /* 2 * Copyright (c) 2004 Widespace, OU 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 7 * 8 * Contributors: 9 * Igor Malinin - initial implementation 10 * 11 * $Id: XMLWordFinder.java,v 1.2 2006-10-21 23:14:13 pombredanne Exp $ 12 */ 13 package net.sourceforge.phpeclipse.xml.ui.internal.text; 14 15 import org.eclipse.jface.text.BadLocationException; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.IRegion; 18 import org.eclipse.jface.text.Region; 19 20 /** 21 * 22 * 23 * @author Igor Malinin 24 */ 25 public class XMLWordFinder { findWord(IDocument document, int offset)26 public static IRegion findWord(IDocument document, int offset) { 27 int length = document.getLength(); 28 29 try { 30 int pos = offset; 31 32 while (pos >= 0) { 33 if (!Character.isUnicodeIdentifierPart(document.getChar(pos))) { 34 break; 35 } 36 --pos; 37 } 38 39 int start = pos; 40 41 pos = offset; 42 43 while (pos < length) { 44 if (!Character.isUnicodeIdentifierPart(document.getChar(pos))) { 45 break; 46 } 47 ++pos; 48 } 49 50 int end = pos; 51 52 if (start == offset) { 53 return new Region(start, end - start); 54 } 55 56 return new Region(start + 1, end - start - 1); 57 } catch (BadLocationException x) { 58 return null; 59 } 60 } 61 } 62