]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/actions/AddBlockCommentAction.java
388ed938b14183191cb918b5f3dc4075475b10c4
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / actions / AddBlockCommentAction.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 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  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
12
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.ResourceBundle;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.BadPartitioningException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentExtension3;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.text.ITypedRegion;
25
26 import org.eclipse.ui.texteditor.ITextEditor;
27
28 import org.eclipse.jdt.ui.text.IJavaPartitions;
29
30
31 /**
32  * Action that encloses the editor's current selection with Java block comment terminators
33  * (<code>&#47;&#42;</code> and <code>&#42;&#47;</code>).
34  *
35  * @since 3.0
36  */
37 public class AddBlockCommentAction extends BlockCommentAction {
38
39         /**
40          * Creates a new instance.
41          *
42          * @param bundle the resource bundle
43          * @param prefix a prefix to be prepended to the various resource keys
44          *   (described in <code>ResourceAction</code> constructor), or
45          *   <code>null</code> if none
46          * @param editor the text editor
47          */
48         public AddBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
49                 super(bundle, prefix, editor);
50         }
51
52         /*
53          * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
54          */
55         @Override
56         protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException {
57                 int selectionOffset= selection.getOffset();
58                 int selectionEndOffset= selectionOffset + selection.getLength();
59                 List<Edit> edits= new LinkedList<Edit>();
60                 ITypedRegion partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, selectionOffset, false);
61
62                 handleFirstPartition(partition, edits, factory, selectionOffset);
63
64                 while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
65                         partition= handleInteriorPartition(partition, edits, factory, docExtension);
66                 }
67
68                 handleLastPartition(partition, edits, factory, selectionEndOffset);
69
70                 executeEdits(edits);
71         }
72
73         /**
74          * Handle the partition under the start offset of the selection.
75          *
76          * @param partition the partition under the start of the selection
77          * @param edits the list of edits to later execute
78          * @param factory the factory for edits
79          * @param offset the start of the selection, which must lie inside
80          *        <code>partition</code>
81          */
82         private void handleFirstPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, int offset) throws BadLocationException {
83
84                 int partOffset= partition.getOffset();
85                 String partType= partition.getType();
86
87                 Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
88
89                 // first partition: mark start of comment
90                 if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
91                         // Java code: right where selection starts
92                         edits.add(factory.createEdit(offset, 0, getCommentStart()));
93                 } else if (isSpecialPartition(partType)) {
94                         // special types: include the entire partition
95                         edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
96                 }       // javadoc: no mark, will only start after comment
97
98         }
99
100         /**
101          * Handles partition boundaries within the selection. The end of the current
102          * partition and the start of the next partition are examined for whether
103          * they contain comment tokens that interfere with the created comment.
104          * <p>
105          * Comment tokens are removed from interior multi-line comments. Javadoc
106          * comments are left as is; instead, multi-line comment tokens are inserted
107          * before and after Javadoc partitions to ensure that the entire selected
108          * area is commented.
109          * </p>
110          * <p>
111          * The next partition is returned.
112          * </p>
113          *
114          * @param partition the current partition
115          * @param edits the list of edits to add to
116          * @param factory the edit factory
117          * @param docExtension the document to get the partitions from
118          * @return the next partition after the current
119          * @throws BadLocationException if accessing the document fails - this can
120          *         only happen if the document gets modified concurrently
121          * @throws BadPartitioningException if the document does not have a Java
122          *         partitioning
123          */
124         private ITypedRegion handleInteriorPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
125
126                 // end of previous partition
127                 String partType= partition.getType();
128                 int partEndOffset= partition.getOffset() + partition.getLength();
129                 int tokenLength= getCommentStart().length();
130
131                 boolean wasJavadoc= false; // true if the previous partition is javadoc
132
133                 if (partType == IJavaPartitions.JAVA_DOC) {
134
135                         wasJavadoc= true;
136
137                 } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
138
139                         // already in a comment - remove ending mark
140                         edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
141
142                 }
143
144                 // advance to next partition
145                 partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false);
146                 partType= partition.getType();
147
148                 // start of next partition
149                 if (wasJavadoc) {
150
151                         // if previous was javadoc, and the current one is not a comment,
152                         // then add a block comment start
153                         if (partType == IDocument.DEFAULT_CONTENT_TYPE
154                                         || isSpecialPartition(partType)) {
155                                 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
156                         }
157
158                 } else { // !wasJavadoc
159
160                         if (partType == IJavaPartitions.JAVA_DOC) {
161                                 // if next is javadoc, end block comment before
162                                 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
163                         } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
164                                 // already in a comment - remove startToken
165                                 edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
166                         }
167                 }
168
169                 return partition;
170         }
171
172         /**
173          * Handles the partition under the end of the selection. For normal java
174          * code, the comment end token is inserted at the selection end; if the
175          * selection ends inside a special (i.e. string, character, line comment)
176          * partition, the entire partition is included inside the comment.
177          *
178          * @param partition the partition under the selection end offset
179          * @param edits the list of edits to add to
180          * @param factory the edit factory
181          * @param endOffset the end offset of the selection
182          */
183         private void handleLastPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
184
185                 String partType= partition.getType();
186
187                 if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
188                         // normal java: end comment where selection ends
189                         edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
190                 } else if (isSpecialPartition(partType)) {
191                         // special types: consume entire partition
192                         edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
193                 }
194
195         }
196
197         /**
198          * Returns whether <code>partType</code> is special, i.e. a Java
199          * <code>String</code>,<code>Character</code>, or
200          * <code>Line End Comment</code> partition.
201          *
202          * @param partType the partition type to check
203          * @return <code>true</code> if <code>partType</code> is special,
204          *         <code>false</code> otherwise
205          */
206         private boolean isSpecialPartition(String partType) {
207                 return partType == IJavaPartitions.JAVA_CHARACTER
208                                 || partType == IJavaPartitions.JAVA_STRING
209                                 || partType == IJavaPartitions.JAVA_SINGLE_LINE_COMMENT;
210         }
211
212         /*
213          * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
214          */
215         @Override
216         protected boolean isValidSelection(ITextSelection selection) {
217                 return selection != null && !selection.isEmpty() && selection.getLength() > 0;
218         }
219
220 }