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;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jface.text.Assert;
30 import org.eclipse.jface.text.BadLocationException;
31 import org.eclipse.jface.text.DocumentCommand;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.IRegion;
34 import org.eclipse.jface.text.IRewriteTarget;
35 import org.eclipse.jface.text.ITextSelection;
36 import org.eclipse.jface.text.ITypedRegion;
37 import org.eclipse.jface.text.Position;
38 import org.eclipse.jface.text.Region;
39 import org.eclipse.jface.text.TextSelection;
40 import org.eclipse.jface.text.TextUtilities;
41 import org.eclipse.jface.text.source.ISourceViewer;
42 import org.eclipse.jface.viewers.ISelection;
43 import org.eclipse.jface.viewers.ISelectionProvider;
44 import org.eclipse.swt.custom.BusyIndicator;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.text.edits.MalformedTreeException;
47 import org.eclipse.text.edits.ReplaceEdit;
48 import org.eclipse.text.edits.TextEdit;
49 import org.eclipse.ui.IEditorInput;
50 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
51 import org.eclipse.ui.texteditor.IDocumentProvider;
52 import org.eclipse.ui.texteditor.ITextEditor;
53 import org.eclipse.ui.texteditor.ITextEditorExtension3;
54 import org.eclipse.ui.texteditor.TextEditorAction;
57 * Indents a line or range of lines in a Java document to its correct position.
58 * No complete AST must be present, the indentation is computed using
59 * heuristics. The algorith used is fast for single lines, but does not store
60 * any information and therefore not so efficient for large line ranges.
62 * @see net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner
63 * @see net.sourceforge.phpdt.internal.ui.text.JavaIndenter
66 public class IndentAction extends TextEditorAction {
68 /** The caret offset after an indent operation. */
69 private int fCaretOffset;
72 * Whether this is the action invoked by TAB. When <code>true</code>,
73 * indentation behaves differently to accomodate normal TAB operation.
75 private final boolean fIsTabAction;
78 * Creates a new instance.
83 * the prefix to use for keys in <code>bundle</code>
87 * whether the action should insert tabs if over the indentation
89 public IndentAction(ResourceBundle bundle, String prefix,
90 ITextEditor editor, boolean isTabAction) {
91 super(bundle, prefix, editor);
92 fIsTabAction = isTabAction;
96 * @see org.eclipse.jface.action.Action#run()
99 // update has been called by the framework
100 if (!isEnabled() || !validateEditorInputState())
103 ITextSelection selection = getSelection();
104 final IDocument document = getDocument();
106 if (document != null) {
108 final int offset = selection.getOffset();
109 final int length = selection.getLength();
110 final Position end = new Position(offset + length);
111 final int firstLine, nLines;
115 document.addPosition(end);
116 firstLine = document.getLineOfOffset(offset);
117 // check for marginal (zero-length) lines
118 int minusOne = length == 0 ? 0 : 1;
119 nLines = document.getLineOfOffset(offset + length - minusOne)
121 } catch (BadLocationException e) {
122 // will only happen on concurrent modification
123 PHPeclipsePlugin.log(new Status(IStatus.ERROR, PHPeclipsePlugin
124 .getPluginId(), IStatus.OK, "", e)); //$NON-NLS-1$
128 Runnable runnable = new Runnable() {
130 IRewriteTarget target = (IRewriteTarget) getTextEditor()
131 .getAdapter(IRewriteTarget.class);
132 if (target != null) {
133 target.beginCompoundChange();
134 target.setRedraw(false);
138 JavaHeuristicScanner scanner = new JavaHeuristicScanner(
140 JavaIndenter indenter = new JavaIndenter(document,
142 boolean hasChanged = false;
143 for (int i = 0; i < nLines; i++) {
144 hasChanged |= indentLine(document, firstLine + i,
145 offset, indenter, scanner);
148 // update caret position: move to new position when
149 // indenting just one line
150 // keep selection when indenting multiple
151 int newOffset, newLength;
153 newOffset = fCaretOffset;
155 } else if (nLines > 1) {
157 newLength = end.getOffset() - offset;
159 newOffset = fCaretOffset;
163 // always reset the selection if anything was replaced
164 // but not when we had a singleline nontab invocation
166 && (hasChanged || newOffset != offset || newLength != length))
167 selectAndReveal(newOffset, newLength);
169 document.removePosition(end);
170 } catch (BadLocationException e) {
171 // will only happen on concurrent modification
172 PHPeclipsePlugin.log(new Status(IStatus.ERROR,
173 PHPeclipsePlugin.getPluginId(), IStatus.OK,
174 "ConcurrentModification in IndentAction", e)); //$NON-NLS-1$
178 if (target != null) {
179 target.endCompoundChange();
180 target.setRedraw(true);
187 Display display = getTextEditor().getEditorSite()
188 .getWorkbenchWindow().getShell().getDisplay();
189 BusyIndicator.showWhile(display, runnable);
197 * Selects the given range on the editor.
200 * the selection offset
202 * the selection range
204 private void selectAndReveal(int newOffset, int newLength) {
205 Assert.isTrue(newOffset >= 0);
206 Assert.isTrue(newLength >= 0);
207 ITextEditor editor = getTextEditor();
208 if (editor instanceof PHPEditor) {
209 ISourceViewer viewer = ((PHPEditor) editor).getViewer();
211 viewer.setSelectedRange(newOffset, newLength);
213 // this is too intrusive, but will never get called anyway
214 getTextEditor().selectAndReveal(newOffset, newLength);
219 * Indents a single line using the java heuristic scanner. Javadoc and
220 * multiline comments are indented as specified by the
221 * <code>JavaDocAutoIndentStrategy</code>.
226 * the line to be indented
232 * the heuristic scanner
233 * @return <code>true</code> if <code>document</code> was modified,
234 * <code>false</code> otherwise
235 * @throws BadLocationException
236 * if the document got changed concurrently
238 private boolean indentLine(IDocument document, int line, int caret,
239 JavaIndenter indenter, JavaHeuristicScanner scanner)
240 throws BadLocationException {
241 IRegion currentLine = document.getLineInformation(line);
242 int offset = currentLine.getOffset();
243 int wsStart = offset; // where we start searching for non-WS; after
244 // the "//" in single line comments
246 String indent = null;
247 if (offset < document.getLength()) {
248 ITypedRegion partition = TextUtilities.getPartition(document,
249 IPHPPartitions.PHP_PARTITIONING, offset, true);
250 String type = partition.getType();
251 if (type.equals(IPHPPartitions.PHP_PHPDOC_COMMENT)
252 || type.equals(IPHPPartitions.PHP_MULTILINE_COMMENT)) {
254 // TODO this is a hack
256 // new JavaDocAutoIndentStrategy().indentLineAtOffset(document,
263 IRegion previousLine = document
264 .getLineInformation(line - 1);
265 start = previousLine.getOffset() + previousLine.getLength();
268 DocumentCommand command = new DocumentCommand() {
270 command.text = "\n"; //$NON-NLS-1$
271 command.offset = start;
272 new JavaDocAutoIndentStrategy(IPHPPartitions.PHP_PARTITIONING)
273 .customizeDocumentCommand(document, command);
275 while (to < command.text.length()
276 && Character.isWhitespace(command.text.charAt(to)))
278 indent = command.text.substring(1, to);
281 // } else if (!fIsTabAction && partition.getOffset() == offset
282 // && type.equals(IPHPPartitions.PHP_SINGLELINE_COMMENT)) {
284 // // line comment starting at position 0 -> indent inside
286 // while (slashes < document.getLength() - 1
287 // && document.get(offset + slashes, 2).equals("//")) //$NON-NLS-1$
290 // wsStart = offset + slashes;
292 // StringBuffer computed = indenter.computeIndentation(offset);
293 // int tabSize = PHPeclipsePlugin
295 // .getPreferenceStore()
297 // AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
298 // while (slashes > 0 && computed.length() > 0) {
299 // char c = computed.charAt(0);
301 // if (slashes > tabSize)
302 // slashes -= tabSize;
305 // else if (c == ' ')
310 // computed.deleteCharAt(0);
313 // indent = document.get(offset, wsStart - offset) + computed;
318 // standard java indentation
319 if (indent == null) {
320 StringBuffer computed = indenter.computeIndentation(offset);
321 if (computed != null)
322 indent = computed.toString();
324 //indent = new String();
325 return true; // prevent affecting html part
329 // get current white space
330 int lineLength = currentLine.getLength();
331 int end = scanner.findNonWhitespaceForwardInAnyPartition(wsStart,
332 offset + lineLength);
333 if (end == JavaHeuristicScanner.NOT_FOUND)
334 end = offset + lineLength;
335 int length = end - offset;
336 String currentIndent = document.get(offset, length);
338 // if we are right before the text start / line end, and already after
339 // the insertion point
340 // then just insert a tab.
341 if (fIsTabAction && caret == end
342 && whiteSpaceLength(currentIndent) >= whiteSpaceLength(indent)) {
343 String tab = getTabEquivalent();
344 document.replace(caret, 0, tab);
345 fCaretOffset = caret + tab.length();
349 // set the caret offset so it can be used when setting the selection
350 if (caret >= offset && caret <= end)
351 fCaretOffset = offset + indent.length();
355 // only change the document if it is a real change
356 if (!indent.equals(currentIndent)) {
357 String deletedText = document.get(offset, length);
358 document.replace(offset, length, indent);
361 && indent.length() > currentIndent.length()
362 && PHPeclipsePlugin.getDefault().getPreferenceStore()
364 PreferenceConstants.EDITOR_SMART_BACKSPACE)) {
365 ITextEditor editor = getTextEditor();
366 if (editor != null) {
367 final SmartBackspaceManager manager = (SmartBackspaceManager) editor
368 .getAdapter(SmartBackspaceManager.class);
369 if (manager != null) {
371 // restore smart portion
372 ReplaceEdit smart = new ReplaceEdit(offset, indent
373 .length(), deletedText);
375 final UndoSpec spec = new UndoSpec(offset
376 + indent.length(), new Region(caret, 0),
377 new TextEdit[] { smart }, 2, null);
378 manager.register(spec);
379 } catch (MalformedTreeException e) {
381 PHPeclipsePlugin.log(new Status(IStatus.ERROR,
382 PHPeclipsePlugin.getPluginId(), IStatus.OK,
383 "Illegal smart backspace action", e)); //$NON-NLS-1$
395 * Returns the size in characters of a string. All characters count one,
396 * tabs count the editor's preference for the tab display
399 * the string to be measured.
402 private int whiteSpaceLength(String indent) {
407 int l = indent.length();
408 int tabSize = PHPeclipsePlugin
410 .getPreferenceStore()
412 AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
414 for (int i = 0; i < l; i++)
415 size += indent.charAt(i) == '\t' ? tabSize : 1;
421 * Returns a tab equivalent, either as a tab character or as spaces,
422 * depending on the editor and formatter preferences.
424 * @return a string representing one tab in the editor, never
427 private String getTabEquivalent() {
429 if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(
430 PreferenceConstants.EDITOR_SPACES_FOR_TABS)) {
431 int size = JavaCore.getPlugin().getPluginPreferences().getInt(
432 DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE);
433 StringBuffer buf = new StringBuffer();
434 for (int i = 0; i < size; i++)
436 tab = buf.toString();
438 tab = "\t"; //$NON-NLS-1$
444 * Returns the editor's selection provider.
446 * @return the editor's selection provider or <code>null</code>
448 private ISelectionProvider getSelectionProvider() {
449 ITextEditor editor = getTextEditor();
450 if (editor != null) {
451 return editor.getSelectionProvider();
457 * @see org.eclipse.ui.texteditor.IUpdate#update()
459 public void update() {
464 setEnabled(canModifyEditor() && isSmartMode()
465 && isValidSelection());
467 setEnabled(canModifyEditor() && !getSelection().isEmpty());
471 * Returns if the current selection is valid, i.e. whether it is empty and
472 * the caret in the whitespace at the start of a line, or covers multiple
475 * @return <code>true</code> if the selection is valid for an indent
478 private boolean isValidSelection() {
479 ITextSelection selection = getSelection();
480 if (selection.isEmpty())
483 int offset = selection.getOffset();
484 int length = selection.getLength();
486 IDocument document = getDocument();
487 if (document == null)
491 IRegion firstLine = document.getLineInformationOfOffset(offset);
492 int lineOffset = firstLine.getOffset();
494 // either the selection has to be empty and the caret in the WS at
496 // or the selection has to extend over multiple lines
498 return document.get(lineOffset, offset - lineOffset).trim()
501 // return lineOffset + firstLine.getLength() < offset + length;
502 return false; // only enable for empty selections for now
504 } catch (BadLocationException e) {
511 * Returns the smart preference state.
513 * @return <code>true</code> if smart mode is on, <code>false</code>
516 private boolean isSmartMode() {
517 ITextEditor editor = getTextEditor();
519 if (editor instanceof ITextEditorExtension3)
520 return ((ITextEditorExtension3) editor).getInsertMode() == ITextEditorExtension3.SMART_INSERT;
526 * Returns the document currently displayed in the editor, or
527 * <code>null</code> if none can be obtained.
529 * @return the current document or <code>null</code>
531 private IDocument getDocument() {
533 ITextEditor editor = getTextEditor();
534 if (editor != null) {
536 IDocumentProvider provider = editor.getDocumentProvider();
537 IEditorInput input = editor.getEditorInput();
538 if (provider != null && input != null)
539 return provider.getDocument(input);
546 * Returns the selection on the editor or an invalid selection if none can
547 * be obtained. Returns never <code>null</code>.
549 * @return the current selection, never <code>null</code>
551 private ITextSelection getSelection() {
552 ISelectionProvider provider = getSelectionProvider();
553 if (provider != null) {
555 ISelection selection = provider.getSelection();
556 if (selection instanceof ITextSelection)
557 return (ITextSelection) selection;
561 return TextSelection.emptySelection();