]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/EditAnnotator.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / text / correction / proposals / EditAnnotator.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.text.correction.proposals;
12
13import org.eclipse.text.edits.CopyTargetEdit;
14import org.eclipse.text.edits.DeleteEdit;
15import org.eclipse.text.edits.InsertEdit;
16import org.eclipse.text.edits.MoveSourceEdit;
17import org.eclipse.text.edits.MoveTargetEdit;
18import org.eclipse.text.edits.ReplaceEdit;
19import org.eclipse.text.edits.TextEdit;
20import org.eclipse.text.edits.TextEditVisitor;
21
22import org.eclipse.jface.text.BadLocationException;
23import org.eclipse.jface.text.IDocument;
24import org.eclipse.jface.text.IRegion;
25
26import org.eclipse.jdt.internal.corext.util.Strings;
27
28/**
29 * Class to annotate edits made by a quick fix/assist to be shown via the quick fix pop-up preview.
30 * E.g. the added changes are shown in bold.
31 *
32 * @since 3.8
33 */
34public class EditAnnotator extends TextEditVisitor {
35 private int fWrittenToPos= 0;
36
37 private final StringBuffer fBuf;
38
39 private final IDocument fPreviewDocument;
40
41 public EditAnnotator(StringBuffer buffer, IDocument previewDoc) {
42 fBuf= buffer;
43 fPreviewDocument= previewDoc;
44 }
45
46 public void unchangedUntil(int pos) {
47 if (pos > fWrittenToPos) {
48 appendContent(fPreviewDocument, fWrittenToPos, pos, true);
49 fWrittenToPos= pos;
50 }
51 }
52
53 @Override
54 public boolean visit(MoveTargetEdit edit) {
55 return true; //rangeAdded(edit);
56 }
57
58 @Override
59 public boolean visit(CopyTargetEdit edit) {
60 return true; //return rangeAdded(edit);
61 }
62
63 @Override
64 public boolean visit(InsertEdit edit) {
65 return rangeAdded(edit);
66 }
67
68 @Override
69 public boolean visit(ReplaceEdit edit) {
70 if (edit.getLength() > 0)
71 return rangeAdded(edit);
72 return rangeRemoved(edit);
73 }
74
75 @Override
76 public boolean visit(MoveSourceEdit edit) {
77 return rangeRemoved(edit);
78 }
79
80 @Override
81 public boolean visit(DeleteEdit edit) {
82 return rangeRemoved(edit);
83 }
84
85 protected boolean rangeRemoved(TextEdit edit) {
86 unchangedUntil(edit.getOffset());
87 return false;
88 }
89
90 private boolean rangeAdded(TextEdit edit) {
91 return annotateEdit(edit, "<b>", "</b>"); //$NON-NLS-1$ //$NON-NLS-2$
92 }
93
94 protected boolean annotateEdit(TextEdit edit, String startTag, String endTag) {
95 unchangedUntil(edit.getOffset());
96 fBuf.append(startTag);
97 appendContent(fPreviewDocument, edit.getOffset(), edit.getExclusiveEnd(), false);
98 fBuf.append(endTag);
99 fWrittenToPos= edit.getExclusiveEnd();
100 return false;
101 }
102
103 private void appendContent(IDocument text, int startOffset, int endOffset, boolean surroundLinesOnly) {
104 final int surroundLines= 1;
105 try {
106 int startLine= text.getLineOfOffset(startOffset);
107 int endLine= text.getLineOfOffset(endOffset);
108
109 boolean dotsAdded= false;
110 if (surroundLinesOnly && startOffset == 0) { // no surround lines for the top no-change range
111 startLine= Math.max(endLine - surroundLines, 0);
112 fBuf.append("...<br>"); //$NON-NLS-1$
113 dotsAdded= true;
114 }
115
116 for (int i= startLine; i <= endLine; i++) {
117 if (surroundLinesOnly) {
118 if ((i - startLine > surroundLines) && (endLine - i > surroundLines)) {
119 if (!dotsAdded) {
120 fBuf.append("...<br>"); //$NON-NLS-1$
121 dotsAdded= true;
122 } else if (endOffset == text.getLength()) {
123 return; // no surround lines for the bottom no-change range
124 }
125 continue;
126 }
127 }
128
129 IRegion lineInfo= text.getLineInformation(i);
130 int start= lineInfo.getOffset();
131 int end= start + lineInfo.getLength();
132
133 int from= Math.max(start, startOffset);
134 int to= Math.min(end, endOffset);
135 String content= text.get(from, to - from);
136 if (surroundLinesOnly && (from == start) && Strings.containsOnlyWhitespaces(content)) {
137 continue; // ignore empty lines except when range started in the middle of a line
138 }
139 for (int k= 0; k < content.length(); k++) {
140 char ch= content.charAt(k);
141 if (ch == '<') {
142 fBuf.append("&lt;"); //$NON-NLS-1$
143 } else if (ch == '>') {
144 fBuf.append("&gt;"); //$NON-NLS-1$
145 } else {
146 fBuf.append(ch);
147 }
148 }
149 if (to == end && to != endOffset) { // new line when at the end of the line, and not end of range
150 fBuf.append("<br>"); //$NON-NLS-1$
151 }
152 }
153 } catch (BadLocationException e) {
154 // ignore
155 }
156 }
157}