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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
13 import java.util.HashMap;
15 import java.util.ResourceBundle;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextOperationTarget;
24 import org.eclipse.jface.text.ITextSelection;
25 import org.eclipse.jface.text.ITypedRegion;
26 import org.eclipse.jface.text.Region;
27 import org.eclipse.jface.text.TextUtilities;
28 import org.eclipse.jface.text.source.ISourceViewer;
29 import org.eclipse.jface.text.source.SourceViewerConfiguration;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.swt.custom.BusyIndicator;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.texteditor.ITextEditor;
35 import org.eclipse.ui.texteditor.ResourceAction;
36 import org.eclipse.ui.texteditor.TextEditorAction;
41 * An action which toggles comment prefixes on the selected lines.
45 public final class ToggleCommentAction extends TextEditorAction {
47 /** The text operation target */
48 private ITextOperationTarget fOperationTarget;
49 /** The document partitioning */
50 private String fDocumentPartitioning;
51 /** The comment prefixes */
52 private Map fPrefixesMap;
55 * Creates and initializes the action for the given text editor. The action
56 * configures its visual representation from the given resource bundle.
58 * @param bundle the resource bundle
59 * @param prefix a prefix to be prepended to the various resource keys
60 * (described in <code>ResourceAction</code> constructor), or
61 * <code>null</code> if none
62 * @param editor the text editor
63 * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
65 public ToggleCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
66 super(bundle, prefix, editor);
70 * Implementation of the <code>IAction</code> prototype. Checks if the selected
71 * lines are all commented or not and uncomments/comments them respectively.
74 if (fOperationTarget == null || fDocumentPartitioning == null || fPrefixesMap == null)
77 ITextEditor editor= getTextEditor();
81 if (!validateEditorInputState())
84 final int operationCode;
85 if (isSelectionCommented(editor.getSelectionProvider().getSelection()))
86 operationCode= ITextOperationTarget.STRIP_PREFIX;
88 operationCode= ITextOperationTarget.PREFIX;
90 Shell shell= editor.getSite().getShell();
91 if (!fOperationTarget.canDoOperation(operationCode)) {
93 MessageDialog.openError(shell, PHPEditorMessages.getString("ToggleComment.error.title"), PHPEditorMessages.getString("ToggleComment.error.message")); //$NON-NLS-1$ //$NON-NLS-2$
97 Display display= null;
98 if (shell != null && !shell.isDisposed())
99 display= shell.getDisplay();
101 BusyIndicator.showWhile(display, new Runnable() {
103 fOperationTarget.doOperation(operationCode);
109 * Is the given selection single-line commented?
111 * @param selection Selection to check
112 * @return <code>true</code> iff all selected lines are commented
114 private boolean isSelectionCommented(ISelection selection) {
115 if (!(selection instanceof ITextSelection))
118 ITextSelection textSelection= (ITextSelection) selection;
119 if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
122 IDocument document= getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
126 IRegion block= getTextBlockFromSelection(textSelection, document);
127 ITypedRegion[] regions= TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
130 int[] lines= new int[regions.length * 2]; // [startline, endline, startline, endline, ...]
131 for (int i= 0, j= 0; i < regions.length; i++, j+= 2) {
132 // start line of region
133 lines[j]= getFirstCompleteLineOfRegion(regions[i], document);
134 // end line of region
135 int length= regions[i].getLength();
136 int offset= regions[i].getOffset() + length;
139 lines[j + 1]= (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
140 lineCount += lines[j + 1] - lines[j] + 1;
144 for (int i= 0, j= 0; i < regions.length; i++, j += 2) {
145 String[] prefixes= (String[]) fPrefixesMap.get(regions[i].getType());
146 if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
147 if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
153 } catch (BadLocationException x) {
155 PHPeclipsePlugin.log(x);
162 * Creates a region describing the text block (something that starts at
163 * the beginning of a line) completely containing the current selection.
165 * @param selection The selection to use
166 * @param document The document
167 * @return the region describing the text block comprising the given selection
169 private IRegion getTextBlockFromSelection(ITextSelection selection, IDocument document) {
172 IRegion line= document.getLineInformationOfOffset(selection.getOffset());
173 int length= selection.getLength() == 0 ? line.getLength() : selection.getLength() + (selection.getOffset() - line.getOffset());
174 return new Region(line.getOffset(), length);
176 } catch (BadLocationException x) {
178 PHPeclipsePlugin.log(x);
185 * Returns the index of the first line whose start offset is in the given text range.
187 * @param region the text range in characters where to find the line
188 * @param document The document
189 * @return the first line whose start index is in the given range, -1 if there is no such line
191 private int getFirstCompleteLineOfRegion(IRegion region, IDocument document) {
195 int startLine= document.getLineOfOffset(region.getOffset());
197 int offset= document.getLineOffset(startLine);
198 if (offset >= region.getOffset())
201 offset= document.getLineOffset(startLine + 1);
202 return (offset > region.getOffset() + region.getLength() ? -1 : startLine + 1);
204 } catch (BadLocationException x) {
206 PHPeclipsePlugin.log(x);
213 * Determines whether each line is prefixed by one of the prefixes.
215 * @param startLine Start line in document
216 * @param endLine End line in document
217 * @param prefixes Possible comment prefixes
218 * @param document The document
219 * @return <code>true</code> iff each line from <code>startLine</code>
220 * to and including <code>endLine</code> is prepended by one
221 * of the <code>prefixes</code>, ignoring whitespace at the
224 private boolean isBlockCommented(int startLine, int endLine, String[] prefixes, IDocument document) {
228 // check for occurrences of prefixes in the given lines
229 for (int i= startLine; i <= endLine; i++) {
231 IRegion line= document.getLineInformation(i);
232 String text= document.get(line.getOffset(), line.getLength());
234 int[] found= TextUtilities.indexOf(prefixes, text, 0);
237 // found a line which is not commented
240 String s= document.get(line.getOffset(), found[0]);
243 // found a line which is not commented
250 } catch (BadLocationException x) {
252 PHPeclipsePlugin.log(x);
259 * Implementation of the <code>IUpdate</code> prototype method discovers
260 * the operation through the current editor's
261 * <code>ITextOperationTarget</code> adapter, and sets the enabled state
264 public void update() {
267 if (!canModifyEditor()) {
272 ITextEditor editor= getTextEditor();
273 if (fOperationTarget == null && editor != null)
274 fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
276 boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) && fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX));
277 setEnabled(isEnabled);
281 * @see TextEditorAction#setEditor(ITextEditor)
283 public void setEditor(ITextEditor editor) {
284 super.setEditor(editor);
285 fOperationTarget= null;
288 public void configure(ISourceViewer sourceViewer, SourceViewerConfiguration configuration) {
291 String[] types= configuration.getConfiguredContentTypes(sourceViewer);
292 Map prefixesMap= new HashMap(types.length);
293 for (int i= 0; i < types.length; i++) {
294 String type= types[i];
295 String[] prefixes= configuration.getDefaultPrefixes(sourceViewer, type);
296 if (prefixes != null && prefixes.length > 0) {
297 int emptyPrefixes= 0;
298 for (int j= 0; j < prefixes.length; j++)
299 if (prefixes[j].length() == 0)
302 if (emptyPrefixes > 0) {
303 String[] nonemptyPrefixes= new String[prefixes.length - emptyPrefixes];
304 for (int j= 0, k= 0; j < prefixes.length; j++) {
305 String prefix= prefixes[j];
306 if (prefix.length() != 0) {
307 nonemptyPrefixes[k]= prefix;
311 prefixes= nonemptyPrefixes;
314 prefixesMap.put(type, prefixes);
317 fDocumentPartitioning= configuration.getConfiguredDocumentPartitioning(sourceViewer);
318 fPrefixesMap= prefixesMap;