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;
39 * An action which toggles comment prefixes on the selected lines.
43 public final class ToggleCommentAction extends TextEditorAction {
45 /** The text operation target */
46 private ITextOperationTarget fOperationTarget;
48 /** The document partitioning */
49 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.
61 * a prefix to be prepended to the various resource keys
62 * (described in <code>ResourceAction</code> constructor), or
63 * <code>null</code> if none
66 * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
68 public ToggleCommentAction(ResourceBundle bundle, String prefix,
70 super(bundle, prefix, editor);
74 * Implementation of the <code>IAction</code> prototype. Checks if the
75 * selected lines are all commented or not and uncomments/comments them
79 if (fOperationTarget == null || fDocumentPartitioning == null
80 || fPrefixesMap == null)
83 ITextEditor editor = getTextEditor();
87 if (!validateEditorInputState())
90 final int operationCode;
91 if (isSelectionCommented(editor.getSelectionProvider().getSelection()))
92 operationCode = ITextOperationTarget.STRIP_PREFIX;
94 operationCode = ITextOperationTarget.PREFIX;
96 Shell shell = editor.getSite().getShell();
97 if (!fOperationTarget.canDoOperation(operationCode)) {
103 .getString("ToggleComment.error.title"), PHPEditorMessages.getString("ToggleComment.error.message")); //$NON-NLS-1$ //$NON-NLS-2$
107 Display display = null;
108 if (shell != null && !shell.isDisposed())
109 display = shell.getDisplay();
111 BusyIndicator.showWhile(display, new Runnable() {
113 fOperationTarget.doOperation(operationCode);
119 * Is the given selection single-line commented?
123 * @return <code>true</code> iff all selected lines are commented
125 private boolean isSelectionCommented(ISelection selection) {
126 if (!(selection instanceof ITextSelection))
129 ITextSelection textSelection = (ITextSelection) selection;
130 if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
133 IDocument document = getTextEditor().getDocumentProvider().getDocument(
134 getTextEditor().getEditorInput());
138 IRegion block = getTextBlockFromSelection(textSelection, document);
139 ITypedRegion[] regions = TextUtilities.computePartitioning(
140 document, fDocumentPartitioning, block.getOffset(), block
141 .getLength(), false);
144 int[] lines = new int[regions.length * 2]; // [startline, endline,
145 // startline, endline,
147 for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
148 // start line of region
149 lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
150 // end line of region
151 int length = regions[i].getLength();
152 int offset = regions[i].getOffset() + length;
155 lines[j + 1] = (lines[j] == -1 ? -1 : document
156 .getLineOfOffset(offset));
157 lineCount += lines[j + 1] - lines[j] + 1;
161 for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
162 String[] prefixes = (String[]) fPrefixesMap.get(regions[i]
164 if (prefixes != null && prefixes.length > 0 && lines[j] >= 0
165 && lines[j + 1] >= 0)
166 if (!isBlockCommented(lines[j], lines[j + 1], prefixes,
173 } catch (BadLocationException x) {
175 PHPeclipsePlugin.log(x);
182 * Creates a region describing the text block (something that starts at the
183 * beginning of a line) completely containing the current selection.
186 * The selection to use
189 * @return the region describing the text block comprising the given
192 private IRegion getTextBlockFromSelection(ITextSelection selection,
193 IDocument document) {
196 IRegion line = document.getLineInformationOfOffset(selection
198 int length = selection.getLength() == 0 ? line.getLength()
199 : selection.getLength()
200 + (selection.getOffset() - line.getOffset());
201 return new Region(line.getOffset(), length);
203 } catch (BadLocationException x) {
205 PHPeclipsePlugin.log(x);
212 * Returns the index of the first line whose start offset is in the given
216 * the text range in characters where to find the line
219 * @return the first line whose start index is in the given range, -1 if
220 * there is no such line
222 private int getFirstCompleteLineOfRegion(IRegion region, IDocument document) {
226 int startLine = document.getLineOfOffset(region.getOffset());
228 int offset = document.getLineOffset(startLine);
229 if (offset >= region.getOffset())
232 offset = document.getLineOffset(startLine + 1);
233 return (offset > region.getOffset() + region.getLength() ? -1
236 } catch (BadLocationException x) {
238 PHPeclipsePlugin.log(x);
245 * Determines whether each line is prefixed by one of the prefixes.
248 * Start line in document
250 * End line in document
252 * Possible comment prefixes
255 * @return <code>true</code> iff each line from <code>startLine</code>
256 * to and including <code>endLine</code> is prepended by one of
257 * the <code>prefixes</code>, ignoring whitespace at the begin of
260 private boolean isBlockCommented(int startLine, int endLine,
261 String[] prefixes, IDocument document) {
265 // check for occurrences of prefixes in the given lines
266 for (int i = startLine; i <= endLine; i++) {
268 IRegion line = document.getLineInformation(i);
269 String text = document.get(line.getOffset(), line.getLength());
271 int[] found = TextUtilities.indexOf(prefixes, text, 0);
274 // found a line which is not commented
277 String s = document.get(line.getOffset(), found[0]);
280 // found a line which is not commented
287 } catch (BadLocationException x) {
289 PHPeclipsePlugin.log(x);
296 * Implementation of the <code>IUpdate</code> prototype method discovers
297 * the operation through the current editor's
298 * <code>ITextOperationTarget</code> adapter, and sets the enabled state
301 public void update() {
304 if (!canModifyEditor()) {
309 ITextEditor editor = getTextEditor();
310 if (fOperationTarget == null && editor != null)
311 fOperationTarget = (ITextOperationTarget) editor
312 .getAdapter(ITextOperationTarget.class);
314 boolean isEnabled = (fOperationTarget != null
315 && fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) && fOperationTarget
316 .canDoOperation(ITextOperationTarget.STRIP_PREFIX));
317 setEnabled(isEnabled);
321 * @see TextEditorAction#setEditor(ITextEditor)
323 public void setEditor(ITextEditor editor) {
324 super.setEditor(editor);
325 fOperationTarget = null;
328 public void configure(ISourceViewer sourceViewer,
329 SourceViewerConfiguration configuration) {
332 String[] types = configuration.getConfiguredContentTypes(sourceViewer);
333 Map prefixesMap = new HashMap(types.length);
334 for (int i = 0; i < types.length; i++) {
335 String type = types[i];
336 String[] prefixes = configuration.getDefaultPrefixes(sourceViewer,
338 if (prefixes != null && prefixes.length > 0) {
339 int emptyPrefixes = 0;
340 for (int j = 0; j < prefixes.length; j++)
341 if (prefixes[j].length() == 0)
344 if (emptyPrefixes > 0) {
345 String[] nonemptyPrefixes = new String[prefixes.length
347 for (int j = 0, k = 0; j < prefixes.length; j++) {
348 String prefix = prefixes[j];
349 if (prefix.length() != 0) {
350 nonemptyPrefixes[k] = prefix;
354 prefixes = nonemptyPrefixes;
357 prefixesMap.put(type, prefixes);
360 fDocumentPartitioning = configuration
361 .getConfiguredDocumentPartitioning(sourceViewer);
362 fPrefixesMap = prefixesMap;