improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / HTMLWordExtractor.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 HTML words in documents.
22  */
23 public class HTMLWordExtractor {
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))
45           break;
46         --position;
47       }
48       if ((position > 0) && (character == '<')) {
49         --position;
50       }
51       if ((position > 1) && (character == '/')) {
52         character = document.getChar(position - 1);
53         if (character == '<') {
54           --position;
55           --position;
56         }
57       }
58       if (position==offset) {
59         return null; 
60       }
61
62       start = position;
63
64       position = offset;
65       int length = document.getLength();
66       character = ' ';
67
68       while (position < length) {
69         character = document.getChar(position);
70         if (!Scanner.isPHPIdentifierPart(character))
71           break;
72         ++position;
73       }
74       if ((position < length) && (character == '>')) {
75         ++position;
76       }
77       start++;
78       end = position;
79
80       if (end > start)
81         return new Point(start, end - start);
82
83     } catch (BadLocationException x) {
84     }
85
86     return null;
87   }
88 }