63934fadee5b2159d8c93729480f98de15cacee6
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / JavaWordFinder.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13  
14 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.Region;
20
21 public class JavaWordFinder {
22         
23         public static IRegion findWord(IDocument document, int offset) {
24                 
25                 int start= -1;
26                 int end= -1;
27                 
28                 
29                 try {
30                         
31                         int pos= offset;
32                         char c;
33                         
34                         while (pos >= 0) {
35                                 c= document.getChar(pos);
36                                 if (!Scanner.isPHPIdentifierPart(c))
37                                         break;
38                                 --pos;
39                         }
40                         
41                         start= pos;
42                         
43                         pos= offset;
44                         int length= document.getLength();
45                         
46                         while (pos < length) {
47                                 c= document.getChar(pos);
48                                 if (!Scanner.isPHPIdentifierPart(c))
49                                         break;
50                                 ++pos;
51                         }
52                         
53                         end= pos;
54                         
55                 } catch (BadLocationException x) {
56                 }
57                 
58                 if (start > -1 && end > -1) {
59                         if (start == offset && end == offset)
60                                 return new Region(offset, 0);
61                         else if (start == offset)
62                                 return new Region(start, end - start);
63                         else
64                                 return new Region(start + 1, end - start - 1);
65                 }
66                 
67                 return null;
68         }
69 }