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.phpdt.internal.ui.actions;
13 import java.util.ResourceBundle;
15 import net.sourceforge.phpdt.core.JavaCore;
16 import net.sourceforge.phpdt.core.formatter.DefaultCodeFormatterConstants;
17 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
18 import net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner;
19 import net.sourceforge.phpdt.internal.ui.text.JavaIndenter;
20 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
21 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager.UndoSpec;
22 import net.sourceforge.phpdt.internal.ui.text.phpdoc.JavaDocAutoIndentStrategy;
23 import net.sourceforge.phpdt.ui.PreferenceConstants;
24 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
26 import net.sourceforge.phpeclipse.ui.WebUI;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.jface.text.Assert;
31 import org.eclipse.jface.text.BadLocationException;
32 import org.eclipse.jface.text.DocumentCommand;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.jface.text.IRegion;
35 import org.eclipse.jface.text.IRewriteTarget;
36 import org.eclipse.jface.text.ITextSelection;
37 import org.eclipse.jface.text.ITypedRegion;
38 import org.eclipse.jface.text.Position;
39 import org.eclipse.jface.text.Region;
40 import org.eclipse.jface.text.TextSelection;
41 import org.eclipse.jface.text.TextUtilities;
42 import org.eclipse.jface.text.source.ISourceViewer;
43 import org.eclipse.jface.viewers.ISelection;
44 import org.eclipse.jface.viewers.ISelectionProvider;
45 import org.eclipse.swt.custom.BusyIndicator;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.text.edits.MalformedTreeException;
48 import org.eclipse.text.edits.ReplaceEdit;
49 import org.eclipse.text.edits.TextEdit;
50 import org.eclipse.ui.IEditorInput;
51 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
52 import org.eclipse.ui.texteditor.IDocumentProvider;
53 import org.eclipse.ui.texteditor.ITextEditor;
54 import org.eclipse.ui.texteditor.ITextEditorExtension3;
55 import org.eclipse.ui.texteditor.TextEditorAction;
58 * Indents a line or range of lines in a Java document to its correct position.
59 * No complete AST must be present, the indentation is computed using
60 * heuristics. The algorith used is fast for single lines, but does not store
61 * any information and therefore not so efficient for large line ranges.
63 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner
64 * @see net.sourceforge.phpdt.internal.ui.text.JavaIndenter
67 public class IndentAction extends TextEditorAction {
69 /** The caret offset after an indent operation. */
70 private int fCaretOffset;
73 * Whether this is the action invoked by TAB. When <code>true</code>,
74 * indentation behaves differently to accomodate normal TAB operation.
76 private final boolean fIsTabAction;
79 * Creates a new instance.
84 * the prefix to use for keys in <code>bundle</code>
88 * whether the action should insert tabs if over the indentation
90 public IndentAction(ResourceBundle bundle, String prefix,
91 ITextEditor editor, boolean isTabAction) {
92 super(bundle, prefix, editor);
93 fIsTabAction = isTabAction;
97 * @see org.eclipse.jface.action.Action#run()
100 // update has been called by the framework
101 if (!isEnabled() || !validateEditorInputState())
104 ITextSelection selection = getSelection();
105 final IDocument document = getDocument();
107 if (document != null) {
109 final int offset = selection.getOffset();
110 final int length = selection.getLength();
111 final Position end = new Position(offset + length);
112 final int firstLine, nLines;
116 document.addPosition(end);
117 firstLine = document.getLineOfOffset(offset);
118 // check for marginal (zero-length) lines
119 int minusOne = length == 0 ? 0 : 1;
120 nLines = document.getLineOfOffset(offset + length - minusOne)
122 } catch (BadLocationException e) {
123 // will only happen on concurrent modification
124 WebUI.log(new Status(IStatus.ERROR, WebUI
125 .getPluginId(), IStatus.OK, "", e)); //$NON-NLS-1$
129 Runnable runnable = new Runnable() {
131 IRewriteTarget target = (IRewriteTarget) getTextEditor()
132 .getAdapter(IRewriteTarget.class);
133 if (target != null) {
134 target.beginCompoundChange();
135 target.setRedraw(false);
139 JavaHeuristicScanner scanner = new JavaHeuristicScanner(
141 JavaIndenter indenter = new JavaIndenter(document,
143 boolean hasChanged = false;
144 for (int i = 0; i < nLines; i++) {
145 hasChanged |= indentLine(document, firstLine + i,
146 offset, indenter, scanner);
149 // update caret position: move to new position when
150 // indenting just one line
151 // keep selection when indenting multiple
152 int newOffset, newLength;
154 newOffset = fCaretOffset;
156 } else if (nLines > 1) {
158 newLength = end.getOffset() - offset;
160 newOffset = fCaretOffset;
164 // always reset the selection if anything was replaced
165 // but not when we had a singleline nontab invocation
167 && (hasChanged || newOffset != offset || newLength != length))
168 selectAndReveal(newOffset, newLength);
170 document.removePosition(end);
171 } catch (BadLocationException e) {
172 // will only happen on concurrent modification
173 WebUI.log(new Status(IStatus.ERROR,
174 WebUI.getPluginId(), IStatus.OK,
175 "ConcurrentModification in IndentAction", e)); //$NON-NLS-1$
179 if (target != null) {
180 target.endCompoundChange();
181 target.setRedraw(true);
188 Display display = getTextEditor().getEditorSite()
189 .getWorkbenchWindow().getShell().getDisplay();
190 BusyIndicator.showWhile(display, runnable);
198 * Selects the given range on the editor.
201 * the selection offset
203 * the selection range
205 private void selectAndReveal(int newOffset, int newLength) {
206 Assert.isTrue(newOffset >= 0);
207 Assert.isTrue(newLength >= 0);
208 ITextEditor editor = getTextEditor();
209 if (editor instanceof PHPEditor) {
210 ISourceViewer viewer = ((PHPEditor) editor).getViewer();
212 viewer.setSelectedRange(newOffset, newLength);
214 // this is too intrusive, but will never get called anyway
215 getTextEditor().selectAndReveal(newOffset, newLength);
220 * Indents a single line using the java heuristic scanner. Javadoc and
221 * multiline comments are indented as specified by the
222 * <code>JavaDocAutoIndentStrategy</code>.
227 * the line to be indented
233 * the heuristic scanner
234 * @return <code>true</code> if <code>document</code> was modified,
235 * <code>false</code> otherwise
236 * @throws BadLocationException
237 * if the document got changed concurrently
239 private boolean indentLine(IDocument document, int line, int caret,
240 JavaIndenter indenter, JavaHeuristicScanner scanner)
241 throws BadLocationException {
242 IRegion currentLine = document.getLineInformation(line);
243 int offset = currentLine.getOffset();
244 int wsStart = offset; // where we start searching for non-WS; after
245 // the "//" in single line comments
247 String indent = null;
248 if (offset < document.getLength()) {
249 ITypedRegion partition = TextUtilities.getPartition(document,
250 IPHPPartitions.PHP_PARTITIONING, offset, true);
251 String type = partition.getType();
252 if (type.equals(IPHPPartitions.PHP_PHPDOC_COMMENT)
253 || type.equals(IPHPPartitions.PHP_MULTILINE_COMMENT)) {
255 // TODO this is a hack
257 // new JavaDocAutoIndentStrategy().indentLineAtOffset(document,
264 IRegion previousLine = document
265 .getLineInformation(line - 1);
266 start = previousLine.getOffset() + previousLine.getLength();
269 DocumentCommand command = new DocumentCommand() {
271 command.text = "\n"; //$NON-NLS-1$
272 command.offset = start;
273 new JavaDocAutoIndentStrategy(IPHPPartitions.PHP_PARTITIONING)
274 .customizeDocumentCommand(document, command);
276 while (to < command.text.length()
277 && Character.isWhitespace(command.text.charAt(to)))
279 indent = command.text.substring(1, to);
282 // } else if (!fIsTabAction && partition.getOffset() == offset
283 // && type.equals(IPHPPartitions.PHP_SINGLELINE_COMMENT)) {
285 // // line comment starting at position 0 -> indent inside
287 // while (slashes < document.getLength() - 1
288 // && document.get(offset + slashes, 2).equals("//")) //$NON-NLS-1$
291 // wsStart = offset + slashes;
293 // StringBuffer computed = indenter.computeIndentation(offset);
294 // int tabSize = PHPeclipsePlugin
296 // .getPreferenceStore()
298 // AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
299 // while (slashes > 0 && computed.length() > 0) {
300 // char c = computed.charAt(0);
302 // if (slashes > tabSize)
303 // slashes -= tabSize;
306 // else if (c == ' ')
311 // computed.deleteCharAt(0);
314 // indent = document.get(offset, wsStart - offset) + computed;
319 // standard java indentation
320 if (indent == null) {
321 StringBuffer computed = indenter.computeIndentation(offset);
322 if (computed != null)
323 indent = computed.toString();
325 //indent = new String();
326 return true; // prevent affecting html part
330 // get current white space
331 int lineLength = currentLine.getLength();
332 int end = scanner.findNonWhitespaceForwardInAnyPartition(wsStart,
333 offset + lineLength);
334 if (end == JavaHeuristicScanner.NOT_FOUND)
335 end = offset + lineLength;
336 int length = end - offset;
337 String currentIndent = document.get(offset, length);
339 // if we are right before the text start / line end, and already after
340 // the insertion point
341 // then just insert a tab.
342 if (fIsTabAction && caret == end
343 && whiteSpaceLength(currentIndent) >= whiteSpaceLength(indent)) {
344 String tab = getTabEquivalent();
345 document.replace(caret, 0, tab);
346 fCaretOffset = caret + tab.length();
350 // set the caret offset so it can be used when setting the selection
351 if (caret >= offset && caret <= end)
352 fCaretOffset = offset + indent.length();
356 // only change the document if it is a real change
357 if (!indent.equals(currentIndent)) {
358 String deletedText = document.get(offset, length);
359 document.replace(offset, length, indent);
362 && indent.length() > currentIndent.length()
363 && WebUI.getDefault().getPreferenceStore()
365 PreferenceConstants.EDITOR_SMART_BACKSPACE)) {
366 ITextEditor editor = getTextEditor();
367 if (editor != null) {
368 final SmartBackspaceManager manager = (SmartBackspaceManager) editor
369 .getAdapter(SmartBackspaceManager.class);
370 if (manager != null) {
372 // restore smart portion
373 ReplaceEdit smart = new ReplaceEdit(offset, indent
374 .length(), deletedText);
376 final UndoSpec spec = new UndoSpec(offset
377 + indent.length(), new Region(caret, 0),
378 new TextEdit[] { smart }, 2, null);
379 manager.register(spec);
380 } catch (MalformedTreeException e) {
382 WebUI.log(new Status(IStatus.ERROR,
383 WebUI.getPluginId(), IStatus.OK,
384 "Illegal smart backspace action", e)); //$NON-NLS-1$
396 * Returns the size in characters of a string. All characters count one,
397 * tabs count the editor's preference for the tab display
400 * the string to be measured.
403 private int whiteSpaceLength(String indent) {
408 int l = indent.length();
409 int tabSize = WebUI.getDefault().getPreferenceStore().getInt(
410 AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
412 for (int i = 0; i < l; i++)
413 size += indent.charAt(i) == '\t' ? tabSize : 1;
419 * Returns a tab equivalent, either as a tab character or as spaces,
420 * depending on the editor and formatter preferences.
422 * @return a string representing one tab in the editor, never
425 private String getTabEquivalent() {
427 if (WebUI.getDefault().getPreferenceStore().getBoolean(
428 PreferenceConstants.EDITOR_SPACES_FOR_TABS)) {
429 int size = JavaCore.getPlugin().getPluginPreferences().getInt(
430 DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE);
431 StringBuffer buf = new StringBuffer();
432 for (int i = 0; i < size; i++)
434 tab = buf.toString();
436 tab = "\t"; //$NON-NLS-1$
442 * Returns the editor's selection provider.
444 * @return the editor's selection provider or <code>null</code>
446 private ISelectionProvider getSelectionProvider() {
447 ITextEditor editor = getTextEditor();
448 if (editor != null) {
449 return editor.getSelectionProvider();
455 * @see org.eclipse.ui.texteditor.IUpdate#update()
457 public void update() {
462 setEnabled(canModifyEditor() && isSmartMode()
463 && isValidSelection());
465 setEnabled(canModifyEditor() && !getSelection().isEmpty());
469 * Returns if the current selection is valid, i.e. whether it is empty and
470 * the caret in the whitespace at the start of a line, or covers multiple
473 * @return <code>true</code> if the selection is valid for an indent
476 private boolean isValidSelection() {
477 ITextSelection selection = getSelection();
478 if (selection.isEmpty())
481 int offset = selection.getOffset();
482 int length = selection.getLength();
484 IDocument document = getDocument();
485 if (document == null)
489 IRegion firstLine = document.getLineInformationOfOffset(offset);
490 int lineOffset = firstLine.getOffset();
492 // either the selection has to be empty and the caret in the WS at
494 // or the selection has to extend over multiple lines
496 return document.get(lineOffset, offset - lineOffset).trim()
499 // return lineOffset + firstLine.getLength() < offset + length;
500 return false; // only enable for empty selections for now
502 } catch (BadLocationException e) {
509 * Returns the smart preference state.
511 * @return <code>true</code> if smart mode is on, <code>false</code>
514 private boolean isSmartMode() {
515 ITextEditor editor = getTextEditor();
517 if (editor instanceof ITextEditorExtension3)
518 return ((ITextEditorExtension3) editor).getInsertMode() == ITextEditorExtension3.SMART_INSERT;
524 * Returns the document currently displayed in the editor, or
525 * <code>null</code> if none can be obtained.
527 * @return the current document or <code>null</code>
529 private IDocument getDocument() {
531 ITextEditor editor = getTextEditor();
532 if (editor != null) {
534 IDocumentProvider provider = editor.getDocumentProvider();
535 IEditorInput input = editor.getEditorInput();
536 if (provider != null && input != null)
537 return provider.getDocument(input);
544 * Returns the selection on the editor or an invalid selection if none can
545 * be obtained. Returns never <code>null</code>.
547 * @return the current selection, never <code>null</code>
549 private ITextSelection getSelection() {
550 ISelectionProvider provider = getSelectionProvider();
551 if (provider != null) {
553 ISelection selection = provider.getSelection();
554 if (selection instanceof ITextSelection)
555 return (ITextSelection) selection;
559 return TextSelection.emptySelection();