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.WebUI;
44 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
46 import org.eclipse.core.resources.IFile;
47 import org.eclipse.core.resources.IWorkspaceRoot;
48 import org.eclipse.core.resources.ResourcesPlugin;
49 import org.eclipse.core.runtime.CoreException;
50 import org.eclipse.core.runtime.IPath;
51 import org.eclipse.core.runtime.IProgressMonitor;
52 import org.eclipse.core.runtime.IStatus;
53 import org.eclipse.core.runtime.Preferences;
54 import org.eclipse.jface.action.Action;
55 import org.eclipse.jface.action.IAction;
56 import org.eclipse.jface.action.IMenuManager;
57 import org.eclipse.jface.dialogs.ErrorDialog;
58 import org.eclipse.jface.dialogs.IMessageProvider;
59 import org.eclipse.jface.dialogs.MessageDialog;
60 import org.eclipse.jface.preference.IPreferenceStore;
61 import org.eclipse.jface.preference.PreferenceConverter;
62 import org.eclipse.jface.text.BadLocationException;
63 import org.eclipse.jface.text.DocumentCommand;
64 import org.eclipse.jface.text.IAutoEditStrategy;
65 import org.eclipse.jface.text.IDocument;
66 import org.eclipse.jface.text.ILineTracker;
67 import org.eclipse.jface.text.IRegion;
68 import org.eclipse.jface.text.ITextOperationTarget;
69 import org.eclipse.jface.text.ITextViewerExtension;
70 import org.eclipse.jface.text.ITypedRegion;
71 import org.eclipse.jface.text.IWidgetTokenKeeper;
72 import org.eclipse.jface.text.contentassist.ContentAssistant;
73 import org.eclipse.jface.text.contentassist.IContentAssistant;
74 import org.eclipse.jface.text.formatter.FormattingContextProperties;
75 import org.eclipse.jface.text.formatter.IFormattingContext;
76 import org.eclipse.jface.text.source.IOverviewRuler;
77 import org.eclipse.jface.text.source.ISourceViewer;
78 import org.eclipse.jface.text.source.IVerticalRuler;
79 import org.eclipse.jface.text.source.SourceViewerConfiguration;
80 import org.eclipse.jface.util.ListenerList;
81 import org.eclipse.jface.util.PropertyChangeEvent;
82 import org.eclipse.jface.window.Window;
83 import org.eclipse.swt.SWT;
84 import org.eclipse.swt.custom.VerifyKeyListener;
85 import org.eclipse.swt.events.VerifyEvent;
86 import org.eclipse.swt.graphics.Color;
87 import org.eclipse.swt.graphics.Point;
88 import org.eclipse.swt.graphics.RGB;
89 import org.eclipse.swt.widgets.Composite;
90 import org.eclipse.swt.widgets.Display;
91 import org.eclipse.swt.widgets.Shell;
92 import org.eclipse.ui.IEditorInput;
93 import org.eclipse.ui.IEditorPart;
94 import org.eclipse.ui.IFileEditorInput;
95 import org.eclipse.ui.IWorkbenchPage;
96 import org.eclipse.ui.IWorkbenchWindow;
97 import org.eclipse.ui.PlatformUI;
98 import org.eclipse.ui.actions.ActionContext;
99 import org.eclipse.ui.actions.ActionGroup;
100 import org.eclipse.ui.dialogs.SaveAsDialog;
101 import org.eclipse.ui.editors.text.IStorageDocumentProvider;
102 import org.eclipse.ui.part.FileEditorInput;
103 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
104 import org.eclipse.ui.texteditor.ContentAssistAction;
105 import org.eclipse.ui.texteditor.IDocumentProvider;
106 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
107 import org.eclipse.ui.texteditor.TextOperationAction;
109 /*******************************************************************************
110 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
111 * program and the accompanying materials are made available under the terms of
112 * the Common Public License v1.0 which accompanies this distribution, and is
113 * available at http://www.eclipse.org/legal/cpl-v10.html
115 * Contributors: IBM Corporation - Initial implementation
117 ******************************************************************************/
119 * PHP specific text editor.
121 public class PHPUnitEditor extends PHPEditor { // implements
122 // IJavaReconcilingListener {
123 interface ITextConverter {
124 void customizeDocumentCommand(IDocument document,
125 DocumentCommand command);
128 // class AdaptedSourceViewer extends JavaSourceViewer {
129 // private List fTextConverters;
131 // private boolean fIgnoreTextConverters = false;
133 // // private JavaCorrectionAssistant fCorrectionAssistant;
134 // public AdaptedSourceViewer(Composite parent, IVerticalRuler
136 // IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
137 // int styles, IPreferenceStore store) {
138 // super(parent, verticalRuler, overviewRuler, showAnnotationsOverview,
142 // // public AdaptedSourceViewer(Composite parent,
143 // // IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
144 // // boolean showAnnotationsOverview, int styles) {
145 // // super(parent, verticalRuler, overviewRuler,
146 // // showAnnotationsOverview, styles);
148 // public IContentAssistant getContentAssistant() {
149 // return fContentAssistant;
153 // * @see ITextOperationTarget#doOperation(int)
155 // public void doOperation(int operation) {
156 // if (getTextWidget() == null)
158 // switch (operation) {
159 // case CONTENTASSIST_PROPOSALS:
160 // String msg = fContentAssistant.showPossibleCompletions();
161 // setStatusLineErrorMessage(msg);
163 // // case CORRECTIONASSIST_PROPOSALS:
164 // // fCorrectionAssistant.showPossibleCompletions();
167 // fIgnoreTextConverters = true;
170 // fIgnoreTextConverters = true;
173 // super.doOperation(operation);
177 // * @see ITextOperationTarget#canDoOperation(int)
179 // public boolean canDoOperation(int operation) {
180 // // if (operation == CORRECTIONASSIST_PROPOSALS)
181 // // return isEditable();
182 // return super.canDoOperation(operation);
186 // * @see TextViewer#handleDispose()
188 // protected void handleDispose() {
189 // // if (fCorrectionAssistant != null) {
190 // // fCorrectionAssistant.uninstall();
191 // // fCorrectionAssistant= null;
193 // super.handleDispose();
196 // public void insertTextConverter(ITextConverter textConverter, int index)
198 // throw new UnsupportedOperationException();
201 // public void addTextConverter(ITextConverter textConverter) {
202 // if (fTextConverters == null) {
203 // fTextConverters = new ArrayList(1);
204 // fTextConverters.add(textConverter);
205 // } else if (!fTextConverters.contains(textConverter))
206 // fTextConverters.add(textConverter);
209 // public void removeTextConverter(ITextConverter textConverter) {
210 // if (fTextConverters != null) {
211 // fTextConverters.remove(textConverter);
212 // if (fTextConverters.size() == 0)
213 // fTextConverters = null;
218 // * @see TextViewer#customizeDocumentCommand(DocumentCommand)
220 // protected void customizeDocumentCommand(DocumentCommand command) {
221 // super.customizeDocumentCommand(command);
222 // if (!fIgnoreTextConverters && fTextConverters != null) {
223 // for (Iterator e = fTextConverters.iterator(); e.hasNext();)
224 // ((ITextConverter) e.next()).customizeDocumentCommand(getDocument(),
227 // fIgnoreTextConverters = false;
230 // // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
231 // public void updateIndentationPrefixes() {
232 // SourceViewerConfiguration configuration = getSourceViewerConfiguration();
233 // String[] types = configuration.getConfiguredContentTypes(this);
234 // for (int i = 0; i < types.length; i++) {
235 // String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
236 // if (prefixes != null && prefixes.length > 0)
237 // setIndentPrefixes(prefixes, types[i]);
242 // * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
244 // public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
245 // if (WorkbenchHelp.isContextHelpDisplayed())
247 // return super.requestWidgetToken(requester);
252 // org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
254 // // public void configure(SourceViewerConfiguration configuration) {
255 // // super.configure(configuration);
256 // // // fCorrectionAssistant= new
257 // // // JavaCorrectionAssistant(CompilationUnitEditor.this);
258 // // // fCorrectionAssistant.install(this);
259 // // //TODO install SmartBracesAutoEditStrategy
260 // // // prependAutoEditStrategy(new SmartBracesAutoEditStrategy(this),
261 // // // IDocument.DEFAULT_CONTENT_TYPE);
263 // public void configure(SourceViewerConfiguration configuration) {
264 // super.configure(configuration);
265 // // fCorrectionAssistant= new
266 // JavaCorrectionAssistant(CompilationUnitEditor.this);
267 // // fCorrectionAssistant.install(this);
268 // IAutoEditStrategy smartSemi= new
269 // SmartSemicolonAutoEditStrategy(IPHPPartitions.PHP_PARTITIONING);
270 // prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
271 // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
272 // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
273 // // prependAutoEditStrategy(smartSemi, IPHPPartitions.JAVA_CHARACTER);
276 class AdaptedSourceViewer extends JavaSourceViewer {
278 private List fTextConverters;
280 private boolean fIgnoreTextConverters = false;
282 // private JavaCorrectionAssistant fCorrectionAssistant;
284 public AdaptedSourceViewer(Composite parent,
285 IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
286 boolean showAnnotationsOverview, int styles,
287 IPreferenceStore store) {
288 super(parent, verticalRuler, overviewRuler,
289 showAnnotationsOverview, styles, store);
292 public IContentAssistant getContentAssistant() {
293 return fContentAssistant;
297 * @see ITextOperationTarget#doOperation(int)
299 public void doOperation(int operation) {
301 if (getTextWidget() == null)
305 case CONTENTASSIST_PROPOSALS:
306 String msg = fContentAssistant.showPossibleCompletions();
307 setStatusLineErrorMessage(msg);
309 // case CORRECTIONASSIST_PROPOSALS:
310 // msg = fCorrectionAssistant.showPossibleCompletions();
311 // setStatusLineErrorMessage(msg);
314 fIgnoreTextConverters = true;
315 super.doOperation(operation);
316 fIgnoreTextConverters = false;
319 fIgnoreTextConverters = true;
320 super.doOperation(operation);
321 fIgnoreTextConverters = false;
325 super.doOperation(operation);
329 * @see ITextOperationTarget#canDoOperation(int)
331 public boolean canDoOperation(int operation) {
332 // if (operation == CORRECTIONASSIST_PROPOSALS)
333 // return isEditable();
335 return super.canDoOperation(operation);
339 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
342 public void unconfigure() {
343 // if (fCorrectionAssistant != null) {
344 // fCorrectionAssistant.uninstall();
345 // fCorrectionAssistant = null;
350 public void insertTextConverter(ITextConverter textConverter, int index) {
351 throw new UnsupportedOperationException();
354 public void addTextConverter(ITextConverter textConverter) {
355 if (fTextConverters == null) {
356 fTextConverters = new ArrayList(1);
357 fTextConverters.add(textConverter);
358 } else if (!fTextConverters.contains(textConverter))
359 fTextConverters.add(textConverter);
362 public void removeTextConverter(ITextConverter textConverter) {
363 if (fTextConverters != null) {
364 fTextConverters.remove(textConverter);
365 if (fTextConverters.size() == 0)
366 fTextConverters = null;
371 * @see TextViewer#customizeDocumentCommand(DocumentCommand)
373 protected void customizeDocumentCommand(DocumentCommand command) {
374 super.customizeDocumentCommand(command);
375 if (!fIgnoreTextConverters && fTextConverters != null) {
376 for (Iterator e = fTextConverters.iterator(); e.hasNext();)
377 ((ITextConverter) e.next()).customizeDocumentCommand(
378 getDocument(), command);
382 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
383 public void updateIndentationPrefixes() {
384 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
385 String[] types = configuration.getConfiguredContentTypes(this);
386 for (int i = 0; i < types.length; i++) {
387 String[] prefixes = configuration.getIndentPrefixes(this,
389 if (prefixes != null && prefixes.length > 0)
390 setIndentPrefixes(prefixes, types[i]);
395 * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
397 public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
398 if (PlatformUI.getWorkbench().getHelpSystem()
399 .isContextHelpDisplayed())
401 return super.requestWidgetToken(requester);
405 * @see IWidgetTokenOwnerExtension#requestWidgetToken(IWidgetTokenKeeper,
409 public boolean requestWidgetToken(IWidgetTokenKeeper requester,
411 if (PlatformUI.getWorkbench().getHelpSystem()
412 .isContextHelpDisplayed())
414 return super.requestWidgetToken(requester, priority);
418 * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
420 public void configure(SourceViewerConfiguration configuration) {
421 super.configure(configuration);
422 // fCorrectionAssistant = new
423 // JavaCorrectionAssistant(CompilationUnitEditor.this);
424 // fCorrectionAssistant.install(this);
425 IAutoEditStrategy smartSemi = new SmartSemicolonAutoEditStrategy(
426 IPHPPartitions.PHP_PARTITIONING);
427 prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
428 prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
429 prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
430 prependAutoEditStrategy(smartSemi,
431 IPHPPartitions.PHP_STRING_HEREDOC);
435 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
438 public IFormattingContext createFormattingContext() {
439 IFormattingContext context = new CommentFormattingContext();
442 IJavaElement inputJavaElement = getInputJavaElement();
443 IJavaProject javaProject = inputJavaElement != null ? inputJavaElement
446 if (javaProject == null)
447 preferences = new HashMap(JavaCore.getOptions());
449 preferences = new HashMap(javaProject.getOptions(true));
451 context.storeToMap(PreferenceConstants.getPreferenceStore(),
454 FormattingContextProperties.CONTEXT_PREFERENCES,
462 * Remembers data related to the current selection to be able to restore it
467 private class RememberedSelection {
468 /** The remembered selection start. */
469 private RememberedOffset fStartOffset = new RememberedOffset();
471 /** The remembered selection end. */
472 private RememberedOffset fEndOffset = new RememberedOffset();
475 * Remember current selection.
477 public void remember() {
479 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method
480 * may be called inside an async call posted to the UI thread, so
481 * protect against intermediate disposal of the editor.
483 ISourceViewer viewer = getSourceViewer();
484 if (viewer != null) {
485 IRegion selection = getSignedSelection(viewer);
486 int startOffset = selection.getOffset();
487 int endOffset = startOffset + selection.getLength();
489 fStartOffset.setOffset(startOffset);
490 fEndOffset.setOffset(endOffset);
495 * Restore remembered selection.
497 public void restore() {
499 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method
500 * may be called inside an async call posted to the UI thread, so
501 * protect against intermediate disposal of the editor.
503 if (getSourceViewer() == null)
508 int startOffset, endOffset;
509 int revealStartOffset, revealEndOffset;
510 if (showsHighlightRangeOnly()) {
511 IJavaElement newStartElement = fStartOffset.getElement();
512 startOffset = fStartOffset
513 .getRememberedOffset(newStartElement);
514 revealStartOffset = fStartOffset.getRevealOffset(
515 newStartElement, startOffset);
516 if (revealStartOffset == -1)
519 IJavaElement newEndElement = fEndOffset.getElement();
520 endOffset = fEndOffset.getRememberedOffset(newEndElement);
521 revealEndOffset = fEndOffset.getRevealOffset(newEndElement,
523 if (revealEndOffset == -1)
526 startOffset = fStartOffset.getOffset();
527 revealStartOffset = startOffset;
528 endOffset = fEndOffset.getOffset();
529 revealEndOffset = endOffset;
532 if (startOffset == -1) {
533 startOffset = endOffset; // fallback to caret offset
534 revealStartOffset = revealEndOffset;
537 if (endOffset == -1) {
538 endOffset = startOffset; // fallback to other offset
539 revealEndOffset = revealStartOffset;
542 IJavaElement element;
543 if (endOffset == -1) {
544 // fallback to element selection
545 element = fEndOffset.getElement();
547 element = fStartOffset.getElement();
549 setSelection(element);
553 if (isValidSelection(revealStartOffset, revealEndOffset
555 && isValidSelection(startOffset, endOffset
557 selectAndReveal(startOffset, endOffset - startOffset,
558 revealStartOffset, revealEndOffset
559 - revealStartOffset);
561 fStartOffset.clear();
566 private boolean isValidSelection(int offset, int length) {
567 IDocumentProvider provider = getDocumentProvider();
568 if (provider != null) {
569 IDocument document = provider.getDocument(getEditorInput());
570 if (document != null) {
571 int end = offset + length;
572 int documentLength = document.getLength();
573 return 0 <= offset && offset <= documentLength && 0 <= end
574 && end <= documentLength;
583 * Remembers additional data for a given offset to be able restore it later.
587 private class RememberedOffset {
588 /** Remembered line for the given offset */
591 /** Remembered column for the given offset */
594 /** Remembered Java element for the given offset */
595 private IJavaElement fElement;
597 /** Remembered Java element line for the given offset */
598 private int fElementLine;
601 * Store visual properties of the given offset.
604 * Offset in the document
606 public void setOffset(int offset) {
608 IDocument document = getSourceViewer().getDocument();
609 fLine = document.getLineOfOffset(offset);
610 fColumn = offset - document.getLineOffset(fLine);
611 fElement = getElementAt(offset, true);
614 if (fElement instanceof IMember) {
615 ISourceRange range = ((IMember) fElement).getNameRange();
617 fElementLine = document.getLineOfOffset(range
620 if (fElementLine == -1)
621 fElementLine = document
622 .getLineOfOffset(getOffset(fElement));
623 } catch (BadLocationException e) {
625 PHPeclipsePlugin.log(e);
627 } catch (JavaModelException e) {
629 PHPeclipsePlugin.log(e.getStatus());
635 * Return offset recomputed from stored visual properties.
637 * @return Offset in the document
639 public int getOffset() {
640 IJavaElement newElement = getElement();
642 int offset = getRememberedOffset(newElement);
644 if (offset != -1 && !containsOffset(newElement, offset)
645 && (offset == 0 || !containsOffset(newElement, offset - 1)))
652 * Return offset recomputed from stored visual properties.
656 * @return Offset in the document
658 public int getRememberedOffset(IJavaElement newElement) {
660 if (newElement == null)
663 IDocument document = getSourceViewer().getDocument();
664 int newElementLine = -1;
665 if (newElement instanceof IMember) {
666 ISourceRange range = ((IMember) newElement).getNameRange();
668 newElementLine = document.getLineOfOffset(range
671 if (newElementLine == -1)
672 newElementLine = document
673 .getLineOfOffset(getOffset(newElement));
674 if (newElementLine == -1)
677 int newLine = fLine + newElementLine - fElementLine;
678 if (newLine < 0 || newLine >= document.getNumberOfLines())
680 int maxColumn = document.getLineLength(newLine);
681 String lineDelimiter = document.getLineDelimiter(newLine);
682 if (lineDelimiter != null)
683 maxColumn = maxColumn - lineDelimiter.length();
685 if (fColumn > maxColumn)
686 offset = document.getLineOffset(newLine) + maxColumn;
688 offset = document.getLineOffset(newLine) + fColumn;
691 } catch (BadLocationException e) {
693 PHPeclipsePlugin.log(e);
695 } catch (JavaModelException e) {
697 PHPeclipsePlugin.log(e.getStatus());
703 * Returns the offset used to reveal the given element based on the
704 * given selection offset.
709 * the selection offset
710 * @return the offset to reveal the given element based on the given
713 public int getRevealOffset(IJavaElement element, int offset) {
714 if (element == null || offset == -1)
717 if (containsOffset(element, offset)) {
719 IJavaElement alternateElement = getElementAt(offset, false);
720 if (element.getHandleIdentifier().equals(
721 alternateElement.getParent().getHandleIdentifier()))
722 return offset - 1; // Solves test case 2 from
723 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
726 } else if (offset > 0 && containsOffset(element, offset - 1))
727 return offset - 1; // Solves test case 1 from
728 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
734 * Return Java element recomputed from stored visual properties.
736 * @return Java element
738 public IJavaElement getElement() {
739 if (fElement == null)
742 return findElement(fElement);
746 * Clears the stored position
748 public void clear() {
756 * Does the given Java element contain the given offset?
762 * @return <code>true</code> iff the Java element contains the offset
764 private boolean containsOffset(IJavaElement element, int offset) {
765 int elementOffset = getOffset(element);
766 int elementLength = getLength(element);
767 return (elementOffset > -1 && elementLength > -1) ? (offset >= elementOffset && offset < elementOffset
773 * Returns the offset of the given Java element.
777 * @return Offset of the given Java element
779 private int getOffset(IJavaElement element) {
780 if (element instanceof ISourceReference) {
781 ISourceReference sr = (ISourceReference) element;
783 ISourceRange srcRange = sr.getSourceRange();
784 if (srcRange != null)
785 return srcRange.getOffset();
786 } catch (JavaModelException e) {
793 * Returns the length of the given Java element.
797 * @return Length of the given Java element
799 private int getLength(IJavaElement element) {
800 if (element instanceof ISourceReference) {
801 ISourceReference sr = (ISourceReference) element;
803 ISourceRange srcRange = sr.getSourceRange();
804 if (srcRange != null)
805 return srcRange.getLength();
806 } catch (JavaModelException e) {
813 * Returns the updated java element for the old java element.
817 * @return Updated Java element
819 private IJavaElement findElement(IJavaElement element) {
824 IWorkingCopyManager manager = WebUI.getDefault()
825 .getWorkingCopyManager();
826 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
831 synchronized (unit) {
832 // unit.reconcile(ICompilationUnit.NO_AST, false, null,
836 IJavaElement[] findings = unit.findElements(element);
837 if (findings != null && findings.length > 0)
840 } catch (JavaModelException x) {
841 PHPeclipsePlugin.log(x.getStatus());
842 // nothing found, be tolerant and go on
851 static class TabConverter implements ITextConverter {
852 private int fTabRatio;
854 private ILineTracker fLineTracker;
856 public TabConverter() {
859 public void setNumberOfSpacesPerTab(int ratio) {
863 public void setLineTracker(ILineTracker lineTracker) {
864 fLineTracker = lineTracker;
867 private int insertTabString(StringBuffer buffer, int offsetInLine) {
870 int remainder = offsetInLine % fTabRatio;
871 remainder = fTabRatio - remainder;
872 for (int i = 0; i < remainder; i++)
877 public void customizeDocumentCommand(IDocument document,
878 DocumentCommand command) {
879 String text = command.text;
882 int index = text.indexOf('\t');
884 StringBuffer buffer = new StringBuffer();
885 fLineTracker.set(command.text);
886 int lines = fLineTracker.getNumberOfLines();
888 for (int i = 0; i < lines; i++) {
889 int offset = fLineTracker.getLineOffset(i);
890 int endOffset = offset + fLineTracker.getLineLength(i);
891 String line = text.substring(offset, endOffset);
894 IRegion firstLine = document
895 .getLineInformationOfOffset(command.offset);
896 position = command.offset - firstLine.getOffset();
898 int length = line.length();
899 for (int j = 0; j < length; j++) {
900 char c = line.charAt(j);
902 position += insertTabString(buffer, position);
909 command.text = buffer.toString();
910 } catch (BadLocationException x) {
916 private static class ExitPolicy implements LinkedPositionUI.ExitPolicy {
917 final char fExitCharacter;
919 public ExitPolicy(char exitCharacter) {
920 fExitCharacter = exitCharacter;
924 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitPolicy#doExit(org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionManager,
925 * org.eclipse.swt.events.VerifyEvent, int, int)
927 public ExitFlags doExit(LinkedPositionManager manager,
928 VerifyEvent event, int offset, int length) {
929 if (event.character == fExitCharacter) {
930 if (manager.anyPositionIncludes(offset, length))
931 return new ExitFlags(LinkedPositionUI.COMMIT
932 | LinkedPositionUI.UPDATE_CARET, false);
934 return new ExitFlags(LinkedPositionUI.COMMIT, true);
936 // Fix for #1380415 (toshihiro) start
937 switch (event.keyCode) {
940 return new ExitFlags(LinkedPositionUI.COMMIT, true);
942 case SWT.ARROW_RIGHT:
943 if (!manager.anyPositionIncludes(offset, length))
944 return new ExitFlags(LinkedPositionUI.COMMIT, true);
948 switch (event.character) {
950 if (manager.getFirstPosition().length == 0)
951 return new ExitFlags(0, false);
956 return new ExitFlags(LinkedPositionUI.COMMIT, true);
963 private static class BracketLevel {
968 LinkedPositionManager fManager;
970 LinkedPositionUI fEditor;
973 private class BracketInserter implements VerifyKeyListener,
974 LinkedPositionUI.ExitListener {
975 private boolean fCloseBracketsPHP = true;
977 private boolean fCloseStringsPHPDQ = true;
979 private boolean fCloseStringsPHPSQ = true;
985 public void setCloseBracketsPHPEnabled(boolean enabled) {
986 fCloseBracketsPHP = enabled;
989 public void setCloseStringsPHPDQEnabled(boolean enabled) {
990 fCloseStringsPHPDQ = enabled;
993 public void setCloseStringsPHPSQEnabled(boolean enabled) {
994 fCloseStringsPHPSQ = enabled;
997 private boolean hasIdentifierToTheRight(IDocument document, int offset) {
1000 IRegion endLine = document.getLineInformationOfOffset(end);
1001 int maxEnd = endLine.getOffset() + endLine.getLength();
1002 while (end != maxEnd
1003 && Character.isWhitespace(document.getChar(end)))
1005 return end != maxEnd
1006 && Scanner.isPHPIdentifierPart(document.getChar(end));
1007 } catch (BadLocationException e) {
1013 private boolean hasIdentifierToTheLeft(IDocument document, int offset) {
1016 IRegion startLine = document.getLineInformationOfOffset(start);
1017 int minStart = startLine.getOffset();
1018 while (start != minStart
1019 && Character.isWhitespace(document.getChar(start - 1)))
1021 return start != minStart
1022 && Scanner.isPHPIdentifierPart(document
1023 .getChar(start - 1));
1024 } catch (BadLocationException e) {
1029 private boolean hasCharacterToTheLeft(IDocument document, int offset,
1033 IRegion startLine = document.getLineInformationOfOffset(start);
1034 int minStart = startLine.getOffset();
1035 while (start != minStart
1036 && Character.isWhitespace(document.getChar(start - 1)))
1038 return start != minStart
1039 && document.getChar(start - 1) == character;
1040 } catch (BadLocationException e) {
1045 private boolean hasCharacterToTheRight(IDocument document, int offset,
1049 IRegion endLine = document.getLineInformationOfOffset(end);
1050 int maxEnd = endLine.getOffset() + endLine.getLength();
1051 while (end != maxEnd
1052 && Character.isWhitespace(document.getChar(end)))
1054 return end != maxEnd && document.getChar(end) == character;
1055 } catch (BadLocationException e) {
1062 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
1064 public void verifyKey(VerifyEvent event) {
1067 final ISourceViewer sourceViewer = getSourceViewer();
1068 IDocument document = sourceViewer.getDocument();
1069 final Point selection = sourceViewer.getSelectedRange();
1070 final int offset = selection.x;
1071 final int length = selection.y;
1073 ITypedRegion partition = document.getPartition(offset);
1074 String type = partition.getType();
1075 if (type.equals(IPHPPartitions.PHP_PARTITIONING)
1076 || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
1077 // you will get IDocument.DEFAULT_CONTENT_TYPE for both PHP
1079 switch (event.character) {
1081 if (hasCharacterToTheRight(document, offset + length,
1086 if (!fCloseBracketsPHP)
1088 if (hasIdentifierToTheRight(document, offset + length))
1092 if (!fCloseBracketsPHP)
1094 if (hasIdentifierToTheRight(document, offset + length))
1098 if (event.character == '"') {
1099 if (!fCloseStringsPHPDQ)
1101 // changed for statements like echo "" print ""
1102 // if (hasIdentifierToTheLeft(document, offset)
1104 // hasIdentifierToTheRight(document, offset +
1106 if (hasIdentifierToTheRight(document, offset
1110 // ITypedRegion partition=
1111 // document.getPartition(offset);
1113 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1115 // (partition.getOffset() != offset))
1117 final char characterDQ = event.character;
1118 final char closingCharacterDQ = getPeerCharacter(characterDQ);
1119 final StringBuffer bufferDQ = new StringBuffer();
1120 bufferDQ.append(characterDQ);
1121 bufferDQ.append(closingCharacterDQ);
1122 document.replace(offset, length, bufferDQ.toString());
1123 LinkedPositionManager managerDQ = new LinkedPositionManager(
1125 managerDQ.addPosition(offset + 1, 0);
1128 LinkedPositionUI editorDQ = new LinkedPositionUI(
1129 sourceViewer, managerDQ);
1130 editorDQ.setCancelListener(this);
1131 editorDQ.setExitPolicy(new ExitPolicy(
1132 closingCharacterDQ));
1133 editorDQ.setFinalCaretOffset(offset + 2);
1135 IRegion newSelectionDQ = editorDQ.getSelectedRegion();
1136 sourceViewer.setSelectedRange(newSelectionDQ
1137 .getOffset(), newSelectionDQ.getLength());
1141 if (event.character == '\'') {
1142 if (!fCloseStringsPHPSQ)
1144 // changed for statements like echo "" print ""
1145 // if (hasIdentifierToTheLeft(document, offset)
1147 // hasIdentifierToTheRight(document, offset +
1149 if (hasIdentifierToTheRight(document, offset
1153 // ITypedRegion partition=
1154 // document.getPartition(offset);
1156 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1158 // (partition.getOffset() != offset))
1160 final char characterSQ = event.character;
1161 final char closingCharacterSQ = getPeerCharacter(characterSQ);
1162 final StringBuffer bufferSQ = new StringBuffer();
1163 bufferSQ.append(characterSQ);
1164 bufferSQ.append(closingCharacterSQ);
1165 document.replace(offset, length, bufferSQ.toString());
1166 LinkedPositionManager managerSQ = new LinkedPositionManager(
1168 managerSQ.addPosition(offset + 1, 0);
1171 LinkedPositionUI editorSQ = new LinkedPositionUI(
1172 sourceViewer, managerSQ);
1173 editorSQ.setCancelListener(this);
1174 editorSQ.setExitPolicy(new ExitPolicy(
1175 closingCharacterSQ));
1176 editorSQ.setFinalCaretOffset(offset + 2);
1178 IRegion newSelectionSQ = editorSQ.getSelectedRegion();
1179 sourceViewer.setSelectedRange(newSelectionSQ
1180 .getOffset(), newSelectionSQ.getLength());
1182 case '\r': { // insert linebreaks and new closing brace
1183 // after brace and return
1184 if (!fCloseBracketsPHP) {
1187 if (hasCharacterToTheLeft(document, offset, '{')
1188 && hasCharacterToTheRight(document, offset, '}')) {
1189 String lineDelimiter = StubUtility
1190 .getLineDelimiterFor(document);
1191 int caretPos = sourceViewer.getTextWidget()
1193 final StringBuffer buffer = new StringBuffer(
1196 IRegion line = document
1197 .getLineInformationOfOffset(offset);
1198 String currentLine = document.get(line.getOffset(),
1201 int max = currentLine.length();
1202 StringBuffer indent = new StringBuffer();
1204 && Character.isWhitespace(currentLine
1206 indent.append(currentLine.charAt(index));
1209 buffer.append(indent);
1210 JavaHeuristicScanner scanner = new JavaHeuristicScanner(
1212 JavaIndenter indenter = new JavaIndenter(document,
1214 buffer.append(indenter.createIndent(1));
1215 int cursorPos = buffer.length();
1216 buffer.append(lineDelimiter);
1217 buffer.append(indent);
1218 document.replace(offset, length, buffer.toString());
1219 sourceViewer.getTextWidget().setCaretOffset(
1220 caretPos + cursorPos);
1226 } catch (BadLocationException e) {
1231 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitListener#exit(boolean)
1233 public void exit(boolean accept) {
1238 final ISourceViewer sourceViewer = getSourceViewer();
1239 IDocument document = sourceViewer.getDocument();
1240 document.replace(fOffset, fLength, null);
1241 } catch (BadLocationException e) {
1246 /** The editor's save policy */
1247 protected ISavePolicy fSavePolicy;
1250 * Listener to annotation model changes that updates the error tick in the
1253 private JavaEditorErrorTickUpdater fJavaEditorErrorTickUpdater;
1255 /** The editor's paint manager */
1256 // private PaintManager fPaintManager;
1257 /** The editor's bracket painter */
1258 // private BracketPainter fBracketPainter;
1259 /** The editor's bracket matcher */
1260 private PHPPairMatcher fBracketMatcher;
1262 /** The editor's line painter */
1263 // private LinePainter fLinePainter;
1264 /** The editor's print margin ruler painter */
1265 // private PrintMarginPainter fPrintMarginPainter;
1266 /** The editor's problem painter */
1267 // private ProblemPainter fProblemPainter;
1268 /** The editor's tab converter */
1269 private TabConverter fTabConverter;
1271 /** History for structure select action */
1272 // private SelectionHistory fSelectionHistory;
1273 /** The preference property change listener for php core. */
1274 // private IPropertyChangeListener fPropertyChangeListener = new
1275 // PropertyChangeListener();
1276 /** The remembered java element */
1277 private IJavaElement fRememberedElement;
1280 * The remembered selection.
1284 private RememberedSelection fRememberedSelection = new RememberedSelection();
1286 /** The remembered php element offset */
1287 private int fRememberedElementOffset;
1289 /** The bracket inserter. */
1290 private BracketInserter fBracketInserter = new BracketInserter();
1292 /** The standard action groups added to the menu */
1293 private GenerateActionGroup fGenerateActionGroup;
1295 private CompositeActionGroup fContextMenuGroup;
1297 // private class PropertyChangeListener implements IPropertyChangeListener {
1299 // * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
1302 // propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
1304 // handlePreferencePropertyChanged(event);
1307 /* Preference key for code formatter tab size */
1308 private final static String CODE_FORMATTER_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
1310 /** Preference key for matching brackets */
1311 // private final static String MATCHING_BRACKETS =
1312 // PreferenceConstants.EDITOR_MATCHING_BRACKETS;
1313 /** Preference key for matching brackets color */
1314 // private final static String MATCHING_BRACKETS_COLOR =
1315 // PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
1316 /** Preference key for highlighting current line */
1317 // private final static String CURRENT_LINE =
1318 // PreferenceConstants.EDITOR_CURRENT_LINE;
1319 /** Preference key for highlight color of current line */
1320 // private final static String CURRENT_LINE_COLOR =
1321 // PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
1322 /** Preference key for showing print marging ruler */
1323 // private final static String PRINT_MARGIN =
1324 // PreferenceConstants.EDITOR_PRINT_MARGIN;
1325 /** Preference key for print margin ruler color */
1326 // private final static String PRINT_MARGIN_COLOR =
1327 // PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
1328 /** Preference key for print margin ruler column */
1329 // private final static String PRINT_MARGIN_COLUMN =
1330 // PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
1331 /** Preference key for inserting spaces rather than tabs */
1332 private final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
1334 /** Preference key for error indication */
1335 // private final static String ERROR_INDICATION =
1336 // PreferenceConstants.EDITOR_PROBLEM_INDICATION;
1337 /** Preference key for error color */
1338 // private final static String ERROR_INDICATION_COLOR =
1339 // PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
1340 /** Preference key for warning indication */
1341 // private final static String WARNING_INDICATION =
1342 // PreferenceConstants.EDITOR_WARNING_INDICATION;
1343 /** Preference key for warning color */
1344 // private final static String WARNING_INDICATION_COLOR =
1345 // PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
1346 /** Preference key for task indication */
1347 private final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
1349 /** Preference key for task color */
1350 private final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
1352 /** Preference key for bookmark indication */
1353 private final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
1355 /** Preference key for bookmark color */
1356 private final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
1358 /** Preference key for search result indication */
1359 private final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
1361 /** Preference key for search result color */
1362 private final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
1364 /** Preference key for unknown annotation indication */
1365 private final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
1367 /** Preference key for unknown annotation color */
1368 private final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
1370 /** Preference key for linked position color */
1371 private final static String LINKED_POSITION_COLOR = PreferenceConstants.EDITOR_LINKED_POSITION_COLOR;
1373 /** Preference key for shwoing the overview ruler */
1374 private final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
1376 /** Preference key for error indication in overview ruler */
1377 private final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
1379 /** Preference key for warning indication in overview ruler */
1380 private final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
1382 /** Preference key for task indication in overview ruler */
1383 private final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
1385 /** Preference key for bookmark indication in overview ruler */
1386 private final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1388 /** Preference key for search result indication in overview ruler */
1389 private final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1391 /** Preference key for unknown annotation indication in overview ruler */
1392 private final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1394 /** Preference key for automatically closing double quoted strings */
1395 private final static String CLOSE_STRINGS_DQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_DQ_PHP;
1397 /** Preference key for automatically closing single quoted strings */
1398 private final static String CLOSE_STRINGS_SQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_SQ_PHP;
1400 /** Preference key for automatically wrapping Java strings */
1401 // private final static String WRAP_STRINGS =
1402 // PreferenceConstants.EDITOR_WRAP_STRINGS_DQ;
1403 /** Preference key for automatically closing brackets and parenthesis */
1404 private final static String CLOSE_BRACKETS_PHP = PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP;
1406 /** Preference key for automatically closing phpdocs and comments */
1407 private final static String CLOSE_JAVADOCS = PreferenceConstants.EDITOR_CLOSE_JAVADOCS;
1409 /** Preference key for automatically adding phpdoc tags */
1410 private final static String ADD_JAVADOC_TAGS = PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS;
1412 /** Preference key for automatically formatting phpdocs */
1413 // private final static String FORMAT_JAVADOCS =
1414 // PreferenceConstants.EDITOR_FORMAT_JAVADOCS;
1415 /** Preference key for automatically closing strings */
1416 private final static String CLOSE_STRINGS_HTML = PreferenceConstants.EDITOR_CLOSE_STRINGS_HTML;
1418 /** Preference key for automatically closing brackets and parenthesis */
1419 private final static String CLOSE_BRACKETS_HTML = PreferenceConstants.EDITOR_CLOSE_BRACKETS_HTML;
1421 /** Preference key for smart paste */
1422 private final static String SMART_PASTE = PreferenceConstants.EDITOR_SMART_PASTE;
1424 // private final static class AnnotationInfo {
1425 // public String fColorPreference;
1426 // public String fOverviewRulerPreference;
1427 // public String fEditorPreference;
1429 // private final static Map ANNOTATION_MAP;
1432 // AnnotationInfo info;
1433 // ANNOTATION_MAP = new HashMap();
1435 // info = new AnnotationInfo();
1436 // info.fColorPreference = TASK_INDICATION_COLOR;
1437 // info.fOverviewRulerPreference = TASK_INDICATION_IN_OVERVIEW_RULER;
1438 // info.fEditorPreference = TASK_INDICATION;
1439 // ANNOTATION_MAP.put(AnnotationType.TASK, info);
1441 // info = new AnnotationInfo();
1442 // info.fColorPreference = ERROR_INDICATION_COLOR;
1443 // info.fOverviewRulerPreference = ERROR_INDICATION_IN_OVERVIEW_RULER;
1444 // info.fEditorPreference = ERROR_INDICATION;
1445 // ANNOTATION_MAP.put(AnnotationType.ERROR, info);
1447 // info = new AnnotationInfo();
1448 // info.fColorPreference = WARNING_INDICATION_COLOR;
1449 // info.fOverviewRulerPreference = WARNING_INDICATION_IN_OVERVIEW_RULER;
1450 // info.fEditorPreference = WARNING_INDICATION;
1451 // ANNOTATION_MAP.put(AnnotationType.WARNING, info);
1453 // info = new AnnotationInfo();
1454 // info.fColorPreference = BOOKMARK_INDICATION_COLOR;
1455 // info.fOverviewRulerPreference = BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1456 // info.fEditorPreference = BOOKMARK_INDICATION;
1457 // ANNOTATION_MAP.put(AnnotationType.BOOKMARK, info);
1459 // info = new AnnotationInfo();
1460 // info.fColorPreference = SEARCH_RESULT_INDICATION_COLOR;
1461 // info.fOverviewRulerPreference =
1462 // SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1463 // info.fEditorPreference = SEARCH_RESULT_INDICATION;
1464 // ANNOTATION_MAP.put(AnnotationType.SEARCH, info);
1466 // info = new AnnotationInfo();
1467 // info.fColorPreference = UNKNOWN_INDICATION_COLOR;
1468 // info.fOverviewRulerPreference = UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1469 // info.fEditorPreference = UNKNOWN_INDICATION;
1470 // ANNOTATION_MAP.put(AnnotationType.UNKNOWN, info);
1473 // private final static AnnotationType[] ANNOTATION_LAYERS =
1474 // new AnnotationType[] {
1475 // AnnotationType.UNKNOWN,
1476 // AnnotationType.BOOKMARK,
1477 // AnnotationType.TASK,
1478 // AnnotationType.SEARCH,
1479 // AnnotationType.WARNING,
1480 // AnnotationType.ERROR };
1482 * Creates a new php unit editor.
1486 * Reconciling listeners.
1490 private ListenerList fReconcilingListeners = new ListenerList();
1493 * Mutex for the reconciler. See
1494 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of
1497 * TODO remove once the underlying problem is solved.
1500 private final Object fReconcilerLock = new Object();
1502 public PHPUnitEditor() {
1504 setDocumentProvider(WebUI.getDefault()
1505 .getCompilationUnitDocumentProvider());
1506 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
1507 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
1508 setOutlinerContextMenuId("#PHPOutlinerContext"); //$NON-NLS-1$
1509 // don't set help contextId, we install our own help context
1511 fJavaEditorErrorTickUpdater = new JavaEditorErrorTickUpdater(this);
1515 * @see AbstractTextEditor#createActions()
1517 protected void createActions() {
1518 super.createActions();
1520 // Action action= new
1521 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1522 // "CorrectionAssistProposal.", this, CORRECTIONASSIST_PROPOSALS);
1524 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CORRECTION_ASSIST_PROPOSALS);
1525 // setAction("CorrectionAssistProposal", action); //$NON-NLS-1$
1526 // markAsStateDependentAction("CorrectionAssistProposal", true);
1528 // // WorkbenchHelp.setHelp(action,
1529 // IJavaHelpContextIds.QUICK_FIX_ACTION);
1530 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(),
1531 "ContentAssistProposal.", this); //$NON-NLS-1$
1533 .setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
1534 setAction("ContentAssistProposal", action); //$NON-NLS-1$
1535 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
1536 // WorkbenchHelp.setHelp(action,
1537 // IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
1539 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1540 // "ContentAssistContextInformation.", this,
1541 // ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION); //$NON-NLS-1$
1543 // .setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
1544 // setAction("ContentAssistContextInformation", action); //$NON-NLS-1$
1545 // markAsStateDependentAction("ContentAssistContextInformation", true);
1547 // WorkbenchHelp.setHelp(action,
1548 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1550 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1551 // "ContentAssistCompletePrefix.", this, CONTENTASSIST_COMPLETE_PREFIX);
1553 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_COMPLETE_PREFIX);
1554 // setAction("ContentAssistCompletePrefix", action); //$NON-NLS-1$
1555 // markAsStateDependentAction("ContentAssistCompletePrefix", true);
1557 // // WorkbenchHelp.setHelp(action,
1558 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1559 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1560 "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
1561 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
1562 setAction("Comment", action); //$NON-NLS-1$
1563 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
1564 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
1565 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1566 "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
1567 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
1568 setAction("Uncomment", action); //$NON-NLS-1$
1569 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
1570 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
1572 action = new ToggleCommentAction(PHPEditorMessages.getResourceBundle(),
1573 "ToggleComment.", this); //$NON-NLS-1$
1575 .setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_COMMENT);
1576 setAction("ToggleComment", action); //$NON-NLS-1$
1577 markAsStateDependentAction("ToggleComment", true); //$NON-NLS-1$
1578 // WorkbenchHelp.setHelp(action,
1579 // IJavaHelpContextIds.TOGGLE_COMMENT_ACTION);
1580 configureToggleCommentAction();
1582 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1583 "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
1584 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
1585 setAction("Format", action); //$NON-NLS-1$
1586 markAsStateDependentAction("Format", true); //$NON-NLS-1$
1587 markAsSelectionDependentAction("Format", true); //$NON-NLS-1$
1588 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
1591 // AddBlockCommentAction(PHPEditorMessages.getResourceBundle(),
1592 // "AddBlockComment.", this); //$NON-NLS-1$
1594 // .setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1595 // setAction("AddBlockComment", action); //$NON-NLS-1$
1596 // markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1597 // markAsSelectionDependentAction("AddBlockComment", true);
1599 // // WorkbenchHelp.setHelp(action,
1600 // // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1601 // action = new RemoveBlockCommentAction(
1602 // PHPEditorMessages.getResourceBundle(), "RemoveBlockComment.", this);
1605 // .setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1606 // setAction("RemoveBlockComment", action); //$NON-NLS-1$
1607 // markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1608 // markAsSelectionDependentAction("RemoveBlockComment", true);
1610 // WorkbenchHelp.setHelp(action,
1611 // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1612 action = new IndentAction(PHPEditorMessages.getResourceBundle(),
1613 "Indent.", this, false); //$NON-NLS-1$
1614 action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1615 setAction("Indent", action); //$NON-NLS-1$
1616 markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1617 markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1618 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1620 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1621 // "Indent.", this, true); //$NON-NLS-1$
1622 // setAction("IndentOnTab", action); //$NON-NLS-1$
1623 // markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1624 // markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1627 action = new AddBlockCommentAction(PHPEditorMessages
1628 .getResourceBundle(), "AddBlockComment.", this); //$NON-NLS-1$
1630 .setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1631 setAction("AddBlockComment", action); //$NON-NLS-1$
1632 markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1633 markAsSelectionDependentAction("AddBlockComment", true); //$NON-NLS-1$
1634 // WorkbenchHelp.setHelp(action,
1635 // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1637 action = new RemoveBlockCommentAction(PHPEditorMessages
1638 .getResourceBundle(), "RemoveBlockComment.", this); //$NON-NLS-1$
1640 .setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1641 setAction("RemoveBlockComment", action); //$NON-NLS-1$
1642 markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1643 markAsSelectionDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1644 // WorkbenchHelp.setHelp(action,
1645 // IJavaHelpContextIds.REMOVE_BLOCK_COMMENT_ACTION);
1647 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1648 // "Indent.", this, false); //$NON-NLS-1$
1649 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1650 // setAction("Indent", action); //$NON-NLS-1$
1651 // markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1652 // markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1653 // // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1655 action = new IndentAction(PHPEditorMessages.getResourceBundle(),
1656 "Indent.", this, true); //$NON-NLS-1$
1657 setAction("IndentOnTab", action); //$NON-NLS-1$
1658 markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1659 markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1661 if (getPreferenceStore().getBoolean(
1662 PreferenceConstants.EDITOR_SMART_TAB)) {
1663 // don't replace Shift Right - have to make sure their enablement is
1664 // mutually exclusive
1665 // removeActionActivationCode(ITextEditorActionConstants.SHIFT_RIGHT);
1666 setActionActivationCode("IndentOnTab", '\t', -1, SWT.NONE); //$NON-NLS-1$
1668 fGenerateActionGroup = new GenerateActionGroup(this,
1669 ITextEditorActionConstants.GROUP_EDIT);
1670 // ActionGroup rg= new RefactorActionGroup(this,
1671 // ITextEditorActionConstants.GROUP_EDIT);
1673 // fActionGroups.addGroup(rg);
1674 fActionGroups.addGroup(fGenerateActionGroup);
1676 // We have to keep the context menu group separate to have better
1677 // control over positioning
1678 fContextMenuGroup = new CompositeActionGroup(
1679 new ActionGroup[] { fGenerateActionGroup
1681 // new LocalHistoryActionGroup(this,
1682 // ITextEditorActionConstants.GROUP_EDIT)
1688 * @see JavaEditor#getElementAt(int)
1690 protected IJavaElement getElementAt(int offset) {
1691 return getElementAt(offset, true);
1695 * Returns the most narrow element including the given offset. If
1696 * <code>reconcile</code> is <code>true</code> the editor's input
1697 * element is reconciled in advance. If it is <code>false</code> this
1698 * method only returns a result if the editor's input element does not need
1702 * the offset included by the retrieved element
1704 * <code>true</code> if working copy should be reconciled
1706 protected IJavaElement getElementAt(int offset, boolean reconcile) {
1707 IWorkingCopyManager manager = WebUI.getDefault()
1708 .getWorkingCopyManager();
1709 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
1713 synchronized (unit) {
1716 return unit.getElementAt(offset);
1717 } else if (unit.isConsistent())
1718 return unit.getElementAt(offset);
1719 } catch (JavaModelException x) {
1720 PHPeclipsePlugin.log(x.getStatus());
1721 // nothing found, be tolerant and go on
1728 * @see JavaEditor#getCorrespondingElement(IJavaElement)
1730 protected IJavaElement getCorrespondingElement(IJavaElement element) {
1732 return EditorUtility.getWorkingCopy(element, true);
1733 } catch (JavaModelException x) {
1734 PHPeclipsePlugin.log(x.getStatus());
1735 // nothing found, be tolerant and go on
1740 public void createPartControl(Composite parent) {
1741 super.createPartControl(parent);
1742 // fPaintManager = new PaintManager(getSourceViewer());
1743 LinePainter linePainter;
1744 linePainter = new LinePainter(getSourceViewer());
1745 linePainter.setHighlightColor(new Color(Display.getCurrent(), 225, 235,
1747 // fPaintManager.addPainter(linePainter);
1748 // if (isBracketHighlightingEnabled())
1749 // startBracketHighlighting();
1750 // if (isLineHighlightingEnabled())
1751 // startLineHighlighting();
1752 // if (isPrintMarginVisible())
1753 // showPrintMargin();
1754 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1755 // while (e.hasNext()) {
1756 // AnnotationType type = (AnnotationType) e.next();
1757 // if (isAnnotationIndicationEnabled(type))
1758 // startAnnotationIndication(type);
1760 if (isTabConversionEnabled())
1761 startTabConversion();
1762 // if (isOverviewRulerVisible())
1763 // showOverviewRuler();
1765 // Preferences preferences =
1766 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1767 // preferences.addPropertyChangeListener(fPropertyChangeListener);
1768 IPreferenceStore preferenceStore = getPreferenceStore();
1769 boolean closeBracketsPHP = preferenceStore
1770 .getBoolean(CLOSE_BRACKETS_PHP);
1771 boolean closeStringsPHPDQ = preferenceStore
1772 .getBoolean(CLOSE_STRINGS_DQ_PHP);
1773 boolean closeStringsPHPSQ = preferenceStore
1774 .getBoolean(CLOSE_STRINGS_SQ_PHP);
1775 fBracketInserter.setCloseBracketsPHPEnabled(closeBracketsPHP);
1776 fBracketInserter.setCloseStringsPHPDQEnabled(closeStringsPHPDQ);
1777 fBracketInserter.setCloseStringsPHPSQEnabled(closeStringsPHPSQ);
1778 ISourceViewer sourceViewer = getSourceViewer();
1779 if (sourceViewer instanceof ITextViewerExtension)
1780 ((ITextViewerExtension) sourceViewer)
1781 .prependVerifyKeyListener(fBracketInserter);
1784 private static char getPeerCharacter(char character) {
1785 switch (character) {
1801 throw new IllegalArgumentException();
1805 // private void startBracketHighlighting() {
1806 // if (fBracketPainter == null) {
1807 // ISourceViewer sourceViewer = getSourceViewer();
1808 // fBracketPainter = new BracketPainter(sourceViewer);
1809 // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1810 // // fPaintManager.addPainter(fBracketPainter);
1814 // private void stopBracketHighlighting() {
1815 // if (fBracketPainter != null) {
1816 // // fPaintManager.removePainter(fBracketPainter);
1817 // fBracketPainter.deactivate(true);
1818 // fBracketPainter.dispose();
1819 // fBracketPainter = null;
1823 // private boolean isBracketHighlightingEnabled() {
1824 // IPreferenceStore store = getPreferenceStore();
1825 // return store.getBoolean(MATCHING_BRACKETS);
1828 // private void startLineHighlighting() {
1829 // if (fLinePainter == null) {
1830 // ISourceViewer sourceViewer = getSourceViewer();
1831 // fLinePainter = new LinePainter(sourceViewer);
1832 // fLinePainter.setHighlightColor(getColor(CURRENT_LINE_COLOR));
1833 // // fPaintManager.addPainter(fLinePainter);
1837 // private void stopLineHighlighting() {
1838 // if (fLinePainter != null) {
1839 // // fPaintManager.removePainter(fLinePainter);
1840 // fLinePainter.deactivate(true);
1841 // fLinePainter.dispose();
1842 // fLinePainter = null;
1846 // private boolean isLineHighlightingEnabled() {
1847 // IPreferenceStore store = getPreferenceStore();
1848 // return store.getBoolean(CURRENT_LINE);
1851 // private void showPrintMargin() {
1852 // if (fPrintMarginPainter == null) {
1853 // fPrintMarginPainter = new PrintMarginPainter(getSourceViewer());
1854 // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
1855 // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
1856 // // fPaintManager.addPainter(fPrintMarginPainter);
1860 // private void hidePrintMargin() {
1861 // if (fPrintMarginPainter != null) {
1862 // // fPaintManager.removePainter(fPrintMarginPainter);
1863 // fPrintMarginPainter.deactivate(true);
1864 // fPrintMarginPainter.dispose();
1865 // fPrintMarginPainter = null;
1869 // private boolean isPrintMarginVisible() {
1870 // IPreferenceStore store = getPreferenceStore();
1871 // return store.getBoolean(PRINT_MARGIN);
1874 private int getTabSize() {
1875 Preferences preferences = PHPeclipsePlugin.getDefault()
1876 .getPluginPreferences();
1877 return preferences.getInt(CODE_FORMATTER_TAB_SIZE);
1880 private boolean isTabConversionEnabled() {
1881 IPreferenceStore store = getPreferenceStore();
1882 return store.getBoolean(SPACES_FOR_TABS);
1885 private Color getColor(String key) {
1886 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
1887 return getColor(rgb);
1890 private Color getColor(RGB rgb) {
1891 JavaTextTools textTools = WebUI.getDefault()
1892 .getJavaTextTools();
1893 return textTools.getColorManager().getColor(rgb);
1896 // private Color getColor(AnnotationType annotationType) {
1897 // AnnotationInfo info = (AnnotationInfo)
1898 // ANNOTATION_MAP.get(annotationType);
1899 // if (info != null)
1900 // return getColor(info.fColorPreference);
1903 public void dispose() {
1904 ISourceViewer sourceViewer = getSourceViewer();
1905 if (sourceViewer instanceof ITextViewerExtension)
1906 ((ITextViewerExtension) sourceViewer)
1907 .removeVerifyKeyListener(fBracketInserter);
1908 // if (fPropertyChangeListener != null) {
1909 // Preferences preferences =
1910 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1911 // preferences.removePropertyChangeListener(fPropertyChangeListener);
1912 // fPropertyChangeListener = null;
1914 if (fJavaEditorErrorTickUpdater != null) {
1915 fJavaEditorErrorTickUpdater.dispose();
1916 fJavaEditorErrorTickUpdater = null;
1918 // if (fSelectionHistory != null)
1919 // fSelectionHistory.dispose();
1920 // if (fPaintManager != null) {
1921 // fPaintManager.dispose();
1922 // fPaintManager = null;
1924 if (fActionGroups != null) {
1925 fActionGroups.dispose();
1926 fActionGroups = null;
1931 // protected AnnotationType getAnnotationType(String preferenceKey) {
1932 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1933 // while (e.hasNext()) {
1934 // AnnotationType type = (AnnotationType) e.next();
1935 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(type);
1936 // if (info != null) {
1937 // if (preferenceKey.equals(info.fColorPreference)
1938 // || preferenceKey.equals(info.fEditorPreference)
1939 // || preferenceKey.equals(info.fOverviewRulerPreference))
1946 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1948 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1950 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1952 String p = event.getProperty();
1953 if (CLOSE_BRACKETS_PHP.equals(p)) {
1955 .setCloseBracketsPHPEnabled(getPreferenceStore()
1959 if (CLOSE_STRINGS_DQ_PHP.equals(p)) {
1961 .setCloseStringsPHPDQEnabled(getPreferenceStore()
1965 if (CLOSE_STRINGS_SQ_PHP.equals(p)) {
1967 .setCloseStringsPHPSQEnabled(getPreferenceStore()
1971 if (SPACES_FOR_TABS.equals(p)) {
1972 if (isTabConversionEnabled())
1973 startTabConversion();
1975 stopTabConversion();
1978 // if (MATCHING_BRACKETS.equals(p)) {
1979 // if (isBracketHighlightingEnabled())
1980 // startBracketHighlighting();
1982 // stopBracketHighlighting();
1985 // if (MATCHING_BRACKETS_COLOR.equals(p)) {
1986 // if (fBracketPainter != null)
1987 // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1990 // if (CURRENT_LINE.equals(p)) {
1991 // if (isLineHighlightingEnabled())
1992 // startLineHighlighting();
1994 // stopLineHighlighting();
1997 // if (CURRENT_LINE_COLOR.equals(p)) {
1998 // if (fLinePainter != null) {
1999 // stopLineHighlighting();
2000 // startLineHighlighting();
2004 // if (PRINT_MARGIN.equals(p)) {
2005 // if (isPrintMarginVisible())
2006 // showPrintMargin();
2008 // hidePrintMargin();
2011 // if (PRINT_MARGIN_COLOR.equals(p)) {
2012 // if (fPrintMarginPainter != null)
2013 // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
2016 // if (PRINT_MARGIN_COLUMN.equals(p)) {
2017 // if (fPrintMarginPainter != null)
2018 // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
2021 // if (OVERVIEW_RULER.equals(p)) {
2022 // if (isOverviewRulerVisible())
2023 // showOverviewRuler();
2025 // hideOverviewRuler();
2028 // AnnotationType type = getAnnotationType(p);
2029 // if (type != null) {
2031 // AnnotationInfo info = (AnnotationInfo)
2032 // ANNOTATION_MAP.get(type);
2033 // if (info.fColorPreference.equals(p)) {
2034 // Color color = getColor(type);
2035 // if (fProblemPainter != null) {
2036 // fProblemPainter.setColor(type, color);
2037 // fProblemPainter.paint(IPainter.CONFIGURATION);
2039 // setColorInOverviewRuler(type, color);
2043 // if (info.fEditorPreference.equals(p)) {
2044 // if (isAnnotationIndicationEnabled(type))
2045 // startAnnotationIndication(type);
2047 // stopAnnotationIndication(type);
2051 // if (info.fOverviewRulerPreference.equals(p)) {
2052 // if (isAnnotationIndicationInOverviewRulerEnabled(type))
2053 // showAnnotationIndicationInOverviewRuler(type, true);
2055 // showAnnotationIndicationInOverviewRuler(type, false);
2059 IContentAssistant c = asv.getContentAssistant();
2060 if (c instanceof ContentAssistant)
2061 ContentAssistPreference.changeConfiguration(
2062 (ContentAssistant) c, getPreferenceStore(), event);
2065 super.handlePreferenceStoreChanged(event);
2070 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
2072 protected void handlePreferencePropertyChanged(
2073 org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
2074 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2076 String p = event.getProperty();
2077 if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
2078 asv.updateIndentationPrefixes();
2079 if (fTabConverter != null)
2080 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2083 super.handlePreferencePropertyChanged(event);
2087 * Handles a property change event describing a change of the php core's
2088 * preferences and updates the preference related editor properties.
2091 * the property change event
2094 // handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
2096 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2097 // if (asv != null) {
2098 // String p = event.getProperty();
2099 // if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
2100 // asv.updateIndentationPrefixes();
2101 // if (fTabConverter != null)
2102 // fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2107 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#createJavaSourceViewer(org.eclipse.swt.widgets.Composite,
2108 * org.eclipse.jface.text.source.IVerticalRuler,
2109 * org.eclipse.jface.text.source.IOverviewRuler, boolean, int)
2111 protected ISourceViewer createJavaSourceViewer(Composite parent,
2112 IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
2113 boolean isOverviewRulerVisible, int styles, IPreferenceStore store) {
2114 return new AdaptedSourceViewer(parent, verticalRuler, overviewRuler,
2115 isOverviewRulerVisible, styles, store);
2118 // protected ISourceViewer createJavaSourceViewer(Composite parent,
2119 // IVerticalRuler ruler, int styles) {
2120 // return new AdaptedSourceViewer(parent, ruler, styles);
2122 private boolean isValidSelection(int offset, int length) {
2123 IDocumentProvider provider = getDocumentProvider();
2124 if (provider != null) {
2125 IDocument document = provider.getDocument(getEditorInput());
2126 if (document != null) {
2127 int end = offset + length;
2128 int documentLength = document.getLength();
2129 return 0 <= offset && offset <= documentLength && 0 <= end
2130 && end <= documentLength;
2137 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getInputElement()
2139 protected IJavaElement getInputJavaElement() {
2140 return WebUI.getDefault().getWorkingCopyManager()
2141 .getWorkingCopy(getEditorInput());
2145 * @see AbstractTextEditor#editorContextMenuAboutToShow(IMenuManager)
2147 public void editorContextMenuAboutToShow(IMenuManager menu) {
2148 super.editorContextMenuAboutToShow(menu);
2149 ActionContext context = new ActionContext(getSelectionProvider()
2151 fContextMenuGroup.setContext(context);
2152 fContextMenuGroup.fillContextMenu(menu);
2153 fContextMenuGroup.setContext(null);
2157 * @see JavaEditor#setOutlinePageInput(JavaOutlinePage, IEditorInput)
2159 protected void setOutlinePageInput(JavaOutlinePage page, IEditorInput input) {
2161 IWorkingCopyManager manager = WebUI.getDefault()
2162 .getWorkingCopyManager();
2163 page.setInput(manager.getWorkingCopy(input));
2168 * @see AbstractTextEditor#performSaveOperation(WorkspaceModifyOperation,
2171 // protected void performSaveOperation(WorkspaceModifyOperation operation,
2172 // IProgressMonitor progressMonitor) {
2173 // IDocumentProvider p = getDocumentProvider();
2174 // if (p instanceof PHPDocumentProvider) {
2175 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2176 // cp.setSavePolicy(fSavePolicy);
2180 // super.performSaveOperation(operation, progressMonitor);
2182 // if (p instanceof PHPDocumentProvider) {
2183 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2184 // cp.setSavePolicy(null);
2189 * @see AbstractTextEditor#doSave(IProgressMonitor)
2191 public void doSave(IProgressMonitor progressMonitor) {
2193 IDocumentProvider p = getDocumentProvider();
2195 // editor has been closed
2199 if (p.isDeleted(getEditorInput())) {
2201 if (isSaveAsAllowed()) {
2204 * 1GEUSSR: ITPUI:ALL - User should never loose changes made in
2205 * the editors. Changed Behavior to make sure that if called
2206 * inside a regular save (because of deletion of input element)
2207 * there is a way to report back to the caller.
2209 performSaveAs(progressMonitor);
2214 * 1GF5YOX: ITPJUI:ALL - Save of delete file claims it's still
2215 * there Missing resources.
2217 Shell shell = getSite().getShell();
2222 .getString("PHPUnitEditor.error.saving.title1"), PHPEditorMessages.getString("PHPUnitEditor.error.saving.message1")); //$NON-NLS-1$ //$NON-NLS-2$
2226 if (getPreferenceStore().getBoolean(
2227 PreferenceConstants.EDITOR_P_RTRIM_ON_SAVE)) {
2228 RTrimAction trimAction = new RTrimAction();
2229 trimAction.setActiveEditor(null, getSite().getPage()
2230 .getActiveEditor());
2231 trimAction.run(null);
2234 setStatusLineErrorMessage(null);
2236 updateState(getEditorInput());
2237 validateState(getEditorInput());
2239 IWorkingCopyManager manager = WebUI.getDefault()
2240 .getWorkingCopyManager();
2241 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2244 synchronized (unit) {
2245 performSave(false, progressMonitor);
2248 performSave(false, progressMonitor);
2252 public boolean isSaveAsAllowed() {
2257 * The compilation unit editor implementation of this
2258 * <code>AbstractTextEditor</code> method asks the user for the workspace
2259 * path of a file resource and saves the document there. See
2260 * http://dev.eclipse.org/bugs/show_bug.cgi?id=6295
2262 * @param progressMonitor
2263 * the progress monitor
2265 protected void performSaveAs(IProgressMonitor progressMonitor) {
2267 Shell shell = getSite().getShell();
2268 IEditorInput input = getEditorInput();
2270 SaveAsDialog dialog = new SaveAsDialog(shell);
2272 IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input)
2275 if (original != null)
2276 dialog.setOriginalFile(original);
2280 IDocumentProvider provider = getDocumentProvider();
2281 if (provider == null) {
2282 // editor has been programmatically closed while the dialog was open
2286 if (provider.isDeleted(input) && original != null) {
2287 String message = PHPEditorMessages
2288 .getFormattedString(
2289 "CompilationUnitEditor.warning.save.delete", new Object[] { original.getName() }); //$NON-NLS-1$
2290 dialog.setErrorMessage(null);
2291 dialog.setMessage(message, IMessageProvider.WARNING);
2294 if (dialog.open() == Window.CANCEL) {
2295 if (progressMonitor != null)
2296 progressMonitor.setCanceled(true);
2300 IPath filePath = dialog.getResult();
2301 if (filePath == null) {
2302 if (progressMonitor != null)
2303 progressMonitor.setCanceled(true);
2307 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
2308 IFile file = workspaceRoot.getFile(filePath);
2309 final IEditorInput newInput = new FileEditorInput(file);
2311 boolean success = false;
2314 provider.aboutToChange(newInput);
2315 getDocumentProvider().saveDocument(progressMonitor, newInput,
2316 getDocumentProvider().getDocument(getEditorInput()), true);
2319 } catch (CoreException x) {
2320 IStatus status = x.getStatus();
2321 if (status == null || status.getSeverity() != IStatus.CANCEL)
2326 .getString("CompilationUnitEditor.error.saving.title2"), PHPEditorMessages.getString("CompilationUnitEditor.error.saving.message2"), x.getStatus()); //$NON-NLS-1$ //$NON-NLS-2$
2328 provider.changed(newInput);
2333 if (progressMonitor != null)
2334 progressMonitor.setCanceled(!success);
2338 * @see AbstractTextEditor#doSetInput(IEditorInput)
2340 protected void doSetInput(IEditorInput input) throws CoreException {
2341 super.doSetInput(input);
2342 configureTabConverter();
2343 configureToggleCommentAction();
2348 // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#installOverrideIndicator(boolean)
2351 // protected void installOverrideIndicator(boolean waitForReconcilation) {
2352 // IAnnotationModel model=
2353 // getDocumentProvider().getAnnotationModel(getEditorInput());
2354 // if (!waitForReconcilation)
2355 // super.installOverrideIndicator(false);
2357 // uninstallOverrideIndicator();
2358 // IJavaElement inputElement= getInputJavaElement();
2359 // if (model == null || inputElement == null)
2362 // fOverrideIndicatorManager= new OverrideIndicatorManager(model,
2363 // inputElement, null);
2364 // addReconcileListener(fOverrideIndicatorManager);
2370 // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#uninstallOverrideIndicator()
2373 // protected void uninstallOverrideIndicator() {
2374 // if (fOverrideIndicatorManager != null)
2375 // removeReconcileListener(fOverrideIndicatorManager);
2376 // super.uninstallOverrideIndicator();
2380 * Configures the toggle comment action
2384 private void configureToggleCommentAction() {
2385 IAction action = getAction("ToggleComment"); //$NON-NLS-1$
2386 if (action instanceof ToggleCommentAction) {
2387 ISourceViewer sourceViewer = getSourceViewer();
2388 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
2389 ((ToggleCommentAction) action).configure(sourceViewer,
2394 // private void configureTabConverter() {
2395 // if (fTabConverter != null) {
2396 // IDocumentProvider provider = getDocumentProvider();
2397 // if (provider instanceof PHPDocumentProvider) {
2398 // PHPDocumentProvider cup = (PHPDocumentProvider) provider;
2399 // fTabConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
2403 private void configureTabConverter() {
2404 if (fTabConverter != null) {
2405 IDocumentProvider provider = getDocumentProvider();
2406 if (provider instanceof ICompilationUnitDocumentProvider) {
2407 ICompilationUnitDocumentProvider cup = (ICompilationUnitDocumentProvider) provider;
2408 fTabConverter.setLineTracker(cup
2409 .createLineTracker(getEditorInput()));
2414 private void startTabConversion() {
2415 if (fTabConverter == null) {
2416 fTabConverter = new TabConverter();
2417 configureTabConverter();
2418 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2419 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2420 asv.addTextConverter(fTabConverter);
2421 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2422 asv.updateIndentationPrefixes();
2426 private void stopTabConversion() {
2427 if (fTabConverter != null) {
2428 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2429 asv.removeTextConverter(fTabConverter);
2430 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2431 asv.updateIndentationPrefixes();
2432 fTabConverter = null;
2437 * @see org.eclipse.ui.texteditor.AbstractTextEditor#performSave(boolean,
2438 * org.eclipse.core.runtime.IProgressMonitor)
2440 protected void performSave(boolean overwrite,
2441 IProgressMonitor progressMonitor) {
2442 // IDocumentProvider p = getDocumentProvider();
2443 // if (p instanceof PHPDocumentProvider) {
2444 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2445 // cp.setSavePolicy(fSavePolicy);
2448 // super.performSave(overwrite, progressMonitor);
2450 // if (p instanceof PHPDocumentProvider) {
2451 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2452 // cp.setSavePolicy(null);
2456 IDocumentProvider p = getDocumentProvider();
2457 if (p instanceof ICompilationUnitDocumentProvider) {
2458 ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2459 cp.setSavePolicy(fSavePolicy);
2462 super.performSave(overwrite, progressMonitor);
2464 if (p instanceof ICompilationUnitDocumentProvider) {
2465 ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2466 cp.setSavePolicy(null);
2472 * @see AbstractTextEditor#doSaveAs
2474 public void doSaveAs() {
2475 if (askIfNonWorkbenchEncodingIsOk()) {
2481 * Asks the user if it is ok to store in non-workbench encoding.
2483 * @return <true>if the user wants to continue
2485 private boolean askIfNonWorkbenchEncodingIsOk() {
2486 IDocumentProvider provider = getDocumentProvider();
2487 if (provider instanceof IStorageDocumentProvider) {
2488 IEditorInput input = getEditorInput();
2489 IStorageDocumentProvider storageProvider = (IStorageDocumentProvider) provider;
2490 String encoding = storageProvider.getEncoding(input);
2491 String defaultEncoding = storageProvider.getDefaultEncoding();
2492 if (encoding != null && !encoding.equals(defaultEncoding)) {
2493 Shell shell = getSite().getShell();
2494 String title = PHPEditorMessages
2495 .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.title"); //$NON-NLS-1$
2501 .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message1"),
2502 new String[] { input.getName(), encoding }); //$NON-NLS-1$
2507 .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message2"),
2508 new String[] { encoding }); //$NON-NLS-1$
2509 return MessageDialog.openQuestion(shell, title, msg);
2516 * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#aboutToBeReconciled()
2519 public void aboutToBeReconciled() {
2521 // Notify AST provider
2522 // PHPeclipsePlugin.getDefault().getASTProvider().aboutToBeReconciled(getInputJavaElement());
2525 Object[] listeners = fReconcilingListeners.getListeners();
2526 for (int i = 0, length = listeners.length; i < length; ++i)
2527 ((IJavaReconcilingListener) listeners[i]).aboutToBeReconciled();
2531 * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#reconciled(CompilationUnit,
2532 * boolean, IProgressMonitor)
2535 public void reconciled(CompilationUnit ast, boolean forced,
2536 IProgressMonitor progressMonitor) {
2538 // Always notify AST provider
2539 // PHPeclipsePlugin.getDefault().getASTProvider().reconciled(ast,
2540 // getInputJavaElement());
2543 // Object[] listeners = fReconcilingListeners.getListeners();
2544 // for (int i = 0, length= listeners.length; i < length; ++i)
2545 // ((IJavaReconcilingListener)listeners[i]).reconciled(ast, forced,
2546 // progressMonitor);
2548 // Update Java Outline page selection
2549 if (!forced && !progressMonitor.isCanceled()) {
2550 Shell shell = getSite().getShell();
2551 if (shell != null && !shell.isDisposed()) {
2552 shell.getDisplay().asyncExec(new Runnable() {
2562 * Returns the updated java element for the old java element.
2564 private IJavaElement findElement(IJavaElement element) {
2565 if (element == null)
2567 IWorkingCopyManager manager = WebUI.getDefault()
2568 .getWorkingCopyManager();
2569 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2572 synchronized (unit) {
2575 IJavaElement[] findings = unit.findElements(element);
2576 if (findings != null && findings.length > 0)
2578 } catch (JavaModelException x) {
2579 PHPeclipsePlugin.log(x.getStatus());
2580 // nothing found, be tolerant and go on
2587 * Returns the offset of the given Java element.
2589 private int getOffset(IJavaElement element) {
2590 if (element instanceof ISourceReference) {
2591 ISourceReference sr = (ISourceReference) element;
2593 ISourceRange srcRange = sr.getSourceRange();
2594 if (srcRange != null)
2595 return srcRange.getOffset();
2596 } catch (JavaModelException e) {
2603 * @see AbstractTextEditor#restoreSelection()
2605 // protected void restoreSelection() {
2607 // if (getSourceViewer() == null || fRememberedSelection == null)
2609 // IJavaElement newElement = findElement(fRememberedElement);
2610 // int newOffset = getOffset(newElement);
2611 // int delta = (newOffset > -1 && fRememberedElementOffset > -1) ? newOffset
2612 // - fRememberedElementOffset : 0;
2613 // if (isValidSelection(delta + fRememberedSelection.getOffset(),
2614 // fRememberedSelection.getLength()))
2615 // selectAndReveal(delta + fRememberedSelection.getOffset(),
2616 // fRememberedSelection.getLength());
2618 // fRememberedSelection = null;
2619 // fRememberedElement = null;
2620 // fRememberedElementOffset = -1;
2624 * Tells whether this is the active editor in the active page.
2626 * @return <code>true</code> if this is the active editor in the active
2628 * @see IWorkbenchPage#getActiveEditor();
2630 protected final boolean isActiveEditor() {
2631 IWorkbenchWindow window = getSite().getWorkbenchWindow();
2632 IWorkbenchPage page = window.getActivePage();
2635 IEditorPart activeEditor = page.getActiveEditor();
2636 return activeEditor != null && activeEditor.equals(this);
2640 * Adds the given listener. Has no effect if an identical listener was not
2641 * already registered.
2644 * The reconcile listener to be added
2647 final void addReconcileListener(IJavaReconcilingListener listener) {
2648 synchronized (fReconcilingListeners) {
2649 fReconcilingListeners.add(listener);
2654 * Removes the given listener. Has no effect if an identical listener was
2655 * not already registered.
2658 * the reconcile listener to be removed
2661 final void removeReconcileListener(IJavaReconcilingListener listener) {
2662 synchronized (fReconcilingListeners) {
2663 fReconcilingListeners.remove(listener);
2667 protected void updateStateDependentActions() {
2668 super.updateStateDependentActions();
2669 fGenerateActionGroup.editorStateChanged();
2673 * @see AbstractTextEditor#rememberSelection()
2675 protected void rememberSelection() {
2676 fRememberedSelection.remember();
2680 * @see AbstractTextEditor#restoreSelection()
2682 protected void restoreSelection() {
2683 fRememberedSelection.restore();
2687 * @see AbstractTextEditor#canHandleMove(IEditorInput, IEditorInput)
2689 protected boolean canHandleMove(IEditorInput originalElement,
2690 IEditorInput movedElement) {
2692 String oldExtension = ""; //$NON-NLS-1$
2693 if (originalElement instanceof IFileEditorInput) {
2694 IFile file = ((IFileEditorInput) originalElement).getFile();
2696 String ext = file.getFileExtension();
2702 String newExtension = ""; //$NON-NLS-1$
2703 if (movedElement instanceof IFileEditorInput) {
2704 IFile file = ((IFileEditorInput) movedElement).getFile();
2706 newExtension = file.getFileExtension();
2709 return oldExtension.equals(newExtension);
2713 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isPrefQuickDiffAlwaysOn()
2715 protected boolean isPrefQuickDiffAlwaysOn() {
2716 // reestablishes the behaviour from AbstractDecoratedTextEditor which
2717 // was hacked by JavaEditor
2718 // to disable the change bar for the class file (attached source) java
2720 IPreferenceStore store = getPreferenceStore();
2722 .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_ALWAYS_ON);
2726 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getAdapter(java.lang.Class)
2728 public Object getAdapter(Class required) {
2729 if (SmartBackspaceManager.class.equals(required)) {
2730 if (getSourceViewer() instanceof JavaSourceViewer) {
2731 return ((JavaSourceViewer) getSourceViewer())
2732 .getBackspaceManager();
2736 return super.getAdapter(required);
2740 * Returns the mutex for the reconciler. See
2741 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of
2744 * TODO remove once the underlying problem is solved.
2747 * @return the lock reconcilers may use to synchronize on
2749 public Object getReconcilerLock() {
2750 return fReconcilerLock;
2756 * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
2758 protected void editorSaved() {
2759 super.editorSaved();
2760 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();
2762 //a.refresh(ShowExternalPreviewAction.PHP_TYPE);
2763 a.doRun(ShowExternalPreviewAction.PHP_TYPE);