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, false);
57 ITypedRegion partition= docExtension.getPartition(IDocumentExtension3.DEFAULT_PARTITIONING, selectionOffset, false);
59 handleFirstPartition(partition, edits, factory, selectionOffset);
61 while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
62 partition= handleInteriorPartition(partition, edits, factory, docExtension);
65 handleLastPartition(partition, edits, factory, selectionEndOffset);
71 * Handle the first partition of the selected text.
78 private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset) throws BadLocationException {
80 int partOffset= partition.getOffset();
81 String partType= partition.getType();
83 Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
85 // first partition: mark start of comment
86 // if (partType == IDocument.DEFAULT_CONTENT_TYPE)
87 if (partType == IPHPPartitions.PHP_PARTITIONING) {
88 // Java code: right where selection starts
89 edits.add(factory.createEdit(offset, 0, getCommentStart()));
90 } else if (isSpecialPartition(partType)) {
91 // special types: include the entire partition
92 edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
93 } // javadoc: no mark, will only start after comment
98 * Handles the end of the given partition and the start of the next partition, which is returned.
103 * @param docExtension
105 * @throws BadLocationException
106 * @throws BadPartitioningException
108 private ITypedRegion handleInteriorPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
110 // end of previous partition
111 String partType= partition.getType();
112 int partEndOffset= partition.getOffset() + partition.getLength();
113 int tokenLength= getCommentStart().length();
115 boolean wasJavadoc= false; // true if the previous partition is javadoc
117 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
121 // } else if (partType == IPHPPartitions.JAVA_MULTI_LINE_COMMENT) {
123 // // already in a comment - remove ending mark
124 // edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
128 // advance to next partition
129 partition= docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, partEndOffset, false);
130 partType= partition.getType();
132 // start of next partition
135 // if previous was javadoc, and the current one is not, then add block comment start
136 if (partType == IDocument.DEFAULT_CONTENT_TYPE
137 || isSpecialPartition(partType)) {
138 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
141 } else { // !wasJavadoc
143 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
144 // if next is javadoc, end block comment before
145 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
146 // } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
147 // // already in a comment - remove startToken
148 // edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
156 * Handles the end of the last partition.
163 private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
165 String partType= partition.getType();
167 // if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
168 if (partType == IPHPPartitions.PHP_PARTITIONING) {
169 // normal java: end comment where selection ends
170 edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
171 } else if (isSpecialPartition(partType)) {
172 // special types: consume entire partition
173 edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
179 * Returns whether <code>partType</code> is special, i.e. a Java <code>String</code>,
180 * <code>Character</code>, or <code>Line End Comment</code> partition.
182 * @param partType the partition type to check
183 * @return <code>true</code> if <code>partType</code> is special, <code>false</code> otherwise
185 private boolean isSpecialPartition(String partType) {
186 return// partType == IPHPPartitions.PHP_CHARACTER
188 partType == IPHPPartitions.PHP_STRING_DQ;
189 //|| partType == IPHPPartitions.PHP_SINGLE_LINE_COMMENT;
193 * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
195 protected boolean isValidSelection(ITextSelection selection) {
196 return selection != null && !selection.isEmpty() && selection.getLength() > 0;