4fd5ea37784ceb4a8b6915f8700d8d2be25ff1ae
[phpeclipse.git] / net.sourceforge.phpeclipse / 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     Klaus Hartlage - www.eclipseproject.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          * @returns Point - x is the start position, y is the end position.
28          *      Return null if it is not found.
29          * @param document the document being searched.
30          * @param offset - the position to start searching from.
31          */
32         public static Point findWord(IDocument document, int offset) {
33
34                 int start = -1;
35                 int end = -1;
36
37                 try {
38
39                         int position = offset;
40                         char character;
41
42                         while (position >= 0) {
43                                 character = document.getChar(position);
44                                 if (!Scanner.isPHPIdentifierPart(character) && (character != '$'))
45                                         break;
46                                 --position;
47                         }
48
49                         start = position;
50
51                         position = offset;
52                         int length = document.getLength();
53
54                         while (position < length) {
55                                 character = document.getChar(position);
56                                 if (!Scanner.isPHPIdentifierPart(character) && (character != '$'))
57                                         break;
58                                 ++position;
59                         }
60
61       start++;
62                         end = position;
63
64                         if (end > start)
65                                 return new Point(start, end - start);
66
67                 } catch (BadLocationException x) {
68                 }
69
70                 return null;
71         }
72 }