01: /*******************************************************************************
02: * Copyright (c) 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.pde.internal.ui.editor.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: /**
18: * PDEWordFinder
19: *
20: */
21: public class PDEWordFinder {
22:
23: /**
24: *
25: */
26: public PDEWordFinder() {
27: // NO-OP
28: }
29:
30: /**
31: * Copied from <code>org.eclipse.jdt.internal.ui.text.JavaWordFinder</code>
32: * @param document
33: * @param offset
34: * @return
35: */
36: public static IRegion findWord(IDocument document, int offset) {
37:
38: int start = -2;
39: int end = -1;
40:
41: try {
42: int pos = offset;
43: char c;
44:
45: while (pos >= 0) {
46: c = document.getChar(pos);
47: if (!Character.isJavaIdentifierPart(c))
48: break;
49: --pos;
50: }
51: start = pos;
52:
53: pos = offset;
54: int length = document.getLength();
55:
56: while (pos < length) {
57: c = document.getChar(pos);
58: if (!Character.isJavaIdentifierPart(c))
59: break;
60: ++pos;
61: }
62: end = pos;
63:
64: } catch (BadLocationException x) {
65: }
66:
67: if (start >= -1 && end > -1) {
68: if (start == offset && end == offset)
69: return new Region(offset, 0);
70: else if (start == offset)
71: return new Region(start, end - start);
72: else
73: return new Region(start + 1, end - start - 1);
74: }
75:
76: return null;
77: }
78:
79: }
|