1 11 package org.eclipse.jface.text.source; 12 13 import java.util.Iterator ; 14 import java.util.LinkedList ; 15 import java.util.List ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.Point; 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.jface.text.DefaultInformationControl; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.jface.text.IInformationControl; 24 import org.eclipse.jface.text.IInformationControlCreator; 25 import org.eclipse.jface.text.information.IInformationProviderExtension2; 26 27 28 34 public class LineChangeHover implements IAnnotationHover, IAnnotationHoverExtension, IInformationProviderExtension2 { 35 36 39 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { 40 return null; 41 } 42 43 50 protected String formatSource(String content) { 51 if (content != null) { 52 StringBuffer sb= new StringBuffer (content); 53 final String tabReplacement= getTabReplacement(); 54 for (int pos= 0; pos < sb.length(); pos++) { 55 if (sb.charAt(pos) == '\t') 56 sb.replace(pos, pos + 1, tabReplacement); 57 } 58 return sb.toString(); 59 } 60 return content; 61 } 62 63 71 protected String getTabReplacement() { 72 return "\t"; } 74 75 87 private String computeContent(ISourceViewer viewer, int first, int last, int maxLines) { 88 ILineDiffer differ= getDiffer(viewer); 89 if (differ == null) 90 return null; 91 92 final List lines= new LinkedList (); 93 for (int l= first; l <= last; l++) { 94 ILineDiffInfo info= differ.getLineInfo(l); 95 if (info != null) 96 lines.add(info); 97 } 98 99 return decorateText(lines, maxLines); 100 } 101 102 114 protected String decorateText(List diffInfos, int maxLines) { 115 119 String text= ""; int added= 0; 121 for (Iterator it= diffInfos.iterator(); it.hasNext();) { 122 ILineDiffInfo info= (ILineDiffInfo)it.next(); 123 String [] original= info.getOriginalText(); 124 int type= info.getChangeType(); 125 int i= 0; 126 if (type == ILineDiffInfo.ADDED) 127 added++; 128 else if (type == ILineDiffInfo.CHANGED) { 129 text += "> " + (original.length > 0 ? original[i++] : ""); maxLines--; 131 } else if (type == ILineDiffInfo.UNCHANGED) { 132 maxLines++; 133 } 134 if (maxLines == 0) 135 return trimTrailing(text); 136 for (; i < original.length; i++) { 137 text += "- " + original[i]; added--; 139 if (--maxLines == 0) 140 return trimTrailing(text); 141 } 142 } 143 text= text.trim(); 144 if (text.length() == 0 && added-- > 0 && maxLines-- > 0) 145 text += "+ "; while (added-- > 0 && maxLines-- > 0) 147 text += "\n+ "; return text; 149 } 150 151 157 private String trimTrailing(String text) { 158 int pos= text.length() - 1; 159 while (pos >= 0 && Character.isWhitespace(text.charAt(pos))) { 160 pos--; 161 } 162 return text.substring(0, pos + 1); 163 } 164 165 170 private ILineDiffer getDiffer(ISourceViewer viewer) { 171 IAnnotationModel model= viewer.getAnnotationModel(); 172 173 if (model == null) 174 return null; 175 176 if (model instanceof IAnnotationModelExtension) { 177 IAnnotationModel diffModel= ((IAnnotationModelExtension)model).getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID); 178 if (diffModel != null) 179 model= diffModel; 180 } 181 if (model instanceof ILineDiffer) 182 return (ILineDiffer)model; 183 return null; 184 } 185 186 196 protected Point computeLineRange(ISourceViewer viewer, int line, int min, int max) { 197 209 210 ILineDiffer differ= getDiffer(viewer); 211 if (differ == null) 212 return new Point(-1, -1); 213 214 216 int l= line; 217 ILineDiffInfo info= differ.getLineInfo(l); 218 while (l >= min && info != null && (info.getChangeType() == ILineDiffInfo.CHANGED || info.getChangeType() == ILineDiffInfo.ADDED)) { 220 info= differ.getLineInfo(--l); 221 } 222 223 int first= Math.min(l + 1, line); 224 225 227 l= line; 228 info= differ.getLineInfo(l); 229 while (l <= max && info != null && (info.getChangeType() == ILineDiffInfo.CHANGED || info.getChangeType() == ILineDiffInfo.ADDED)) { 231 info= differ.getLineInfo(++l); 232 } 233 234 int last= Math.max(l - 1, line); 235 236 return new Point(first, last); 237 } 238 239 242 public Object getHoverInfo(ISourceViewer sourceViewer, ILineRange lineRange, int visibleLines) { 243 int first= adaptFirstLine(sourceViewer, lineRange.getStartLine()); 244 int last= adaptLastLine(sourceViewer, lineRange.getStartLine() + lineRange.getNumberOfLines() - 1); 245 String content= computeContent(sourceViewer, first, last, visibleLines); 246 return formatSource(content); 247 } 248 249 258 private int adaptFirstLine(ISourceViewer viewer, int startLine) { 259 ILineDiffer differ= getDiffer(viewer); 260 if (differ != null && startLine > 0) { 261 int l= startLine - 1; 262 ILineDiffInfo info= differ.getLineInfo(l); 263 if (info != null && info.getChangeType() == ILineDiffInfo.UNCHANGED && info.getRemovedLinesBelow() > 0) 264 return l; 265 } 266 return startLine; 267 } 268 269 278 private int adaptLastLine(ISourceViewer viewer, int lastLine) { 279 ILineDiffer differ= getDiffer(viewer); 280 if (differ != null && lastLine > 0) { 281 ILineDiffInfo info= differ.getLineInfo(lastLine); 282 if (info != null && info.getChangeType() == ILineDiffInfo.UNCHANGED) 283 return lastLine - 1; 284 } 285 return lastLine; 286 } 287 288 291 public ILineRange getHoverLineRange(ISourceViewer viewer, int lineNumber) { 292 IDocument document= viewer.getDocument(); 293 if (document != null) { 294 Point range= computeLineRange(viewer, lineNumber, 0, Math.max(0, document.getNumberOfLines() - 1)); 295 if (range.x != -1 && range.y != -1) 296 return new LineRange(range.x, range.y - range.x + 1); 297 } 298 return null; 299 } 300 301 304 public boolean canHandleMouseCursor() { 305 return false; 306 } 307 308 311 public IInformationControlCreator getHoverControlCreator() { 312 return null; 313 } 314 315 319 public IInformationControlCreator getInformationPresenterControlCreator() { 320 return new IInformationControlCreator() { 321 public IInformationControl createInformationControl(Shell parent) { 322 int shellStyle= SWT.RESIZE | SWT.TOOL; 323 int style= SWT.V_SCROLL | SWT.H_SCROLL; 324 return new DefaultInformationControl(parent, shellStyle, style, null); 325 } 326 }; 327 } 328 } 329 | Popular Tags |