KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JavaWordFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui;
12  
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IRegion;
16 import org.eclipse.jface.text.Region;
17
18 /**
19  * Contains a static helper method to search documents for the 'word' that encloses the current
20  * selection.
21  */

22 public class JavaWordFinder {
23     
24     /**
25      * Returns the IRegion containing the java identifier ("word") enclosing the specified offset or
26      * <code>null</code> if the document or offset is invalid. Checks characters before and after the
27      * offset to see if they are allowed java identifier characters until a separator character (period,
28      * space, etc) is found.
29      *
30      * @param document The document to search
31      * @param offset The offset to start looking for the word
32      * @return IRegion containing the word or <code>null</code>
33      */

34     public static IRegion findWord(IDocument document, int offset) {
35         
36         if (document == null){
37             return null;
38         }
39         
40         int start= -2;
41         int end= -1;
42         
43         
44         try {
45             
46             int pos= offset;
47             char c;
48             
49             while (pos >= 0) {
50                 c= document.getChar(pos);
51                 if (!Character.isJavaIdentifierPart(c))
52                     break;
53                 --pos;
54             }
55             
56             start= pos;
57             
58             pos= offset;
59             int length= document.getLength();
60             
61             while (pos < length) {
62                 c= document.getChar(pos);
63                 if (!Character.isJavaIdentifierPart(c))
64                     break;
65                 ++pos;
66             }
67             
68             end= pos;
69             
70         } catch (BadLocationException x) {
71         }
72         
73         if (start >= -1 && end > -1) {
74             if (start == offset && end == offset)
75                 return new Region(offset, 0);
76             else if (start == offset)
77                 return new Region(start, end - start);
78             else
79                 return new Region(start + 1, end - start - 1);
80         }
81         
82         return null;
83     }
84 }
85
Popular Tags