1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 import org.eclipse.jface.text.BadLocationException; 14 import org.eclipse.jface.text.IDocument; 15 import org.eclipse.jface.text.IRegion; 16 import org.eclipse.jface.text.Region; 17 18 22 public class JavaWordFinder { 23 24 34 public static IRegion findWord(IDocument document, int offset) { 35 36 if (document == null){ 37 return null; 38 } 39 40 int start= -2; 41 int end= -1; 42 43 44 try { 45 46 int pos= offset; 47 char c; 48 49 while (pos >= 0) { 50 c= document.getChar(pos); 51 if (!Character.isJavaIdentifierPart(c)) 52 break; 53 --pos; 54 } 55 56 start= pos; 57 58 pos= offset; 59 int length= document.getLength(); 60 61 while (pos < length) { 62 c= document.getChar(pos); 63 if (!Character.isJavaIdentifierPart(c)) 64 break; 65 ++pos; 66 } 67 68 end= pos; 69 70 } catch (BadLocationException x) { 71 } 72 73 if (start >= -1 && end > -1) { 74 if (start == offset && end == offset) 75 return new Region(offset, 0); 76 else if (start == offset) 77 return new Region(start, end - start); 78 else 79 return new Region(start + 1, end - start - 1); 80 } 81 82 return null; 83 } 84 } 85 | Popular Tags |