improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / actions / RemoveBlockCommentAction.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
12
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.ResourceBundle;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.BadPartitioningException;
19 import org.eclipse.jface.text.IDocumentExtension3;
20 import org.eclipse.jface.text.ITextSelection;
21 import org.eclipse.jface.text.ITypedRegion;
22
23 import org.eclipse.ui.texteditor.ITextEditor;
24
25 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
26
27 /**
28  * Action that removes the enclosing comment marks from a Java block comment.
29  * 
30  * @since 3.0
31  */
32 public class RemoveBlockCommentAction extends BlockCommentAction {
33
34         /**
35          * Creates a new instance.
36          * 
37          * @param bundle the resource bundle
38          * @param prefix a prefix to be prepended to the various resource keys
39          *   (described in <code>ResourceAction</code> constructor), or 
40          *   <code>null</code> if none
41          * @param editor the text editor
42          */
43         public RemoveBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
44                 super(bundle, prefix, editor);
45         }
46         
47         /*
48          * @see net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction.Edit.EditFactory)
49          */
50         protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException {
51                 List edits= new LinkedList();
52                 int tokenLength= getCommentStart().length();
53                 
54                 int offset= selection.getOffset();
55                 int endOffset= offset + selection.getLength();
56
57                 ITypedRegion partition= docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, offset, false);
58                 int partOffset= partition.getOffset();
59                 int partEndOffset= partOffset + partition.getLength();
60                 
61                 while (partEndOffset < endOffset) {
62                         
63                         if (partition.getType() == IPHPPartitions.PHP_MULTILINE_COMMENT) {
64                                 edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
65                                 edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
66                         }
67                         
68                         partition= docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, partEndOffset, false);
69                         partOffset= partition.getOffset();
70                         partEndOffset= partOffset + partition.getLength();
71                 }
72
73                 if (partition.getType() == IPHPPartitions.PHP_MULTILINE_COMMENT) {
74                         edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
75                         edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
76                 }
77
78                 executeEdits(edits);
79         }
80
81         /*
82          * @see net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
83          */
84         protected boolean isValidSelection(ITextSelection selection) {
85                 return selection != null && !selection.isEmpty();
86         }
87
88 }