1 package net.sourceforge.phpeclipse.phpeditor;
3 import java.text.MessageFormat;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
10 import net.sourceforge.phpdt.core.ICompilationUnit;
11 import net.sourceforge.phpdt.core.IJavaElement;
12 import net.sourceforge.phpdt.core.IJavaProject;
13 import net.sourceforge.phpdt.core.IMember;
14 import net.sourceforge.phpdt.core.ISourceRange;
15 import net.sourceforge.phpdt.core.ISourceReference;
16 import net.sourceforge.phpdt.core.JavaCore;
17 import net.sourceforge.phpdt.core.JavaModelException;
18 import net.sourceforge.phpdt.core.dom.CompilationUnit;
19 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
20 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
21 import net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction;
22 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
23 import net.sourceforge.phpdt.internal.ui.actions.IndentAction;
24 import net.sourceforge.phpdt.internal.ui.actions.RemoveBlockCommentAction;
25 import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
26 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
27 import net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner;
28 import net.sourceforge.phpdt.internal.ui.text.JavaIndenter;
29 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
30 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
31 import net.sourceforge.phpdt.internal.ui.text.SmartSemicolonAutoEditStrategy;
32 import net.sourceforge.phpdt.internal.ui.text.comment.CommentFormattingContext;
33 import net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener;
34 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
35 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
36 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI.ExitFlags;
37 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
38 import net.sourceforge.phpdt.ui.PreferenceConstants;
39 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
40 import net.sourceforge.phpdt.ui.text.JavaTextTools;
41 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
42 import net.sourceforge.phpeclipse.phpeditor.actions.RTrimAction;
43 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
45 import org.eclipse.core.resources.IFile;
46 import org.eclipse.core.resources.IWorkspaceRoot;
47 import org.eclipse.core.resources.ResourcesPlugin;
48 import org.eclipse.core.runtime.CoreException;
49 import org.eclipse.core.runtime.IPath;
50 import org.eclipse.core.runtime.IProgressMonitor;
51 import org.eclipse.core.runtime.IStatus;
52 import org.eclipse.core.runtime.Preferences;
53 import org.eclipse.jface.action.Action;
54 import org.eclipse.jface.action.IAction;
55 import org.eclipse.jface.action.IMenuManager;
56 import org.eclipse.jface.dialogs.ErrorDialog;
57 import org.eclipse.jface.dialogs.IMessageProvider;
58 import org.eclipse.jface.dialogs.MessageDialog;
59 import org.eclipse.jface.preference.IPreferenceStore;
60 import org.eclipse.jface.preference.PreferenceConverter;
61 import org.eclipse.jface.text.BadLocationException;
62 import org.eclipse.jface.text.DocumentCommand;
63 import org.eclipse.jface.text.IAutoEditStrategy;
64 import org.eclipse.jface.text.IDocument;
65 import org.eclipse.jface.text.ILineTracker;
66 import org.eclipse.jface.text.IRegion;
67 import org.eclipse.jface.text.ITextOperationTarget;
68 import org.eclipse.jface.text.ITextViewerExtension;
69 import org.eclipse.jface.text.ITypedRegion;
70 import org.eclipse.jface.text.IWidgetTokenKeeper;
71 import org.eclipse.jface.text.contentassist.ContentAssistant;
72 import org.eclipse.jface.text.contentassist.IContentAssistant;
73 import org.eclipse.jface.text.formatter.FormattingContextProperties;
74 import org.eclipse.jface.text.formatter.IFormattingContext;
75 import org.eclipse.jface.text.source.IOverviewRuler;
76 import org.eclipse.jface.text.source.ISourceViewer;
77 import org.eclipse.jface.text.source.IVerticalRuler;
78 import org.eclipse.jface.text.source.SourceViewerConfiguration;
79 import org.eclipse.jface.util.ListenerList;
80 import org.eclipse.jface.util.PropertyChangeEvent;
81 import org.eclipse.jface.window.Window;
82 import org.eclipse.swt.SWT;
83 import org.eclipse.swt.custom.VerifyKeyListener;
84 import org.eclipse.swt.events.VerifyEvent;
85 import org.eclipse.swt.graphics.Color;
86 import org.eclipse.swt.graphics.Point;
87 import org.eclipse.swt.graphics.RGB;
88 import org.eclipse.swt.widgets.Composite;
89 import org.eclipse.swt.widgets.Display;
90 import org.eclipse.swt.widgets.Shell;
91 import org.eclipse.ui.IEditorInput;
92 import org.eclipse.ui.IEditorPart;
93 import org.eclipse.ui.IFileEditorInput;
94 import org.eclipse.ui.IWorkbenchPage;
95 import org.eclipse.ui.IWorkbenchWindow;
96 import org.eclipse.ui.PlatformUI;
97 import org.eclipse.ui.actions.ActionContext;
98 import org.eclipse.ui.actions.ActionGroup;
99 import org.eclipse.ui.dialogs.SaveAsDialog;
100 import org.eclipse.ui.editors.text.IStorageDocumentProvider;
101 import org.eclipse.ui.part.FileEditorInput;
102 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
103 import org.eclipse.ui.texteditor.ContentAssistAction;
104 import org.eclipse.ui.texteditor.IDocumentProvider;
105 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
106 import org.eclipse.ui.texteditor.TextOperationAction;
108 /*******************************************************************************
109 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
110 * program and the accompanying materials are made available under the terms of
111 * the Common Public License v1.0 which accompanies this distribution, and is
112 * available at http://www.eclipse.org/legal/cpl-v10.html
114 * Contributors: IBM Corporation - Initial implementation
116 ******************************************************************************/
118 * PHP specific text editor.
120 public class PHPUnitEditor extends PHPEditor { // implements
121 // IJavaReconcilingListener {
122 interface ITextConverter {
123 void customizeDocumentCommand(IDocument document,
124 DocumentCommand command);
127 // class AdaptedSourceViewer extends JavaSourceViewer {
128 // private List fTextConverters;
130 // private boolean fIgnoreTextConverters = false;
132 // // private JavaCorrectionAssistant fCorrectionAssistant;
133 // public AdaptedSourceViewer(Composite parent, IVerticalRuler
135 // IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
136 // int styles, IPreferenceStore store) {
137 // super(parent, verticalRuler, overviewRuler, showAnnotationsOverview,
141 // // public AdaptedSourceViewer(Composite parent,
142 // // IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
143 // // boolean showAnnotationsOverview, int styles) {
144 // // super(parent, verticalRuler, overviewRuler,
145 // // showAnnotationsOverview, styles);
147 // public IContentAssistant getContentAssistant() {
148 // return fContentAssistant;
152 // * @see ITextOperationTarget#doOperation(int)
154 // public void doOperation(int operation) {
155 // if (getTextWidget() == null)
157 // switch (operation) {
158 // case CONTENTASSIST_PROPOSALS:
159 // String msg = fContentAssistant.showPossibleCompletions();
160 // setStatusLineErrorMessage(msg);
162 // // case CORRECTIONASSIST_PROPOSALS:
163 // // fCorrectionAssistant.showPossibleCompletions();
166 // fIgnoreTextConverters = true;
169 // fIgnoreTextConverters = true;
172 // super.doOperation(operation);
176 // * @see ITextOperationTarget#canDoOperation(int)
178 // public boolean canDoOperation(int operation) {
179 // // if (operation == CORRECTIONASSIST_PROPOSALS)
180 // // return isEditable();
181 // return super.canDoOperation(operation);
185 // * @see TextViewer#handleDispose()
187 // protected void handleDispose() {
188 // // if (fCorrectionAssistant != null) {
189 // // fCorrectionAssistant.uninstall();
190 // // fCorrectionAssistant= null;
192 // super.handleDispose();
195 // public void insertTextConverter(ITextConverter textConverter, int index)
197 // throw new UnsupportedOperationException();
200 // public void addTextConverter(ITextConverter textConverter) {
201 // if (fTextConverters == null) {
202 // fTextConverters = new ArrayList(1);
203 // fTextConverters.add(textConverter);
204 // } else if (!fTextConverters.contains(textConverter))
205 // fTextConverters.add(textConverter);
208 // public void removeTextConverter(ITextConverter textConverter) {
209 // if (fTextConverters != null) {
210 // fTextConverters.remove(textConverter);
211 // if (fTextConverters.size() == 0)
212 // fTextConverters = null;
217 // * @see TextViewer#customizeDocumentCommand(DocumentCommand)
219 // protected void customizeDocumentCommand(DocumentCommand command) {
220 // super.customizeDocumentCommand(command);
221 // if (!fIgnoreTextConverters && fTextConverters != null) {
222 // for (Iterator e = fTextConverters.iterator(); e.hasNext();)
223 // ((ITextConverter) e.next()).customizeDocumentCommand(getDocument(),
226 // fIgnoreTextConverters = false;
229 // // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
230 // public void updateIndentationPrefixes() {
231 // SourceViewerConfiguration configuration = getSourceViewerConfiguration();
232 // String[] types = configuration.getConfiguredContentTypes(this);
233 // for (int i = 0; i < types.length; i++) {
234 // String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
235 // if (prefixes != null && prefixes.length > 0)
236 // setIndentPrefixes(prefixes, types[i]);
241 // * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
243 // public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
244 // if (WorkbenchHelp.isContextHelpDisplayed())
246 // return super.requestWidgetToken(requester);
251 // org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
253 // // public void configure(SourceViewerConfiguration configuration) {
254 // // super.configure(configuration);
255 // // // fCorrectionAssistant= new
256 // // // JavaCorrectionAssistant(CompilationUnitEditor.this);
257 // // // fCorrectionAssistant.install(this);
258 // // //TODO install SmartBracesAutoEditStrategy
259 // // // prependAutoEditStrategy(new SmartBracesAutoEditStrategy(this),
260 // // // IDocument.DEFAULT_CONTENT_TYPE);
262 // public void configure(SourceViewerConfiguration configuration) {
263 // super.configure(configuration);
264 // // fCorrectionAssistant= new
265 // JavaCorrectionAssistant(CompilationUnitEditor.this);
266 // // fCorrectionAssistant.install(this);
267 // IAutoEditStrategy smartSemi= new
268 // SmartSemicolonAutoEditStrategy(IPHPPartitions.PHP_PARTITIONING);
269 // prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
270 // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
271 // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
272 // // prependAutoEditStrategy(smartSemi, IPHPPartitions.JAVA_CHARACTER);
275 class AdaptedSourceViewer extends JavaSourceViewer {
277 private List fTextConverters;
279 private boolean fIgnoreTextConverters = false;
281 // private JavaCorrectionAssistant fCorrectionAssistant;
283 public AdaptedSourceViewer(Composite parent,
284 IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
285 boolean showAnnotationsOverview, int styles,
286 IPreferenceStore store) {
287 super(parent, verticalRuler, overviewRuler,
288 showAnnotationsOverview, styles, store);
291 public IContentAssistant getContentAssistant() {
292 return fContentAssistant;
296 * @see ITextOperationTarget#doOperation(int)
298 public void doOperation(int operation) {
300 if (getTextWidget() == null)
304 case CONTENTASSIST_PROPOSALS:
305 String msg = fContentAssistant.showPossibleCompletions();
306 setStatusLineErrorMessage(msg);
308 // case CORRECTIONASSIST_PROPOSALS:
309 // msg = fCorrectionAssistant.showPossibleCompletions();
310 // setStatusLineErrorMessage(msg);
313 fIgnoreTextConverters = true;
314 super.doOperation(operation);
315 fIgnoreTextConverters = false;
318 fIgnoreTextConverters = true;
319 super.doOperation(operation);
320 fIgnoreTextConverters = false;
324 super.doOperation(operation);
328 * @see ITextOperationTarget#canDoOperation(int)
330 public boolean canDoOperation(int operation) {
331 // if (operation == CORRECTIONASSIST_PROPOSALS)
332 // return isEditable();
334 return super.canDoOperation(operation);
338 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
341 public void unconfigure() {
342 // if (fCorrectionAssistant != null) {
343 // fCorrectionAssistant.uninstall();
344 // fCorrectionAssistant = null;
349 public void insertTextConverter(ITextConverter textConverter, int index) {
350 throw new UnsupportedOperationException();
353 public void addTextConverter(ITextConverter textConverter) {
354 if (fTextConverters == null) {
355 fTextConverters = new ArrayList(1);
356 fTextConverters.add(textConverter);
357 } else if (!fTextConverters.contains(textConverter))
358 fTextConverters.add(textConverter);
361 public void removeTextConverter(ITextConverter textConverter) {
362 if (fTextConverters != null) {
363 fTextConverters.remove(textConverter);
364 if (fTextConverters.size() == 0)
365 fTextConverters = null;
370 * @see TextViewer#customizeDocumentCommand(DocumentCommand)
372 protected void customizeDocumentCommand(DocumentCommand command) {
373 super.customizeDocumentCommand(command);
374 if (!fIgnoreTextConverters && fTextConverters != null) {
375 for (Iterator e = fTextConverters.iterator(); e.hasNext();)
376 ((ITextConverter) e.next()).customizeDocumentCommand(
377 getDocument(), command);
381 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
382 public void updateIndentationPrefixes() {
383 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
384 String[] types = configuration.getConfiguredContentTypes(this);
385 for (int i = 0; i < types.length; i++) {
386 String[] prefixes = configuration.getIndentPrefixes(this,
388 if (prefixes != null && prefixes.length > 0)
389 setIndentPrefixes(prefixes, types[i]);
394 * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
396 public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
397 if (PlatformUI.getWorkbench().getHelpSystem()
398 .isContextHelpDisplayed())
400 return super.requestWidgetToken(requester);
404 * @see IWidgetTokenOwnerExtension#requestWidgetToken(IWidgetTokenKeeper,
408 public boolean requestWidgetToken(IWidgetTokenKeeper requester,
410 if (PlatformUI.getWorkbench().getHelpSystem()
411 .isContextHelpDisplayed())
413 return super.requestWidgetToken(requester, priority);
417 * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
419 public void configure(SourceViewerConfiguration configuration) {
420 super.configure(configuration);
421 // fCorrectionAssistant = new
422 // JavaCorrectionAssistant(CompilationUnitEditor.this);
423 // fCorrectionAssistant.install(this);
424 IAutoEditStrategy smartSemi = new SmartSemicolonAutoEditStrategy(
425 IPHPPartitions.PHP_PARTITIONING);
426 prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
427 prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
428 prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
429 prependAutoEditStrategy(smartSemi,
430 IPHPPartitions.PHP_STRING_HEREDOC);
434 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
437 public IFormattingContext createFormattingContext() {
438 IFormattingContext context = new CommentFormattingContext();
441 IJavaElement inputJavaElement = getInputJavaElement();
442 IJavaProject javaProject = inputJavaElement != null ? inputJavaElement
445 if (javaProject == null)
446 preferences = new HashMap(JavaCore.getOptions());
448 preferences = new HashMap(javaProject.getOptions(true));
450 context.storeToMap(PreferenceConstants.getPreferenceStore(),
453 FormattingContextProperties.CONTEXT_PREFERENCES,
461 * Remembers data related to the current selection to be able to restore it
466 private class RememberedSelection {
467 /** The remembered selection start. */
468 private RememberedOffset fStartOffset = new RememberedOffset();
470 /** The remembered selection end. */
471 private RememberedOffset fEndOffset = new RememberedOffset();
474 * Remember current selection.
476 public void remember() {
478 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method
479 * may be called inside an async call posted to the UI thread, so
480 * protect against intermediate disposal of the editor.
482 ISourceViewer viewer = getSourceViewer();
483 if (viewer != null) {
484 IRegion selection = getSignedSelection(viewer);
485 int startOffset = selection.getOffset();
486 int endOffset = startOffset + selection.getLength();
488 fStartOffset.setOffset(startOffset);
489 fEndOffset.setOffset(endOffset);
494 * Restore remembered selection.
496 public void restore() {
498 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method
499 * may be called inside an async call posted to the UI thread, so
500 * protect against intermediate disposal of the editor.
502 if (getSourceViewer() == null)
507 int startOffset, endOffset;
508 int revealStartOffset, revealEndOffset;
509 if (showsHighlightRangeOnly()) {
510 IJavaElement newStartElement = fStartOffset.getElement();
511 startOffset = fStartOffset
512 .getRememberedOffset(newStartElement);
513 revealStartOffset = fStartOffset.getRevealOffset(
514 newStartElement, startOffset);
515 if (revealStartOffset == -1)
518 IJavaElement newEndElement = fEndOffset.getElement();
519 endOffset = fEndOffset.getRememberedOffset(newEndElement);
520 revealEndOffset = fEndOffset.getRevealOffset(newEndElement,
522 if (revealEndOffset == -1)
525 startOffset = fStartOffset.getOffset();
526 revealStartOffset = startOffset;
527 endOffset = fEndOffset.getOffset();
528 revealEndOffset = endOffset;
531 if (startOffset == -1) {
532 startOffset = endOffset; // fallback to caret offset
533 revealStartOffset = revealEndOffset;
536 if (endOffset == -1) {
537 endOffset = startOffset; // fallback to other offset
538 revealEndOffset = revealStartOffset;
541 IJavaElement element;
542 if (endOffset == -1) {
543 // fallback to element selection
544 element = fEndOffset.getElement();
546 element = fStartOffset.getElement();
548 setSelection(element);
552 if (isValidSelection(revealStartOffset, revealEndOffset
554 && isValidSelection(startOffset, endOffset
556 selectAndReveal(startOffset, endOffset - startOffset,
557 revealStartOffset, revealEndOffset
558 - revealStartOffset);
560 fStartOffset.clear();
565 private boolean isValidSelection(int offset, int length) {
566 IDocumentProvider provider = getDocumentProvider();
567 if (provider != null) {
568 IDocument document = provider.getDocument(getEditorInput());
569 if (document != null) {
570 int end = offset + length;
571 int documentLength = document.getLength();
572 return 0 <= offset && offset <= documentLength && 0 <= end
573 && end <= documentLength;
582 * Remembers additional data for a given offset to be able restore it later.
586 private class RememberedOffset {
587 /** Remembered line for the given offset */
590 /** Remembered column for the given offset */
593 /** Remembered Java element for the given offset */
594 private IJavaElement fElement;
596 /** Remembered Java element line for the given offset */
597 private int fElementLine;
600 * Store visual properties of the given offset.
603 * Offset in the document
605 public void setOffset(int offset) {
607 IDocument document = getSourceViewer().getDocument();
608 fLine = document.getLineOfOffset(offset);
609 fColumn = offset - document.getLineOffset(fLine);
610 fElement = getElementAt(offset, true);
613 if (fElement instanceof IMember) {
614 ISourceRange range = ((IMember) fElement).getNameRange();
616 fElementLine = document.getLineOfOffset(range
619 if (fElementLine == -1)
620 fElementLine = document
621 .getLineOfOffset(getOffset(fElement));
622 } catch (BadLocationException e) {
624 PHPeclipsePlugin.log(e);
626 } catch (JavaModelException e) {
628 PHPeclipsePlugin.log(e.getStatus());
634 * Return offset recomputed from stored visual properties.
636 * @return Offset in the document
638 public int getOffset() {
639 IJavaElement newElement = getElement();
641 int offset = getRememberedOffset(newElement);
643 if (offset != -1 && !containsOffset(newElement, offset)
644 && (offset == 0 || !containsOffset(newElement, offset - 1)))
651 * Return offset recomputed from stored visual properties.
655 * @return Offset in the document
657 public int getRememberedOffset(IJavaElement newElement) {
659 if (newElement == null)
662 IDocument document = getSourceViewer().getDocument();
663 int newElementLine = -1;
664 if (newElement instanceof IMember) {
665 ISourceRange range = ((IMember) newElement).getNameRange();
667 newElementLine = document.getLineOfOffset(range
670 if (newElementLine == -1)
671 newElementLine = document
672 .getLineOfOffset(getOffset(newElement));
673 if (newElementLine == -1)
676 int newLine = fLine + newElementLine - fElementLine;
677 if (newLine < 0 || newLine >= document.getNumberOfLines())
679 int maxColumn = document.getLineLength(newLine);
680 String lineDelimiter = document.getLineDelimiter(newLine);
681 if (lineDelimiter != null)
682 maxColumn = maxColumn - lineDelimiter.length();
684 if (fColumn > maxColumn)
685 offset = document.getLineOffset(newLine) + maxColumn;
687 offset = document.getLineOffset(newLine) + fColumn;
690 } catch (BadLocationException e) {
692 PHPeclipsePlugin.log(e);
694 } catch (JavaModelException e) {
696 PHPeclipsePlugin.log(e.getStatus());
702 * Returns the offset used to reveal the given element based on the
703 * given selection offset.
708 * the selection offset
709 * @return the offset to reveal the given element based on the given
712 public int getRevealOffset(IJavaElement element, int offset) {
713 if (element == null || offset == -1)
716 if (containsOffset(element, offset)) {
718 IJavaElement alternateElement = getElementAt(offset, false);
719 if (element.getHandleIdentifier().equals(
720 alternateElement.getParent().getHandleIdentifier()))
721 return offset - 1; // Solves test case 2 from
722 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
725 } else if (offset > 0 && containsOffset(element, offset - 1))
726 return offset - 1; // Solves test case 1 from
727 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
733 * Return Java element recomputed from stored visual properties.
735 * @return Java element
737 public IJavaElement getElement() {
738 if (fElement == null)
741 return findElement(fElement);
745 * Clears the stored position
747 public void clear() {
755 * Does the given Java element contain the given offset?
761 * @return <code>true</code> iff the Java element contains the offset
763 private boolean containsOffset(IJavaElement element, int offset) {
764 int elementOffset = getOffset(element);
765 int elementLength = getLength(element);
766 return (elementOffset > -1 && elementLength > -1) ? (offset >= elementOffset && offset < elementOffset
772 * Returns the offset of the given Java element.
776 * @return Offset of the given Java element
778 private int getOffset(IJavaElement element) {
779 if (element instanceof ISourceReference) {
780 ISourceReference sr = (ISourceReference) element;
782 ISourceRange srcRange = sr.getSourceRange();
783 if (srcRange != null)
784 return srcRange.getOffset();
785 } catch (JavaModelException e) {
792 * Returns the length of the given Java element.
796 * @return Length of the given Java element
798 private int getLength(IJavaElement element) {
799 if (element instanceof ISourceReference) {
800 ISourceReference sr = (ISourceReference) element;
802 ISourceRange srcRange = sr.getSourceRange();
803 if (srcRange != null)
804 return srcRange.getLength();
805 } catch (JavaModelException e) {
812 * Returns the updated java element for the old java element.
816 * @return Updated Java element
818 private IJavaElement findElement(IJavaElement element) {
823 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
824 .getWorkingCopyManager();
825 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
830 synchronized (unit) {
831 // unit.reconcile(ICompilationUnit.NO_AST, false, null,
835 IJavaElement[] findings = unit.findElements(element);
836 if (findings != null && findings.length > 0)
839 } catch (JavaModelException x) {
840 PHPeclipsePlugin.log(x.getStatus());
841 // nothing found, be tolerant and go on
850 static class TabConverter implements ITextConverter {
851 private int fTabRatio;
853 private ILineTracker fLineTracker;
855 public TabConverter() {
858 public void setNumberOfSpacesPerTab(int ratio) {
862 public void setLineTracker(ILineTracker lineTracker) {
863 fLineTracker = lineTracker;
866 private int insertTabString(StringBuffer buffer, int offsetInLine) {
869 int remainder = offsetInLine % fTabRatio;
870 remainder = fTabRatio - remainder;
871 for (int i = 0; i < remainder; i++)
876 public void customizeDocumentCommand(IDocument document,
877 DocumentCommand command) {
878 String text = command.text;
881 int index = text.indexOf('\t');
883 StringBuffer buffer = new StringBuffer();
884 fLineTracker.set(command.text);
885 int lines = fLineTracker.getNumberOfLines();
887 for (int i = 0; i < lines; i++) {
888 int offset = fLineTracker.getLineOffset(i);
889 int endOffset = offset + fLineTracker.getLineLength(i);
890 String line = text.substring(offset, endOffset);
893 IRegion firstLine = document
894 .getLineInformationOfOffset(command.offset);
895 position = command.offset - firstLine.getOffset();
897 int length = line.length();
898 for (int j = 0; j < length; j++) {
899 char c = line.charAt(j);
901 position += insertTabString(buffer, position);
908 command.text = buffer.toString();
909 } catch (BadLocationException x) {
915 private static class ExitPolicy implements LinkedPositionUI.ExitPolicy {
916 final char fExitCharacter;
918 public ExitPolicy(char exitCharacter) {
919 fExitCharacter = exitCharacter;
923 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitPolicy#doExit(org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionManager,
924 * org.eclipse.swt.events.VerifyEvent, int, int)
926 public ExitFlags doExit(LinkedPositionManager manager,
927 VerifyEvent event, int offset, int length) {
928 if (event.character == fExitCharacter) {
929 if (manager.anyPositionIncludes(offset, length))
930 return new ExitFlags(LinkedPositionUI.COMMIT
931 | LinkedPositionUI.UPDATE_CARET, false);
933 return new ExitFlags(LinkedPositionUI.COMMIT, true);
935 // Fix for #1380415 (toshihiro) start
936 switch (event.keyCode) {
939 return new ExitFlags(LinkedPositionUI.COMMIT, true);
941 case SWT.ARROW_RIGHT:
942 if (!manager.anyPositionIncludes(offset, length))
943 return new ExitFlags(LinkedPositionUI.COMMIT, true);
947 switch (event.character) {
949 if (manager.getFirstPosition().length == 0)
950 return new ExitFlags(0, false);
955 return new ExitFlags(LinkedPositionUI.COMMIT, true);
962 private static class BracketLevel {
967 LinkedPositionManager fManager;
969 LinkedPositionUI fEditor;
972 private class BracketInserter implements VerifyKeyListener,
973 LinkedPositionUI.ExitListener {
974 private boolean fCloseBracketsPHP = true;
976 private boolean fCloseStringsPHPDQ = true;
978 private boolean fCloseStringsPHPSQ = true;
984 public void setCloseBracketsPHPEnabled(boolean enabled) {
985 fCloseBracketsPHP = enabled;
988 public void setCloseStringsPHPDQEnabled(boolean enabled) {
989 fCloseStringsPHPDQ = enabled;
992 public void setCloseStringsPHPSQEnabled(boolean enabled) {
993 fCloseStringsPHPSQ = enabled;
996 private boolean hasIdentifierToTheRight(IDocument document, int offset) {
999 IRegion endLine = document.getLineInformationOfOffset(end);
1000 int maxEnd = endLine.getOffset() + endLine.getLength();
1001 while (end != maxEnd
1002 && Character.isWhitespace(document.getChar(end)))
1004 return end != maxEnd
1005 && Scanner.isPHPIdentifierPart(document.getChar(end));
1006 } catch (BadLocationException e) {
1012 private boolean hasIdentifierToTheLeft(IDocument document, int offset) {
1015 IRegion startLine = document.getLineInformationOfOffset(start);
1016 int minStart = startLine.getOffset();
1017 while (start != minStart
1018 && Character.isWhitespace(document.getChar(start - 1)))
1020 return start != minStart
1021 && Scanner.isPHPIdentifierPart(document
1022 .getChar(start - 1));
1023 } catch (BadLocationException e) {
1028 private boolean hasCharacterToTheLeft(IDocument document, int offset,
1032 IRegion startLine = document.getLineInformationOfOffset(start);
1033 int minStart = startLine.getOffset();
1034 while (start != minStart
1035 && Character.isWhitespace(document.getChar(start - 1)))
1037 return start != minStart
1038 && document.getChar(start - 1) == character;
1039 } catch (BadLocationException e) {
1044 private boolean hasCharacterToTheRight(IDocument document, int offset,
1048 IRegion endLine = document.getLineInformationOfOffset(end);
1049 int maxEnd = endLine.getOffset() + endLine.getLength();
1050 while (end != maxEnd
1051 && Character.isWhitespace(document.getChar(end)))
1053 return end != maxEnd && document.getChar(end) == character;
1054 } catch (BadLocationException e) {
1061 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
1063 public void verifyKey(VerifyEvent event) {
1066 final ISourceViewer sourceViewer = getSourceViewer();
1067 IDocument document = sourceViewer.getDocument();
1068 final Point selection = sourceViewer.getSelectedRange();
1069 final int offset = selection.x;
1070 final int length = selection.y;
1072 ITypedRegion partition = document.getPartition(offset);
1073 String type = partition.getType();
1074 if (type.equals(IPHPPartitions.PHP_PARTITIONING)
1075 || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
1076 // you will get IDocument.DEFAULT_CONTENT_TYPE for both PHP
1078 switch (event.character) {
1080 if (hasCharacterToTheRight(document, offset + length,
1085 if (!fCloseBracketsPHP)
1087 if (hasIdentifierToTheRight(document, offset + length))
1091 if (!fCloseBracketsPHP)
1093 if (hasIdentifierToTheRight(document, offset + length))
1097 if (event.character == '"') {
1098 if (!fCloseStringsPHPDQ)
1100 // changed for statements like echo "" print ""
1101 // if (hasIdentifierToTheLeft(document, offset)
1103 // hasIdentifierToTheRight(document, offset +
1105 if (hasIdentifierToTheRight(document, offset
1109 // ITypedRegion partition=
1110 // document.getPartition(offset);
1112 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1114 // (partition.getOffset() != offset))
1116 final char characterDQ = event.character;
1117 final char closingCharacterDQ = getPeerCharacter(characterDQ);
1118 final StringBuffer bufferDQ = new StringBuffer();
1119 bufferDQ.append(characterDQ);
1120 bufferDQ.append(closingCharacterDQ);
1121 document.replace(offset, length, bufferDQ.toString());
1122 LinkedPositionManager managerDQ = new LinkedPositionManager(
1124 managerDQ.addPosition(offset + 1, 0);
1127 LinkedPositionUI editorDQ = new LinkedPositionUI(
1128 sourceViewer, managerDQ);
1129 editorDQ.setCancelListener(this);
1130 editorDQ.setExitPolicy(new ExitPolicy(
1131 closingCharacterDQ));
1132 editorDQ.setFinalCaretOffset(offset + 2);
1134 IRegion newSelectionDQ = editorDQ.getSelectedRegion();
1135 sourceViewer.setSelectedRange(newSelectionDQ
1136 .getOffset(), newSelectionDQ.getLength());
1140 if (event.character == '\'') {
1141 if (!fCloseStringsPHPSQ)
1143 // changed for statements like echo "" print ""
1144 // if (hasIdentifierToTheLeft(document, offset)
1146 // hasIdentifierToTheRight(document, offset +
1148 if (hasIdentifierToTheRight(document, offset
1152 // ITypedRegion partition=
1153 // document.getPartition(offset);
1155 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1157 // (partition.getOffset() != offset))
1159 final char characterSQ = event.character;
1160 final char closingCharacterSQ = getPeerCharacter(characterSQ);
1161 final StringBuffer bufferSQ = new StringBuffer();
1162 bufferSQ.append(characterSQ);
1163 bufferSQ.append(closingCharacterSQ);
1164 document.replace(offset, length, bufferSQ.toString());
1165 LinkedPositionManager managerSQ = new LinkedPositionManager(
1167 managerSQ.addPosition(offset + 1, 0);
1170 LinkedPositionUI editorSQ = new LinkedPositionUI(
1171 sourceViewer, managerSQ);
1172 editorSQ.setCancelListener(this);
1173 editorSQ.setExitPolicy(new ExitPolicy(
1174 closingCharacterSQ));
1175 editorSQ.setFinalCaretOffset(offset + 2);
1177 IRegion newSelectionSQ = editorSQ.getSelectedRegion();
1178 sourceViewer.setSelectedRange(newSelectionSQ
1179 .getOffset(), newSelectionSQ.getLength());
1181 case '\r': { // insert linebreaks and new closing brace
1182 // after brace and return
1183 if (!fCloseBracketsPHP) {
1186 if (hasCharacterToTheLeft(document, offset, '{')
1187 && hasCharacterToTheRight(document, offset, '}')) {
1188 String lineDelimiter = StubUtility
1189 .getLineDelimiterFor(document);
1190 int caretPos = sourceViewer.getTextWidget()
1192 final StringBuffer buffer = new StringBuffer(
1195 IRegion line = document
1196 .getLineInformationOfOffset(offset);
1197 String currentLine = document.get(line.getOffset(),
1200 int max = currentLine.length();
1201 StringBuffer indent = new StringBuffer();
1203 && Character.isWhitespace(currentLine
1205 indent.append(currentLine.charAt(index));
1208 buffer.append(indent);
1209 JavaHeuristicScanner scanner = new JavaHeuristicScanner(
1211 JavaIndenter indenter = new JavaIndenter(document,
1213 buffer.append(indenter.createIndent(1));
1214 int cursorPos = buffer.length();
1215 buffer.append(lineDelimiter);
1216 buffer.append(indent);
1217 document.replace(offset, length, buffer.toString());
1218 sourceViewer.getTextWidget().setCaretOffset(
1219 caretPos + cursorPos);
1225 } catch (BadLocationException e) {
1230 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitListener#exit(boolean)
1232 public void exit(boolean accept) {
1237 final ISourceViewer sourceViewer = getSourceViewer();
1238 IDocument document = sourceViewer.getDocument();
1239 document.replace(fOffset, fLength, null);
1240 } catch (BadLocationException e) {
1245 /** The editor's save policy */
1246 protected ISavePolicy fSavePolicy;
1249 * Listener to annotation model changes that updates the error tick in the
1252 private JavaEditorErrorTickUpdater fJavaEditorErrorTickUpdater;
1254 /** The editor's paint manager */
1255 // private PaintManager fPaintManager;
1256 /** The editor's bracket painter */
1257 // private BracketPainter fBracketPainter;
1258 /** The editor's bracket matcher */
1259 private PHPPairMatcher fBracketMatcher;
1261 /** The editor's line painter */
1262 // private LinePainter fLinePainter;
1263 /** The editor's print margin ruler painter */
1264 // private PrintMarginPainter fPrintMarginPainter;
1265 /** The editor's problem painter */
1266 // private ProblemPainter fProblemPainter;
1267 /** The editor's tab converter */
1268 private TabConverter fTabConverter;
1270 /** History for structure select action */
1271 // private SelectionHistory fSelectionHistory;
1272 /** The preference property change listener for php core. */
1273 // private IPropertyChangeListener fPropertyChangeListener = new
1274 // PropertyChangeListener();
1275 /** The remembered java element */
1276 private IJavaElement fRememberedElement;
1279 * The remembered selection.
1283 private RememberedSelection fRememberedSelection = new RememberedSelection();
1285 /** The remembered php element offset */
1286 private int fRememberedElementOffset;
1288 /** The bracket inserter. */
1289 private BracketInserter fBracketInserter = new BracketInserter();
1291 /** The standard action groups added to the menu */
1292 private GenerateActionGroup fGenerateActionGroup;
1294 private CompositeActionGroup fContextMenuGroup;
1296 // private class PropertyChangeListener implements IPropertyChangeListener {
1298 // * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
1301 // propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
1303 // handlePreferencePropertyChanged(event);
1306 /* Preference key for code formatter tab size */
1307 private final static String CODE_FORMATTER_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
1309 /** Preference key for matching brackets */
1310 // private final static String MATCHING_BRACKETS =
1311 // PreferenceConstants.EDITOR_MATCHING_BRACKETS;
1312 /** Preference key for matching brackets color */
1313 // private final static String MATCHING_BRACKETS_COLOR =
1314 // PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
1315 /** Preference key for highlighting current line */
1316 // private final static String CURRENT_LINE =
1317 // PreferenceConstants.EDITOR_CURRENT_LINE;
1318 /** Preference key for highlight color of current line */
1319 // private final static String CURRENT_LINE_COLOR =
1320 // PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
1321 /** Preference key for showing print marging ruler */
1322 // private final static String PRINT_MARGIN =
1323 // PreferenceConstants.EDITOR_PRINT_MARGIN;
1324 /** Preference key for print margin ruler color */
1325 // private final static String PRINT_MARGIN_COLOR =
1326 // PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
1327 /** Preference key for print margin ruler column */
1328 // private final static String PRINT_MARGIN_COLUMN =
1329 // PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
1330 /** Preference key for inserting spaces rather than tabs */
1331 private final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
1333 /** Preference key for error indication */
1334 // private final static String ERROR_INDICATION =
1335 // PreferenceConstants.EDITOR_PROBLEM_INDICATION;
1336 /** Preference key for error color */
1337 // private final static String ERROR_INDICATION_COLOR =
1338 // PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
1339 /** Preference key for warning indication */
1340 // private final static String WARNING_INDICATION =
1341 // PreferenceConstants.EDITOR_WARNING_INDICATION;
1342 /** Preference key for warning color */
1343 // private final static String WARNING_INDICATION_COLOR =
1344 // PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
1345 /** Preference key for task indication */
1346 private final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
1348 /** Preference key for task color */
1349 private final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
1351 /** Preference key for bookmark indication */
1352 private final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
1354 /** Preference key for bookmark color */
1355 private final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
1357 /** Preference key for search result indication */
1358 private final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
1360 /** Preference key for search result color */
1361 private final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
1363 /** Preference key for unknown annotation indication */
1364 private final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
1366 /** Preference key for unknown annotation color */
1367 private final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
1369 /** Preference key for linked position color */
1370 private final static String LINKED_POSITION_COLOR = PreferenceConstants.EDITOR_LINKED_POSITION_COLOR;
1372 /** Preference key for shwoing the overview ruler */
1373 private final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
1375 /** Preference key for error indication in overview ruler */
1376 private final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
1378 /** Preference key for warning indication in overview ruler */
1379 private final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
1381 /** Preference key for task indication in overview ruler */
1382 private final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
1384 /** Preference key for bookmark indication in overview ruler */
1385 private final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1387 /** Preference key for search result indication in overview ruler */
1388 private final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1390 /** Preference key for unknown annotation indication in overview ruler */
1391 private final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1393 /** Preference key for automatically closing double quoted strings */
1394 private final static String CLOSE_STRINGS_DQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_DQ_PHP;
1396 /** Preference key for automatically closing single quoted strings */
1397 private final static String CLOSE_STRINGS_SQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_SQ_PHP;
1399 /** Preference key for automatically wrapping Java strings */
1400 // private final static String WRAP_STRINGS =
1401 // PreferenceConstants.EDITOR_WRAP_STRINGS_DQ;
1402 /** Preference key for automatically closing brackets and parenthesis */
1403 private final static String CLOSE_BRACKETS_PHP = PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP;
1405 /** Preference key for automatically closing phpdocs and comments */
1406 private final static String CLOSE_JAVADOCS = PreferenceConstants.EDITOR_CLOSE_JAVADOCS;
1408 /** Preference key for automatically adding phpdoc tags */
1409 private final static String ADD_JAVADOC_TAGS = PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS;
1411 /** Preference key for automatically formatting phpdocs */
1412 // private final static String FORMAT_JAVADOCS =
1413 // PreferenceConstants.EDITOR_FORMAT_JAVADOCS;
1414 /** Preference key for automatically closing strings */
1415 private final static String CLOSE_STRINGS_HTML = PreferenceConstants.EDITOR_CLOSE_STRINGS_HTML;
1417 /** Preference key for automatically closing brackets and parenthesis */
1418 private final static String CLOSE_BRACKETS_HTML = PreferenceConstants.EDITOR_CLOSE_BRACKETS_HTML;
1420 /** Preference key for smart paste */
1421 private final static String SMART_PASTE = PreferenceConstants.EDITOR_SMART_PASTE;
1423 // private final static class AnnotationInfo {
1424 // public String fColorPreference;
1425 // public String fOverviewRulerPreference;
1426 // public String fEditorPreference;
1428 // private final static Map ANNOTATION_MAP;
1431 // AnnotationInfo info;
1432 // ANNOTATION_MAP = new HashMap();
1434 // info = new AnnotationInfo();
1435 // info.fColorPreference = TASK_INDICATION_COLOR;
1436 // info.fOverviewRulerPreference = TASK_INDICATION_IN_OVERVIEW_RULER;
1437 // info.fEditorPreference = TASK_INDICATION;
1438 // ANNOTATION_MAP.put(AnnotationType.TASK, info);
1440 // info = new AnnotationInfo();
1441 // info.fColorPreference = ERROR_INDICATION_COLOR;
1442 // info.fOverviewRulerPreference = ERROR_INDICATION_IN_OVERVIEW_RULER;
1443 // info.fEditorPreference = ERROR_INDICATION;
1444 // ANNOTATION_MAP.put(AnnotationType.ERROR, info);
1446 // info = new AnnotationInfo();
1447 // info.fColorPreference = WARNING_INDICATION_COLOR;
1448 // info.fOverviewRulerPreference = WARNING_INDICATION_IN_OVERVIEW_RULER;
1449 // info.fEditorPreference = WARNING_INDICATION;
1450 // ANNOTATION_MAP.put(AnnotationType.WARNING, info);
1452 // info = new AnnotationInfo();
1453 // info.fColorPreference = BOOKMARK_INDICATION_COLOR;
1454 // info.fOverviewRulerPreference = BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1455 // info.fEditorPreference = BOOKMARK_INDICATION;
1456 // ANNOTATION_MAP.put(AnnotationType.BOOKMARK, info);
1458 // info = new AnnotationInfo();
1459 // info.fColorPreference = SEARCH_RESULT_INDICATION_COLOR;
1460 // info.fOverviewRulerPreference =
1461 // SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1462 // info.fEditorPreference = SEARCH_RESULT_INDICATION;
1463 // ANNOTATION_MAP.put(AnnotationType.SEARCH, info);
1465 // info = new AnnotationInfo();
1466 // info.fColorPreference = UNKNOWN_INDICATION_COLOR;
1467 // info.fOverviewRulerPreference = UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1468 // info.fEditorPreference = UNKNOWN_INDICATION;
1469 // ANNOTATION_MAP.put(AnnotationType.UNKNOWN, info);
1472 // private final static AnnotationType[] ANNOTATION_LAYERS =
1473 // new AnnotationType[] {
1474 // AnnotationType.UNKNOWN,
1475 // AnnotationType.BOOKMARK,
1476 // AnnotationType.TASK,
1477 // AnnotationType.SEARCH,
1478 // AnnotationType.WARNING,
1479 // AnnotationType.ERROR };
1481 * Creates a new php unit editor.
1485 * Reconciling listeners.
1489 private ListenerList fReconcilingListeners = new ListenerList();
1492 * Mutex for the reconciler. See
1493 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of
1496 * TODO remove once the underlying problem is solved.
1499 private final Object fReconcilerLock = new Object();
1501 public PHPUnitEditor() {
1503 setDocumentProvider(PHPeclipsePlugin.getDefault()
1504 .getCompilationUnitDocumentProvider());
1505 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
1506 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
1507 setOutlinerContextMenuId("#PHPOutlinerContext"); //$NON-NLS-1$
1508 // don't set help contextId, we install our own help context
1510 fJavaEditorErrorTickUpdater = new JavaEditorErrorTickUpdater(this);
1514 * @see AbstractTextEditor#createActions()
1516 protected void createActions() {
1517 super.createActions();
1519 // Action action= new
1520 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1521 // "CorrectionAssistProposal.", this, CORRECTIONASSIST_PROPOSALS);
1523 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CORRECTION_ASSIST_PROPOSALS);
1524 // setAction("CorrectionAssistProposal", action); //$NON-NLS-1$
1525 // markAsStateDependentAction("CorrectionAssistProposal", true);
1527 // // WorkbenchHelp.setHelp(action,
1528 // IJavaHelpContextIds.QUICK_FIX_ACTION);
1529 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(),
1530 "ContentAssistProposal.", this); //$NON-NLS-1$
1532 .setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
1533 setAction("ContentAssistProposal", action); //$NON-NLS-1$
1534 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
1535 // WorkbenchHelp.setHelp(action,
1536 // IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
1538 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1539 // "ContentAssistContextInformation.", this,
1540 // ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION); //$NON-NLS-1$
1542 // .setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
1543 // setAction("ContentAssistContextInformation", action); //$NON-NLS-1$
1544 // markAsStateDependentAction("ContentAssistContextInformation", true);
1546 // WorkbenchHelp.setHelp(action,
1547 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1549 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1550 // "ContentAssistCompletePrefix.", this, CONTENTASSIST_COMPLETE_PREFIX);
1552 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_COMPLETE_PREFIX);
1553 // setAction("ContentAssistCompletePrefix", action); //$NON-NLS-1$
1554 // markAsStateDependentAction("ContentAssistCompletePrefix", true);
1556 // // WorkbenchHelp.setHelp(action,
1557 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1558 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1559 "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
1560 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
1561 setAction("Comment", action); //$NON-NLS-1$
1562 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
1563 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
1564 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1565 "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
1566 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
1567 setAction("Uncomment", action); //$NON-NLS-1$
1568 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
1569 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
1571 action = new ToggleCommentAction(PHPEditorMessages.getResourceBundle(),
1572 "ToggleComment.", this); //$NON-NLS-1$
1574 .setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_COMMENT);
1575 setAction("ToggleComment", action); //$NON-NLS-1$
1576 markAsStateDependentAction("ToggleComment", true); //$NON-NLS-1$
1577 // WorkbenchHelp.setHelp(action,
1578 // IJavaHelpContextIds.TOGGLE_COMMENT_ACTION);
1579 configureToggleCommentAction();
1581 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1582 "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
1583 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
1584 setAction("Format", action); //$NON-NLS-1$
1585 markAsStateDependentAction("Format", true); //$NON-NLS-1$
1586 markAsSelectionDependentAction("Format", true); //$NON-NLS-1$
1587 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
1590 // AddBlockCommentAction(PHPEditorMessages.getResourceBundle(),
1591 // "AddBlockComment.", this); //$NON-NLS-1$
1593 // .setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1594 // setAction("AddBlockComment", action); //$NON-NLS-1$
1595 // markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1596 // markAsSelectionDependentAction("AddBlockComment", true);
1598 // // WorkbenchHelp.setHelp(action,
1599 // // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1600 // action = new RemoveBlockCommentAction(
1601 // PHPEditorMessages.getResourceBundle(), "RemoveBlockComment.", this);
1604 // .setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1605 // setAction("RemoveBlockComment", action); //$NON-NLS-1$
1606 // markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1607 // markAsSelectionDependentAction("RemoveBlockComment", true);
1609 // WorkbenchHelp.setHelp(action,
1610 // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1611 action = new IndentAction(PHPEditorMessages.getResourceBundle(),
1612 "Indent.", this, false); //$NON-NLS-1$
1613 action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1614 setAction("Indent", action); //$NON-NLS-1$
1615 markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1616 markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1617 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1619 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1620 // "Indent.", this, true); //$NON-NLS-1$
1621 // setAction("IndentOnTab", action); //$NON-NLS-1$
1622 // markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1623 // markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1626 action = new AddBlockCommentAction(PHPEditorMessages
1627 .getResourceBundle(), "AddBlockComment.", this); //$NON-NLS-1$
1629 .setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1630 setAction("AddBlockComment", action); //$NON-NLS-1$
1631 markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1632 markAsSelectionDependentAction("AddBlockComment", true); //$NON-NLS-1$
1633 // WorkbenchHelp.setHelp(action,
1634 // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1636 action = new RemoveBlockCommentAction(PHPEditorMessages
1637 .getResourceBundle(), "RemoveBlockComment.", this); //$NON-NLS-1$
1639 .setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1640 setAction("RemoveBlockComment", action); //$NON-NLS-1$
1641 markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1642 markAsSelectionDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1643 // WorkbenchHelp.setHelp(action,
1644 // IJavaHelpContextIds.REMOVE_BLOCK_COMMENT_ACTION);
1646 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1647 // "Indent.", this, false); //$NON-NLS-1$
1648 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1649 // setAction("Indent", action); //$NON-NLS-1$
1650 // markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1651 // markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1652 // // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1654 action = new IndentAction(PHPEditorMessages.getResourceBundle(),
1655 "Indent.", this, true); //$NON-NLS-1$
1656 setAction("IndentOnTab", action); //$NON-NLS-1$
1657 markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1658 markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1660 if (getPreferenceStore().getBoolean(
1661 PreferenceConstants.EDITOR_SMART_TAB)) {
1662 // don't replace Shift Right - have to make sure their enablement is
1663 // mutually exclusive
1664 // removeActionActivationCode(ITextEditorActionConstants.SHIFT_RIGHT);
1665 setActionActivationCode("IndentOnTab", '\t', -1, SWT.NONE); //$NON-NLS-1$
1667 fGenerateActionGroup = new GenerateActionGroup(this,
1668 ITextEditorActionConstants.GROUP_EDIT);
1669 // ActionGroup rg= new RefactorActionGroup(this,
1670 // ITextEditorActionConstants.GROUP_EDIT);
1672 // fActionGroups.addGroup(rg);
1673 fActionGroups.addGroup(fGenerateActionGroup);
1675 // We have to keep the context menu group separate to have better
1676 // control over positioning
1677 fContextMenuGroup = new CompositeActionGroup(
1678 new ActionGroup[] { fGenerateActionGroup
1680 // new LocalHistoryActionGroup(this,
1681 // ITextEditorActionConstants.GROUP_EDIT)
1687 * @see JavaEditor#getElementAt(int)
1689 protected IJavaElement getElementAt(int offset) {
1690 return getElementAt(offset, true);
1694 * Returns the most narrow element including the given offset. If
1695 * <code>reconcile</code> is <code>true</code> the editor's input
1696 * element is reconciled in advance. If it is <code>false</code> this
1697 * method only returns a result if the editor's input element does not need
1701 * the offset included by the retrieved element
1703 * <code>true</code> if working copy should be reconciled
1705 protected IJavaElement getElementAt(int offset, boolean reconcile) {
1706 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
1707 .getWorkingCopyManager();
1708 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
1712 synchronized (unit) {
1715 return unit.getElementAt(offset);
1716 } else if (unit.isConsistent())
1717 return unit.getElementAt(offset);
1718 } catch (JavaModelException x) {
1719 PHPeclipsePlugin.log(x.getStatus());
1720 // nothing found, be tolerant and go on
1727 * @see JavaEditor#getCorrespondingElement(IJavaElement)
1729 protected IJavaElement getCorrespondingElement(IJavaElement element) {
1731 return EditorUtility.getWorkingCopy(element, true);
1732 } catch (JavaModelException x) {
1733 PHPeclipsePlugin.log(x.getStatus());
1734 // nothing found, be tolerant and go on
1739 public void createPartControl(Composite parent) {
1740 super.createPartControl(parent);
1741 // fPaintManager = new PaintManager(getSourceViewer());
1742 LinePainter linePainter;
1743 linePainter = new LinePainter(getSourceViewer());
1744 linePainter.setHighlightColor(new Color(Display.getCurrent(), 225, 235,
1746 // fPaintManager.addPainter(linePainter);
1747 // if (isBracketHighlightingEnabled())
1748 // startBracketHighlighting();
1749 // if (isLineHighlightingEnabled())
1750 // startLineHighlighting();
1751 // if (isPrintMarginVisible())
1752 // showPrintMargin();
1753 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1754 // while (e.hasNext()) {
1755 // AnnotationType type = (AnnotationType) e.next();
1756 // if (isAnnotationIndicationEnabled(type))
1757 // startAnnotationIndication(type);
1759 if (isTabConversionEnabled())
1760 startTabConversion();
1761 // if (isOverviewRulerVisible())
1762 // showOverviewRuler();
1764 // Preferences preferences =
1765 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1766 // preferences.addPropertyChangeListener(fPropertyChangeListener);
1767 IPreferenceStore preferenceStore = getPreferenceStore();
1768 boolean closeBracketsPHP = preferenceStore
1769 .getBoolean(CLOSE_BRACKETS_PHP);
1770 boolean closeStringsPHPDQ = preferenceStore
1771 .getBoolean(CLOSE_STRINGS_DQ_PHP);
1772 boolean closeStringsPHPSQ = preferenceStore
1773 .getBoolean(CLOSE_STRINGS_SQ_PHP);
1774 fBracketInserter.setCloseBracketsPHPEnabled(closeBracketsPHP);
1775 fBracketInserter.setCloseStringsPHPDQEnabled(closeStringsPHPDQ);
1776 fBracketInserter.setCloseStringsPHPSQEnabled(closeStringsPHPSQ);
1777 ISourceViewer sourceViewer = getSourceViewer();
1778 if (sourceViewer instanceof ITextViewerExtension)
1779 ((ITextViewerExtension) sourceViewer)
1780 .prependVerifyKeyListener(fBracketInserter);
1783 private static char getPeerCharacter(char character) {
1784 switch (character) {
1800 throw new IllegalArgumentException();
1804 // private void startBracketHighlighting() {
1805 // if (fBracketPainter == null) {
1806 // ISourceViewer sourceViewer = getSourceViewer();
1807 // fBracketPainter = new BracketPainter(sourceViewer);
1808 // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1809 // // fPaintManager.addPainter(fBracketPainter);
1813 // private void stopBracketHighlighting() {
1814 // if (fBracketPainter != null) {
1815 // // fPaintManager.removePainter(fBracketPainter);
1816 // fBracketPainter.deactivate(true);
1817 // fBracketPainter.dispose();
1818 // fBracketPainter = null;
1822 // private boolean isBracketHighlightingEnabled() {
1823 // IPreferenceStore store = getPreferenceStore();
1824 // return store.getBoolean(MATCHING_BRACKETS);
1827 // private void startLineHighlighting() {
1828 // if (fLinePainter == null) {
1829 // ISourceViewer sourceViewer = getSourceViewer();
1830 // fLinePainter = new LinePainter(sourceViewer);
1831 // fLinePainter.setHighlightColor(getColor(CURRENT_LINE_COLOR));
1832 // // fPaintManager.addPainter(fLinePainter);
1836 // private void stopLineHighlighting() {
1837 // if (fLinePainter != null) {
1838 // // fPaintManager.removePainter(fLinePainter);
1839 // fLinePainter.deactivate(true);
1840 // fLinePainter.dispose();
1841 // fLinePainter = null;
1845 // private boolean isLineHighlightingEnabled() {
1846 // IPreferenceStore store = getPreferenceStore();
1847 // return store.getBoolean(CURRENT_LINE);
1850 // private void showPrintMargin() {
1851 // if (fPrintMarginPainter == null) {
1852 // fPrintMarginPainter = new PrintMarginPainter(getSourceViewer());
1853 // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
1854 // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
1855 // // fPaintManager.addPainter(fPrintMarginPainter);
1859 // private void hidePrintMargin() {
1860 // if (fPrintMarginPainter != null) {
1861 // // fPaintManager.removePainter(fPrintMarginPainter);
1862 // fPrintMarginPainter.deactivate(true);
1863 // fPrintMarginPainter.dispose();
1864 // fPrintMarginPainter = null;
1868 // private boolean isPrintMarginVisible() {
1869 // IPreferenceStore store = getPreferenceStore();
1870 // return store.getBoolean(PRINT_MARGIN);
1873 private int getTabSize() {
1874 Preferences preferences = PHPeclipsePlugin.getDefault()
1875 .getPluginPreferences();
1876 return preferences.getInt(CODE_FORMATTER_TAB_SIZE);
1879 private boolean isTabConversionEnabled() {
1880 IPreferenceStore store = getPreferenceStore();
1881 return store.getBoolean(SPACES_FOR_TABS);
1884 private Color getColor(String key) {
1885 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
1886 return getColor(rgb);
1889 private Color getColor(RGB rgb) {
1890 JavaTextTools textTools = PHPeclipsePlugin.getDefault()
1891 .getJavaTextTools();
1892 return textTools.getColorManager().getColor(rgb);
1895 // private Color getColor(AnnotationType annotationType) {
1896 // AnnotationInfo info = (AnnotationInfo)
1897 // ANNOTATION_MAP.get(annotationType);
1898 // if (info != null)
1899 // return getColor(info.fColorPreference);
1902 public void dispose() {
1903 ISourceViewer sourceViewer = getSourceViewer();
1904 if (sourceViewer instanceof ITextViewerExtension)
1905 ((ITextViewerExtension) sourceViewer)
1906 .removeVerifyKeyListener(fBracketInserter);
1907 // if (fPropertyChangeListener != null) {
1908 // Preferences preferences =
1909 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1910 // preferences.removePropertyChangeListener(fPropertyChangeListener);
1911 // fPropertyChangeListener = null;
1913 if (fJavaEditorErrorTickUpdater != null) {
1914 fJavaEditorErrorTickUpdater.dispose();
1915 fJavaEditorErrorTickUpdater = null;
1917 // if (fSelectionHistory != null)
1918 // fSelectionHistory.dispose();
1919 // if (fPaintManager != null) {
1920 // fPaintManager.dispose();
1921 // fPaintManager = null;
1923 if (fActionGroups != null) {
1924 fActionGroups.dispose();
1925 fActionGroups = null;
1930 // protected AnnotationType getAnnotationType(String preferenceKey) {
1931 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1932 // while (e.hasNext()) {
1933 // AnnotationType type = (AnnotationType) e.next();
1934 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(type);
1935 // if (info != null) {
1936 // if (preferenceKey.equals(info.fColorPreference)
1937 // || preferenceKey.equals(info.fEditorPreference)
1938 // || preferenceKey.equals(info.fOverviewRulerPreference))
1945 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1947 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1949 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1951 String p = event.getProperty();
1952 if (CLOSE_BRACKETS_PHP.equals(p)) {
1954 .setCloseBracketsPHPEnabled(getPreferenceStore()
1958 if (CLOSE_STRINGS_DQ_PHP.equals(p)) {
1960 .setCloseStringsPHPDQEnabled(getPreferenceStore()
1964 if (CLOSE_STRINGS_SQ_PHP.equals(p)) {
1966 .setCloseStringsPHPSQEnabled(getPreferenceStore()
1970 if (SPACES_FOR_TABS.equals(p)) {
1971 if (isTabConversionEnabled())
1972 startTabConversion();
1974 stopTabConversion();
1977 // if (MATCHING_BRACKETS.equals(p)) {
1978 // if (isBracketHighlightingEnabled())
1979 // startBracketHighlighting();
1981 // stopBracketHighlighting();
1984 // if (MATCHING_BRACKETS_COLOR.equals(p)) {
1985 // if (fBracketPainter != null)
1986 // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1989 // if (CURRENT_LINE.equals(p)) {
1990 // if (isLineHighlightingEnabled())
1991 // startLineHighlighting();
1993 // stopLineHighlighting();
1996 // if (CURRENT_LINE_COLOR.equals(p)) {
1997 // if (fLinePainter != null) {
1998 // stopLineHighlighting();
1999 // startLineHighlighting();
2003 // if (PRINT_MARGIN.equals(p)) {
2004 // if (isPrintMarginVisible())
2005 // showPrintMargin();
2007 // hidePrintMargin();
2010 // if (PRINT_MARGIN_COLOR.equals(p)) {
2011 // if (fPrintMarginPainter != null)
2012 // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
2015 // if (PRINT_MARGIN_COLUMN.equals(p)) {
2016 // if (fPrintMarginPainter != null)
2017 // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
2020 // if (OVERVIEW_RULER.equals(p)) {
2021 // if (isOverviewRulerVisible())
2022 // showOverviewRuler();
2024 // hideOverviewRuler();
2027 // AnnotationType type = getAnnotationType(p);
2028 // if (type != null) {
2030 // AnnotationInfo info = (AnnotationInfo)
2031 // ANNOTATION_MAP.get(type);
2032 // if (info.fColorPreference.equals(p)) {
2033 // Color color = getColor(type);
2034 // if (fProblemPainter != null) {
2035 // fProblemPainter.setColor(type, color);
2036 // fProblemPainter.paint(IPainter.CONFIGURATION);
2038 // setColorInOverviewRuler(type, color);
2042 // if (info.fEditorPreference.equals(p)) {
2043 // if (isAnnotationIndicationEnabled(type))
2044 // startAnnotationIndication(type);
2046 // stopAnnotationIndication(type);
2050 // if (info.fOverviewRulerPreference.equals(p)) {
2051 // if (isAnnotationIndicationInOverviewRulerEnabled(type))
2052 // showAnnotationIndicationInOverviewRuler(type, true);
2054 // showAnnotationIndicationInOverviewRuler(type, false);
2058 IContentAssistant c = asv.getContentAssistant();
2059 if (c instanceof ContentAssistant)
2060 ContentAssistPreference.changeConfiguration(
2061 (ContentAssistant) c, getPreferenceStore(), event);
2064 super.handlePreferenceStoreChanged(event);
2069 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
2071 protected void handlePreferencePropertyChanged(
2072 org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
2073 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2075 String p = event.getProperty();
2076 if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
2077 asv.updateIndentationPrefixes();
2078 if (fTabConverter != null)
2079 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2082 super.handlePreferencePropertyChanged(event);
2086 * Handles a property change event describing a change of the php core's
2087 * preferences and updates the preference related editor properties.
2090 * the property change event
2093 // handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
2095 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2096 // if (asv != null) {
2097 // String p = event.getProperty();
2098 // if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
2099 // asv.updateIndentationPrefixes();
2100 // if (fTabConverter != null)
2101 // fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2106 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#createJavaSourceViewer(org.eclipse.swt.widgets.Composite,
2107 * org.eclipse.jface.text.source.IVerticalRuler,
2108 * org.eclipse.jface.text.source.IOverviewRuler, boolean, int)
2110 protected ISourceViewer createJavaSourceViewer(Composite parent,
2111 IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
2112 boolean isOverviewRulerVisible, int styles, IPreferenceStore store) {
2113 return new AdaptedSourceViewer(parent, verticalRuler, overviewRuler,
2114 isOverviewRulerVisible, styles, store);
2117 // protected ISourceViewer createJavaSourceViewer(Composite parent,
2118 // IVerticalRuler ruler, int styles) {
2119 // return new AdaptedSourceViewer(parent, ruler, styles);
2121 private boolean isValidSelection(int offset, int length) {
2122 IDocumentProvider provider = getDocumentProvider();
2123 if (provider != null) {
2124 IDocument document = provider.getDocument(getEditorInput());
2125 if (document != null) {
2126 int end = offset + length;
2127 int documentLength = document.getLength();
2128 return 0 <= offset && offset <= documentLength && 0 <= end
2129 && end <= documentLength;
2136 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getInputElement()
2138 protected IJavaElement getInputJavaElement() {
2139 return PHPeclipsePlugin.getDefault().getWorkingCopyManager()
2140 .getWorkingCopy(getEditorInput());
2144 * @see AbstractTextEditor#editorContextMenuAboutToShow(IMenuManager)
2146 public void editorContextMenuAboutToShow(IMenuManager menu) {
2147 super.editorContextMenuAboutToShow(menu);
2148 ActionContext context = new ActionContext(getSelectionProvider()
2150 fContextMenuGroup.setContext(context);
2151 fContextMenuGroup.fillContextMenu(menu);
2152 fContextMenuGroup.setContext(null);
2156 * @see JavaEditor#setOutlinePageInput(JavaOutlinePage, IEditorInput)
2158 protected void setOutlinePageInput(JavaOutlinePage page, IEditorInput input) {
2160 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
2161 .getWorkingCopyManager();
2162 page.setInput(manager.getWorkingCopy(input));
2167 * @see AbstractTextEditor#performSaveOperation(WorkspaceModifyOperation,
2170 // protected void performSaveOperation(WorkspaceModifyOperation operation,
2171 // IProgressMonitor progressMonitor) {
2172 // IDocumentProvider p = getDocumentProvider();
2173 // if (p instanceof PHPDocumentProvider) {
2174 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2175 // cp.setSavePolicy(fSavePolicy);
2179 // super.performSaveOperation(operation, progressMonitor);
2181 // if (p instanceof PHPDocumentProvider) {
2182 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2183 // cp.setSavePolicy(null);
2188 * @see AbstractTextEditor#doSave(IProgressMonitor)
2190 public void doSave(IProgressMonitor progressMonitor) {
2192 IDocumentProvider p = getDocumentProvider();
2194 // editor has been closed
2198 if (p.isDeleted(getEditorInput())) {
2200 if (isSaveAsAllowed()) {
2203 * 1GEUSSR: ITPUI:ALL - User should never loose changes made in
2204 * the editors. Changed Behavior to make sure that if called
2205 * inside a regular save (because of deletion of input element)
2206 * there is a way to report back to the caller.
2208 performSaveAs(progressMonitor);
2213 * 1GF5YOX: ITPJUI:ALL - Save of delete file claims it's still
2214 * there Missing resources.
2216 Shell shell = getSite().getShell();
2221 .getString("PHPUnitEditor.error.saving.title1"), PHPEditorMessages.getString("PHPUnitEditor.error.saving.message1")); //$NON-NLS-1$ //$NON-NLS-2$
2225 if (getPreferenceStore().getBoolean(
2226 PreferenceConstants.EDITOR_P_RTRIM_ON_SAVE)) {
2227 RTrimAction trimAction = new RTrimAction();
2228 trimAction.setActiveEditor(null, getSite().getPage()
2229 .getActiveEditor());
2230 trimAction.run(null);
2233 setStatusLineErrorMessage(null);
2235 updateState(getEditorInput());
2236 validateState(getEditorInput());
2238 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
2239 .getWorkingCopyManager();
2240 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2243 synchronized (unit) {
2244 performSave(false, progressMonitor);
2247 performSave(false, progressMonitor);
2251 public boolean isSaveAsAllowed() {
2256 * The compilation unit editor implementation of this
2257 * <code>AbstractTextEditor</code> method asks the user for the workspace
2258 * path of a file resource and saves the document there. See
2259 * http://dev.eclipse.org/bugs/show_bug.cgi?id=6295
2261 * @param progressMonitor
2262 * the progress monitor
2264 protected void performSaveAs(IProgressMonitor progressMonitor) {
2266 Shell shell = getSite().getShell();
2267 IEditorInput input = getEditorInput();
2269 SaveAsDialog dialog = new SaveAsDialog(shell);
2271 IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input)
2274 if (original != null)
2275 dialog.setOriginalFile(original);
2279 IDocumentProvider provider = getDocumentProvider();
2280 if (provider == null) {
2281 // editor has been programmatically closed while the dialog was open
2285 if (provider.isDeleted(input) && original != null) {
2286 String message = PHPEditorMessages
2287 .getFormattedString(
2288 "CompilationUnitEditor.warning.save.delete", new Object[] { original.getName() }); //$NON-NLS-1$
2289 dialog.setErrorMessage(null);
2290 dialog.setMessage(message, IMessageProvider.WARNING);
2293 if (dialog.open() == Window.CANCEL) {
2294 if (progressMonitor != null)
2295 progressMonitor.setCanceled(true);
2299 IPath filePath = dialog.getResult();
2300 if (filePath == null) {
2301 if (progressMonitor != null)
2302 progressMonitor.setCanceled(true);
2306 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
2307 IFile file = workspaceRoot.getFile(filePath);
2308 final IEditorInput newInput = new FileEditorInput(file);
2310 boolean success = false;
2313 provider.aboutToChange(newInput);
2314 getDocumentProvider().saveDocument(progressMonitor, newInput,
2315 getDocumentProvider().getDocument(getEditorInput()), true);
2318 } catch (CoreException x) {
2319 IStatus status = x.getStatus();
2320 if (status == null || status.getSeverity() != IStatus.CANCEL)
2325 .getString("CompilationUnitEditor.error.saving.title2"), PHPEditorMessages.getString("CompilationUnitEditor.error.saving.message2"), x.getStatus()); //$NON-NLS-1$ //$NON-NLS-2$
2327 provider.changed(newInput);
2332 if (progressMonitor != null)
2333 progressMonitor.setCanceled(!success);
2337 * @see AbstractTextEditor#doSetInput(IEditorInput)
2339 protected void doSetInput(IEditorInput input) throws CoreException {
2340 super.doSetInput(input);
2341 configureTabConverter();
2342 configureToggleCommentAction();
2347 // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#installOverrideIndicator(boolean)
2350 // protected void installOverrideIndicator(boolean waitForReconcilation) {
2351 // IAnnotationModel model=
2352 // getDocumentProvider().getAnnotationModel(getEditorInput());
2353 // if (!waitForReconcilation)
2354 // super.installOverrideIndicator(false);
2356 // uninstallOverrideIndicator();
2357 // IJavaElement inputElement= getInputJavaElement();
2358 // if (model == null || inputElement == null)
2361 // fOverrideIndicatorManager= new OverrideIndicatorManager(model,
2362 // inputElement, null);
2363 // addReconcileListener(fOverrideIndicatorManager);
2369 // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#uninstallOverrideIndicator()
2372 // protected void uninstallOverrideIndicator() {
2373 // if (fOverrideIndicatorManager != null)
2374 // removeReconcileListener(fOverrideIndicatorManager);
2375 // super.uninstallOverrideIndicator();
2379 * Configures the toggle comment action
2383 private void configureToggleCommentAction() {
2384 IAction action = getAction("ToggleComment"); //$NON-NLS-1$
2385 if (action instanceof ToggleCommentAction) {
2386 ISourceViewer sourceViewer = getSourceViewer();
2387 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
2388 ((ToggleCommentAction) action).configure(sourceViewer,
2393 // private void configureTabConverter() {
2394 // if (fTabConverter != null) {
2395 // IDocumentProvider provider = getDocumentProvider();
2396 // if (provider instanceof PHPDocumentProvider) {
2397 // PHPDocumentProvider cup = (PHPDocumentProvider) provider;
2398 // fTabConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
2402 private void configureTabConverter() {
2403 if (fTabConverter != null) {
2404 IDocumentProvider provider = getDocumentProvider();
2405 if (provider instanceof ICompilationUnitDocumentProvider) {
2406 ICompilationUnitDocumentProvider cup = (ICompilationUnitDocumentProvider) provider;
2407 fTabConverter.setLineTracker(cup
2408 .createLineTracker(getEditorInput()));
2413 private void startTabConversion() {
2414 if (fTabConverter == null) {
2415 fTabConverter = new TabConverter();
2416 configureTabConverter();
2417 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2418 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2419 asv.addTextConverter(fTabConverter);
2420 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2421 asv.updateIndentationPrefixes();
2425 private void stopTabConversion() {
2426 if (fTabConverter != null) {
2427 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2428 asv.removeTextConverter(fTabConverter);
2429 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2430 asv.updateIndentationPrefixes();
2431 fTabConverter = null;
2436 * @see org.eclipse.ui.texteditor.AbstractTextEditor#performSave(boolean,
2437 * org.eclipse.core.runtime.IProgressMonitor)
2439 protected void performSave(boolean overwrite,
2440 IProgressMonitor progressMonitor) {
2441 // IDocumentProvider p = getDocumentProvider();
2442 // if (p instanceof PHPDocumentProvider) {
2443 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2444 // cp.setSavePolicy(fSavePolicy);
2447 // super.performSave(overwrite, progressMonitor);
2449 // if (p instanceof PHPDocumentProvider) {
2450 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2451 // cp.setSavePolicy(null);
2455 IDocumentProvider p = getDocumentProvider();
2456 if (p instanceof ICompilationUnitDocumentProvider) {
2457 ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2458 cp.setSavePolicy(fSavePolicy);
2461 super.performSave(overwrite, progressMonitor);
2463 if (p instanceof ICompilationUnitDocumentProvider) {
2464 ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2465 cp.setSavePolicy(null);
2471 * @see AbstractTextEditor#doSaveAs
2473 public void doSaveAs() {
2474 if (askIfNonWorkbenchEncodingIsOk()) {
2480 * Asks the user if it is ok to store in non-workbench encoding.
2482 * @return <true>if the user wants to continue
2484 private boolean askIfNonWorkbenchEncodingIsOk() {
2485 IDocumentProvider provider = getDocumentProvider();
2486 if (provider instanceof IStorageDocumentProvider) {
2487 IEditorInput input = getEditorInput();
2488 IStorageDocumentProvider storageProvider = (IStorageDocumentProvider) provider;
2489 String encoding = storageProvider.getEncoding(input);
2490 String defaultEncoding = storageProvider.getDefaultEncoding();
2491 if (encoding != null && !encoding.equals(defaultEncoding)) {
2492 Shell shell = getSite().getShell();
2493 String title = PHPEditorMessages
2494 .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.title"); //$NON-NLS-1$
2500 .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message1"),
2501 new String[] { input.getName(), encoding }); //$NON-NLS-1$
2506 .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message2"),
2507 new String[] { encoding }); //$NON-NLS-1$
2508 return MessageDialog.openQuestion(shell, title, msg);
2515 * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#aboutToBeReconciled()
2518 public void aboutToBeReconciled() {
2520 // Notify AST provider
2521 // PHPeclipsePlugin.getDefault().getASTProvider().aboutToBeReconciled(getInputJavaElement());
2524 Object[] listeners = fReconcilingListeners.getListeners();
2525 for (int i = 0, length = listeners.length; i < length; ++i)
2526 ((IJavaReconcilingListener) listeners[i]).aboutToBeReconciled();
2530 * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#reconciled(CompilationUnit,
2531 * boolean, IProgressMonitor)
2534 public void reconciled(CompilationUnit ast, boolean forced,
2535 IProgressMonitor progressMonitor) {
2537 // Always notify AST provider
2538 // PHPeclipsePlugin.getDefault().getASTProvider().reconciled(ast,
2539 // getInputJavaElement());
2542 // Object[] listeners = fReconcilingListeners.getListeners();
2543 // for (int i = 0, length= listeners.length; i < length; ++i)
2544 // ((IJavaReconcilingListener)listeners[i]).reconciled(ast, forced,
2545 // progressMonitor);
2547 // Update Java Outline page selection
2548 if (!forced && !progressMonitor.isCanceled()) {
2549 Shell shell = getSite().getShell();
2550 if (shell != null && !shell.isDisposed()) {
2551 shell.getDisplay().asyncExec(new Runnable() {
2561 * Returns the updated java element for the old java element.
2563 private IJavaElement findElement(IJavaElement element) {
2564 if (element == null)
2566 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
2567 .getWorkingCopyManager();
2568 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2571 synchronized (unit) {
2574 IJavaElement[] findings = unit.findElements(element);
2575 if (findings != null && findings.length > 0)
2577 } catch (JavaModelException x) {
2578 PHPeclipsePlugin.log(x.getStatus());
2579 // nothing found, be tolerant and go on
2586 * Returns the offset of the given Java element.
2588 private int getOffset(IJavaElement element) {
2589 if (element instanceof ISourceReference) {
2590 ISourceReference sr = (ISourceReference) element;
2592 ISourceRange srcRange = sr.getSourceRange();
2593 if (srcRange != null)
2594 return srcRange.getOffset();
2595 } catch (JavaModelException e) {
2602 * @see AbstractTextEditor#restoreSelection()
2604 // protected void restoreSelection() {
2606 // if (getSourceViewer() == null || fRememberedSelection == null)
2608 // IJavaElement newElement = findElement(fRememberedElement);
2609 // int newOffset = getOffset(newElement);
2610 // int delta = (newOffset > -1 && fRememberedElementOffset > -1) ? newOffset
2611 // - fRememberedElementOffset : 0;
2612 // if (isValidSelection(delta + fRememberedSelection.getOffset(),
2613 // fRememberedSelection.getLength()))
2614 // selectAndReveal(delta + fRememberedSelection.getOffset(),
2615 // fRememberedSelection.getLength());
2617 // fRememberedSelection = null;
2618 // fRememberedElement = null;
2619 // fRememberedElementOffset = -1;
2623 * Tells whether this is the active editor in the active page.
2625 * @return <code>true</code> if this is the active editor in the active
2627 * @see IWorkbenchPage#getActiveEditor();
2629 protected final boolean isActiveEditor() {
2630 IWorkbenchWindow window = getSite().getWorkbenchWindow();
2631 IWorkbenchPage page = window.getActivePage();
2634 IEditorPart activeEditor = page.getActiveEditor();
2635 return activeEditor != null && activeEditor.equals(this);
2639 * Adds the given listener. Has no effect if an identical listener was not
2640 * already registered.
2643 * The reconcile listener to be added
2646 final void addReconcileListener(IJavaReconcilingListener listener) {
2647 synchronized (fReconcilingListeners) {
2648 fReconcilingListeners.add(listener);
2653 * Removes the given listener. Has no effect if an identical listener was
2654 * not already registered.
2657 * the reconcile listener to be removed
2660 final void removeReconcileListener(IJavaReconcilingListener listener) {
2661 synchronized (fReconcilingListeners) {
2662 fReconcilingListeners.remove(listener);
2666 protected void updateStateDependentActions() {
2667 super.updateStateDependentActions();
2668 fGenerateActionGroup.editorStateChanged();
2672 * @see AbstractTextEditor#rememberSelection()
2674 protected void rememberSelection() {
2675 fRememberedSelection.remember();
2679 * @see AbstractTextEditor#restoreSelection()
2681 protected void restoreSelection() {
2682 fRememberedSelection.restore();
2686 * @see AbstractTextEditor#canHandleMove(IEditorInput, IEditorInput)
2688 protected boolean canHandleMove(IEditorInput originalElement,
2689 IEditorInput movedElement) {
2691 String oldExtension = ""; //$NON-NLS-1$
2692 if (originalElement instanceof IFileEditorInput) {
2693 IFile file = ((IFileEditorInput) originalElement).getFile();
2695 String ext = file.getFileExtension();
2701 String newExtension = ""; //$NON-NLS-1$
2702 if (movedElement instanceof IFileEditorInput) {
2703 IFile file = ((IFileEditorInput) movedElement).getFile();
2705 newExtension = file.getFileExtension();
2708 return oldExtension.equals(newExtension);
2712 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isPrefQuickDiffAlwaysOn()
2714 protected boolean isPrefQuickDiffAlwaysOn() {
2715 // reestablishes the behaviour from AbstractDecoratedTextEditor which
2716 // was hacked by JavaEditor
2717 // to disable the change bar for the class file (attached source) java
2719 IPreferenceStore store = getPreferenceStore();
2721 .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_ALWAYS_ON);
2725 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getAdapter(java.lang.Class)
2727 public Object getAdapter(Class required) {
2728 if (SmartBackspaceManager.class.equals(required)) {
2729 if (getSourceViewer() instanceof JavaSourceViewer) {
2730 return ((JavaSourceViewer) getSourceViewer())
2731 .getBackspaceManager();
2735 return super.getAdapter(required);
2739 * Returns the mutex for the reconciler. See
2740 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of
2743 * TODO remove once the underlying problem is solved.
2746 * @return the lock reconcilers may use to synchronize on
2748 public Object getReconcilerLock() {
2749 return fReconcilerLock;
2755 * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
2757 protected void editorSaved() {
2758 super.editorSaved();
2759 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();
2761 a.refresh(ShowExternalPreviewAction.PHP_TYPE);