ed9b88cda369751e34914c94b4a5b16711d0686b
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / phpeditor / php / PHPWordExtractor.java
1 /**********************************************************************
2  Copyright (c) 2000, 2002 IBM Corp. 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 implementation
10  www.phpeclipse.de
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
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.swt.graphics.Point;
19
20 /**
21  * Detects PHP words in documents.
22  */
23 public class PHPWordExtractor {
24
25         /**
26          * Find the location of the word at offset in document.
27          * 
28          * @returns Point - x is the start position, y is the end position. Return
29          *          null if it is not found.
30          * @param document
31          *            the document being searched.
32          * @param offset -
33          *            the position to start searching from.
34          */
35         public static Point findWord(IDocument document, int offset) {
36
37                 int start = -1;
38                 int end = -1;
39
40                 try {
41
42                         int position = offset;
43                         char character;
44
45                         while (position >= 0) {
46                                 character = document.getChar(position);
47                                 if (!Scanner.isPHPIdentifierPart(character)
48                                                 && (character != '$'))
49                                         break;
50                                 --position;
51                         }
52
53                         start = position;
54
55                         position = offset;
56                         int length = document.getLength();
57
58                         while (position < length) {
59                                 character = document.getChar(position);
60                                 if (!Scanner.isPHPIdentifierPart(character)
61                                                 && (character != '$'))
62                                         break;
63                                 ++position;
64                         }
65
66                         start++;
67                         end = position;
68
69                         if (end > start)
70                                 return new Point(start, end - start);
71
72                 } catch (BadLocationException x) {
73                 }
74
75                 return null;
76         }
77 }