]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/text/JavaWordFinder.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / text / JavaWordFinder.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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 com.ibm.icu.text.UTF16;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.Region;
20
21 public class JavaWordFinder {
22
23         public static IRegion findWord(IDocument document, int offset) {
24
25                 int start= -2;
26                 int end= -1;
27
28                 try {
29                         int pos= offset;
30                         char c;
31
32                         while (pos >= 0) {
33                                 c= document.getChar(pos);
34                                 if (!Character.isJavaIdentifierPart(c)) {
35                                         // Check for surrogates
36                                         if (UTF16.isSurrogate(c)) {
37                                                 /*
38                                                  * XXX: Here we should create the code point and test whether
39                                                  * it is a Java identifier part. Currently this is not possible
40                                                  * because java.lang.Character in 1.4 does not support surrogates
41                                                  * and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
42                                                  * is not correctly implemented.
43                                                  */
44                                         } else {
45                                                 break;
46                                         }
47                                 }
48                                 --pos;
49                         }
50                         start= pos;
51
52                         pos= offset;
53                         int length= document.getLength();
54
55                         while (pos < length) {
56                                 c= document.getChar(pos);
57                                 if (!Character.isJavaIdentifierPart(c)) {
58                                         if (UTF16.isSurrogate(c)) {
59                                                 /*
60                                                  * XXX: Here we should create the code point and test whether
61                                                  * it is a Java identifier part. Currently this is not possible
62                                                  * because java.lang.Character in 1.4 does not support surrogates
63                                                  * and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
64                                                  * is not correctly implemented.
65                                                  */
66                                         } else {
67                                                 break;
68                                         }
69
70                                 }
71                                 ++pos;
72                         }
73                         end= pos;
74
75                 } catch (BadLocationException x) {
76                 }
77
78                 if (start >= -1 && end > -1) {
79                         if (start == offset && end == offset)
80                                 return new Region(offset, 0);
81                         else if (start == offset)
82                                 return new Region(start, end - start);
83                         else
84                                 return new Region(start + 1, end - start - 1);
85                 }
86
87                 return null;
88         }
89 }