37944fbe9989b44289a5717fa242cd946536eb0d
[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 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
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 public class JavaWordFinder {
21
22         public static IRegion findWord(IDocument document, int offset) {
23
24                 int start = -1;
25                 int end = -1;
26
27                 try {
28
29                         int pos = offset;
30                         char c = ' ';
31
32                         while (pos >= 0) {
33                                 c = document.getChar(pos);
34                                 if (c == '$') {
35                                         --pos;
36                                         break;
37                                 }
38                                 if (!Scanner.isPHPIdentifierPart(c))
39                                         break;
40
41                                 --pos;
42                         }
43
44                         start = pos;
45
46                         pos = offset;
47                         int length = document.getLength();
48
49                         while (pos < length) {
50                                 c = document.getChar(pos);
51                                 if (!Scanner.isPHPIdentifierPart(c))
52                                         break;
53                                 ++pos;
54                         }
55
56                         end = pos;
57
58                 } catch (BadLocationException x) {
59                 }
60
61                 if (start > -1 && end > -1) {
62                         if (start == offset && end == offset)
63                                 return new Region(offset, 0);
64                         else if (start == offset)
65                                 return new Region(start, end - start);
66                         else
67                                 return new Region(start + 1, end - start - 1);
68                 }
69
70                 return null;
71         }
72 }