KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > PDEWordFinder


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.pde.internal.ui.editor.text;
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 /**
20  * PDEWordFinder
21  *
22  */

23 public class PDEWordFinder {
24
25     /**
26      *
27      */

28     public PDEWordFinder() {
29         // NO-OP
30
}
31
32     /**
33      * Copied from <code>org.eclipse.jdt.internal.ui.text.JavaWordFinder</code>
34      * @param document
35      * @param offset
36      * @return
37      */

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