]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/TaskMarkerProposal.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / text / correction / proposals / TaskMarkerProposal.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2012 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.core.runtime.CoreException;
14import org.eclipse.core.runtime.IStatus;
15
16import org.eclipse.text.edits.ReplaceEdit;
17import org.eclipse.text.edits.TextEdit;
18
19import org.eclipse.jface.text.BadLocationException;
20import org.eclipse.jface.text.IDocument;
21import org.eclipse.jface.text.IRegion;
22import org.eclipse.jface.text.Position;
23
24import org.eclipse.jdt.core.ICompilationUnit;
25import org.eclipse.jdt.core.ToolFactory;
26import org.eclipse.jdt.core.compiler.IScanner;
27import org.eclipse.jdt.core.compiler.ITerminalSymbols;
28import org.eclipse.jdt.core.compiler.InvalidInputException;
29
30import org.eclipse.jdt.internal.corext.dom.TokenScanner;
31
32import org.eclipse.jdt.ui.text.java.IProblemLocation;
33import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal;
34
35import org.eclipse.jdt.internal.ui.JavaPluginImages;
36import org.eclipse.jdt.internal.ui.JavaUIStatus;
37import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
38
39public class TaskMarkerProposal extends CUCorrectionProposal {
40
41 private IProblemLocation fLocation;
42
43 public TaskMarkerProposal(ICompilationUnit cu, IProblemLocation location, int relevance) {
44 super("", cu, relevance, null); //$NON-NLS-1$
45 fLocation= location;
46
47 setDisplayName(CorrectionMessages.TaskMarkerProposal_description);
48 setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
49 }
50
51 /* (non-Javadoc)
52 * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation.TextBuffer)
53 */
54 @Override
55 protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreException {
56 super.addEdits(document, rootEdit);
57
58 try {
59 Position pos= getUpdatedPosition(document);
60 if (pos != null) {
61 rootEdit.addChild(new ReplaceEdit(pos.getOffset(), pos.getLength(), "")); //$NON-NLS-1$
62 } else {
63 rootEdit.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), "")); //$NON-NLS-1$
64 }
65 } catch (BadLocationException e) {
66 throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
67 }
68 }
69
70 private Position getUpdatedPosition(IDocument document) throws BadLocationException {
71 IScanner scanner= ToolFactory.createScanner(true, false, false, false);
72 scanner.setSource(document.get().toCharArray());
73
74 int token= getSurroundingComment(scanner);
75 if (token == ITerminalSymbols.TokenNameEOF) {
76 return null;
77 }
78 int commentStart= scanner.getCurrentTokenStartPosition();
79 int commentEnd= scanner.getCurrentTokenEndPosition() + 1;
80
81 int contentStart= commentStart + 2;
82 int contentEnd= commentEnd;
83 if (token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
84 contentStart= commentStart + 3;
85 contentEnd= commentEnd - 2;
86 } else if (token == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
87 contentEnd= commentEnd - 2;
88 }
89 if (hasContent(document, contentStart, fLocation.getOffset()) || hasContent(document, contentEnd, fLocation.getOffset() + fLocation.getLength())) {
90 return new Position(fLocation.getOffset(), fLocation.getLength());
91 }
92
93 IRegion startRegion= document.getLineInformationOfOffset(commentStart);
94 int start= startRegion.getOffset();
95 boolean contentAtBegining= hasContent(document, start, commentStart);
96
97 if (contentAtBegining) {
98 start= commentStart;
99 }
100
101 int end;
102 if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
103 if (contentAtBegining) {
104 end= startRegion.getOffset() + startRegion.getLength(); // only to the end of the line
105 } else {
106 end= commentEnd; // includes new line
107 }
108 } else {
109 int endLine= document.getLineOfOffset(commentEnd - 1);
110 if (endLine + 1 == document.getNumberOfLines() || contentAtBegining) {
111 IRegion endRegion= document.getLineInformation(endLine);
112 end= endRegion.getOffset() + endRegion.getLength();
113 } else {
114 IRegion endRegion= document.getLineInformation(endLine + 1);
115 end= endRegion.getOffset();
116 }
117 }
118 if (hasContent(document, commentEnd, end)) {
119 end= commentEnd;
120 start= commentStart; // only remove comment
121 }
122 return new Position(start, end - start);
123 }
124
125 private int getSurroundingComment(IScanner scanner) {
126 try {
127 int start= fLocation.getOffset();
128 int end= start + fLocation.getLength();
129
130 int token= scanner.getNextToken();
131 while (token != ITerminalSymbols.TokenNameEOF) {
132 if (TokenScanner.isComment(token)) {
133 int currStart= scanner.getCurrentTokenStartPosition();
134 int currEnd= scanner.getCurrentTokenEndPosition() + 1;
135 if (currStart <= start && end <= currEnd) {
136 return token;
137 }
138 }
139 token= scanner.getNextToken();
140 }
141
142 } catch (InvalidInputException e) {
143 // ignore
144 }
145 return ITerminalSymbols.TokenNameEOF;
146 }
147
148 private boolean hasContent(IDocument document, int start, int end) throws BadLocationException {
149 for (int i= start; i < end; i++) {
150 char ch= document.getChar(i);
151 if (!Character.isWhitespace(ch)) {
152 return true;
153 }
154 }
155 return false;
156 }
157
158}