1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.ResourceBundle;
17 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
18 import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner;
21 //import org.eclipse.jface.text.Assert;
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.BadPartitioningException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IDocumentExtension3;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.text.ITypedRegion;
29 import org.eclipse.ui.texteditor.ITextEditor;
32 * Action that encloses the editor's current selection with Java block comment
33 * terminators (<code>/*</code> and <code>*/</code>).
37 public class AddBlockCommentAction extends BlockCommentAction {
40 * Creates a new instance.
45 * a prefix to be prepended to the various resource keys
46 * (described in <code>ResourceAction</code> constructor), or
47 * <code>null</code> if none
51 public AddBlockCommentAction(ResourceBundle bundle, String prefix,
53 super(bundle, prefix, editor);
57 * @see net.sourceforge.phpdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection,
58 * org.eclipse.jface.text.IDocumentExtension3,
59 * net.sourceforge.phpdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
61 protected void runInternal(ITextSelection selection,
62 IDocumentExtension3 docExtension, Edit.EditFactory factory)
63 throws BadLocationException, BadPartitioningException {
64 int selectionOffset = selection.getOffset();
65 int selectionEndOffset = selectionOffset + selection.getLength();
66 List edits = new LinkedList();
67 ITypedRegion partition = docExtension.getPartition(
68 IPHPPartitions.PHP_PARTITIONING, selectionOffset, false);
70 handleFirstPartition(partition, edits, factory, selectionOffset);
72 while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
73 partition = handleInteriorPartition(partition, edits, factory,
77 handleLastPartition(partition, edits, factory, selectionEndOffset);
83 * Handle the first partition of the selected text.
90 private void handleFirstPartition(ITypedRegion partition, List edits,
91 Edit.EditFactory factory, int offset) throws BadLocationException {
93 int partOffset = partition.getOffset();
94 String partType = partition.getType();
96 Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
98 // first partition: mark start of comment
99 if (partType == IDocument.DEFAULT_CONTENT_TYPE
100 || partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE) {
101 // Java code: right where selection starts
102 edits.add(factory.createEdit(offset, 0, getCommentStart()));
103 } else if (isSpecialPartition(partType)) {
104 // special types: include the entire partition
105 edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
106 } // javadoc: no mark, will only start after comment
111 * Handles the end of the given partition and the start of the next
112 * partition, which is returned.
117 * @param docExtension
119 * @throws BadLocationException
120 * @throws BadPartitioningException
122 private ITypedRegion handleInteriorPartition(ITypedRegion partition,
123 List edits, Edit.EditFactory factory,
124 IDocumentExtension3 docExtension) throws BadPartitioningException,
125 BadLocationException {
127 // end of previous partition
128 String partType = partition.getType();
129 int partEndOffset = partition.getOffset() + partition.getLength();
130 int tokenLength = getCommentStart().length();
132 boolean wasJavadoc = false; // true if the previous partition is javadoc
134 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
138 } else if (partType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
140 // already in a comment - remove ending mark
141 edits.add(factory.createEdit(partEndOffset - tokenLength,
142 tokenLength, "")); //$NON-NLS-1$
146 // advance to next partition
147 partition = docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING,
148 partEndOffset, false);
149 partType = partition.getType();
151 // start of next partition
154 // if previous was javadoc, and the current one is not, then add
155 // block comment start
156 if (partType == IDocument.DEFAULT_CONTENT_TYPE
157 || partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE
158 || isSpecialPartition(partType)) {
159 edits.add(factory.createEdit(partition.getOffset(), 0,
163 } else { // !wasJavadoc
165 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
166 // if next is javadoc, end block comment before
167 edits.add(factory.createEdit(partition.getOffset(), 0,
169 } else if (partType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
170 // already in a comment - remove startToken
171 edits.add(factory.createEdit(partition.getOffset(),
172 getCommentStart().length(), "")); //$NON-NLS-1$
180 * Handles the end of the last partition.
187 private void handleLastPartition(ITypedRegion partition, List edits,
188 Edit.EditFactory factory, int endOffset)
189 throws BadLocationException {
191 String partType = partition.getType();
193 if (partType == IDocument.DEFAULT_CONTENT_TYPE
194 || partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE) {
195 // normal java: end comment where selection ends
196 edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
197 } else if (isSpecialPartition(partType)) {
198 // special types: consume entire partition
199 edits.add(factory.createEdit(partition.getOffset()
200 + partition.getLength(), 0, getCommentEnd()));
206 * Returns whether <code>partType</code> is special, i.e. a Java
207 * <code>String</code>,<code>Character</code>, or
208 * <code>Line End Comment</code> partition.
211 * the partition type to check
212 * @return <code>true</code> if <code>partType</code> is special,
213 * <code>false</code> otherwise
215 private boolean isSpecialPartition(String partType) {
216 // return partType == IJavaPartitions.JAVA_CHARACTER
217 return partType == IPHPPartitions.PHP_STRING_DQ
218 || partType == IPHPPartitions.PHP_STRING_SQ
219 || partType == IPHPPartitions.PHP_STRING_HEREDOC
220 || partType == IPHPPartitions.PHP_SINGLELINE_COMMENT;
224 * @see net.sourceforge.phpdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
226 protected boolean isValidSelection(ITextSelection selection) {
227 return selection != null && !selection.isEmpty()
228 && selection.getLength() > 0;