A massive organize imports and formatting of the sources using default Eclipse code...
[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          * 
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                                         break;
49                                 --position;
50                         }
51                         if ((position > 0) && (character == '<')) {
52                                 --position;
53                         }
54                         if ((position > 1) && (character == '/')) {
55                                 character = document.getChar(position - 1);
56                                 if (character == '<') {
57                                         --position;
58                                         --position;
59                                 }
60                         }
61                         if (position == offset) {
62                                 return null;
63                         }
64
65                         start = position;
66
67                         position = offset;
68                         int length = document.getLength();
69                         character = ' ';
70
71                         while (position < length) {
72                                 character = document.getChar(position);
73                                 if (!Scanner.isPHPIdentifierPart(character))
74                                         break;
75                                 ++position;
76                         }
77                         if ((position < length) && (character == '>')) {
78                                 ++position;
79                         }
80                         start++;
81                         end = position;
82
83                         if (end > start)
84                                 return new Point(start, end - start);
85
86                 } catch (BadLocationException x) {
87                 }
88
89                 return null;
90         }
91 }