KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > TaskMarkerProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.correction;
12
13
14 import org.eclipse.text.edits.ReplaceEdit;
15 import org.eclipse.text.edits.TextEdit;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.Position;
24
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.ToolFactory;
27 import org.eclipse.jdt.core.compiler.IScanner;
28 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
29 import org.eclipse.jdt.core.compiler.InvalidInputException;
30
31 import org.eclipse.jdt.ui.text.java.IProblemLocation;
32
33 import org.eclipse.jdt.internal.corext.dom.TokenScanner;
34 import org.eclipse.jdt.internal.ui.JavaPluginImages;
35 import org.eclipse.jdt.internal.ui.JavaUIStatus;
36
37 /**
38   */

39 public 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     protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreException {
55         super.addEdits(document, rootEdit);
56
57         try {
58             Position pos= getUpdatedPosition(document);
59             if (pos != null) {
60                 rootEdit.addChild(new ReplaceEdit(pos.getOffset(), pos.getLength(), "")); //$NON-NLS-1$
61
} else {
62                 rootEdit.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), "")); //$NON-NLS-1$
63
}
64         } catch (BadLocationException e) {
65             throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
66         }
67     }
68
69     private Position getUpdatedPosition(IDocument document) throws BadLocationException {
70         IScanner scanner= ToolFactory.createScanner(true, false, false, false);
71         scanner.setSource(document.get().toCharArray());
72
73         int token= getSurroundingComment(scanner);
74         if (token == ITerminalSymbols.TokenNameEOF) {
75             return null;
76         }
77         int commentStart= scanner.getCurrentTokenStartPosition();
78         int commentEnd= scanner.getCurrentTokenEndPosition() + 1;
79
80         int contentStart= commentStart + 2;
81         int contentEnd= commentEnd;
82         if (token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
83             contentStart= commentStart + 3;
84             contentEnd= commentEnd - 2;
85         } else if (token == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
86             contentEnd= commentEnd - 2;
87         }
88         if (hasContent(document, contentStart, fLocation.getOffset()) || hasContent(document, contentEnd, fLocation.getOffset() + fLocation.getLength())) {
89             return new Position(fLocation.getOffset(), fLocation.getLength());
90         }
91
92         IRegion startRegion= document.getLineInformationOfOffset(commentStart);
93         int start= startRegion.getOffset();
94         boolean contentAtBegining= hasContent(document, start, commentStart);
95
96         if (contentAtBegining) {
97             start= commentStart;
98         }
99
100         int end;
101         if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
102             if (contentAtBegining) {
103                 end= startRegion.getOffset() + startRegion.getLength(); // only to the end of the line
104
} else {
105                 end= commentEnd; // includes new line
106
}
107         } else {
108             int endLine= document.getLineOfOffset(commentEnd - 1);
109             if (endLine + 1 == document.getNumberOfLines() || contentAtBegining) {
110                 IRegion endRegion= document.getLineInformation(endLine);
111                 end= endRegion.getOffset() + endRegion.getLength();
112             } else {
113                 IRegion endRegion= document.getLineInformation(endLine + 1);
114                 end= endRegion.getOffset();
115             }
116         }
117         if (hasContent(document, commentEnd, end)) {
118             end= commentEnd;
119             start= commentStart; // only remove comment
120
}
121         return new Position(start, end - start);
122     }
123
124     private int getSurroundingComment(IScanner scanner) {
125         try {
126             int start= fLocation.getOffset();
127             int end= start + fLocation.getLength();
128
129             int token= scanner.getNextToken();
130             while (token != ITerminalSymbols.TokenNameEOF) {
131                 if (TokenScanner.isComment(token)) {
132                     int currStart= scanner.getCurrentTokenStartPosition();
133                     int currEnd= scanner.getCurrentTokenEndPosition() + 1;
134                     if (currStart <= start && end <= currEnd) {
135                         return token;
136                     }
137                 }
138                 token= scanner.getNextToken();
139             }
140
141         } catch (InvalidInputException e) {
142             // ignore
143
}
144         return ITerminalSymbols.TokenNameEOF;
145     }
146
147     private boolean hasContent(IDocument document, int start, int end) throws BadLocationException {
148         for (int i= start; i < end; i++) {
149             char ch= document.getChar(i);
150             if (!Character.isWhitespace(ch)) {
151                 return true;
152             }
153         }
154         return false;
155     }
156
157 }
158
Popular Tags