01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.text;
11:
12: import org.eclipse.jface.text.BadLocationException;
13: import org.eclipse.jface.text.IDocument;
14: import org.eclipse.jface.text.IRegion;
15: import org.eclipse.jface.text.Region;
16:
17: public class JavaWordFinder {
18:
19: public static IRegion findWord(IDocument document, int offset) {
20:
21: int start = -2;
22: int end = -1;
23:
24: try {
25: int pos = offset;
26: char c;
27:
28: while (pos >= 0) {
29: c = document.getChar(pos);
30: if (!Character.isJavaIdentifierPart(c))
31: break;
32: --pos;
33: }
34: start = pos;
35:
36: pos = offset;
37: int length = document.getLength();
38:
39: while (pos < length) {
40: c = document.getChar(pos);
41: if (!Character.isJavaIdentifierPart(c))
42: break;
43: ++pos;
44: }
45: end = pos;
46:
47: } catch (BadLocationException x) {
48: }
49:
50: if (start >= -1 && end > -1) {
51: if (start == offset && end == offset)
52: return new Region(offset, 0);
53: else if (start == offset)
54: return new Region(start, end - start);
55: else
56: return new Region(start + 1, end - start - 1);
57: }
58:
59: return null;
60: }
61: }
|