*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / text / XMLWordFinder.java
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.1 2004-09-02 18:28:03 jsurfer 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  * 
24  * @author Igor Malinin
25  */
26 public class XMLWordFinder {
27         public static IRegion findWord(IDocument document, int offset) {
28                 int length = document.getLength();
29
30                 try {
31                         int pos = offset;
32
33                         while (pos >= 0) {
34                                 if (!Character.isUnicodeIdentifierPart(document.getChar(pos))) {
35                                         break;
36                                 }
37                                 --pos;
38                         }
39
40                         int start = pos;
41
42                         pos = offset;
43
44                         while (pos < length) {
45                                 if (!Character.isUnicodeIdentifierPart(document.getChar(pos))) {
46                                         break;
47                                 }
48                                 ++pos;
49                         }
50
51                         int end = pos;
52
53                         if (start == offset) {
54                                 return new Region(start, end - start);
55                         }
56
57                         return new Region(start + 1, end - start - 1);
58                 } catch (BadLocationException x) {
59                         return null;
60                 }
61         }
62 }