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;
20 import org.eclipse.jface.text.Assert;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.BadPartitioningException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IDocumentExtension3;
25 import org.eclipse.jface.text.ITextSelection;
26 import org.eclipse.jface.text.ITypedRegion;
27 import org.eclipse.ui.texteditor.ITextEditor;
30 * Action that encloses the editor's current selection with Java block comment terminators (<code>/*</code> and
31 * <code>*/</code>).
35 public class AddBlockCommentAction extends BlockCommentAction {
38 * Creates a new instance.
43 * a prefix to be prepended to the various resource keys (described in <code>ResourceAction</code> constructor), or
44 * <code>null</code> if none
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,
54 * 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)
57 throws BadLocationException, BadPartitioningException {
58 int selectionOffset = selection.getOffset();
59 int selectionEndOffset = selectionOffset + selection.getLength();
60 List edits = new LinkedList();
61 ITypedRegion partition = docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, selectionOffset, false);
63 handleFirstPartition(partition, edits, factory, selectionOffset);
65 while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
66 partition = handleInteriorPartition(partition, edits, factory, docExtension);
69 handleLastPartition(partition, edits, factory, selectionEndOffset);
75 * Handle the first partition of the selected text.
82 private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset)
83 throws BadLocationException {
85 int partOffset = partition.getOffset();
86 String partType = partition.getType();
88 Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
90 // first partition: mark start of comment
91 if (partType == IDocument.DEFAULT_CONTENT_TYPE ||
92 partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE) {
93 // Java code: right where selection starts
94 edits.add(factory.createEdit(offset, 0, getCommentStart()));
95 } else if (isSpecialPartition(partType)) {
96 // special types: include the entire partition
97 edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
98 } // javadoc: no mark, will only start after comment
103 * Handles the end of the given partition and the start of the next partition, which is returned.
108 * @param docExtension
110 * @throws BadLocationException
111 * @throws BadPartitioningException
113 private ITypedRegion handleInteriorPartition(ITypedRegion partition, List edits, Edit.EditFactory factory,
114 IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
116 // end of previous partition
117 String partType = partition.getType();
118 int partEndOffset = partition.getOffset() + partition.getLength();
119 int tokenLength = getCommentStart().length();
121 boolean wasJavadoc = false; // true if the previous partition is javadoc
123 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
127 } else if (partType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
129 // already in a comment - remove ending mark
130 edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
134 // advance to next partition
135 partition = docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, partEndOffset, false);
136 partType = partition.getType();
138 // start of next partition
141 // if previous was javadoc, and the current one is not, then add block comment start
142 if (partType == IDocument.DEFAULT_CONTENT_TYPE ||
143 partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE ||
144 isSpecialPartition(partType)) {
145 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
148 } else { // !wasJavadoc
150 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
151 // if next is javadoc, end block comment before
152 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
153 } else if (partType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
154 // already in a comment - remove startToken
155 edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
163 * Handles the end of the last partition.
170 private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset)
171 throws BadLocationException {
173 String partType = partition.getType();
175 if (partType == IDocument.DEFAULT_CONTENT_TYPE ||
176 partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE) {
177 // normal java: end comment where selection ends
178 edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
179 } else if (isSpecialPartition(partType)) {
180 // special types: consume entire partition
181 edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
187 * Returns whether <code>partType</code> is special, i.e. a Java <code>String</code>,<code>Character</code>, or
188 * <code>Line End Comment</code> partition.
191 * the partition type to check
192 * @return <code>true</code> if <code>partType</code> is special, <code>false</code> otherwise
194 private boolean isSpecialPartition(String partType) {
195 // return partType == IJavaPartitions.JAVA_CHARACTER
196 return partType == IPHPPartitions.PHP_STRING_DQ || partType == IPHPPartitions.PHP_STRING_SQ
197 || partType == IPHPPartitions.PHP_SINGLELINE_COMMENT;
201 * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
203 protected boolean isValidSelection(ITextSelection selection) {
204 return selection != null && !selection.isEmpty() && selection.getLength() > 0;