KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.text;
12
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.Region;
18
19 public class JavaWordFinder {
20     
21     public static IRegion findWord(IDocument document, int offset) {
22
23         int start= -2;
24         int end= -1;
25         
26         try {
27             int pos= offset;
28             char c;
29
30             while (pos >= 0) {
31                 c= document.getChar(pos);
32                 if (!Character.isJavaIdentifierPart(c))
33                     break;
34                 --pos;
35             }
36             start= pos;
37
38             pos= offset;
39             int length= document.getLength();
40
41             while (pos < length) {
42                 c= document.getChar(pos);
43                 if (!Character.isJavaIdentifierPart(c))
44                     break;
45                 ++pos;
46             }
47             end= pos;
48
49         } catch (BadLocationException x) {
50         }
51
52         if (start >= -1 && end > -1) {
53             if (start == offset && end == offset)
54                 return new Region(offset, 0);
55             else if (start == offset)
56                 return new Region(start, end - start);
57             else
58                 return new Region(start + 1, end - start - 1);
59         }
60
61         return null;
62     }
63 }
64
Popular Tags