Added a new PHP Parser Preference Page (global and on project level)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / actions / AddBlockCommentAction.java
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
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.corext.Assert;
18 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
19
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;
27
28 /**
29  * Action that encloses the editor's current selection with Java block comment terminators
30  * (<code>&#47;&#42;</code> and <code>&#42;&#47;</code>).
31  * 
32  * @since 3.0
33  */
34 public class AddBlockCommentAction extends BlockCommentAction {
35
36         /**
37          * Creates a new instance.
38          * 
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
44          */
45         public AddBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
46                 super(bundle, prefix, editor);
47         }
48         
49         /*
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)
51          */
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);
57
58                 handleFirstPartition(partition, edits, factory, selectionOffset);
59
60                 while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
61                         partition= handleInteriorPartition(partition, edits, factory, docExtension);
62                 }
63                 
64                 handleLastPartition(partition, edits, factory, selectionEndOffset);
65                 
66                 executeEdits(edits);
67         }
68
69         /**
70          * Handle the first partition of the selected text.
71          * 
72          * @param partition
73          * @param edits
74          * @param factory
75          * @param offset
76          */
77         private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset) throws BadLocationException {
78                 
79                 int partOffset= partition.getOffset();
80                 String partType= partition.getType();
81                 
82                 Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
83                 
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
92                 
93         }
94
95         /**
96          * Handles the end of the given partition and the start of the next partition, which is returned.
97          * 
98          * @param partition
99          * @param edits
100          * @param factory
101          * @param docExtension
102          * @return
103          * @throws BadLocationException
104          * @throws BadPartitioningException
105          */
106         private ITypedRegion handleInteriorPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
107
108                 // end of previous partition
109                 String partType= partition.getType();
110                 int partEndOffset= partition.getOffset() + partition.getLength();
111                 int tokenLength= getCommentStart().length();
112                 
113                 boolean wasJavadoc= false; // true if the previous partition is javadoc
114                 
115                 if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
116                         
117                         wasJavadoc= true;
118                         
119 //              } else if (partType == IPHPPartitions.JAVA_MULTI_LINE_COMMENT) {
120 //                      
121 //                      // already in a comment - remove ending mark
122 //                      edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
123 //                      
124                 }
125
126                 // advance to next partition
127                 partition= docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, partEndOffset);
128                 partType= partition.getType();
129
130                 // start of next partition
131                 if (wasJavadoc) {
132                         
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()));
137                         }
138                         
139                 } else { // !wasJavadoc
140                 
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$
147                         }
148                 }
149                 
150                 return partition;
151         }
152
153         /**
154          * Handles the end of the last partition.
155          * 
156          * @param partition
157          * @param edits
158          * @param factory
159          * @param endOffset
160          */
161         private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
162
163                 String partType= partition.getType();
164                 
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()));
171                 }
172                 
173         }
174
175         /**
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.
178          * 
179          * @param partType the partition type to check
180          * @return <code>true</code> if <code>partType</code> is special, <code>false</code> otherwise
181          */
182         private boolean isSpecialPartition(String partType) {
183                 return// partType == IPHPPartitions.PHP_CHARACTER
184                                 //|| 
185                                 partType == IPHPPartitions.PHP_STRING_DQ;
186                                 //|| partType == IPHPPartitions.PHP_SINGLE_LINE_COMMENT;
187         }
188
189         /*
190          * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
191          */
192         protected boolean isValidSelection(ITextSelection selection) {
193                 return selection != null && !selection.isEmpty() && selection.getLength() > 0;
194         }
195
196 }