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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.ResourceBundle;
17 import org.eclipse.core.runtime.Assert;
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;
26 import org.eclipse.ui.texteditor.ITextEditor;
28 import org.eclipse.jdt.ui.text.IJavaPartitions;
32 * Action that encloses the editor's current selection with Java block comment terminators
33 * (<code>/*</code> and <code>*/</code>).
37 public class AddBlockCommentAction extends BlockCommentAction {
40 * Creates a new instance.
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
48 public AddBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
49 super(bundle, prefix, editor);
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)
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);
62 handleFirstPartition(partition, edits, factory, selectionOffset);
64 while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
65 partition= handleInteriorPartition(partition, edits, factory, docExtension);
68 handleLastPartition(partition, edits, factory, selectionEndOffset);
74 * Handle the partition under the start offset of the selection.
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>
82 private void handleFirstPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, int offset) throws BadLocationException {
84 int partOffset= partition.getOffset();
85 String partType= partition.getType();
87 Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
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
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.
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
111 * The next partition is returned.
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
124 private ITypedRegion handleInteriorPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
126 // end of previous partition
127 String partType= partition.getType();
128 int partEndOffset= partition.getOffset() + partition.getLength();
129 int tokenLength= getCommentStart().length();
131 boolean wasJavadoc= false; // true if the previous partition is javadoc
133 if (partType == IJavaPartitions.JAVA_DOC) {
137 } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
139 // already in a comment - remove ending mark
140 edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
144 // advance to next partition
145 partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false);
146 partType= partition.getType();
148 // start of next partition
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()));
158 } else { // !wasJavadoc
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$
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.
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
183 private void handleLastPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
185 String partType= partition.getType();
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()));
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.
202 * @param partType the partition type to check
203 * @return <code>true</code> if <code>partType</code> is special,
204 * <code>false</code> otherwise
206 private boolean isSpecialPartition(String partType) {
207 return partType == IJavaPartitions.JAVA_CHARACTER
208 || partType == IJavaPartitions.JAVA_STRING
209 || partType == IJavaPartitions.JAVA_SINGLE_LINE_COMMENT;
213 * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
216 protected boolean isValidSelection(ITextSelection selection) {
217 return selection != null && !selection.isEmpty() && selection.getLength() > 0;