129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.source.LineChangeHover;
|
|
14
|
131
|
15 import dwtx.jface.text.source.ISharedTextColors; // packageimport
|
|
16 import dwtx.jface.text.source.ILineRange; // packageimport
|
|
17 import dwtx.jface.text.source.IAnnotationPresentation; // packageimport
|
|
18 import dwtx.jface.text.source.IVerticalRulerInfoExtension; // packageimport
|
|
19 import dwtx.jface.text.source.ICharacterPairMatcher; // packageimport
|
|
20 import dwtx.jface.text.source.TextInvocationContext; // packageimport
|
|
21 import dwtx.jface.text.source.IChangeRulerColumn; // packageimport
|
|
22 import dwtx.jface.text.source.IAnnotationMap; // packageimport
|
|
23 import dwtx.jface.text.source.IAnnotationModelListenerExtension; // packageimport
|
|
24 import dwtx.jface.text.source.ISourceViewerExtension2; // packageimport
|
|
25 import dwtx.jface.text.source.IAnnotationHover; // packageimport
|
|
26 import dwtx.jface.text.source.ContentAssistantFacade; // packageimport
|
|
27 import dwtx.jface.text.source.IAnnotationAccess; // packageimport
|
|
28 import dwtx.jface.text.source.IVerticalRulerExtension; // packageimport
|
|
29 import dwtx.jface.text.source.IVerticalRulerColumn; // packageimport
|
|
30 import dwtx.jface.text.source.LineNumberRulerColumn; // packageimport
|
|
31 import dwtx.jface.text.source.MatchingCharacterPainter; // packageimport
|
|
32 import dwtx.jface.text.source.IAnnotationModelExtension; // packageimport
|
|
33 import dwtx.jface.text.source.ILineDifferExtension; // packageimport
|
|
34 import dwtx.jface.text.source.DefaultCharacterPairMatcher; // packageimport
|
|
35 import dwtx.jface.text.source.LineNumberChangeRulerColumn; // packageimport
|
|
36 import dwtx.jface.text.source.IAnnotationAccessExtension; // packageimport
|
|
37 import dwtx.jface.text.source.ISourceViewer; // packageimport
|
|
38 import dwtx.jface.text.source.AnnotationModel; // packageimport
|
|
39 import dwtx.jface.text.source.ILineDifferExtension2; // packageimport
|
|
40 import dwtx.jface.text.source.IAnnotationModelListener; // packageimport
|
|
41 import dwtx.jface.text.source.IVerticalRuler; // packageimport
|
|
42 import dwtx.jface.text.source.DefaultAnnotationHover; // packageimport
|
|
43 import dwtx.jface.text.source.SourceViewer; // packageimport
|
|
44 import dwtx.jface.text.source.SourceViewerConfiguration; // packageimport
|
|
45 import dwtx.jface.text.source.AnnotationBarHoverManager; // packageimport
|
|
46 import dwtx.jface.text.source.CompositeRuler; // packageimport
|
|
47 import dwtx.jface.text.source.ImageUtilities; // packageimport
|
|
48 import dwtx.jface.text.source.VisualAnnotationModel; // packageimport
|
|
49 import dwtx.jface.text.source.IAnnotationModel; // packageimport
|
|
50 import dwtx.jface.text.source.ISourceViewerExtension3; // packageimport
|
|
51 import dwtx.jface.text.source.ILineDiffInfo; // packageimport
|
|
52 import dwtx.jface.text.source.VerticalRulerEvent; // packageimport
|
|
53 import dwtx.jface.text.source.ChangeRulerColumn; // packageimport
|
|
54 import dwtx.jface.text.source.ILineDiffer; // packageimport
|
|
55 import dwtx.jface.text.source.AnnotationModelEvent; // packageimport
|
|
56 import dwtx.jface.text.source.AnnotationColumn; // packageimport
|
|
57 import dwtx.jface.text.source.AnnotationRulerColumn; // packageimport
|
|
58 import dwtx.jface.text.source.IAnnotationHoverExtension; // packageimport
|
|
59 import dwtx.jface.text.source.AbstractRulerColumn; // packageimport
|
|
60 import dwtx.jface.text.source.ISourceViewerExtension; // packageimport
|
|
61 import dwtx.jface.text.source.AnnotationMap; // packageimport
|
|
62 import dwtx.jface.text.source.IVerticalRulerInfo; // packageimport
|
|
63 import dwtx.jface.text.source.IAnnotationModelExtension2; // packageimport
|
|
64 import dwtx.jface.text.source.LineRange; // packageimport
|
|
65 import dwtx.jface.text.source.IAnnotationAccessExtension2; // packageimport
|
|
66 import dwtx.jface.text.source.VerticalRuler; // packageimport
|
|
67 import dwtx.jface.text.source.JFaceTextMessages; // packageimport
|
|
68 import dwtx.jface.text.source.IOverviewRuler; // packageimport
|
|
69 import dwtx.jface.text.source.Annotation; // packageimport
|
|
70 import dwtx.jface.text.source.IVerticalRulerListener; // packageimport
|
|
71 import dwtx.jface.text.source.ISourceViewerExtension4; // packageimport
|
|
72 import dwtx.jface.text.source.AnnotationPainter; // packageimport
|
|
73 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport
|
|
74 import dwtx.jface.text.source.OverviewRuler; // packageimport
|
|
75 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport
|
|
76
|
|
77
|
129
|
78 import dwt.dwthelper.utils;
|
|
79
|
|
80 import java.util.Iterator;
|
|
81 import java.util.LinkedList;
|
|
82 import java.util.List;
|
|
83
|
|
84 import dwt.graphics.Point;
|
|
85 import dwt.widgets.Shell;
|
|
86 import dwtx.jface.action.ToolBarManager;
|
|
87 import dwtx.jface.text.DefaultInformationControl;
|
|
88 import dwtx.jface.text.IDocument;
|
|
89 import dwtx.jface.text.IInformationControl;
|
|
90 import dwtx.jface.text.IInformationControlCreator;
|
|
91 import dwtx.jface.text.information.IInformationProviderExtension2;
|
|
92
|
|
93
|
|
94 /**
|
|
95 * A hover for line oriented diffs. It determines the text to show as hover for a certain line in the
|
|
96 * document.
|
|
97 *
|
|
98 * @since 3.0
|
|
99 */
|
|
100 public class LineChangeHover : IAnnotationHover, IAnnotationHoverExtension, IInformationProviderExtension2 {
|
|
101
|
|
102 /*
|
|
103 * @see dwtx.jface.text.source.IAnnotationHover#getHoverInfo(dwtx.jface.text.source.ISourceViewer, int)
|
|
104 */
|
|
105 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
|
|
106 return null;
|
|
107 }
|
|
108
|
|
109 /**
|
|
110 * Formats the source w/ syntax coloring etc. This implementation replaces tabs with spaces.
|
|
111 * May be overridden by subclasses.
|
|
112 *
|
|
113 * @param content the hover content
|
|
114 * @return <code>content</code> reformatted
|
|
115 */
|
|
116 protected String formatSource(String content) {
|
|
117 if (content !is null) {
|
|
118 StringBuffer sb= new StringBuffer(content);
|
|
119 final String tabReplacement= getTabReplacement();
|
|
120 for (int pos= 0; pos < sb.length(); pos++) {
|
|
121 if (sb.charAt(pos) is '\t')
|
|
122 sb.replace(pos, pos + 1, tabReplacement);
|
|
123 }
|
|
124 return sb.toString();
|
|
125 }
|
|
126 return content;
|
|
127 }
|
|
128
|
|
129 /**
|
|
130 * Returns a replacement for the tab character. The default implementation
|
|
131 * returns a tabulator character, but subclasses may override to specify a
|
|
132 * number of spaces.
|
|
133 *
|
|
134 * @return a whitespace String that will be substituted for the tabulator
|
|
135 * character
|
|
136 */
|
|
137 protected String getTabReplacement() {
|
|
138 return "\t"; //$NON-NLS-1$
|
|
139 }
|
|
140
|
|
141 /**
|
|
142 * Computes the content of the hover for the document contained in <code>viewer</code> on
|
|
143 * line <code>line</code>.
|
|
144 *
|
|
145 * @param viewer the connected viewer
|
|
146 * @param first the first line in <code>viewer</code>'s document to consider
|
|
147 * @param last the last line in <code>viewer</code>'s document to consider
|
|
148 * @param maxLines the max number of lines
|
|
149 * @return The hover content corresponding to the parameters
|
|
150 * @see #getHoverInfo(ISourceViewer, int)
|
|
151 * @see #getHoverInfo(ISourceViewer, ILineRange, int)
|
|
152 */
|
|
153 private String computeContent(ISourceViewer viewer, int first, int last, int maxLines) {
|
|
154 ILineDiffer differ= getDiffer(viewer);
|
|
155 if (differ is null)
|
|
156 return null;
|
|
157
|
|
158 final List lines= new LinkedList();
|
|
159 for (int l= first; l <= last; l++) {
|
|
160 ILineDiffInfo info= differ.getLineInfo(l);
|
|
161 if (info !is null)
|
|
162 lines.add(info);
|
|
163 }
|
|
164
|
|
165 return decorateText(lines, maxLines);
|
|
166 }
|
|
167
|
|
168 /**
|
|
169 * Takes a list of <code>ILineDiffInfo</code>s and computes a hover of at most <code>maxLines</code>.
|
|
170 * Added lines are prefixed with a <code>'+'</code>, changed lines with <code>'>'</code> and
|
|
171 * deleted lines with <code>'-'</code>.
|
|
172 * <p>Deleted and added lines can even each other out, so that a number of deleted lines get
|
|
173 * displayed where - in the current document - the added lines are.
|
|
174 *
|
|
175 * @param diffInfos a <code>List</code> of <code>ILineDiffInfo</code>
|
|
176 * @param maxLines the maximum number of lines. Note that adding up all annotations might give
|
|
177 * more than that due to deleted lines.
|
|
178 * @return a <code>String</code> suitable for hover display
|
|
179 */
|
|
180 protected String decorateText(List diffInfos, int maxLines) {
|
|
181 /* maxLines controls the size of the hover (not more than what fits into the display are of
|
|
182 * the viewer).
|
|
183 * added controls how many lines are added - added lines are
|
|
184 */
|
|
185 String text= ""; //$NON-NLS-1$
|
|
186 int added= 0;
|
|
187 for (Iterator it= diffInfos.iterator(); it.hasNext();) {
|
|
188 ILineDiffInfo info= (ILineDiffInfo)it.next();
|
|
189 String[] original= info.getOriginalText();
|
|
190 int type= info.getChangeType();
|
|
191 int i= 0;
|
|
192 if (type is ILineDiffInfo.ADDED)
|
|
193 added++;
|
|
194 else if (type is ILineDiffInfo.CHANGED) {
|
|
195 text += "> " + (original.length > 0 ? original[i++] : ""); //$NON-NLS-1$ //$NON-NLS-2$
|
|
196 maxLines--;
|
|
197 } else if (type is ILineDiffInfo.UNCHANGED) {
|
|
198 maxLines++;
|
|
199 }
|
|
200 if (maxLines is 0)
|
|
201 return trimTrailing(text);
|
|
202 for (; i < original.length; i++) {
|
|
203 text += "- " + original[i]; //$NON-NLS-1$
|
|
204 added--;
|
|
205 if (--maxLines is 0)
|
|
206 return trimTrailing(text);
|
|
207 }
|
|
208 }
|
|
209 text= text.trim();
|
|
210 if (text.length() is 0 && added-- > 0 && maxLines-- > 0)
|
|
211 text += "+ "; //$NON-NLS-1$
|
|
212 while (added-- > 0 && maxLines-- > 0)
|
|
213 text += "\n+ "; //$NON-NLS-1$
|
|
214 return text;
|
|
215 }
|
|
216
|
|
217 /**
|
|
218 * Trims trailing spaces
|
|
219 *
|
|
220 * @param text a <code>String</code>
|
|
221 * @return a copy of <code>text</code> with trailing spaces removed
|
|
222 */
|
|
223 private String trimTrailing(String text) {
|
|
224 int pos= text.length() - 1;
|
|
225 while (pos >= 0 && Character.isWhitespace(text.charAt(pos))) {
|
|
226 pos--;
|
|
227 }
|
|
228 return text.substring(0, pos + 1);
|
|
229 }
|
|
230
|
|
231 /**
|
|
232 * Extracts the line differ - if any - from the viewer's document's annotation model.
|
|
233 * @param viewer the viewer
|
|
234 * @return a line differ for the document displayed in viewer, or <code>null</code>.
|
|
235 */
|
|
236 private ILineDiffer getDiffer(ISourceViewer viewer) {
|
|
237 IAnnotationModel model= viewer.getAnnotationModel();
|
|
238
|
|
239 if (model is null)
|
|
240 return null;
|
|
241
|
|
242 if (model instanceof IAnnotationModelExtension) {
|
|
243 IAnnotationModel diffModel= ((IAnnotationModelExtension)model).getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);
|
|
244 if (diffModel !is null)
|
|
245 model= diffModel;
|
|
246 }
|
|
247 if (model instanceof ILineDiffer) {
|
|
248 if (model instanceof ILineDifferExtension2 && ((ILineDifferExtension2)model).isSuspended())
|
|
249 return null;
|
|
250 return (ILineDiffer)model;
|
|
251 }
|
|
252 return null;
|
|
253 }
|
|
254
|
|
255 /**
|
|
256 * Computes the block of lines which form a contiguous block of changes covering <code>line</code>.
|
|
257 *
|
|
258 * @param viewer the source viewer showing
|
|
259 * @param line the line which a hover is displayed for
|
|
260 * @param min the first line in <code>viewer</code>'s document to consider
|
|
261 * @param max the last line in <code>viewer</code>'s document to consider
|
|
262 * @return the selection in the document displayed in <code>viewer</code> containing <code>line</code>
|
|
263 * that is covered by the hover information returned by the receiver.
|
|
264 */
|
|
265 protected Point computeLineRange(ISourceViewer viewer, int line, int min, int max) {
|
|
266 /* Algorithm:
|
|
267 * All lines that have changes to themselves (added, changed) are taken that form a
|
|
268 * contiguous block of lines that includes <code>line</code>.
|
|
269 *
|
|
270 * If <code>line</code> is itself unchanged, if there is a deleted line either above or
|
|
271 * below, or both, the lines +/- 1 from <code>line</code> are included in the search as well,
|
|
272 * without applying this last rule to them, though. (I.e., if <code>line</code> is unchanged,
|
|
273 * but has a deleted line above, this one is taken in. If the line above has changes, the block
|
|
274 * is extended from there. If the line has no changes itself, the search stops).
|
|
275 *
|
|
276 * The block never extends the visible line range of the viewer.
|
|
277 */
|
|
278
|
|
279 ILineDiffer differ= getDiffer(viewer);
|
|
280 if (differ is null)
|
|
281 return new Point(-1, -1);
|
|
282
|
|
283 // backward search
|
|
284
|
|
285 int l= line;
|
|
286 ILineDiffInfo info= differ.getLineInfo(l);
|
|
287 // search backwards until a line has no changes to itself
|
|
288 while (l >= min && info !is null && (info.getChangeType() is ILineDiffInfo.CHANGED || info.getChangeType() is ILineDiffInfo.ADDED)) {
|
|
289 info= differ.getLineInfo(--l);
|
|
290 }
|
|
291
|
|
292 int first= Math.min(l + 1, line);
|
|
293
|
|
294 // forward search
|
|
295
|
|
296 l= line;
|
|
297 info= differ.getLineInfo(l);
|
|
298 // search forward until a line has no changes to itself
|
|
299 while (l <= max && info !is null && (info.getChangeType() is ILineDiffInfo.CHANGED || info.getChangeType() is ILineDiffInfo.ADDED)) {
|
|
300 info= differ.getLineInfo(++l);
|
|
301 }
|
|
302
|
|
303 int last= Math.max(l - 1, line);
|
|
304
|
|
305 return new Point(first, last);
|
|
306 }
|
|
307
|
|
308 /*
|
|
309 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverInfo(dwtx.jface.text.source.ISourceViewer, dwtx.jface.text.source.ILineRange, int)
|
|
310 */
|
|
311 public Object getHoverInfo(ISourceViewer sourceViewer, ILineRange lineRange, int visibleLines) {
|
|
312 int first= adaptFirstLine(sourceViewer, lineRange.getStartLine());
|
|
313 int last= adaptLastLine(sourceViewer, lineRange.getStartLine() + lineRange.getNumberOfLines() - 1);
|
|
314 String content= computeContent(sourceViewer, first, last, visibleLines);
|
|
315 return formatSource(content);
|
|
316 }
|
|
317
|
|
318 /**
|
|
319 * Adapts the start line to the implementation of <code>ILineDiffInfo</code>.
|
|
320 *
|
|
321 * @param viewer the source viewer
|
|
322 * @param startLine the line to adapt
|
|
323 * @return <code>startLine - 1</code> if that line exists and is an
|
|
324 * unchanged line followed by deletions, <code>startLine</code>
|
|
325 * otherwise
|
|
326 */
|
|
327 private int adaptFirstLine(ISourceViewer viewer, int startLine) {
|
|
328 ILineDiffer differ= getDiffer(viewer);
|
|
329 if (differ !is null && startLine > 0) {
|
|
330 int l= startLine - 1;
|
|
331 ILineDiffInfo info= differ.getLineInfo(l);
|
|
332 if (info !is null && info.getChangeType() is ILineDiffInfo.UNCHANGED && info.getRemovedLinesBelow() > 0)
|
|
333 return l;
|
|
334 }
|
|
335 return startLine;
|
|
336 }
|
|
337
|
|
338 /**
|
|
339 * Adapts the last line to the implementation of <code>ILineDiffInfo</code>.
|
|
340 *
|
|
341 * @param viewer the source viewer
|
|
342 * @param lastLine the line to adapt
|
|
343 * @return <code>lastLine - 1</code> if that line exists and is an
|
|
344 * unchanged line followed by deletions, <code>startLine</code>
|
|
345 * otherwise
|
|
346 */
|
|
347 private int adaptLastLine(ISourceViewer viewer, int lastLine) {
|
|
348 ILineDiffer differ= getDiffer(viewer);
|
|
349 if (differ !is null && lastLine > 0) {
|
|
350 ILineDiffInfo info= differ.getLineInfo(lastLine);
|
|
351 if (info !is null && info.getChangeType() is ILineDiffInfo.UNCHANGED)
|
|
352 return lastLine - 1;
|
|
353 }
|
|
354 return lastLine;
|
|
355 }
|
|
356
|
|
357 /*
|
|
358 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverLineRange(dwtx.jface.text.source.ISourceViewer, int)
|
|
359 */
|
|
360 public ILineRange getHoverLineRange(ISourceViewer viewer, int lineNumber) {
|
|
361 IDocument document= viewer.getDocument();
|
|
362 if (document !is null) {
|
|
363 Point range= computeLineRange(viewer, lineNumber, 0, Math.max(0, document.getNumberOfLines() - 1));
|
|
364 if (range.x !is -1 && range.y !is -1)
|
|
365 return new LineRange(range.x, range.y - range.x + 1);
|
|
366 }
|
|
367 return null;
|
|
368 }
|
|
369
|
|
370 /*
|
|
371 * @see dwtx.jface.text.source.IAnnotationHoverExtension#canHandleMouseCursor()
|
|
372 */
|
|
373 public bool canHandleMouseCursor() {
|
|
374 return false;
|
|
375 }
|
|
376
|
|
377 /*
|
|
378 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverControlCreator()
|
|
379 */
|
|
380 public IInformationControlCreator getHoverControlCreator() {
|
|
381 return null;
|
|
382 }
|
|
383
|
|
384 /*
|
|
385 * @see dwtx.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator()
|
|
386 * @since 3.2
|
|
387 */
|
|
388 public IInformationControlCreator getInformationPresenterControlCreator() {
|
|
389 return new IInformationControlCreator() {
|
|
390 public IInformationControl createInformationControl(Shell parent) {
|
|
391 return new DefaultInformationControl(parent, (ToolBarManager)null, null);
|
|
392 }
|
|
393 };
|
|
394 }
|
|
395 }
|