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