1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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.corext.Assert;
18 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.BadPartitioningException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IDocumentExtension3;
24 import org.eclipse.jface.text.ITextSelection;
25 import org.eclipse.jface.text.ITypedRegion;
26 import org.eclipse.ui.texteditor.ITextEditor;
29 * Action that encloses the editor's current selection with Java block comment terminators
30 * (<code>/*</code> and <code>*/</code>).
34 public class AddBlockCommentAction extends BlockCommentAction {
37 * Creates a new instance.
39 * @param bundle the resource bundle
40 * @param prefix a prefix to be prepended to the various resource keys
41 * (described in <code>ResourceAction</code> constructor), or
42 * <code>null</code> if none
43 * @param editor the text editor
45 public AddBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
46 super(bundle, prefix, editor);
50 * @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)
52 protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException {
53 int selectionOffset= selection.getOffset();
54 int selectionEndOffset= selectionOffset + selection.getLength();
55 List edits= new LinkedList();
56 ITypedRegion partition= docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, selectionOffset);
58 handleFirstPartition(partition, edits, factory, selectionOffset);
60 while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
61 partition= handleInteriorPartition(partition, edits, factory, docExtension);
64 handleLastPartition(partition, edits, factory, selectionEndOffset);
70 * Handle the first partition of the selected text.
77 private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset) throws BadLocationException {
79 int partOffset= partition.getOffset();
80 String partType= partition.getType();
82 Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
84 // first partition: mark start of comment
85 if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
86 // Java code: right where selection starts
87 edits.add(factory.createEdit(offset, 0, getCommentStart()));
88 } else if (isSpecialPartition(partType)) {
89 // special types: include the entire partition
90 edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
91 } // javadoc: no mark, will only start after comment
96 * Handles the end of the given partition and the start of the next partition, which is returned.
101 * @param docExtension
103 * @throws BadLocationException
104 * @throws BadPartitioningException
106 private ITypedRegion handleInteriorPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
108 // end of previous partition
109 String partType= partition.getType();
110 int partEndOffset= partition.getOffset() + partition.getLength();
111 int tokenLength= getCommentStart().length();
113 boolean wasJavadoc= false; // true if the previous partition is javadoc
115 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
119 // } else if (partType == IPHPPartitions.JAVA_MULTI_LINE_COMMENT) {
121 // // already in a comment - remove ending mark
122 // edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
126 // advance to next partition
127 partition= docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, partEndOffset);
128 partType= partition.getType();
130 // start of next partition
133 // if previous was javadoc, and the current one is not, then add block comment start
134 if (partType == IDocument.DEFAULT_CONTENT_TYPE
135 || isSpecialPartition(partType)) {
136 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
139 } else { // !wasJavadoc
141 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
142 // if next is javadoc, end block comment before
143 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
144 // } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
145 // // already in a comment - remove startToken
146 // edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
154 * Handles the end of the last partition.
161 private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
163 String partType= partition.getType();
165 if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
166 // normal java: end comment where selection ends
167 edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
168 } else if (isSpecialPartition(partType)) {
169 // special types: consume entire partition
170 edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
176 * Returns whether <code>partType</code> is special, i.e. a Java <code>String</code>,
177 * <code>Character</code>, or <code>Line End Comment</code> partition.
179 * @param partType the partition type to check
180 * @return <code>true</code> if <code>partType</code> is special, <code>false</code> otherwise
182 private boolean isSpecialPartition(String partType) {
183 return// partType == IPHPPartitions.PHP_CHARACTER
185 partType == IPHPPartitions.PHP_STRING_DQ;
186 //|| partType == IPHPPartitions.PHP_SINGLE_LINE_COMMENT;
190 * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
192 protected boolean isValidSelection(ITextSelection selection) {
193 return selection != null && !selection.isEmpty() && selection.getLength() > 0;