1 11 12 package org.eclipse.pde.internal.ui.editor.text; 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 23 public class PDEWordFinder { 24 25 28 public PDEWordFinder() { 29 } 31 32 38 public static IRegion findWord(IDocument document, int offset) { 39 40 int start= -2; 41 int end= -1; 42 43 try { 44 int pos= offset; 45 char c; 46 47 while (pos >= 0) { 48 c= document.getChar(pos); 49 if (!Character.isJavaIdentifierPart(c)) 50 break; 51 --pos; 52 } 53 start= pos; 54 55 pos= offset; 56 int length= document.getLength(); 57 58 while (pos < length) { 59 c= document.getChar(pos); 60 if (!Character.isJavaIdentifierPart(c)) 61 break; 62 ++pos; 63 } 64 end= pos; 65 66 } catch (BadLocationException x) { 67 } 68 69 if (start >= -1 && end > -1) { 70 if (start == offset && end == offset) 71 return new Region(offset, 0); 72 else if (start == offset) 73 return new Region(start, end - start); 74 else 75 return new Region(start + 1, end - start - 1); 76 } 77 78 return null; 79 } 80 81 } 82 | Popular Tags |