1 11 12 package org.eclipse.jdt.internal.ui.text.java; 13 14 import com.ibm.icu.text.BreakIterator; 15 import java.text.CharacterIterator ; 16 17 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.IRegion; 20 import org.eclipse.jface.text.ITextDoubleClickStrategy; 21 import org.eclipse.jface.text.ITextViewer; 22 import org.eclipse.jface.text.Region; 23 24 25 31 public class JavadocDoubleClickStrategy implements ITextDoubleClickStrategy { 32 33 34 42 static class DocumentCharacterIterator implements CharacterIterator { 43 44 45 private IDocument fDocument; 46 47 private int fOffset= -1; 48 49 private int fEndOffset= -1; 50 51 private int fIndex= -1; 52 53 54 public DocumentCharacterIterator() { 55 } 56 57 63 public void setDocument(IDocument document, IRegion iteratorRange) { 64 fDocument= document; 65 fOffset= iteratorRange.getOffset(); 66 fEndOffset= fOffset + iteratorRange.getLength(); 67 } 68 69 72 public char first() { 73 fIndex= fOffset; 74 return current(); 75 } 76 77 80 public char last() { 81 fIndex= fOffset < fEndOffset ? fEndOffset -1 : fEndOffset; 82 return current(); 83 } 84 85 88 public char current() { 89 if (fOffset <= fIndex && fIndex < fEndOffset) { 90 try { 91 return fDocument.getChar(fIndex); 92 } catch (BadLocationException x) { 93 } 94 } 95 return DONE; 96 } 97 98 101 public char next() { 102 ++fIndex; 103 int end= getEndIndex(); 104 if (fIndex >= end) { 105 fIndex= end; 106 return DONE; 107 } 108 return current(); 109 } 110 111 114 public char previous() { 115 if (fIndex == fOffset) 116 return DONE; 117 118 if (fIndex > fOffset) 119 -- fIndex; 120 121 return current(); 122 } 123 124 127 public char setIndex(int index) { 128 fIndex= index; 129 return current(); 130 } 131 132 135 public int getBeginIndex() { 136 return fOffset; 137 } 138 139 142 public int getEndIndex() { 143 return fEndOffset; 144 } 145 146 149 public int getIndex() { 150 return fIndex; 151 } 152 153 156 public Object clone() { 157 DocumentCharacterIterator i= new DocumentCharacterIterator(); 158 i.fDocument= fDocument; 159 i.fIndex= fIndex; 160 i.fOffset= fOffset; 161 i.fEndOffset= fEndOffset; 162 return i; 163 } 164 } 165 166 167 171 private DocumentCharacterIterator fDocIter= new DocumentCharacterIterator(); 172 173 174 177 public JavadocDoubleClickStrategy() { 178 super(); 179 } 180 181 184 public void doubleClicked(ITextViewer text) { 185 186 int position= text.getSelectedRange().x; 187 188 if (position < 0) 189 return; 190 191 IRegion word= getWordRegion(text.getDocument(), position); 192 193 if (word != null) 194 text.setSelectedRange(word.getOffset(), word.getLength()); 195 } 196 197 204 private IRegion getWordRegion(IDocument document, int position) { 205 try { 206 207 IRegion line= document.getLineInformationOfOffset(position); 208 if (position == line.getOffset() + line.getLength()) 209 return null; 210 211 fDocIter.setDocument(document, line); 212 213 BreakIterator breakIter= BreakIterator.getWordInstance(); 214 breakIter.setText(fDocIter); 215 216 int start= breakIter.preceding(position); 217 if (start == BreakIterator.DONE) 218 start= line.getOffset(); 219 220 int end= breakIter.following(position); 221 if (end == BreakIterator.DONE) 222 end= line.getOffset() + line.getLength(); 223 224 if (breakIter.isBoundary(position)) { 225 if (end - position > position- start) 226 start= position; 227 else 228 end= position; 229 } 230 231 if (start > 0 && document.getChar(start - 1) == '@' && Character.isJavaIdentifierPart(document.getChar(start)) 232 && (start == 1 || Character.isWhitespace(document.getChar(start - 2)) || document.getChar(start - 2) == '{')) { 233 start--; 235 } else if (end == position && end == start + 1 && end < line.getOffset() + line.getLength() && document.getChar(end) == '@') { 236 return getWordRegion(document, position + 1); 238 } 239 240 if (start == end) 241 return null; 242 return new Region(start, end - start); 243 244 } catch (BadLocationException x) { 245 return null; 246 } 247 } 248 } 249 | Popular Tags |