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.ui.actions.AddBlockCommentAction;
21 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
22 import net.sourceforge.phpdt.internal.ui.actions.IndentAction;
23 import net.sourceforge.phpdt.internal.ui.actions.RemoveBlockCommentAction;
24 import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
25 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
26 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
27 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
28 import net.sourceforge.phpdt.internal.ui.text.SmartSemicolonAutoEditStrategy;
29 import net.sourceforge.phpdt.internal.ui.text.comment.CommentFormattingContext;
30 import net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener;
31 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
32 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
33 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI.ExitFlags;
34 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
35 import net.sourceforge.phpdt.ui.PreferenceConstants;
36 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
37 import net.sourceforge.phpdt.ui.text.JavaTextTools;
38 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
39 import net.sourceforge.phpeclipse.phpeditor.actions.RTrimAction;
40 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
42 import org.eclipse.core.internal.runtime.ListenerList;
43 import org.eclipse.core.resources.IFile;
44 import org.eclipse.core.resources.IWorkspaceRoot;
45 import org.eclipse.core.resources.ResourcesPlugin;
46 import org.eclipse.core.runtime.CoreException;
47 import org.eclipse.core.runtime.IPath;
48 import org.eclipse.core.runtime.IProgressMonitor;
49 import org.eclipse.core.runtime.IStatus;
50 import org.eclipse.core.runtime.Preferences;
51 import org.eclipse.jface.action.Action;
52 import org.eclipse.jface.action.IAction;
53 import org.eclipse.jface.action.IMenuManager;
54 import org.eclipse.jface.dialogs.ErrorDialog;
55 import org.eclipse.jface.dialogs.IMessageProvider;
56 import org.eclipse.jface.dialogs.MessageDialog;
57 import org.eclipse.jface.preference.IPreferenceStore;
58 import org.eclipse.jface.preference.PreferenceConverter;
59 import org.eclipse.jface.text.BadLocationException;
60 import org.eclipse.jface.text.DocumentCommand;
61 import org.eclipse.jface.text.IAutoEditStrategy;
62 import org.eclipse.jface.text.IDocument;
63 import org.eclipse.jface.text.ILineTracker;
64 import org.eclipse.jface.text.IRegion;
65 import org.eclipse.jface.text.ITextOperationTarget;
66 import org.eclipse.jface.text.ITextViewerExtension;
67 import org.eclipse.jface.text.ITypedRegion;
68 import org.eclipse.jface.text.IWidgetTokenKeeper;
69 import org.eclipse.jface.text.contentassist.ContentAssistant;
70 import org.eclipse.jface.text.contentassist.IContentAssistant;
71 import org.eclipse.jface.text.formatter.FormattingContextProperties;
72 import org.eclipse.jface.text.formatter.IFormattingContext;
73 import org.eclipse.jface.text.source.IOverviewRuler;
74 import org.eclipse.jface.text.source.ISourceViewer;
75 import org.eclipse.jface.text.source.IVerticalRuler;
76 import org.eclipse.jface.text.source.SourceViewerConfiguration;
77 import org.eclipse.jface.util.PropertyChangeEvent;
78 import org.eclipse.jface.window.Window;
79 import org.eclipse.swt.SWT;
80 import org.eclipse.swt.custom.VerifyKeyListener;
81 import org.eclipse.swt.events.VerifyEvent;
82 import org.eclipse.swt.graphics.Color;
83 import org.eclipse.swt.graphics.Point;
84 import org.eclipse.swt.graphics.RGB;
85 import org.eclipse.swt.widgets.Composite;
86 import org.eclipse.swt.widgets.Display;
87 import org.eclipse.swt.widgets.Shell;
88 import org.eclipse.ui.IEditorInput;
89 import org.eclipse.ui.IEditorPart;
90 import org.eclipse.ui.IFileEditorInput;
91 import org.eclipse.ui.IWorkbenchPage;
92 import org.eclipse.ui.IWorkbenchWindow;
93 import org.eclipse.ui.actions.ActionContext;
94 import org.eclipse.ui.actions.ActionGroup;
95 import org.eclipse.ui.dialogs.SaveAsDialog;
96 import org.eclipse.ui.editors.text.IStorageDocumentProvider;
97 import org.eclipse.ui.help.WorkbenchHelp;
98 import org.eclipse.ui.part.FileEditorInput;
99 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
100 import org.eclipse.ui.texteditor.ContentAssistAction;
101 import org.eclipse.ui.texteditor.IDocumentProvider;
102 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
103 import org.eclipse.ui.texteditor.TextOperationAction;
105 /*******************************************************************************
106 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
107 * program and the accompanying materials are made available under the terms of
108 * the Common Public License v1.0 which accompanies this distribution, and is
109 * available at http://www.eclipse.org/legal/cpl-v10.html
111 * Contributors: IBM Corporation - Initial implementation
113 ******************************************************************************/
115 * PHP specific text editor.
117 public class PHPUnitEditor extends PHPEditor { //implements
118 // IJavaReconcilingListener {
119 interface ITextConverter {
120 void customizeDocumentCommand(IDocument document, DocumentCommand command);
123 // class AdaptedSourceViewer extends JavaSourceViewer {
124 // private List fTextConverters;
126 // private boolean fIgnoreTextConverters = false;
128 // // private JavaCorrectionAssistant fCorrectionAssistant;
129 // public AdaptedSourceViewer(Composite parent, IVerticalRuler verticalRuler,
130 // IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
131 // int styles, IPreferenceStore store) {
132 // super(parent, verticalRuler, overviewRuler, showAnnotationsOverview,
136 // // public AdaptedSourceViewer(Composite parent,
137 // // IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
138 // // boolean showAnnotationsOverview, int styles) {
139 // // super(parent, verticalRuler, overviewRuler,
140 // // showAnnotationsOverview, styles);
142 // public IContentAssistant getContentAssistant() {
143 // return fContentAssistant;
147 // * @see ITextOperationTarget#doOperation(int)
149 // public void doOperation(int operation) {
150 // if (getTextWidget() == null)
152 // switch (operation) {
153 // case CONTENTASSIST_PROPOSALS:
154 // String msg = fContentAssistant.showPossibleCompletions();
155 // setStatusLineErrorMessage(msg);
157 // // case CORRECTIONASSIST_PROPOSALS:
158 // // fCorrectionAssistant.showPossibleCompletions();
161 // fIgnoreTextConverters = true;
164 // fIgnoreTextConverters = true;
167 // super.doOperation(operation);
171 // * @see ITextOperationTarget#canDoOperation(int)
173 // public boolean canDoOperation(int operation) {
174 // // if (operation == CORRECTIONASSIST_PROPOSALS)
175 // // return isEditable();
176 // return super.canDoOperation(operation);
180 // * @see TextViewer#handleDispose()
182 // protected void handleDispose() {
183 // // if (fCorrectionAssistant != null) {
184 // // fCorrectionAssistant.uninstall();
185 // // fCorrectionAssistant= null;
187 // super.handleDispose();
190 // public void insertTextConverter(ITextConverter textConverter, int index) {
191 // throw new UnsupportedOperationException();
194 // public void addTextConverter(ITextConverter textConverter) {
195 // if (fTextConverters == null) {
196 // fTextConverters = new ArrayList(1);
197 // fTextConverters.add(textConverter);
198 // } else if (!fTextConverters.contains(textConverter))
199 // fTextConverters.add(textConverter);
202 // public void removeTextConverter(ITextConverter textConverter) {
203 // if (fTextConverters != null) {
204 // fTextConverters.remove(textConverter);
205 // if (fTextConverters.size() == 0)
206 // fTextConverters = null;
211 // * @see TextViewer#customizeDocumentCommand(DocumentCommand)
213 // protected void customizeDocumentCommand(DocumentCommand command) {
214 // super.customizeDocumentCommand(command);
215 // if (!fIgnoreTextConverters && fTextConverters != null) {
216 // for (Iterator e = fTextConverters.iterator(); e.hasNext();)
217 // ((ITextConverter) e.next()).customizeDocumentCommand(getDocument(),
220 // fIgnoreTextConverters = false;
223 // // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
224 // public void updateIndentationPrefixes() {
225 // SourceViewerConfiguration configuration = getSourceViewerConfiguration();
226 // String[] types = configuration.getConfiguredContentTypes(this);
227 // for (int i = 0; i < types.length; i++) {
228 // String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
229 // if (prefixes != null && prefixes.length > 0)
230 // setIndentPrefixes(prefixes, types[i]);
235 // * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
237 // public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
238 // if (WorkbenchHelp.isContextHelpDisplayed())
240 // return super.requestWidgetToken(requester);
244 //// * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
246 //// public void configure(SourceViewerConfiguration configuration) {
247 //// super.configure(configuration);
248 //// // fCorrectionAssistant= new
249 //// // JavaCorrectionAssistant(CompilationUnitEditor.this);
250 //// // fCorrectionAssistant.install(this);
251 //// //TODO install SmartBracesAutoEditStrategy
252 //// // prependAutoEditStrategy(new SmartBracesAutoEditStrategy(this),
253 //// // IDocument.DEFAULT_CONTENT_TYPE);
255 // public void configure(SourceViewerConfiguration configuration) {
256 // super.configure(configuration);
257 //// fCorrectionAssistant= new JavaCorrectionAssistant(CompilationUnitEditor.this);
258 //// fCorrectionAssistant.install(this);
259 // IAutoEditStrategy smartSemi= new SmartSemicolonAutoEditStrategy(IPHPPartitions.PHP_PARTITIONING);
260 // prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
261 // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
262 // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
263 //// prependAutoEditStrategy(smartSemi, IPHPPartitions.JAVA_CHARACTER);
266 class AdaptedSourceViewer extends JavaSourceViewer {
268 private List fTextConverters;
270 private boolean fIgnoreTextConverters = false;
272 // private JavaCorrectionAssistant fCorrectionAssistant;
274 public AdaptedSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
275 boolean showAnnotationsOverview, int styles, IPreferenceStore store) {
276 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles, store);
279 public IContentAssistant getContentAssistant() {
280 return fContentAssistant;
284 * @see ITextOperationTarget#doOperation(int)
286 public void doOperation(int operation) {
288 if (getTextWidget() == null)
292 case CONTENTASSIST_PROPOSALS:
293 String msg = fContentAssistant.showPossibleCompletions();
294 setStatusLineErrorMessage(msg);
296 // case CORRECTIONASSIST_PROPOSALS:
297 // msg = fCorrectionAssistant.showPossibleCompletions();
298 // setStatusLineErrorMessage(msg);
301 fIgnoreTextConverters = true;
302 super.doOperation(operation);
303 fIgnoreTextConverters = false;
306 fIgnoreTextConverters = true;
307 super.doOperation(operation);
308 fIgnoreTextConverters = false;
312 super.doOperation(operation);
316 * @see ITextOperationTarget#canDoOperation(int)
318 public boolean canDoOperation(int operation) {
319 // if (operation == CORRECTIONASSIST_PROPOSALS)
320 // return isEditable();
322 return super.canDoOperation(operation);
326 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
329 public void unconfigure() {
330 // if (fCorrectionAssistant != null) {
331 // fCorrectionAssistant.uninstall();
332 // fCorrectionAssistant = null;
337 public void insertTextConverter(ITextConverter textConverter, int index) {
338 throw new UnsupportedOperationException();
341 public void addTextConverter(ITextConverter textConverter) {
342 if (fTextConverters == null) {
343 fTextConverters = new ArrayList(1);
344 fTextConverters.add(textConverter);
345 } else if (!fTextConverters.contains(textConverter))
346 fTextConverters.add(textConverter);
349 public void removeTextConverter(ITextConverter textConverter) {
350 if (fTextConverters != null) {
351 fTextConverters.remove(textConverter);
352 if (fTextConverters.size() == 0)
353 fTextConverters = null;
358 * @see TextViewer#customizeDocumentCommand(DocumentCommand)
360 protected void customizeDocumentCommand(DocumentCommand command) {
361 super.customizeDocumentCommand(command);
362 if (!fIgnoreTextConverters && fTextConverters != null) {
363 for (Iterator e = fTextConverters.iterator(); e.hasNext();)
364 ((ITextConverter) e.next()).customizeDocumentCommand(getDocument(), command);
368 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
369 public void updateIndentationPrefixes() {
370 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
371 String[] types = configuration.getConfiguredContentTypes(this);
372 for (int i = 0; i < types.length; i++) {
373 String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
374 if (prefixes != null && prefixes.length > 0)
375 setIndentPrefixes(prefixes, types[i]);
380 * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
382 public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
383 if (WorkbenchHelp.isContextHelpDisplayed())
385 return super.requestWidgetToken(requester);
389 * @see IWidgetTokenOwnerExtension#requestWidgetToken(IWidgetTokenKeeper, int)
392 public boolean requestWidgetToken(IWidgetTokenKeeper requester, int priority) {
393 if (WorkbenchHelp.isContextHelpDisplayed())
395 return super.requestWidgetToken(requester, priority);
399 * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
401 public void configure(SourceViewerConfiguration configuration) {
402 super.configure(configuration);
403 // fCorrectionAssistant = new JavaCorrectionAssistant(CompilationUnitEditor.this);
404 // fCorrectionAssistant.install(this);
405 IAutoEditStrategy smartSemi = new SmartSemicolonAutoEditStrategy(IPHPPartitions.PHP_PARTITIONING);
406 prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
407 prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
408 prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
409 prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_HEREDOC);
413 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
416 public IFormattingContext createFormattingContext() {
417 IFormattingContext context = new CommentFormattingContext();
420 IJavaElement inputJavaElement = getInputJavaElement();
421 IJavaProject javaProject = inputJavaElement != null ? inputJavaElement.getJavaProject() : null;
422 if (javaProject == null)
423 preferences = new HashMap(JavaCore.getOptions());
425 preferences = new HashMap(javaProject.getOptions(true));
427 context.storeToMap(PreferenceConstants.getPreferenceStore(), preferences, false);
428 context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
435 * Remembers data related to the current selection to be able to restore it later.
439 private class RememberedSelection {
440 /** The remembered selection start. */
441 private RememberedOffset fStartOffset = new RememberedOffset();
443 /** The remembered selection end. */
444 private RememberedOffset fEndOffset = new RememberedOffset();
447 * Remember current selection.
449 public void remember() {
451 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method may be called inside an async call posted to the UI thread,
452 * so protect against intermediate disposal of the editor.
454 ISourceViewer viewer = getSourceViewer();
455 if (viewer != null) {
456 IRegion selection = getSignedSelection(viewer);
457 int startOffset = selection.getOffset();
458 int endOffset = startOffset + selection.getLength();
460 fStartOffset.setOffset(startOffset);
461 fEndOffset.setOffset(endOffset);
466 * Restore remembered selection.
468 public void restore() {
470 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method may be called inside an async call posted to the UI thread,
471 * so protect against intermediate disposal of the editor.
473 if (getSourceViewer() == null)
478 int startOffset, endOffset;
479 int revealStartOffset, revealEndOffset;
480 if (showsHighlightRangeOnly()) {
481 IJavaElement newStartElement = fStartOffset.getElement();
482 startOffset = fStartOffset.getRememberedOffset(newStartElement);
483 revealStartOffset = fStartOffset.getRevealOffset(newStartElement, startOffset);
484 if (revealStartOffset == -1)
487 IJavaElement newEndElement = fEndOffset.getElement();
488 endOffset = fEndOffset.getRememberedOffset(newEndElement);
489 revealEndOffset = fEndOffset.getRevealOffset(newEndElement, endOffset);
490 if (revealEndOffset == -1)
493 startOffset = fStartOffset.getOffset();
494 revealStartOffset = startOffset;
495 endOffset = fEndOffset.getOffset();
496 revealEndOffset = endOffset;
499 if (startOffset == -1) {
500 startOffset = endOffset; // fallback to caret offset
501 revealStartOffset = revealEndOffset;
504 if (endOffset == -1) {
505 endOffset = startOffset; // fallback to other offset
506 revealEndOffset = revealStartOffset;
509 IJavaElement element;
510 if (endOffset == -1) {
511 // fallback to element selection
512 element = fEndOffset.getElement();
514 element = fStartOffset.getElement();
516 setSelection(element);
520 if (isValidSelection(revealStartOffset, revealEndOffset - revealStartOffset)
521 && isValidSelection(startOffset, endOffset - startOffset))
522 selectAndReveal(startOffset, endOffset - startOffset, revealStartOffset, revealEndOffset - revealStartOffset);
524 fStartOffset.clear();
529 private boolean isValidSelection(int offset, int length) {
530 IDocumentProvider provider = getDocumentProvider();
531 if (provider != null) {
532 IDocument document = provider.getDocument(getEditorInput());
533 if (document != null) {
534 int end = offset + length;
535 int documentLength = document.getLength();
536 return 0 <= offset && offset <= documentLength && 0 <= end && end <= documentLength;
545 * Remembers additional data for a given offset to be able restore it later.
549 private class RememberedOffset {
550 /** Remembered line for the given offset */
553 /** Remembered column for the given offset */
556 /** Remembered Java element for the given offset */
557 private IJavaElement fElement;
559 /** Remembered Java element line for the given offset */
560 private int fElementLine;
563 * Store visual properties of the given offset.
566 * Offset in the document
568 public void setOffset(int offset) {
570 IDocument document = getSourceViewer().getDocument();
571 fLine = document.getLineOfOffset(offset);
572 fColumn = offset - document.getLineOffset(fLine);
573 fElement = getElementAt(offset, true);
576 if (fElement instanceof IMember) {
577 ISourceRange range = ((IMember) fElement).getNameRange();
579 fElementLine = document.getLineOfOffset(range.getOffset());
581 if (fElementLine == -1)
582 fElementLine = document.getLineOfOffset(getOffset(fElement));
583 } catch (BadLocationException e) {
585 PHPeclipsePlugin.log(e);
587 } catch (JavaModelException e) {
589 PHPeclipsePlugin.log(e.getStatus());
595 * Return offset recomputed from stored visual properties.
597 * @return Offset in the document
599 public int getOffset() {
600 IJavaElement newElement = getElement();
602 int offset = getRememberedOffset(newElement);
604 if (offset != -1 && !containsOffset(newElement, offset) && (offset == 0 || !containsOffset(newElement, offset - 1)))
611 * Return offset recomputed from stored visual properties.
615 * @return Offset in the document
617 public int getRememberedOffset(IJavaElement newElement) {
619 if (newElement == null)
622 IDocument document = getSourceViewer().getDocument();
623 int newElementLine = -1;
624 if (newElement instanceof IMember) {
625 ISourceRange range = ((IMember) newElement).getNameRange();
627 newElementLine = document.getLineOfOffset(range.getOffset());
629 if (newElementLine == -1)
630 newElementLine = document.getLineOfOffset(getOffset(newElement));
631 if (newElementLine == -1)
634 int newLine = fLine + newElementLine - fElementLine;
635 if (newLine < 0 || newLine >= document.getNumberOfLines())
637 int maxColumn = document.getLineLength(newLine);
638 String lineDelimiter = document.getLineDelimiter(newLine);
639 if (lineDelimiter != null)
640 maxColumn = maxColumn - lineDelimiter.length();
642 if (fColumn > maxColumn)
643 offset = document.getLineOffset(newLine) + maxColumn;
645 offset = document.getLineOffset(newLine) + fColumn;
648 } catch (BadLocationException e) {
650 PHPeclipsePlugin.log(e);
652 } catch (JavaModelException e) {
654 PHPeclipsePlugin.log(e.getStatus());
660 * Returns the offset used to reveal the given element based on the given selection offset.
665 * the selection offset
666 * @return the offset to reveal the given element based on the given selection offset
668 public int getRevealOffset(IJavaElement element, int offset) {
669 if (element == null || offset == -1)
672 if (containsOffset(element, offset)) {
674 IJavaElement alternateElement = getElementAt(offset, false);
675 if (element.getHandleIdentifier().equals(alternateElement.getParent().getHandleIdentifier()))
676 return offset - 1; // Solves test case 2 from https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
679 } else if (offset > 0 && containsOffset(element, offset - 1))
680 return offset - 1; // Solves test case 1 from https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
686 * Return Java element recomputed from stored visual properties.
688 * @return Java element
690 public IJavaElement getElement() {
691 if (fElement == null)
694 return findElement(fElement);
698 * Clears the stored position
700 public void clear() {
708 * Does the given Java element contain the given offset?
714 * @return <code>true</code> iff the Java element contains the offset
716 private boolean containsOffset(IJavaElement element, int offset) {
717 int elementOffset = getOffset(element);
718 int elementLength = getLength(element);
719 return (elementOffset > -1 && elementLength > -1) ? (offset >= elementOffset && offset < elementOffset + elementLength)
724 * Returns the offset of the given Java element.
728 * @return Offset of the given Java element
730 private int getOffset(IJavaElement element) {
731 if (element instanceof ISourceReference) {
732 ISourceReference sr = (ISourceReference) element;
734 ISourceRange srcRange = sr.getSourceRange();
735 if (srcRange != null)
736 return srcRange.getOffset();
737 } catch (JavaModelException e) {
744 * Returns the length of the given Java element.
748 * @return Length of the given Java element
750 private int getLength(IJavaElement element) {
751 if (element instanceof ISourceReference) {
752 ISourceReference sr = (ISourceReference) element;
754 ISourceRange srcRange = sr.getSourceRange();
755 if (srcRange != null)
756 return srcRange.getLength();
757 } catch (JavaModelException e) {
764 * Returns the updated java element for the old java element.
768 * @return Updated Java element
770 private IJavaElement findElement(IJavaElement element) {
775 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
776 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
781 synchronized (unit) {
782 // unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
785 IJavaElement[] findings = unit.findElements(element);
786 if (findings != null && findings.length > 0)
789 } catch (JavaModelException x) {
790 PHPeclipsePlugin.log(x.getStatus());
791 // nothing found, be tolerant and go on
800 static class TabConverter implements ITextConverter {
801 private int fTabRatio;
803 private ILineTracker fLineTracker;
805 public TabConverter() {
808 public void setNumberOfSpacesPerTab(int ratio) {
812 public void setLineTracker(ILineTracker lineTracker) {
813 fLineTracker = lineTracker;
816 private int insertTabString(StringBuffer buffer, int offsetInLine) {
819 int remainder = offsetInLine % fTabRatio;
820 remainder = fTabRatio - remainder;
821 for (int i = 0; i < remainder; i++)
826 public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
827 String text = command.text;
830 int index = text.indexOf('\t');
832 StringBuffer buffer = new StringBuffer();
833 fLineTracker.set(command.text);
834 int lines = fLineTracker.getNumberOfLines();
836 for (int i = 0; i < lines; i++) {
837 int offset = fLineTracker.getLineOffset(i);
838 int endOffset = offset + fLineTracker.getLineLength(i);
839 String line = text.substring(offset, endOffset);
842 IRegion firstLine = document.getLineInformationOfOffset(command.offset);
843 position = command.offset - firstLine.getOffset();
845 int length = line.length();
846 for (int j = 0; j < length; j++) {
847 char c = line.charAt(j);
849 position += insertTabString(buffer, position);
856 command.text = buffer.toString();
857 } catch (BadLocationException x) {
863 private static class ExitPolicy implements LinkedPositionUI.ExitPolicy {
864 final char fExitCharacter;
866 public ExitPolicy(char exitCharacter) {
867 fExitCharacter = exitCharacter;
871 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitPolicy#doExit(org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionManager,
872 * org.eclipse.swt.events.VerifyEvent, int, int)
874 public ExitFlags doExit(LinkedPositionManager manager, VerifyEvent event, int offset, int length) {
875 if (event.character == fExitCharacter) {
876 if (manager.anyPositionIncludes(offset, length))
877 return new ExitFlags(LinkedPositionUI.COMMIT | LinkedPositionUI.UPDATE_CARET, false);
879 return new ExitFlags(LinkedPositionUI.COMMIT, true);
881 switch (event.character) {
883 if (manager.getFirstPosition().length == 0)
884 return new ExitFlags(0, false);
889 return new ExitFlags(LinkedPositionUI.COMMIT, true);
896 private static class BracketLevel {
901 LinkedPositionManager fManager;
903 LinkedPositionUI fEditor;
906 private class BracketInserter implements VerifyKeyListener, LinkedPositionUI.ExitListener {
907 private boolean fCloseBracketsPHP = true;
909 private boolean fCloseStringsPHPDQ = true;
911 private boolean fCloseStringsPHPSQ = true;
913 private boolean fCloseBracketsHTML = true;
915 private boolean fCloseStringsHTML = true;
921 public void setCloseBracketsPHPEnabled(boolean enabled) {
922 fCloseBracketsPHP = enabled;
925 public void setCloseStringsPHPDQEnabled(boolean enabled) {
926 fCloseStringsPHPDQ = enabled;
929 public void setCloseStringsPHPSQEnabled(boolean enabled) {
930 fCloseStringsPHPSQ = enabled;
933 public void setCloseBracketsHTMLEnabled(boolean enabled) {
934 fCloseBracketsHTML = enabled;
937 public void setCloseStringsHTMLEnabled(boolean enabled) {
938 fCloseStringsHTML = enabled;
941 private boolean hasIdentifierToTheRight(IDocument document, int offset) {
944 IRegion endLine = document.getLineInformationOfOffset(end);
945 int maxEnd = endLine.getOffset() + endLine.getLength();
946 while (end != maxEnd && Character.isWhitespace(document.getChar(end)))
948 return end != maxEnd && Scanner.isPHPIdentifierPart(document.getChar(end));
949 } catch (BadLocationException e) {
955 private boolean hasIdentifierToTheLeft(IDocument document, int offset) {
958 IRegion startLine = document.getLineInformationOfOffset(start);
959 int minStart = startLine.getOffset();
960 while (start != minStart && Character.isWhitespace(document.getChar(start - 1)))
962 return start != minStart && Scanner.isPHPIdentifierPart(document.getChar(start - 1));
963 } catch (BadLocationException e) {
968 private boolean hasCharacterToTheRight(IDocument document, int offset, char character) {
971 IRegion endLine = document.getLineInformationOfOffset(end);
972 int maxEnd = endLine.getOffset() + endLine.getLength();
973 while (end != maxEnd && Character.isWhitespace(document.getChar(end)))
975 return end != maxEnd && document.getChar(end) == character;
976 } catch (BadLocationException e) {
983 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
985 public void verifyKey(VerifyEvent event) {
988 final ISourceViewer sourceViewer = getSourceViewer();
989 IDocument document = sourceViewer.getDocument();
990 final Point selection = sourceViewer.getSelectedRange();
991 final int offset = selection.x;
992 final int length = selection.y;
994 ITypedRegion partition = document.getPartition(offset);
995 String type = partition.getType();
996 if (type.equals(IPHPPartitions.PHP_PARTITIONING) || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
997 // you will get IDocument.DEFAULT_CONTENT_TYPE for both PHP and HTML area
998 switch (event.character) {
1000 if (hasCharacterToTheRight(document, offset + length, '('))
1004 if (!fCloseBracketsPHP)
1006 if (hasIdentifierToTheRight(document, offset + length))
1010 if (event.character == '"') {
1011 if (!fCloseStringsPHPDQ)
1013 // changed for statements like echo "" print ""
1014 // if (hasIdentifierToTheLeft(document, offset)
1016 // hasIdentifierToTheRight(document, offset +
1018 if (hasIdentifierToTheRight(document, offset + length))
1021 // ITypedRegion partition=
1022 // document.getPartition(offset);
1024 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1026 // (partition.getOffset() != offset))
1028 final char characterDQ = event.character;
1029 final char closingCharacterDQ = getPeerCharacter(characterDQ);
1030 final StringBuffer bufferDQ = new StringBuffer();
1031 bufferDQ.append(characterDQ);
1032 bufferDQ.append(closingCharacterDQ);
1033 document.replace(offset, length, bufferDQ.toString());
1034 LinkedPositionManager managerDQ = new LinkedPositionManager(document);
1035 managerDQ.addPosition(offset + 1, 0);
1038 LinkedPositionUI editorDQ = new LinkedPositionUI(sourceViewer, managerDQ);
1039 editorDQ.setCancelListener(this);
1040 editorDQ.setExitPolicy(new ExitPolicy(closingCharacterDQ));
1041 editorDQ.setFinalCaretOffset(offset + 2);
1043 IRegion newSelectionDQ = editorDQ.getSelectedRegion();
1044 sourceViewer.setSelectedRange(newSelectionDQ.getOffset(), newSelectionDQ.getLength());
1049 if (event.character == '\'') {
1050 if (!fCloseStringsPHPSQ)
1052 // changed for statements like echo "" print ""
1053 // if (hasIdentifierToTheLeft(document, offset)
1055 // hasIdentifierToTheRight(document, offset +
1057 if (hasIdentifierToTheRight(document, offset + length))
1060 // ITypedRegion partition=
1061 // document.getPartition(offset);
1063 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1065 // (partition.getOffset() != offset))
1067 final char characterSQ = event.character;
1068 final char closingCharacterSQ = getPeerCharacter(characterSQ);
1069 final StringBuffer bufferSQ = new StringBuffer();
1070 bufferSQ.append(characterSQ);
1071 bufferSQ.append(closingCharacterSQ);
1072 document.replace(offset, length, bufferSQ.toString());
1073 LinkedPositionManager managerSQ = new LinkedPositionManager(document);
1074 managerSQ.addPosition(offset + 1, 0);
1077 LinkedPositionUI editorSQ = new LinkedPositionUI(sourceViewer, managerSQ);
1078 editorSQ.setCancelListener(this);
1079 editorSQ.setExitPolicy(new ExitPolicy(closingCharacterSQ));
1080 editorSQ.setFinalCaretOffset(offset + 2);
1082 IRegion newSelectionSQ = editorSQ.getSelectedRegion();
1083 sourceViewer.setSelectedRange(newSelectionSQ.getOffset(), newSelectionSQ.getLength());
1087 // } else if (type.equals(IPHPPartitions.HTML)) {
1088 // switch (event.character) {
1090 // if (hasCharacterToTheRight(document, offset + length, '('))
1094 // if (!fCloseBracketsHTML)
1096 // if (hasIdentifierToTheRight(document, offset + length))
1100 // if (event.character == '"') {
1101 // if (!fCloseStringsHTML)
1103 // if (hasIdentifierToTheLeft(document, offset)
1104 // || hasIdentifierToTheRight(document, offset + length))
1107 // // ITypedRegion partition=
1108 // // document.getPartition(offset);
1110 // // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1112 // // (partition.getOffset() != offset))
1114 // final char character = event.character;
1115 // final char closingCharacter = getPeerCharacter(character);
1116 // final StringBuffer buffer = new StringBuffer();
1117 // buffer.append(character);
1118 // buffer.append(closingCharacter);
1119 // document.replace(offset, length, buffer.toString());
1120 // LinkedPositionManager manager = new LinkedPositionManager(document);
1121 // manager.addPosition(offset + 1, 0);
1122 // fOffset = offset;
1124 // LinkedPositionUI editor = new LinkedPositionUI(sourceViewer,
1126 // editor.setCancelListener(this);
1127 // editor.setExitPolicy(new ExitPolicy(closingCharacter));
1128 // editor.setFinalCaretOffset(offset + 2);
1130 // IRegion newSelection = editor.getSelectedRegion();
1131 // sourceViewer.setSelectedRange(newSelection.getOffset(),
1132 // newSelection.getLength());
1133 // event.doit = false;
1136 } catch (BadLocationException e) {
1141 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitListener#exit(boolean)
1143 public void exit(boolean accept) {
1148 final ISourceViewer sourceViewer = getSourceViewer();
1149 IDocument document = sourceViewer.getDocument();
1150 document.replace(fOffset, fLength, null);
1151 } catch (BadLocationException e) {
1156 /** The editor's save policy */
1157 protected ISavePolicy fSavePolicy;
1160 * Listener to annotation model changes that updates the error tick in the tab image
1162 private JavaEditorErrorTickUpdater fJavaEditorErrorTickUpdater;
1164 /** The editor's paint manager */
1165 // private PaintManager fPaintManager;
1166 /** The editor's bracket painter */
1167 // private BracketPainter fBracketPainter;
1168 /** The editor's bracket matcher */
1169 private PHPPairMatcher fBracketMatcher;
1171 /** The editor's line painter */
1172 // private LinePainter fLinePainter;
1173 /** The editor's print margin ruler painter */
1174 // private PrintMarginPainter fPrintMarginPainter;
1175 /** The editor's problem painter */
1176 // private ProblemPainter fProblemPainter;
1177 /** The editor's tab converter */
1178 private TabConverter fTabConverter;
1180 /** History for structure select action */
1181 //private SelectionHistory fSelectionHistory;
1182 /** The preference property change listener for php core. */
1183 // private IPropertyChangeListener fPropertyChangeListener = new
1184 // PropertyChangeListener();
1185 /** The remembered java element */
1186 private IJavaElement fRememberedElement;
1189 * The remembered selection.
1193 private RememberedSelection fRememberedSelection = new RememberedSelection();
1195 /** The remembered php element offset */
1196 private int fRememberedElementOffset;
1198 /** The bracket inserter. */
1199 private BracketInserter fBracketInserter = new BracketInserter();
1201 /** The standard action groups added to the menu */
1202 private GenerateActionGroup fGenerateActionGroup;
1204 private CompositeActionGroup fContextMenuGroup;
1206 // private class PropertyChangeListener implements IPropertyChangeListener {
1208 // * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
1211 // propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
1213 // handlePreferencePropertyChanged(event);
1216 /* Preference key for code formatter tab size */
1217 private final static String CODE_FORMATTER_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
1219 /** Preference key for matching brackets */
1220 // private final static String MATCHING_BRACKETS = PreferenceConstants.EDITOR_MATCHING_BRACKETS;
1221 /** Preference key for matching brackets color */
1222 // private final static String MATCHING_BRACKETS_COLOR = PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
1223 /** Preference key for highlighting current line */
1224 // private final static String CURRENT_LINE = PreferenceConstants.EDITOR_CURRENT_LINE;
1225 /** Preference key for highlight color of current line */
1226 // private final static String CURRENT_LINE_COLOR = PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
1227 /** Preference key for showing print marging ruler */
1228 // private final static String PRINT_MARGIN = PreferenceConstants.EDITOR_PRINT_MARGIN;
1229 /** Preference key for print margin ruler color */
1230 // private final static String PRINT_MARGIN_COLOR = PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
1231 /** Preference key for print margin ruler column */
1232 // private final static String PRINT_MARGIN_COLUMN = PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
1233 /** Preference key for inserting spaces rather than tabs */
1234 private final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
1236 /** Preference key for error indication */
1237 // private final static String ERROR_INDICATION =
1238 // PreferenceConstants.EDITOR_PROBLEM_INDICATION;
1239 /** Preference key for error color */
1240 // private final static String ERROR_INDICATION_COLOR =
1241 // PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
1242 /** Preference key for warning indication */
1243 // private final static String WARNING_INDICATION =
1244 // PreferenceConstants.EDITOR_WARNING_INDICATION;
1245 /** Preference key for warning color */
1246 // private final static String WARNING_INDICATION_COLOR =
1247 // PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
1248 /** Preference key for task indication */
1249 private final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
1251 /** Preference key for task color */
1252 private final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
1254 /** Preference key for bookmark indication */
1255 private final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
1257 /** Preference key for bookmark color */
1258 private final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
1260 /** Preference key for search result indication */
1261 private final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
1263 /** Preference key for search result color */
1264 private final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
1266 /** Preference key for unknown annotation indication */
1267 private final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
1269 /** Preference key for unknown annotation color */
1270 private final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
1272 /** Preference key for linked position color */
1273 private final static String LINKED_POSITION_COLOR = PreferenceConstants.EDITOR_LINKED_POSITION_COLOR;
1275 /** Preference key for shwoing the overview ruler */
1276 private final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
1278 /** Preference key for error indication in overview ruler */
1279 private final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
1281 /** Preference key for warning indication in overview ruler */
1282 private final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
1284 /** Preference key for task indication in overview ruler */
1285 private final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
1287 /** Preference key for bookmark indication in overview ruler */
1288 private final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1290 /** Preference key for search result indication in overview ruler */
1291 private final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1293 /** Preference key for unknown annotation indication in overview ruler */
1294 private final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1296 /** Preference key for automatically closing double quoted strings */
1297 private final static String CLOSE_STRINGS_DQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_DQ_PHP;
1299 /** Preference key for automatically closing single quoted strings */
1300 private final static String CLOSE_STRINGS_SQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_SQ_PHP;
1302 /** Preference key for automatically wrapping Java strings */
1303 // private final static String WRAP_STRINGS = PreferenceConstants.EDITOR_WRAP_STRINGS_DQ;
1304 /** Preference key for automatically closing brackets and parenthesis */
1305 private final static String CLOSE_BRACKETS_PHP = PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP;
1307 /** Preference key for automatically closing phpdocs and comments */
1308 private final static String CLOSE_JAVADOCS = PreferenceConstants.EDITOR_CLOSE_JAVADOCS;
1310 /** Preference key for automatically adding phpdoc tags */
1311 private final static String ADD_JAVADOC_TAGS = PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS;
1313 /** Preference key for automatically formatting phpdocs */
1314 // private final static String FORMAT_JAVADOCS = PreferenceConstants.EDITOR_FORMAT_JAVADOCS;
1315 /** Preference key for automatically closing strings */
1316 private final static String CLOSE_STRINGS_HTML = PreferenceConstants.EDITOR_CLOSE_STRINGS_HTML;
1318 /** Preference key for automatically closing brackets and parenthesis */
1319 private final static String CLOSE_BRACKETS_HTML = PreferenceConstants.EDITOR_CLOSE_BRACKETS_HTML;
1321 /** Preference key for smart paste */
1322 private final static String SMART_PASTE = PreferenceConstants.EDITOR_SMART_PASTE;
1324 // private final static class AnnotationInfo {
1325 // public String fColorPreference;
1326 // public String fOverviewRulerPreference;
1327 // public String fEditorPreference;
1329 // private final static Map ANNOTATION_MAP;
1332 // AnnotationInfo info;
1333 // ANNOTATION_MAP = new HashMap();
1335 // info = new AnnotationInfo();
1336 // info.fColorPreference = TASK_INDICATION_COLOR;
1337 // info.fOverviewRulerPreference = TASK_INDICATION_IN_OVERVIEW_RULER;
1338 // info.fEditorPreference = TASK_INDICATION;
1339 // ANNOTATION_MAP.put(AnnotationType.TASK, info);
1341 // info = new AnnotationInfo();
1342 // info.fColorPreference = ERROR_INDICATION_COLOR;
1343 // info.fOverviewRulerPreference = ERROR_INDICATION_IN_OVERVIEW_RULER;
1344 // info.fEditorPreference = ERROR_INDICATION;
1345 // ANNOTATION_MAP.put(AnnotationType.ERROR, info);
1347 // info = new AnnotationInfo();
1348 // info.fColorPreference = WARNING_INDICATION_COLOR;
1349 // info.fOverviewRulerPreference = WARNING_INDICATION_IN_OVERVIEW_RULER;
1350 // info.fEditorPreference = WARNING_INDICATION;
1351 // ANNOTATION_MAP.put(AnnotationType.WARNING, info);
1353 // info = new AnnotationInfo();
1354 // info.fColorPreference = BOOKMARK_INDICATION_COLOR;
1355 // info.fOverviewRulerPreference = BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1356 // info.fEditorPreference = BOOKMARK_INDICATION;
1357 // ANNOTATION_MAP.put(AnnotationType.BOOKMARK, info);
1359 // info = new AnnotationInfo();
1360 // info.fColorPreference = SEARCH_RESULT_INDICATION_COLOR;
1361 // info.fOverviewRulerPreference =
1362 // SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1363 // info.fEditorPreference = SEARCH_RESULT_INDICATION;
1364 // ANNOTATION_MAP.put(AnnotationType.SEARCH, info);
1366 // info = new AnnotationInfo();
1367 // info.fColorPreference = UNKNOWN_INDICATION_COLOR;
1368 // info.fOverviewRulerPreference = UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1369 // info.fEditorPreference = UNKNOWN_INDICATION;
1370 // ANNOTATION_MAP.put(AnnotationType.UNKNOWN, info);
1373 // private final static AnnotationType[] ANNOTATION_LAYERS =
1374 // new AnnotationType[] {
1375 // AnnotationType.UNKNOWN,
1376 // AnnotationType.BOOKMARK,
1377 // AnnotationType.TASK,
1378 // AnnotationType.SEARCH,
1379 // AnnotationType.WARNING,
1380 // AnnotationType.ERROR };
1382 * Creates a new php unit editor.
1386 * Reconciling listeners.
1390 private ListenerList fReconcilingListeners = new ListenerList();
1393 * Mutex for the reconciler. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of the problem.
1395 * TODO remove once the underlying problem is solved.
1398 private final Object fReconcilerLock = new Object();
1400 public PHPUnitEditor() {
1402 setDocumentProvider(PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider());
1403 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
1404 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
1405 setOutlinerContextMenuId("#PHPOutlinerContext"); //$NON-NLS-1$
1406 // don't set help contextId, we install our own help context
1408 fJavaEditorErrorTickUpdater = new JavaEditorErrorTickUpdater(this);
1412 * @see AbstractTextEditor#createActions()
1414 protected void createActions() {
1415 super.createActions();
1417 // Action action= new
1418 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1419 // "CorrectionAssistProposal.", this, CORRECTIONASSIST_PROPOSALS);
1421 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CORRECTION_ASSIST_PROPOSALS);
1422 // setAction("CorrectionAssistProposal", action); //$NON-NLS-1$
1423 // markAsStateDependentAction("CorrectionAssistProposal", true);
1425 //// WorkbenchHelp.setHelp(action,
1426 // IJavaHelpContextIds.QUICK_FIX_ACTION);
1427 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
1428 action.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
1429 setAction("ContentAssistProposal", action); //$NON-NLS-1$
1430 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
1431 // WorkbenchHelp.setHelp(action,
1432 // IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
1433 // action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1434 // "ContentAssistContextInformation.", this,
1435 // ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION); //$NON-NLS-1$
1437 // .setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
1438 // setAction("ContentAssistContextInformation", action); //$NON-NLS-1$
1439 // markAsStateDependentAction("ContentAssistContextInformation", true);
1441 // WorkbenchHelp.setHelp(action,
1442 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1444 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1445 // "ContentAssistCompletePrefix.", this, CONTENTASSIST_COMPLETE_PREFIX);
1447 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_COMPLETE_PREFIX);
1448 // setAction("ContentAssistCompletePrefix", action); //$NON-NLS-1$
1449 // markAsStateDependentAction("ContentAssistCompletePrefix", true);
1451 //// WorkbenchHelp.setHelp(action,
1452 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1453 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
1454 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
1455 setAction("Comment", action); //$NON-NLS-1$
1456 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
1457 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
1458 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
1459 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
1460 setAction("Uncomment", action); //$NON-NLS-1$
1461 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
1462 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
1464 action = new ToggleCommentAction(PHPEditorMessages.getResourceBundle(), "ToggleComment.", this); //$NON-NLS-1$
1465 action.setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_COMMENT);
1466 setAction("ToggleComment", action); //$NON-NLS-1$
1467 markAsStateDependentAction("ToggleComment", true); //$NON-NLS-1$
1468 //WorkbenchHelp.setHelp(action,
1469 // IJavaHelpContextIds.TOGGLE_COMMENT_ACTION);
1470 configureToggleCommentAction();
1472 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
1473 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
1474 setAction("Format", action); //$NON-NLS-1$
1475 markAsStateDependentAction("Format", true); //$NON-NLS-1$
1476 markAsSelectionDependentAction("Format", true); //$NON-NLS-1$
1477 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
1479 // action = new AddBlockCommentAction(PHPEditorMessages.getResourceBundle(),
1480 // "AddBlockComment.", this); //$NON-NLS-1$
1482 // .setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1483 // setAction("AddBlockComment", action); //$NON-NLS-1$
1484 // markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1485 // markAsSelectionDependentAction("AddBlockComment", true); //$NON-NLS-1$
1486 // // WorkbenchHelp.setHelp(action,
1487 // // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1488 // action = new RemoveBlockCommentAction(
1489 // PHPEditorMessages.getResourceBundle(), "RemoveBlockComment.", this); //$NON-NLS-1$
1491 // .setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1492 // setAction("RemoveBlockComment", action); //$NON-NLS-1$
1493 // markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1494 // markAsSelectionDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1495 // WorkbenchHelp.setHelp(action,
1496 // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1497 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1498 // "Indent.", this, false); //$NON-NLS-1$
1499 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1500 // setAction("Indent", action); //$NON-NLS-1$
1501 // markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1502 // markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1503 //// WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1505 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1506 // "Indent.", this, true); //$NON-NLS-1$
1507 // setAction("IndentOnTab", action); //$NON-NLS-1$
1508 // markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1509 // markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1512 action = new AddBlockCommentAction(PHPEditorMessages.getResourceBundle(), "AddBlockComment.", this); //$NON-NLS-1$
1513 action.setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1514 setAction("AddBlockComment", action); //$NON-NLS-1$
1515 markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1516 markAsSelectionDependentAction("AddBlockComment", true); //$NON-NLS-1$
1517 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1519 action = new RemoveBlockCommentAction(PHPEditorMessages.getResourceBundle(), "RemoveBlockComment.", this); //$NON-NLS-1$
1520 action.setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1521 setAction("RemoveBlockComment", action); //$NON-NLS-1$
1522 markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1523 markAsSelectionDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1524 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.REMOVE_BLOCK_COMMENT_ACTION);
1526 // action= new IndentAction(PHPEditorMessages.getResourceBundle(), "Indent.", this, false); //$NON-NLS-1$
1527 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1528 // setAction("Indent", action); //$NON-NLS-1$
1529 // markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1530 // markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1531 //// WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1533 action = new IndentAction(PHPEditorMessages.getResourceBundle(), "Indent.", this, true); //$NON-NLS-1$
1534 setAction("IndentOnTab", action); //$NON-NLS-1$
1535 markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1536 markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1538 if (getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_TAB)) {
1539 // don't replace Shift Right - have to make sure their enablement is
1540 // mutually exclusive
1541 // removeActionActivationCode(ITextEditorActionConstants.SHIFT_RIGHT);
1542 setActionActivationCode("IndentOnTab", '\t', -1, SWT.NONE); //$NON-NLS-1$
1544 fGenerateActionGroup = new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
1545 // ActionGroup rg= new RefactorActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
1547 // fActionGroups.addGroup(rg);
1548 fActionGroups.addGroup(fGenerateActionGroup);
1550 // We have to keep the context menu group separate to have better control over positioning
1551 fContextMenuGroup = new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup
1553 // new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)
1559 * @see JavaEditor#getElementAt(int)
1561 protected IJavaElement getElementAt(int offset) {
1562 return getElementAt(offset, true);
1566 * Returns the most narrow element including the given offset. If <code>reconcile</code> is <code>true</code> the editor's
1567 * input element is reconciled in advance. If it is <code>false</code> this method only returns a result if the editor's input
1568 * element does not need to be reconciled.
1571 * the offset included by the retrieved element
1573 * <code>true</code> if working copy should be reconciled
1575 protected IJavaElement getElementAt(int offset, boolean reconcile) {
1576 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
1577 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
1581 synchronized (unit) {
1584 return unit.getElementAt(offset);
1585 } else if (unit.isConsistent())
1586 return unit.getElementAt(offset);
1587 } catch (JavaModelException x) {
1588 PHPeclipsePlugin.log(x.getStatus());
1589 // nothing found, be tolerant and go on
1596 * @see JavaEditor#getCorrespondingElement(IJavaElement)
1598 protected IJavaElement getCorrespondingElement(IJavaElement element) {
1600 return EditorUtility.getWorkingCopy(element, true);
1601 } catch (JavaModelException x) {
1602 PHPeclipsePlugin.log(x.getStatus());
1603 // nothing found, be tolerant and go on
1608 public void createPartControl(Composite parent) {
1609 super.createPartControl(parent);
1610 // fPaintManager = new PaintManager(getSourceViewer());
1611 LinePainter linePainter;
1612 linePainter = new LinePainter(getSourceViewer());
1613 linePainter.setHighlightColor(new Color(Display.getCurrent(), 225, 235, 224));
1614 // fPaintManager.addPainter(linePainter);
1615 // if (isBracketHighlightingEnabled())
1616 // startBracketHighlighting();
1617 // if (isLineHighlightingEnabled())
1618 // startLineHighlighting();
1619 // if (isPrintMarginVisible())
1620 // showPrintMargin();
1621 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1622 // while (e.hasNext()) {
1623 // AnnotationType type = (AnnotationType) e.next();
1624 // if (isAnnotationIndicationEnabled(type))
1625 // startAnnotationIndication(type);
1627 if (isTabConversionEnabled())
1628 startTabConversion();
1629 // if (isOverviewRulerVisible())
1630 // showOverviewRuler();
1632 // Preferences preferences =
1633 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1634 // preferences.addPropertyChangeListener(fPropertyChangeListener);
1635 IPreferenceStore preferenceStore = getPreferenceStore();
1636 boolean closeBracketsPHP = preferenceStore.getBoolean(CLOSE_BRACKETS_PHP);
1637 boolean closeStringsPHPDQ = preferenceStore.getBoolean(CLOSE_STRINGS_DQ_PHP);
1638 boolean closeStringsPHPSQ = preferenceStore.getBoolean(CLOSE_STRINGS_SQ_PHP);
1639 boolean closeBracketsHTML = preferenceStore.getBoolean(CLOSE_BRACKETS_HTML);
1640 boolean closeStringsHTML = preferenceStore.getBoolean(CLOSE_STRINGS_HTML);
1641 fBracketInserter.setCloseBracketsPHPEnabled(closeBracketsPHP);
1642 fBracketInserter.setCloseStringsPHPDQEnabled(closeStringsPHPDQ);
1643 fBracketInserter.setCloseStringsPHPSQEnabled(closeStringsPHPSQ);
1644 fBracketInserter.setCloseBracketsHTMLEnabled(closeBracketsHTML);
1645 fBracketInserter.setCloseStringsHTMLEnabled(closeStringsHTML);
1646 ISourceViewer sourceViewer = getSourceViewer();
1647 if (sourceViewer instanceof ITextViewerExtension)
1648 ((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(fBracketInserter);
1651 private static char getPeerCharacter(char character) {
1652 switch (character) {
1666 throw new IllegalArgumentException();
1670 // private void startBracketHighlighting() {
1671 // if (fBracketPainter == null) {
1672 // ISourceViewer sourceViewer = getSourceViewer();
1673 // fBracketPainter = new BracketPainter(sourceViewer);
1674 // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1675 // // fPaintManager.addPainter(fBracketPainter);
1679 // private void stopBracketHighlighting() {
1680 // if (fBracketPainter != null) {
1681 // // fPaintManager.removePainter(fBracketPainter);
1682 // fBracketPainter.deactivate(true);
1683 // fBracketPainter.dispose();
1684 // fBracketPainter = null;
1688 // private boolean isBracketHighlightingEnabled() {
1689 // IPreferenceStore store = getPreferenceStore();
1690 // return store.getBoolean(MATCHING_BRACKETS);
1693 // private void startLineHighlighting() {
1694 // if (fLinePainter == null) {
1695 // ISourceViewer sourceViewer = getSourceViewer();
1696 // fLinePainter = new LinePainter(sourceViewer);
1697 // fLinePainter.setHighlightColor(getColor(CURRENT_LINE_COLOR));
1698 // // fPaintManager.addPainter(fLinePainter);
1702 // private void stopLineHighlighting() {
1703 // if (fLinePainter != null) {
1704 // // fPaintManager.removePainter(fLinePainter);
1705 // fLinePainter.deactivate(true);
1706 // fLinePainter.dispose();
1707 // fLinePainter = null;
1711 // private boolean isLineHighlightingEnabled() {
1712 // IPreferenceStore store = getPreferenceStore();
1713 // return store.getBoolean(CURRENT_LINE);
1716 // private void showPrintMargin() {
1717 // if (fPrintMarginPainter == null) {
1718 // fPrintMarginPainter = new PrintMarginPainter(getSourceViewer());
1719 // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
1720 // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
1721 // // fPaintManager.addPainter(fPrintMarginPainter);
1725 // private void hidePrintMargin() {
1726 // if (fPrintMarginPainter != null) {
1727 // // fPaintManager.removePainter(fPrintMarginPainter);
1728 // fPrintMarginPainter.deactivate(true);
1729 // fPrintMarginPainter.dispose();
1730 // fPrintMarginPainter = null;
1734 // private boolean isPrintMarginVisible() {
1735 // IPreferenceStore store = getPreferenceStore();
1736 // return store.getBoolean(PRINT_MARGIN);
1739 private int getTabSize() {
1740 Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1741 return preferences.getInt(CODE_FORMATTER_TAB_SIZE);
1744 private boolean isTabConversionEnabled() {
1745 IPreferenceStore store = getPreferenceStore();
1746 return store.getBoolean(SPACES_FOR_TABS);
1749 private Color getColor(String key) {
1750 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
1751 return getColor(rgb);
1754 private Color getColor(RGB rgb) {
1755 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
1756 return textTools.getColorManager().getColor(rgb);
1759 // private Color getColor(AnnotationType annotationType) {
1760 // AnnotationInfo info = (AnnotationInfo)
1761 // ANNOTATION_MAP.get(annotationType);
1762 // if (info != null)
1763 // return getColor(info.fColorPreference);
1766 public void dispose() {
1767 ISourceViewer sourceViewer = getSourceViewer();
1768 if (sourceViewer instanceof ITextViewerExtension)
1769 ((ITextViewerExtension) sourceViewer).removeVerifyKeyListener(fBracketInserter);
1770 // if (fPropertyChangeListener != null) {
1771 // Preferences preferences =
1772 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1773 // preferences.removePropertyChangeListener(fPropertyChangeListener);
1774 // fPropertyChangeListener = null;
1776 if (fJavaEditorErrorTickUpdater != null) {
1777 fJavaEditorErrorTickUpdater.dispose();
1778 fJavaEditorErrorTickUpdater = null;
1780 // if (fSelectionHistory != null)
1781 // fSelectionHistory.dispose();
1782 // if (fPaintManager != null) {
1783 // fPaintManager.dispose();
1784 // fPaintManager = null;
1786 if (fActionGroups != null) {
1787 fActionGroups.dispose();
1788 fActionGroups = null;
1793 // protected AnnotationType getAnnotationType(String preferenceKey) {
1794 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1795 // while (e.hasNext()) {
1796 // AnnotationType type = (AnnotationType) e.next();
1797 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(type);
1798 // if (info != null) {
1799 // if (preferenceKey.equals(info.fColorPreference)
1800 // || preferenceKey.equals(info.fEditorPreference)
1801 // || preferenceKey.equals(info.fOverviewRulerPreference))
1808 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1810 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1812 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1814 String p = event.getProperty();
1815 if (CLOSE_BRACKETS_PHP.equals(p)) {
1816 fBracketInserter.setCloseBracketsPHPEnabled(getPreferenceStore().getBoolean(p));
1819 if (CLOSE_STRINGS_DQ_PHP.equals(p)) {
1820 fBracketInserter.setCloseStringsPHPDQEnabled(getPreferenceStore().getBoolean(p));
1823 if (CLOSE_STRINGS_SQ_PHP.equals(p)) {
1824 fBracketInserter.setCloseStringsPHPSQEnabled(getPreferenceStore().getBoolean(p));
1827 if (CLOSE_BRACKETS_HTML.equals(p)) {
1828 fBracketInserter.setCloseBracketsHTMLEnabled(getPreferenceStore().getBoolean(p));
1831 if (CLOSE_STRINGS_HTML.equals(p)) {
1832 fBracketInserter.setCloseStringsHTMLEnabled(getPreferenceStore().getBoolean(p));
1835 if (SPACES_FOR_TABS.equals(p)) {
1836 if (isTabConversionEnabled())
1837 startTabConversion();
1839 stopTabConversion();
1842 // if (MATCHING_BRACKETS.equals(p)) {
1843 // if (isBracketHighlightingEnabled())
1844 // startBracketHighlighting();
1846 // stopBracketHighlighting();
1849 // if (MATCHING_BRACKETS_COLOR.equals(p)) {
1850 // if (fBracketPainter != null)
1851 // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1854 // if (CURRENT_LINE.equals(p)) {
1855 // if (isLineHighlightingEnabled())
1856 // startLineHighlighting();
1858 // stopLineHighlighting();
1861 // if (CURRENT_LINE_COLOR.equals(p)) {
1862 // if (fLinePainter != null) {
1863 // stopLineHighlighting();
1864 // startLineHighlighting();
1868 // if (PRINT_MARGIN.equals(p)) {
1869 // if (isPrintMarginVisible())
1870 // showPrintMargin();
1872 // hidePrintMargin();
1875 // if (PRINT_MARGIN_COLOR.equals(p)) {
1876 // if (fPrintMarginPainter != null)
1877 // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
1880 // if (PRINT_MARGIN_COLUMN.equals(p)) {
1881 // if (fPrintMarginPainter != null)
1882 // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
1885 // if (OVERVIEW_RULER.equals(p)) {
1886 // if (isOverviewRulerVisible())
1887 // showOverviewRuler();
1889 // hideOverviewRuler();
1892 // AnnotationType type = getAnnotationType(p);
1893 // if (type != null) {
1895 // AnnotationInfo info = (AnnotationInfo)
1896 // ANNOTATION_MAP.get(type);
1897 // if (info.fColorPreference.equals(p)) {
1898 // Color color = getColor(type);
1899 // if (fProblemPainter != null) {
1900 // fProblemPainter.setColor(type, color);
1901 // fProblemPainter.paint(IPainter.CONFIGURATION);
1903 // setColorInOverviewRuler(type, color);
1907 // if (info.fEditorPreference.equals(p)) {
1908 // if (isAnnotationIndicationEnabled(type))
1909 // startAnnotationIndication(type);
1911 // stopAnnotationIndication(type);
1915 // if (info.fOverviewRulerPreference.equals(p)) {
1916 // if (isAnnotationIndicationInOverviewRulerEnabled(type))
1917 // showAnnotationIndicationInOverviewRuler(type, true);
1919 // showAnnotationIndicationInOverviewRuler(type, false);
1923 IContentAssistant c = asv.getContentAssistant();
1924 if (c instanceof ContentAssistant)
1925 ContentAssistPreference.changeConfiguration((ContentAssistant) c, getPreferenceStore(), event);
1928 super.handlePreferenceStoreChanged(event);
1933 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
1935 protected void handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
1936 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1938 String p = event.getProperty();
1939 if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
1940 asv.updateIndentationPrefixes();
1941 if (fTabConverter != null)
1942 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
1945 super.handlePreferencePropertyChanged(event);
1949 * Handles a property change event describing a change of the php core's preferences and updates the preference related editor
1953 * the property change event
1956 // handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
1958 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1959 // if (asv != null) {
1960 // String p = event.getProperty();
1961 // if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
1962 // asv.updateIndentationPrefixes();
1963 // if (fTabConverter != null)
1964 // fTabConverter.setNumberOfSpacesPerTab(getTabSize());
1969 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#createJavaSourceViewer(org.eclipse.swt.widgets.Composite,
1970 * org.eclipse.jface.text.source.IVerticalRuler, org.eclipse.jface.text.source.IOverviewRuler, boolean, int)
1972 protected ISourceViewer createJavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
1973 boolean isOverviewRulerVisible, int styles, IPreferenceStore store) {
1974 return new AdaptedSourceViewer(parent, verticalRuler, overviewRuler, isOverviewRulerVisible, styles, store);
1977 // protected ISourceViewer createJavaSourceViewer(Composite parent,
1978 // IVerticalRuler ruler, int styles) {
1979 // return new AdaptedSourceViewer(parent, ruler, styles);
1981 private boolean isValidSelection(int offset, int length) {
1982 IDocumentProvider provider = getDocumentProvider();
1983 if (provider != null) {
1984 IDocument document = provider.getDocument(getEditorInput());
1985 if (document != null) {
1986 int end = offset + length;
1987 int documentLength = document.getLength();
1988 return 0 <= offset && offset <= documentLength && 0 <= end && end <= documentLength;
1995 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getInputElement()
1997 protected IJavaElement getInputJavaElement() {
1998 return PHPeclipsePlugin.getDefault().getWorkingCopyManager().getWorkingCopy(getEditorInput());
2002 * @see AbstractTextEditor#editorContextMenuAboutToShow(IMenuManager)
2004 public void editorContextMenuAboutToShow(IMenuManager menu) {
2005 super.editorContextMenuAboutToShow(menu);
2006 ActionContext context = new ActionContext(getSelectionProvider().getSelection());
2007 fContextMenuGroup.setContext(context);
2008 fContextMenuGroup.fillContextMenu(menu);
2009 fContextMenuGroup.setContext(null);
2013 * @see JavaEditor#setOutlinePageInput(JavaOutlinePage, IEditorInput)
2015 protected void setOutlinePageInput(JavaOutlinePage page, IEditorInput input) {
2017 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
2018 page.setInput(manager.getWorkingCopy(input));
2023 * @see AbstractTextEditor#performSaveOperation(WorkspaceModifyOperation, IProgressMonitor)
2025 // protected void performSaveOperation(WorkspaceModifyOperation operation,
2026 // IProgressMonitor progressMonitor) {
2027 // IDocumentProvider p = getDocumentProvider();
2028 // if (p instanceof PHPDocumentProvider) {
2029 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2030 // cp.setSavePolicy(fSavePolicy);
2034 // super.performSaveOperation(operation, progressMonitor);
2036 // if (p instanceof PHPDocumentProvider) {
2037 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2038 // cp.setSavePolicy(null);
2043 * @see AbstractTextEditor#doSave(IProgressMonitor)
2045 public void doSave(IProgressMonitor progressMonitor) {
2047 IDocumentProvider p = getDocumentProvider();
2049 // editor has been closed
2053 if (p.isDeleted(getEditorInput())) {
2055 if (isSaveAsAllowed()) {
2058 * 1GEUSSR: ITPUI:ALL - User should never loose changes made in the editors. Changed Behavior to make sure that if called
2059 * inside a regular save (because of deletion of input element) there is a way to report back to the caller.
2061 performSaveAs(progressMonitor);
2066 * 1GF5YOX: ITPJUI:ALL - Save of delete file claims it's still there Missing resources.
2068 Shell shell = getSite().getShell();
2072 PHPEditorMessages.getString("PHPUnitEditor.error.saving.title1"), PHPEditorMessages.getString("PHPUnitEditor.error.saving.message1")); //$NON-NLS-1$ //$NON-NLS-2$
2076 if (getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_P_RTRIM_ON_SAVE)) {
2077 RTrimAction trimAction = new RTrimAction();
2078 trimAction.setActiveEditor(null, getSite().getPage().getActiveEditor());
2079 trimAction.run(null);
2082 setStatusLineErrorMessage(null);
2084 updateState(getEditorInput());
2085 validateState(getEditorInput());
2087 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
2088 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2091 synchronized (unit) {
2092 performSave(false, progressMonitor);
2095 performSave(false, progressMonitor);
2099 public boolean isSaveAsAllowed() {
2104 * The compilation unit editor implementation of this <code>AbstractTextEditor</code> method asks the user for the workspace
2105 * path of a file resource and saves the document there. See http://dev.eclipse.org/bugs/show_bug.cgi?id=6295
2107 * @param progressMonitor
2108 * the progress monitor
2110 protected void performSaveAs(IProgressMonitor progressMonitor) {
2112 Shell shell = getSite().getShell();
2113 IEditorInput input = getEditorInput();
2115 SaveAsDialog dialog = new SaveAsDialog(shell);
2117 IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
2118 if (original != null)
2119 dialog.setOriginalFile(original);
2123 IDocumentProvider provider = getDocumentProvider();
2124 if (provider == null) {
2125 // editor has been programmatically closed while the dialog was open
2129 if (provider.isDeleted(input) && original != null) {
2130 String message = PHPEditorMessages.getFormattedString(
2131 "CompilationUnitEditor.warning.save.delete", new Object[] { original.getName() }); //$NON-NLS-1$
2132 dialog.setErrorMessage(null);
2133 dialog.setMessage(message, IMessageProvider.WARNING);
2136 if (dialog.open() == Window.CANCEL) {
2137 if (progressMonitor != null)
2138 progressMonitor.setCanceled(true);
2142 IPath filePath = dialog.getResult();
2143 if (filePath == null) {
2144 if (progressMonitor != null)
2145 progressMonitor.setCanceled(true);
2149 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
2150 IFile file = workspaceRoot.getFile(filePath);
2151 final IEditorInput newInput = new FileEditorInput(file);
2153 boolean success = false;
2156 provider.aboutToChange(newInput);
2157 getDocumentProvider().saveDocument(progressMonitor, newInput, getDocumentProvider().getDocument(getEditorInput()), true);
2160 } catch (CoreException x) {
2161 IStatus status = x.getStatus();
2162 if (status == null || status.getSeverity() != IStatus.CANCEL)
2166 PHPEditorMessages.getString("CompilationUnitEditor.error.saving.title2"), PHPEditorMessages.getString("CompilationUnitEditor.error.saving.message2"), x.getStatus()); //$NON-NLS-1$ //$NON-NLS-2$
2168 provider.changed(newInput);
2173 if (progressMonitor != null)
2174 progressMonitor.setCanceled(!success);
2178 * @see AbstractTextEditor#doSetInput(IEditorInput)
2180 protected void doSetInput(IEditorInput input) throws CoreException {
2181 super.doSetInput(input);
2182 configureTabConverter();
2183 configureToggleCommentAction();
2188 // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#installOverrideIndicator(boolean)
2191 // protected void installOverrideIndicator(boolean waitForReconcilation) {
2192 // IAnnotationModel model=
2193 // getDocumentProvider().getAnnotationModel(getEditorInput());
2194 // if (!waitForReconcilation)
2195 // super.installOverrideIndicator(false);
2197 // uninstallOverrideIndicator();
2198 // IJavaElement inputElement= getInputJavaElement();
2199 // if (model == null || inputElement == null)
2202 // fOverrideIndicatorManager= new OverrideIndicatorManager(model,
2203 // inputElement, null);
2204 // addReconcileListener(fOverrideIndicatorManager);
2210 // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#uninstallOverrideIndicator()
2213 // protected void uninstallOverrideIndicator() {
2214 // if (fOverrideIndicatorManager != null)
2215 // removeReconcileListener(fOverrideIndicatorManager);
2216 // super.uninstallOverrideIndicator();
2220 * Configures the toggle comment action
2224 private void configureToggleCommentAction() {
2225 IAction action = getAction("ToggleComment"); //$NON-NLS-1$
2226 if (action instanceof ToggleCommentAction) {
2227 ISourceViewer sourceViewer = getSourceViewer();
2228 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
2229 ((ToggleCommentAction) action).configure(sourceViewer, configuration);
2233 // private void configureTabConverter() {
2234 // if (fTabConverter != null) {
2235 // IDocumentProvider provider = getDocumentProvider();
2236 // if (provider instanceof PHPDocumentProvider) {
2237 // PHPDocumentProvider cup = (PHPDocumentProvider) provider;
2238 // fTabConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
2242 private void configureTabConverter() {
2243 if (fTabConverter != null) {
2244 IDocumentProvider provider = getDocumentProvider();
2245 if (provider instanceof ICompilationUnitDocumentProvider) {
2246 ICompilationUnitDocumentProvider cup = (ICompilationUnitDocumentProvider) provider;
2247 fTabConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
2252 private void startTabConversion() {
2253 if (fTabConverter == null) {
2254 fTabConverter = new TabConverter();
2255 configureTabConverter();
2256 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2257 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2258 asv.addTextConverter(fTabConverter);
2259 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2260 asv.updateIndentationPrefixes();
2264 private void stopTabConversion() {
2265 if (fTabConverter != null) {
2266 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2267 asv.removeTextConverter(fTabConverter);
2268 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2269 asv.updateIndentationPrefixes();
2270 fTabConverter = null;
2275 * @see org.eclipse.ui.texteditor.AbstractTextEditor#performSave(boolean, org.eclipse.core.runtime.IProgressMonitor)
2277 protected void performSave(boolean overwrite, IProgressMonitor progressMonitor) {
2278 // IDocumentProvider p = getDocumentProvider();
2279 // if (p instanceof PHPDocumentProvider) {
2280 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2281 // cp.setSavePolicy(fSavePolicy);
2284 // super.performSave(overwrite, progressMonitor);
2286 // if (p instanceof PHPDocumentProvider) {
2287 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2288 // cp.setSavePolicy(null);
2292 IDocumentProvider p = getDocumentProvider();
2293 if (p instanceof ICompilationUnitDocumentProvider) {
2294 ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2295 cp.setSavePolicy(fSavePolicy);
2298 super.performSave(overwrite, progressMonitor);
2300 if (p instanceof ICompilationUnitDocumentProvider) {
2301 ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2302 cp.setSavePolicy(null);
2308 * @see AbstractTextEditor#doSaveAs
2310 public void doSaveAs() {
2311 if (askIfNonWorkbenchEncodingIsOk()) {
2317 * Asks the user if it is ok to store in non-workbench encoding.
2319 * @return <true>if the user wants to continue
2321 private boolean askIfNonWorkbenchEncodingIsOk() {
2322 IDocumentProvider provider = getDocumentProvider();
2323 if (provider instanceof IStorageDocumentProvider) {
2324 IEditorInput input = getEditorInput();
2325 IStorageDocumentProvider storageProvider = (IStorageDocumentProvider) provider;
2326 String encoding = storageProvider.getEncoding(input);
2327 String defaultEncoding = storageProvider.getDefaultEncoding();
2328 if (encoding != null && !encoding.equals(defaultEncoding)) {
2329 Shell shell = getSite().getShell();
2330 String title = PHPEditorMessages.getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.title"); //$NON-NLS-1$
2333 msg = MessageFormat.format(PHPEditorMessages.getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message1"),
2334 new String[] { input.getName(), encoding }); //$NON-NLS-1$
2336 msg = MessageFormat.format(PHPEditorMessages.getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message2"),
2337 new String[] { encoding }); //$NON-NLS-1$
2338 return MessageDialog.openQuestion(shell, title, msg);
2345 * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#aboutToBeReconciled()
2348 public void aboutToBeReconciled() {
2350 // Notify AST provider
2351 // PHPeclipsePlugin.getDefault().getASTProvider().aboutToBeReconciled(getInputJavaElement());
2354 Object[] listeners = fReconcilingListeners.getListeners();
2355 for (int i = 0, length = listeners.length; i < length; ++i)
2356 ((IJavaReconcilingListener) listeners[i]).aboutToBeReconciled();
2360 * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#reconciled(CompilationUnit, boolean,
2364 public void reconciled(CompilationUnit ast, boolean forced, IProgressMonitor progressMonitor) {
2366 // Always notify AST provider
2367 // PHPeclipsePlugin.getDefault().getASTProvider().reconciled(ast, getInputJavaElement());
2370 // Object[] listeners = fReconcilingListeners.getListeners();
2371 // for (int i = 0, length= listeners.length; i < length; ++i)
2372 // ((IJavaReconcilingListener)listeners[i]).reconciled(ast, forced, progressMonitor);
2374 // Update Java Outline page selection
2375 if (!forced && !progressMonitor.isCanceled()) {
2376 Shell shell = getSite().getShell();
2377 if (shell != null && !shell.isDisposed()) {
2378 shell.getDisplay().asyncExec(new Runnable() {
2388 * Returns the updated java element for the old java element.
2390 private IJavaElement findElement(IJavaElement element) {
2391 if (element == null)
2393 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
2394 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2397 synchronized (unit) {
2400 IJavaElement[] findings = unit.findElements(element);
2401 if (findings != null && findings.length > 0)
2403 } catch (JavaModelException x) {
2404 PHPeclipsePlugin.log(x.getStatus());
2405 // nothing found, be tolerant and go on
2412 * Returns the offset of the given Java element.
2414 private int getOffset(IJavaElement element) {
2415 if (element instanceof ISourceReference) {
2416 ISourceReference sr = (ISourceReference) element;
2418 ISourceRange srcRange = sr.getSourceRange();
2419 if (srcRange != null)
2420 return srcRange.getOffset();
2421 } catch (JavaModelException e) {
2428 * @see AbstractTextEditor#restoreSelection()
2430 // protected void restoreSelection() {
2432 // if (getSourceViewer() == null || fRememberedSelection == null)
2434 // IJavaElement newElement = findElement(fRememberedElement);
2435 // int newOffset = getOffset(newElement);
2436 // int delta = (newOffset > -1 && fRememberedElementOffset > -1) ? newOffset
2437 // - fRememberedElementOffset : 0;
2438 // if (isValidSelection(delta + fRememberedSelection.getOffset(),
2439 // fRememberedSelection.getLength()))
2440 // selectAndReveal(delta + fRememberedSelection.getOffset(),
2441 // fRememberedSelection.getLength());
2443 // fRememberedSelection = null;
2444 // fRememberedElement = null;
2445 // fRememberedElementOffset = -1;
2449 * Tells whether this is the active editor in the active page.
2451 * @return <code>true</code> if this is the active editor in the active page
2452 * @see IWorkbenchPage#getActiveEditor();
2454 protected final boolean isActiveEditor() {
2455 IWorkbenchWindow window = getSite().getWorkbenchWindow();
2456 IWorkbenchPage page = window.getActivePage();
2459 IEditorPart activeEditor = page.getActiveEditor();
2460 return activeEditor != null && activeEditor.equals(this);
2464 * Adds the given listener. Has no effect if an identical listener was not already registered.
2467 * The reconcile listener to be added
2470 final void addReconcileListener(IJavaReconcilingListener listener) {
2471 synchronized (fReconcilingListeners) {
2472 fReconcilingListeners.add(listener);
2477 * Removes the given listener. Has no effect if an identical listener was not already registered.
2480 * the reconcile listener to be removed
2483 final void removeReconcileListener(IJavaReconcilingListener listener) {
2484 synchronized (fReconcilingListeners) {
2485 fReconcilingListeners.remove(listener);
2489 protected void updateStateDependentActions() {
2490 super.updateStateDependentActions();
2491 fGenerateActionGroup.editorStateChanged();
2495 * @see AbstractTextEditor#rememberSelection()
2497 protected void rememberSelection() {
2498 fRememberedSelection.remember();
2502 * @see AbstractTextEditor#restoreSelection()
2504 protected void restoreSelection() {
2505 fRememberedSelection.restore();
2509 * @see AbstractTextEditor#canHandleMove(IEditorInput, IEditorInput)
2511 protected boolean canHandleMove(IEditorInput originalElement, IEditorInput movedElement) {
2513 String oldExtension = ""; //$NON-NLS-1$
2514 if (originalElement instanceof IFileEditorInput) {
2515 IFile file = ((IFileEditorInput) originalElement).getFile();
2517 String ext = file.getFileExtension();
2523 String newExtension = ""; //$NON-NLS-1$
2524 if (movedElement instanceof IFileEditorInput) {
2525 IFile file = ((IFileEditorInput) movedElement).getFile();
2527 newExtension = file.getFileExtension();
2530 return oldExtension.equals(newExtension);
2534 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isPrefQuickDiffAlwaysOn()
2536 protected boolean isPrefQuickDiffAlwaysOn() {
2537 // reestablishes the behaviour from AbstractDecoratedTextEditor which was hacked by JavaEditor
2538 // to disable the change bar for the class file (attached source) java editor.
2539 IPreferenceStore store = getPreferenceStore();
2540 return store.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_ALWAYS_ON);
2544 * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getAdapter(java.lang.Class)
2546 public Object getAdapter(Class required) {
2547 if (SmartBackspaceManager.class.equals(required)) {
2548 if (getSourceViewer() instanceof JavaSourceViewer) {
2549 return ((JavaSourceViewer) getSourceViewer()).getBackspaceManager();
2553 return super.getAdapter(required);
2557 * Returns the mutex for the reconciler. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of the problem.
2559 * TODO remove once the underlying problem is solved.
2562 * @return the lock reconcilers may use to synchronize on
2564 public Object getReconcilerLock() {
2565 return fReconcilerLock;
2571 * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
2573 protected void editorSaved() {
2574 super.editorSaved();
2575 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();
2577 a.refresh(ShowExternalPreviewAction.PHP_TYPE);