1 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 |