1 package net.sourceforge.phpeclipse.phpeditor;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.ResourceBundle;
18 import java.util.StringTokenizer;
20 import net.sourceforge.phpdt.core.ICompilationUnit;
21 import net.sourceforge.phpdt.core.IJavaElement;
22 import net.sourceforge.phpdt.core.IMember;
23 import net.sourceforge.phpdt.core.ISourceRange;
24 import net.sourceforge.phpdt.core.ISourceReference;
25 import net.sourceforge.phpdt.core.JavaModelException;
26 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
27 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
28 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
29 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
30 import net.sourceforge.phpdt.ui.IContextMenuConstants;
31 import net.sourceforge.phpdt.ui.JavaUI;
32 import net.sourceforge.phpdt.ui.PreferenceConstants;
33 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
34 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
35 import net.sourceforge.phpdt.ui.text.IColorManager;
36 import net.sourceforge.phpdt.ui.text.JavaTextTools;
37 import net.sourceforge.phpeclipse.PHPCore;
38 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
39 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
41 import org.eclipse.core.resources.IMarker;
42 import org.eclipse.core.resources.IResource;
43 import org.eclipse.core.runtime.CoreException;
44 import org.eclipse.core.runtime.IStatus;
45 import org.eclipse.core.runtime.Preferences;
46 import org.eclipse.jface.action.Action;
47 import org.eclipse.jface.action.GroupMarker;
48 import org.eclipse.jface.action.IAction;
49 import org.eclipse.jface.action.MenuManager;
50 import org.eclipse.jface.action.Separator;
51 import org.eclipse.jface.preference.IPreferenceStore;
52 import org.eclipse.jface.preference.PreferenceConverter;
53 import org.eclipse.jface.text.BadLocationException;
54 import org.eclipse.jface.text.DefaultInformationControl;
55 import org.eclipse.jface.text.DocumentEvent;
56 import org.eclipse.jface.text.IDocument;
57 import org.eclipse.jface.text.IDocumentListener;
58 import org.eclipse.jface.text.IInformationControl;
59 import org.eclipse.jface.text.IInformationControlCreator;
60 import org.eclipse.jface.text.IRegion;
61 import org.eclipse.jface.text.ITextHover;
62 import org.eclipse.jface.text.ITextInputListener;
63 import org.eclipse.jface.text.ITextOperationTarget;
64 import org.eclipse.jface.text.ITextSelection;
65 import org.eclipse.jface.text.ITextViewer;
66 import org.eclipse.jface.text.ITextViewerExtension2;
67 import org.eclipse.jface.text.ITextViewerExtension3;
68 import org.eclipse.jface.text.ITypedRegion;
69 import org.eclipse.jface.text.Position;
70 import org.eclipse.jface.text.Region;
71 import org.eclipse.jface.text.TextSelection;
72 import org.eclipse.jface.text.information.IInformationProvider;
73 import org.eclipse.jface.text.information.InformationPresenter;
74 import org.eclipse.jface.text.source.Annotation;
75 import org.eclipse.jface.text.source.AnnotationRulerColumn;
76 import org.eclipse.jface.text.source.CompositeRuler;
77 import org.eclipse.jface.text.source.IAnnotationAccess;
78 import org.eclipse.jface.text.source.IAnnotationModel;
79 import org.eclipse.jface.text.source.IOverviewRuler;
80 import org.eclipse.jface.text.source.ISharedTextColors;
81 import org.eclipse.jface.text.source.ISourceViewer;
82 import org.eclipse.jface.text.source.ISourceViewerExtension;
83 import org.eclipse.jface.text.source.IVerticalRuler;
84 import org.eclipse.jface.text.source.IVerticalRulerColumn;
85 import org.eclipse.jface.text.source.LineNumberRulerColumn;
86 import org.eclipse.jface.text.source.OverviewRuler;
87 import org.eclipse.jface.text.source.SourceViewer;
88 import org.eclipse.jface.text.source.SourceViewerConfiguration;
89 import org.eclipse.jface.util.IPropertyChangeListener;
90 import org.eclipse.jface.util.PropertyChangeEvent;
91 import org.eclipse.jface.viewers.ISelection;
92 import org.eclipse.jface.viewers.ISelectionChangedListener;
93 import org.eclipse.jface.viewers.ISelectionProvider;
94 import org.eclipse.jface.viewers.IStructuredSelection;
95 import org.eclipse.jface.viewers.SelectionChangedEvent;
96 import org.eclipse.jface.viewers.StructuredSelection;
97 import org.eclipse.swt.SWT;
98 import org.eclipse.swt.custom.BidiSegmentEvent;
99 import org.eclipse.swt.custom.BidiSegmentListener;
100 import org.eclipse.swt.custom.StyleRange;
101 import org.eclipse.swt.custom.StyledText;
102 import org.eclipse.swt.events.FocusEvent;
103 import org.eclipse.swt.events.FocusListener;
104 import org.eclipse.swt.events.KeyEvent;
105 import org.eclipse.swt.events.KeyListener;
106 import org.eclipse.swt.events.MouseEvent;
107 import org.eclipse.swt.events.MouseListener;
108 import org.eclipse.swt.events.MouseMoveListener;
109 import org.eclipse.swt.events.PaintEvent;
110 import org.eclipse.swt.events.PaintListener;
111 import org.eclipse.swt.graphics.Color;
112 import org.eclipse.swt.graphics.Cursor;
113 import org.eclipse.swt.graphics.GC;
114 import org.eclipse.swt.graphics.Image;
115 import org.eclipse.swt.graphics.Point;
116 import org.eclipse.swt.graphics.RGB;
117 import org.eclipse.swt.widgets.Composite;
118 import org.eclipse.swt.widgets.Control;
119 import org.eclipse.swt.widgets.Display;
120 import org.eclipse.swt.widgets.Shell;
121 import org.eclipse.ui.IEditorInput;
122 import org.eclipse.ui.IPageLayout;
123 import org.eclipse.ui.IPartService;
124 import org.eclipse.ui.IViewPart;
125 import org.eclipse.ui.IWorkbenchPage;
126 import org.eclipse.ui.IWorkbenchPart;
127 import org.eclipse.ui.IWorkbenchWindow;
128 import org.eclipse.ui.actions.ActionContext;
129 import org.eclipse.ui.actions.ActionGroup;
130 import org.eclipse.ui.editors.text.DefaultEncodingSupport;
131 import org.eclipse.ui.editors.text.IEncodingSupport;
132 import org.eclipse.ui.part.IShowInTargetList;
133 import org.eclipse.ui.texteditor.AddTaskAction;
134 import org.eclipse.ui.texteditor.ContentAssistAction;
135 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
136 import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds;
137 import org.eclipse.ui.texteditor.IDocumentProvider;
138 import org.eclipse.ui.texteditor.IEditorStatusLine;
139 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
140 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
141 import org.eclipse.ui.texteditor.MarkerAnnotation;
142 import org.eclipse.ui.texteditor.ResourceAction;
143 import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
144 import org.eclipse.ui.texteditor.StatusTextEditor;
145 import org.eclipse.ui.texteditor.TextEditorAction;
146 import org.eclipse.ui.texteditor.TextOperationAction;
147 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
148 import org.eclipse.ui.views.tasklist.TaskList;
151 * PHP specific text editor.
153 public abstract class PHPEditor extends StatusTextEditor implements IViewPartInputProvider { // extends TextEditor {
155 * "Smart" runnable for updating the outline page's selection.
157 class OutlinePageSelectionUpdater implements Runnable {
159 /** Has the runnable already been posted? */
160 private boolean fPosted = false;
162 public OutlinePageSelectionUpdater() {
166 * @see Runnable#run()
169 synchronizeOutlinePageSelection();
174 * Posts this runnable into the event queue.
180 Shell shell = getSite().getShell();
181 if (shell != null & !shell.isDisposed()) {
183 shell.getDisplay().asyncExec(this);
187 class SelectionChangedListener implements ISelectionChangedListener {
188 public void selectionChanged(SelectionChangedEvent event) {
189 doSelectionChanged(event);
196 class MouseClickListener
197 implements KeyListener, MouseListener, MouseMoveListener, FocusListener, PaintListener, IPropertyChangeListener, IDocumentListener, ITextInputListener {
199 /** The session is active. */
200 private boolean fActive;
202 /** The currently active style range. */
203 private IRegion fActiveRegion;
204 /** The currently active style range as position. */
205 private Position fRememberedPosition;
206 /** The hand cursor. */
207 private Cursor fCursor;
209 /** The link color. */
210 private Color fColor;
211 /** The key modifier mask. */
212 private int fKeyModifierMask;
214 public void deactivate() {
218 public void deactivate(boolean redrawAll) {
222 repairRepresentation(redrawAll);
226 public void install() {
228 ISourceViewer sourceViewer = getSourceViewer();
229 if (sourceViewer == null)
232 StyledText text = sourceViewer.getTextWidget();
233 if (text == null || text.isDisposed())
236 updateColor(sourceViewer);
238 sourceViewer.addTextInputListener(this);
240 IDocument document = sourceViewer.getDocument();
241 if (document != null)
242 document.addDocumentListener(this);
244 text.addKeyListener(this);
245 text.addMouseListener(this);
246 text.addMouseMoveListener(this);
247 text.addFocusListener(this);
248 text.addPaintListener(this);
250 updateKeyModifierMask();
252 IPreferenceStore preferenceStore = getPreferenceStore();
253 preferenceStore.addPropertyChangeListener(this);
256 private void updateKeyModifierMask() {
257 String modifiers = getPreferenceStore().getString(BROWSER_LIKE_LINKS_KEY_MODIFIER);
258 fKeyModifierMask = computeStateMask(modifiers);
259 if (fKeyModifierMask == -1) {
260 // Fallback to stored state mask
261 fKeyModifierMask = getPreferenceStore().getInt(BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK);
265 private int computeStateMask(String modifiers) {
266 if (modifiers == null)
269 if (modifiers.length() == 0)
273 StringTokenizer modifierTokenizer = new StringTokenizer(modifiers, ",;.:+-* "); //$NON-NLS-1$
274 while (modifierTokenizer.hasMoreTokens()) {
275 int modifier = EditorUtility.findLocalizedModifier(modifierTokenizer.nextToken());
276 if (modifier == 0 || (stateMask & modifier) == modifier)
278 stateMask = stateMask | modifier;
283 public void uninstall() {
285 if (fColor != null) {
290 if (fCursor != null) {
295 ISourceViewer sourceViewer = getSourceViewer();
296 if (sourceViewer == null)
299 sourceViewer.removeTextInputListener(this);
301 IDocument document = sourceViewer.getDocument();
302 if (document != null)
303 document.removeDocumentListener(this);
305 IPreferenceStore preferenceStore = getPreferenceStore();
306 if (preferenceStore != null)
307 preferenceStore.removePropertyChangeListener(this);
309 StyledText text = sourceViewer.getTextWidget();
310 if (text == null || text.isDisposed())
313 text.removeKeyListener(this);
314 text.removeMouseListener(this);
315 text.removeMouseMoveListener(this);
316 text.removeFocusListener(this);
317 text.removePaintListener(this);
321 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
323 public void propertyChange(PropertyChangeEvent event) {
324 if (event.getProperty().equals(PHPEditor.LINK_COLOR)) {
325 ISourceViewer viewer = getSourceViewer();
328 } else if (event.getProperty().equals(BROWSER_LIKE_LINKS_KEY_MODIFIER)) {
329 updateKeyModifierMask();
333 private void updateColor(ISourceViewer viewer) {
337 StyledText text = viewer.getTextWidget();
338 if (text == null || text.isDisposed())
341 Display display = text.getDisplay();
342 fColor = createColor(getPreferenceStore(), PHPEditor.LINK_COLOR, display);
346 * Creates a color from the information stored in the given preference store.
347 * Returns <code>null</code> if there is no such information available.
349 private Color createColor(IPreferenceStore store, String key, Display display) {
353 if (store.contains(key)) {
355 if (store.isDefault(key))
356 rgb = PreferenceConverter.getDefaultColor(store, key);
358 rgb = PreferenceConverter.getColor(store, key);
361 return new Color(display, rgb);
367 private void repairRepresentation() {
368 repairRepresentation(false);
371 private void repairRepresentation(boolean redrawAll) {
373 if (fActiveRegion == null)
376 ISourceViewer viewer = getSourceViewer();
377 if (viewer != null) {
380 int offset = fActiveRegion.getOffset();
381 int length = fActiveRegion.getLength();
384 if (!redrawAll && viewer instanceof ITextViewerExtension2)
385 ((ITextViewerExtension2) viewer).invalidateTextPresentation(offset, length);
387 viewer.invalidateTextPresentation();
390 if (viewer instanceof ITextViewerExtension3) {
391 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
392 offset = extension.modelOffset2WidgetOffset(offset);
394 offset -= viewer.getVisibleRegion().getOffset();
397 StyledText text = viewer.getTextWidget();
399 text.redrawRange(offset, length, true);
400 } catch (IllegalArgumentException x) {
401 PHPeclipsePlugin.log(x);
405 fActiveRegion = null;
408 // will eventually be replaced by a method provided by jdt.core
409 private IRegion selectWord(IDocument document, int anchor) {
415 while (offset >= 0) {
416 c = document.getChar(offset);
417 if (!Character.isJavaIdentifierPart(c))
425 int length = document.getLength();
427 while (offset < length) {
428 c = document.getChar(offset);
429 if (!Character.isJavaIdentifierPart(c))
437 return new Region(start, 0);
439 return new Region(start + 1, end - start - 1);
441 } catch (BadLocationException x) {
446 IRegion getCurrentTextRegion(ISourceViewer viewer) {
448 int offset = getCurrentTextOffset(viewer);
453 // IJavaElement input= SelectionConverter.getInput(PHPEditor.this);
454 // if (input == null)
459 // IJavaElement[] elements= null;
460 // synchronized (input) {
461 // elements= ((ICodeAssist) input).codeSelect(offset, 0);
464 // if (elements == null || elements.length == 0)
467 // return selectWord(viewer.getDocument(), offset);
469 // } catch (JavaModelException e) {
474 private int getCurrentTextOffset(ISourceViewer viewer) {
477 StyledText text = viewer.getTextWidget();
478 if (text == null || text.isDisposed())
481 Display display = text.getDisplay();
482 Point absolutePosition = display.getCursorLocation();
483 Point relativePosition = text.toControl(absolutePosition);
485 int widgetOffset = text.getOffsetAtLocation(relativePosition);
486 if (viewer instanceof ITextViewerExtension3) {
487 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
488 return extension.widgetOffset2ModelOffset(widgetOffset);
490 return widgetOffset + viewer.getVisibleRegion().getOffset();
493 } catch (IllegalArgumentException e) {
498 private void highlightRegion(ISourceViewer viewer, IRegion region) {
500 if (region.equals(fActiveRegion))
503 repairRepresentation();
505 StyledText text = viewer.getTextWidget();
506 if (text == null || text.isDisposed())
513 if (viewer instanceof ITextViewerExtension3) {
514 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
515 IRegion widgetRange = extension.modelRange2WidgetRange(region);
516 if (widgetRange == null)
519 offset = widgetRange.getOffset();
520 length = widgetRange.getLength();
523 offset = region.getOffset() - viewer.getVisibleRegion().getOffset();
524 length = region.getLength();
527 StyleRange oldStyleRange = text.getStyleRangeAtOffset(offset);
528 Color foregroundColor = fColor;
529 Color backgroundColor = oldStyleRange == null ? text.getBackground() : oldStyleRange.background;
530 StyleRange styleRange = new StyleRange(offset, length, foregroundColor, backgroundColor);
531 text.setStyleRange(styleRange);
534 text.redrawRange(offset, length, true);
536 fActiveRegion = region;
539 private void activateCursor(ISourceViewer viewer) {
540 StyledText text = viewer.getTextWidget();
541 if (text == null || text.isDisposed())
543 Display display = text.getDisplay();
545 fCursor = new Cursor(display, SWT.CURSOR_HAND);
546 text.setCursor(fCursor);
549 private void resetCursor(ISourceViewer viewer) {
550 StyledText text = viewer.getTextWidget();
551 if (text != null && !text.isDisposed())
552 text.setCursor(null);
554 if (fCursor != null) {
561 * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
563 public void keyPressed(KeyEvent event) {
570 if (event.keyCode != fKeyModifierMask) {
577 // removed for #25871
579 // ISourceViewer viewer= getSourceViewer();
580 // if (viewer == null)
583 // IRegion region= getCurrentTextRegion(viewer);
584 // if (region == null)
587 // highlightRegion(viewer, region);
588 // activateCursor(viewer);
592 * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
594 public void keyReleased(KeyEvent event) {
603 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
605 public void mouseDoubleClick(MouseEvent e) {
608 * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
610 public void mouseDown(MouseEvent event) {
615 if (event.stateMask != fKeyModifierMask) {
620 if (event.button != 1) {
627 * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
629 public void mouseUp(MouseEvent e) {
639 boolean wasActive = fCursor != null;
644 IAction action = getAction("OpenEditor"); //$NON-NLS-1$
651 * @see org.eclipse.swt.events.MouseMoveListener#mouseMove(org.eclipse.swt.events.MouseEvent)
653 public void mouseMove(MouseEvent event) {
655 if (event.widget instanceof Control && !((Control) event.widget).isFocusControl()) {
661 if (event.stateMask != fKeyModifierMask)
663 // modifier was already pressed
667 ISourceViewer viewer = getSourceViewer();
668 if (viewer == null) {
673 StyledText text = viewer.getTextWidget();
674 if (text == null || text.isDisposed()) {
679 if ((event.stateMask & SWT.BUTTON1) != 0 && text.getSelectionCount() != 0) {
684 IRegion region = getCurrentTextRegion(viewer);
685 if (region == null || region.getLength() == 0) {
686 repairRepresentation();
690 highlightRegion(viewer, region);
691 activateCursor(viewer);
695 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
697 public void focusGained(FocusEvent e) {
701 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
703 public void focusLost(FocusEvent event) {
708 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
710 public void documentAboutToBeChanged(DocumentEvent event) {
711 if (fActive && fActiveRegion != null) {
712 fRememberedPosition = new Position(fActiveRegion.getOffset(), fActiveRegion.getLength());
714 event.getDocument().addPosition(fRememberedPosition);
715 } catch (BadLocationException x) {
716 fRememberedPosition = null;
722 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
724 public void documentChanged(DocumentEvent event) {
725 if (fRememberedPosition != null && !fRememberedPosition.isDeleted()) {
726 event.getDocument().removePosition(fRememberedPosition);
727 fActiveRegion = new Region(fRememberedPosition.getOffset(), fRememberedPosition.getLength());
729 fRememberedPosition = null;
731 ISourceViewer viewer = getSourceViewer();
732 if (viewer != null) {
733 StyledText widget = viewer.getTextWidget();
734 if (widget != null && !widget.isDisposed()) {
735 widget.getDisplay().asyncExec(new Runnable() {
745 * @see org.eclipse.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IDocument)
747 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
748 if (oldInput == null)
751 oldInput.removeDocumentListener(this);
755 * @see org.eclipse.jface.text.ITextInputListener#inputDocumentChanged(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IDocument)
757 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
758 if (newInput == null)
760 newInput.addDocumentListener(this);
764 * @see PaintListener#paintControl(PaintEvent)
766 public void paintControl(PaintEvent event) {
767 if (fActiveRegion == null)
770 ISourceViewer viewer = getSourceViewer();
774 StyledText text = viewer.getTextWidget();
775 if (text == null || text.isDisposed())
781 if (viewer instanceof ITextViewerExtension3) {
783 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
784 IRegion widgetRange = extension.modelRange2WidgetRange(new Region(offset, length));
785 if (widgetRange == null)
788 offset = widgetRange.getOffset();
789 length = widgetRange.getLength();
793 IRegion region = viewer.getVisibleRegion();
794 if (!includes(region, fActiveRegion))
797 offset = fActiveRegion.getOffset() - region.getOffset();
798 length = fActiveRegion.getLength();
802 Point minLocation = getMinimumLocation(text, offset, length);
803 Point maxLocation = getMaximumLocation(text, offset, length);
805 int x1 = minLocation.x;
806 int x2 = minLocation.x + maxLocation.x - minLocation.x - 1;
807 int y = minLocation.y + text.getLineHeight() - 1;
810 if (fColor != null && !fColor.isDisposed())
811 gc.setForeground(fColor);
812 gc.drawLine(x1, y, x2, y);
815 private boolean includes(IRegion region, IRegion position) {
816 return position.getOffset() >= region.getOffset() && position.getOffset() + position.getLength() <= region.getOffset() + region.getLength();
819 private Point getMinimumLocation(StyledText text, int offset, int length) {
820 Point minLocation = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
822 for (int i = 0; i <= length; i++) {
823 Point location = text.getLocationAtOffset(offset + i);
825 if (location.x < minLocation.x)
826 minLocation.x = location.x;
827 if (location.y < minLocation.y)
828 minLocation.y = location.y;
834 private Point getMaximumLocation(StyledText text, int offset, int length) {
835 Point maxLocation = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
837 for (int i = 0; i <= length; i++) {
838 Point location = text.getLocationAtOffset(offset + i);
840 if (location.x > maxLocation.x)
841 maxLocation.x = location.x;
842 if (location.y > maxLocation.y)
843 maxLocation.y = location.y;
851 * This action dispatches into two behaviours: If there is no current text
852 * hover, the javadoc is displayed using information presenter. If there is
853 * a current text hover, it is converted into a information presenter in
854 * order to make it sticky.
856 class InformationDispatchAction extends TextEditorAction {
858 /** The wrapped text operation action. */
859 private final TextOperationAction fTextOperationAction;
862 * Creates a dispatch action.
864 public InformationDispatchAction(ResourceBundle resourceBundle, String prefix, final TextOperationAction textOperationAction) {
865 super(resourceBundle, prefix, PHPEditor.this);
866 if (textOperationAction == null)
867 throw new IllegalArgumentException();
868 fTextOperationAction = textOperationAction;
872 * @see org.eclipse.jface.action.IAction#run()
876 ISourceViewer sourceViewer = getSourceViewer();
877 if (sourceViewer == null) {
878 fTextOperationAction.run();
882 if (!(sourceViewer instanceof ITextViewerExtension2)) {
883 fTextOperationAction.run();
887 ITextViewerExtension2 textViewerExtension2 = (ITextViewerExtension2) sourceViewer;
889 // does a text hover exist?
890 ITextHover textHover = textViewerExtension2.getCurrentTextHover();
891 if (textHover == null) {
892 fTextOperationAction.run();
896 Point hoverEventLocation = textViewerExtension2.getHoverEventLocation();
897 int offset = computeOffsetAtLocation(sourceViewer, hoverEventLocation.x, hoverEventLocation.y);
899 fTextOperationAction.run();
904 // get the text hover content
905 IDocument document = sourceViewer.getDocument();
906 String contentType = document.getContentType(offset);
908 final IRegion hoverRegion = textHover.getHoverRegion(sourceViewer, offset);
909 if (hoverRegion == null)
912 final String hoverInfo = textHover.getHoverInfo(sourceViewer, hoverRegion);
914 // with information provider
915 IInformationProvider informationProvider = new IInformationProvider() {
917 * @see org.eclipse.jface.text.information.IInformationProvider#getSubject(org.eclipse.jface.text.ITextViewer, int)
919 public IRegion getSubject(ITextViewer textViewer, int offset) {
923 * @see org.eclipse.jface.text.information.IInformationProvider#getInformation(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
925 public String getInformation(ITextViewer textViewer, IRegion subject) {
930 fInformationPresenter.setOffset(offset);
931 fInformationPresenter.setInformationProvider(informationProvider, contentType);
932 fInformationPresenter.showInformation();
934 } catch (BadLocationException e) {
938 // modified version from TextViewer
939 private int computeOffsetAtLocation(ITextViewer textViewer, int x, int y) {
941 StyledText styledText = textViewer.getTextWidget();
942 IDocument document = textViewer.getDocument();
944 if (document == null)
948 int widgetLocation = styledText.getOffsetAtLocation(new Point(x, y));
949 if (textViewer instanceof ITextViewerExtension3) {
950 ITextViewerExtension3 extension = (ITextViewerExtension3) textViewer;
951 return extension.widgetOffset2ModelOffset(widgetLocation);
953 IRegion visibleRegion = textViewer.getVisibleRegion();
954 return widgetLocation + visibleRegion.getOffset();
956 } catch (IllegalArgumentException e) {
963 static protected class AnnotationAccess implements IAnnotationAccess {
965 * @see org.eclipse.jface.text.source.IAnnotationAccess#getType(org.eclipse.jface.text.source.Annotation)
967 public Object getType(Annotation annotation) {
968 if (annotation instanceof IJavaAnnotation) {
969 IJavaAnnotation javaAnnotation = (IJavaAnnotation) annotation;
970 if (javaAnnotation.isRelevant())
971 return javaAnnotation.getAnnotationType();
977 * @see org.eclipse.jface.text.source.IAnnotationAccess#isMultiLine(org.eclipse.jface.text.source.Annotation)
979 public boolean isMultiLine(Annotation annotation) {
984 * @see org.eclipse.jface.text.source.IAnnotationAccess#isTemporary(org.eclipse.jface.text.source.Annotation)
986 public boolean isTemporary(Annotation annotation) {
987 if (annotation instanceof IJavaAnnotation) {
988 IJavaAnnotation javaAnnotation = (IJavaAnnotation) annotation;
989 if (javaAnnotation.isRelevant())
990 return javaAnnotation.isTemporary();
996 private class PropertyChangeListener implements org.eclipse.core.runtime.Preferences.IPropertyChangeListener {
998 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
1000 public void propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
1001 handlePreferencePropertyChanged(event);
1004 /** Preference key for showing the line number ruler */
1005 private final static String LINE_NUMBER_RULER = PreferenceConstants.EDITOR_LINE_NUMBER_RULER;
1006 /** Preference key for the foreground color of the line numbers */
1007 private final static String LINE_NUMBER_COLOR = PreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR;
1008 /** Preference key for the link color */
1009 private final static String LINK_COLOR = PreferenceConstants.EDITOR_LINK_COLOR;
1010 /** Preference key for compiler task tags */
1011 private final static String COMPILER_TASK_TAGS = PHPCore.COMPILER_TASK_TAGS;
1013 // protected PHPActionGroup fActionGroups;
1014 // /** The outline page */
1015 // private AbstractContentOutlinePage fOutlinePage;
1016 /** The outline page */
1017 protected JavaOutlinePage fOutlinePage;
1018 /** Outliner context menu Id */
1019 protected String fOutlinerContextMenuId;
1020 /** Indicates whether this editor should react on outline page selection changes */
1021 private int fIgnoreOutlinePageSelection;
1023 /** The outline page selection updater */
1024 private OutlinePageSelectionUpdater fUpdater;
1025 // protected PHPSyntaxParserThread fValidationThread = null;
1027 // private IPreferenceStore fPHPPrefStore;
1028 /** The selection changed listener */
1029 protected ISelectionChangedListener fSelectionChangedListener = new SelectionChangedListener();
1031 /** The editor's bracket matcher */
1032 private PHPPairMatcher fBracketMatcher = new PHPPairMatcher(BRACKETS);
1035 /** The line number ruler column */
1036 private LineNumberRulerColumn fLineNumberRulerColumn;
1037 /** This editor's encoding support */
1038 private DefaultEncodingSupport fEncodingSupport;
1039 /** The mouse listener */
1040 private MouseClickListener fMouseListener;
1042 protected CompositeActionGroup fActionGroups;
1043 /** The standard action groups added to the menu */
1044 protected GenerateActionGroup fGenerateActionGroup;
1045 protected CompositeActionGroup fContextMenuGroup;
1047 /** The information presenter. */
1048 private InformationPresenter fInformationPresenter;
1049 /** The annotation access */
1050 protected IAnnotationAccess fAnnotationAccess = new AnnotationAccess();
1051 /** The overview ruler */
1052 protected OverviewRuler isOverviewRulerVisible;
1053 /** The source viewer decoration support */
1054 protected SourceViewerDecorationSupport fSourceViewerDecorationSupport;
1055 /** The overview ruler */
1056 protected OverviewRuler fOverviewRuler;
1058 /** The preference property change listener for java core. */
1059 private org.eclipse.core.runtime.Preferences.IPropertyChangeListener fPropertyChangeListener = new PropertyChangeListener();
1061 * Returns the most narrow java element including the given offset
1063 * @param offset the offset inside of the requested element
1065 abstract protected IJavaElement getElementAt(int offset);
1068 * Returns the java element of this editor's input corresponding to the given IJavaElement
1070 abstract protected IJavaElement getCorrespondingElement(IJavaElement element);
1072 * Sets the input of the editor's outline page.
1074 abstract protected void setOutlinePageInput(JavaOutlinePage page, IEditorInput input);
1077 * Default constructor.
1079 public PHPEditor() {
1081 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
1082 setSourceViewerConfiguration(new PHPSourceViewerConfiguration(textTools, this));
1083 setRangeIndicator(new DefaultRangeIndicator());
1084 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
1086 // don't activate this scope without synchronizing plugin.xml !!!
1087 // setKeyBindingScopes(new String[] { "net.sourceforge.phpdt.ui.phpEditorScope" }); //$NON-NLS-1$
1089 if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE))
1090 fUpdater= new OutlinePageSelectionUpdater();
1094 // * @see IMember#getCompilationUnit()
1096 // public ICompilationUnit getCompilationUnit() {
1100 // * @see org.phpeclipse.phpdt.internal.compiler.env.ICompilationUnit#getContents()
1102 // public char[] getContents() {
1103 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
1105 // return doc.get().toCharArray();
1108 * @see org.eclipse.ui.texteditor.AbstractTextEditor#updatePropertyDependentActions()
1110 protected void updatePropertyDependentActions() {
1111 super.updatePropertyDependentActions();
1112 if (fEncodingSupport != null)
1113 fEncodingSupport.reset();
1117 * Update the hovering behavior depending on the preferences.
1119 private void updateHoverBehavior() {
1120 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
1121 String[] types = configuration.getConfiguredContentTypes(getSourceViewer());
1123 for (int i = 0; i < types.length; i++) {
1125 String t = types[i];
1127 int[] stateMasks = configuration.getConfiguredTextHoverStateMasks(getSourceViewer(), t);
1129 ISourceViewer sourceViewer = getSourceViewer();
1130 if (sourceViewer instanceof ITextViewerExtension2) {
1131 if (stateMasks != null) {
1132 for (int j = 0; j < stateMasks.length; j++) {
1133 int stateMask = stateMasks[j];
1134 ITextHover textHover = configuration.getTextHover(sourceViewer, t, stateMask);
1135 ((ITextViewerExtension2) sourceViewer).setTextHover(textHover, t, stateMask);
1138 ITextHover textHover = configuration.getTextHover(sourceViewer, t);
1139 ((ITextViewerExtension2) sourceViewer).setTextHover(textHover, t, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
1142 sourceViewer.setTextHover(configuration.getTextHover(sourceViewer, t), t);
1146 public void updatedTitleImage(Image image) {
1147 setTitleImage(image);
1150 * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
1152 public Object getViewPartInput() {
1153 return getEditorInput().getAdapter(IResource.class);
1156 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSetSelection(ISelection)
1158 protected void doSetSelection(ISelection selection) {
1159 super.doSetSelection(selection);
1160 synchronizeOutlinePageSelection();
1163 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.
1164 * widgets.Composite)
1166 public void createPartControl(Composite parent) {
1167 super.createPartControl(parent);
1169 fSourceViewerDecorationSupport.install(getPreferenceStore());
1171 Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1172 preferences.addPropertyChangeListener(fPropertyChangeListener);
1174 IInformationControlCreator informationControlCreator = new IInformationControlCreator() {
1175 public IInformationControl createInformationControl(Shell parent) {
1176 boolean cutDown = false;
1177 int style = cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL);
1178 return new DefaultInformationControl(parent, SWT.RESIZE, style, new HTMLTextPresenter(cutDown));
1182 fInformationPresenter = new InformationPresenter(informationControlCreator);
1183 fInformationPresenter.setSizeConstraints(60, 10, true, true);
1184 fInformationPresenter.install(getSourceViewer());
1189 * Returns this document's complete text.
1191 * @return the document's complete text
1193 public String get() {
1194 IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
1199 * Sets the outliner's context menu ID.
1201 protected void setOutlinerContextMenuId(String menuId) {
1202 fOutlinerContextMenuId = menuId;
1206 * Returns the standard action group of this editor.
1208 protected ActionGroup getActionGroup() {
1209 return fActionGroups;
1212 // public JavaOutlinePage getfOutlinePage() {
1213 // return fOutlinePage;
1216 /** The <code>PHPEditor</code> implementation of this
1217 * <code>AbstractTextEditor</code> method extend the
1218 * actions to add those specific to the receiver
1220 protected void createActions() {
1221 super.createActions();
1223 ResourceAction resAction = new AddTaskAction(PHPEditorMessages.getResourceBundle(), "AddTask.", this); //$NON-NLS-1$
1224 resAction.setHelpContextId(IAbstractTextEditorHelpContextIds.ADD_TASK_ACTION);
1225 resAction.setActionDefinitionId(ITextEditorActionDefinitionIds.ADD_TASK);
1226 setAction(ITextEditorActionConstants.ADD_TASK, resAction);
1228 resAction = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "ShowJavaDoc.", this, ISourceViewer.INFORMATION, true); //$NON-NLS-1$
1229 resAction = new InformationDispatchAction(PHPEditorMessages.getResourceBundle(), "ShowJavaDoc.", (TextOperationAction) resAction); //$NON-NLS-1$
1230 resAction.setActionDefinitionId(PHPEditorActionDefinitionIds.SHOW_JAVADOC);
1231 setAction("ShowJavaDoc", resAction); //$NON-NLS-1$
1232 // WorkbenchHelp.setHelp(resAction, IJavaHelpContextIds.SHOW_JAVADOC_ACTION);
1238 new TextOperationAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip.", this, ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
1240 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
1241 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
1242 setAction("ContentAssistProposal", action); //$NON-NLS-1$
1243 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
1244 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
1246 fEncodingSupport = new DefaultEncodingSupport();
1247 fEncodingSupport.initialize(this);
1249 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
1250 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
1251 setAction("Comment", action); //$NON-NLS-1$
1252 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
1253 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
1255 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
1256 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
1257 setAction("Uncomment", action); //$NON-NLS-1$
1258 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
1259 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
1261 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
1262 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
1263 setAction("Format", action); //$NON-NLS-1$
1264 markAsStateDependentAction("Format", true); //$NON-NLS-1$
1265 markAsSelectionDependentAction("Format", true); //$NON-NLS-1$
1266 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
1268 action = new GotoMatchingBracketAction(this);
1269 action.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
1270 setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action);
1272 fGenerateActionGroup = new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
1274 fActionGroups = new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
1276 // We have to keep the context menu group separate to have better control over positioning
1277 fContextMenuGroup = new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
1279 // new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)});
1281 // if (fValidationThread == null) {
1282 // fValidationThread =
1283 // new PHPSyntaxParserThread(this, getSourceViewer());
1284 // //Thread defaults
1286 // fValidationThread.start();
1289 // fValidationThread.setText(getSourceViewer().getTextWidget().getText());
1292 /** The <code>PHPEditor</code> implementation of this
1293 * <code>AbstractTextEditor</code> method performs any extra
1294 * disposal actions required by the php editor.
1296 public void dispose() {
1297 // PHPEditorEnvironment.disconnect(this);
1298 if (fOutlinePage != null)
1299 fOutlinePage.setInput(null);
1301 if (fActionGroups != null)
1302 fActionGroups.dispose();
1304 if (isBrowserLikeLinks())
1305 disableBrowserLikeLinks();
1307 if (fEncodingSupport != null) {
1308 fEncodingSupport.dispose();
1309 fEncodingSupport = null;
1312 if (fPropertyChangeListener != null) {
1313 Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1314 preferences.removePropertyChangeListener(fPropertyChangeListener);
1315 fPropertyChangeListener = null;
1318 if (fSourceViewerDecorationSupport != null) {
1319 fSourceViewerDecorationSupport.dispose();
1320 fSourceViewerDecorationSupport = null;
1323 if (fBracketMatcher != null) {
1324 fBracketMatcher.dispose();
1325 fBracketMatcher = null;
1330 /** The <code>PHPEditor</code> implementation of this
1331 * <code>AbstractTextEditor</code> method performs any extra
1332 * revert behavior required by the php editor.
1334 // public void doRevertToSaved() {
1335 // super.doRevertToSaved();
1336 // if (fOutlinePage != null)
1337 // fOutlinePage.update();
1340 /** The <code>PHPEditor</code> implementation of this
1341 * <code>AbstractTextEditor</code> method performs any extra
1342 * save behavior required by the php editor.
1344 // public void doSave(IProgressMonitor monitor) {
1345 // super.doSave(monitor);
1346 // compile or not, according to the user preferences
1347 // IPreferenceStore store = getPreferenceStore();
1349 // the parse on save was changed to the eclipse "builders" concept
1350 // if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
1351 // IAction a = PHPParserAction.getInstance();
1356 // if (SWT.getPlatform().equals("win32")) {
1357 // IAction a = ShowExternalPreviewAction.getInstance();
1361 // if (fOutlinePage != null)
1362 // fOutlinePage.update();
1365 /** The <code>PHPEditor</code> implementation of this
1366 * <code>AbstractTextEditor</code> method performs any extra
1367 * save as behavior required by the php editor.
1369 // public void doSaveAs() {
1370 // super.doSaveAs();
1371 // if (fOutlinePage != null)
1372 // fOutlinePage.update();
1375 * @see StatusTextEditor#getStatusHeader(IStatus)
1377 protected String getStatusHeader(IStatus status) {
1378 if (fEncodingSupport != null) {
1379 String message = fEncodingSupport.getStatusHeader(status);
1380 if (message != null)
1383 return super.getStatusHeader(status);
1387 * @see StatusTextEditor#getStatusBanner(IStatus)
1389 protected String getStatusBanner(IStatus status) {
1390 if (fEncodingSupport != null) {
1391 String message = fEncodingSupport.getStatusBanner(status);
1392 if (message != null)
1395 return super.getStatusBanner(status);
1399 * @see StatusTextEditor#getStatusMessage(IStatus)
1401 protected String getStatusMessage(IStatus status) {
1402 if (fEncodingSupport != null) {
1403 String message = fEncodingSupport.getStatusMessage(status);
1404 if (message != null)
1407 return super.getStatusMessage(status);
1409 /** The <code>PHPEditor</code> implementation of this
1410 * <code>AbstractTextEditor</code> method performs sets the
1411 * input of the outline page after AbstractTextEditor has set input.
1413 // protected void doSetInput(IEditorInput input) throws CoreException {
1414 // super.doSetInput(input);
1416 // if (fEncodingSupport != null)
1417 // fEncodingSupport.reset();
1418 // if (fOutlinePage != null)
1419 // fOutlinePage.setInput(input);
1420 // // setOutlinePageInput(fOutlinePage, input);
1422 protected void doSetInput(IEditorInput input) throws CoreException {
1423 super.doSetInput(input);
1424 if (fEncodingSupport != null)
1425 fEncodingSupport.reset();
1426 setOutlinePageInput(fOutlinePage, input);
1429 * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
1431 // public Object getViewPartInput() {
1432 // return getEditorInput().getAdapter(IFile.class);
1435 /** The <code>PHPEditor</code> implementation of this
1436 * <code>AbstractTextEditor</code> method adds any
1437 * PHPEditor specific entries.
1439 public void editorContextMenuAboutToShow(MenuManager menu) {
1440 super.editorContextMenuAboutToShow(menu);
1441 menu.appendToGroup(ITextEditorActionConstants.GROUP_UNDO, new Separator(IContextMenuConstants.GROUP_OPEN));
1442 menu.insertAfter(IContextMenuConstants.GROUP_OPEN, new GroupMarker(IContextMenuConstants.GROUP_SHOW));
1444 ActionContext context = new ActionContext(getSelectionProvider().getSelection());
1445 fContextMenuGroup.setContext(context);
1446 fContextMenuGroup.fillContextMenu(menu);
1447 fContextMenuGroup.setContext(null);
1448 // addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format"); //$NON-NLS-1$
1450 // ActionContext context =
1451 // new ActionContext(getSelectionProvider().getSelection());
1452 // fContextMenuGroup.setContext(context);
1453 // fContextMenuGroup.fillContextMenu(menu);
1454 // fContextMenuGroup.setContext(null);
1458 * Creates the outline page used with this editor.
1460 protected JavaOutlinePage createOutlinePage() {
1462 // AbstractContentOutlinePage page = new PHPContentOutlinePage(getDocumentProvider(), this);
1464 // page.addSelectionChangedListener(fSelectionChangedListener);
1465 // // setOutlinePageInput(page, getEditorInput());
1466 // if (getEditorInput() != null)
1467 // fOutlinePage.setInput(getEditorInput());
1470 JavaOutlinePage page = new JavaOutlinePage(fOutlinerContextMenuId, this);
1472 page.addSelectionChangedListener(fSelectionChangedListener);
1473 setOutlinePageInput(page, getEditorInput());
1479 * Informs the editor that its outliner has been closed.
1481 public void outlinePageClosed() {
1482 if (fOutlinePage != null) {
1483 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1484 fOutlinePage = null;
1485 resetHighlightRange();
1489 * Synchronizes the outliner selection with the actual cursor
1490 * position in the editor.
1492 public void synchronizeOutlinePageSelection() {
1494 // if (isEditingScriptRunning())
1497 ISourceViewer sourceViewer = getSourceViewer();
1498 if (sourceViewer == null || fOutlinePage == null)
1501 StyledText styledText = sourceViewer.getTextWidget();
1502 if (styledText == null)
1506 if (sourceViewer instanceof ITextViewerExtension3) {
1507 ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
1508 caret = extension.widgetOffset2ModelOffset(styledText.getCaretOffset());
1510 int offset = sourceViewer.getVisibleRegion().getOffset();
1511 caret = offset + styledText.getCaretOffset();
1514 IJavaElement element = getElementAt(caret);
1515 if (element instanceof ISourceReference) {
1516 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1517 fOutlinePage.select((ISourceReference) element);
1518 fOutlinePage.addSelectionChangedListener(fSelectionChangedListener);
1521 protected void updateStateDependentActions() {
1522 super.updateStateDependentActions();
1523 fGenerateActionGroup.editorStateChanged();
1526 protected void setSelection(ISourceReference reference, boolean moveCursor) {
1528 ISelection selection = getSelectionProvider().getSelection();
1529 if (selection instanceof TextSelection) {
1530 TextSelection textSelection = (TextSelection) selection;
1531 if (textSelection.getOffset() != 0 || textSelection.getLength() != 0)
1532 markInNavigationHistory();
1535 if (reference != null) {
1537 StyledText textWidget = null;
1539 ISourceViewer sourceViewer = getSourceViewer();
1540 if (sourceViewer != null)
1541 textWidget = sourceViewer.getTextWidget();
1543 if (textWidget == null)
1548 ISourceRange range = reference.getSourceRange();
1552 int offset = range.getOffset();
1553 int length = range.getLength();
1555 if (offset < 0 || length < 0)
1558 textWidget.setRedraw(false);
1560 setHighlightRange(offset, length, moveCursor);
1568 if (reference instanceof IMember) {
1569 range = ((IMember) reference).getNameRange();
1570 if (range != null) {
1571 offset = range.getOffset();
1572 length = range.getLength();
1575 // else if (reference instanceof IImportDeclaration) {
1576 // String name= ((IImportDeclaration) reference).getElementName();
1577 // if (name != null && name.length() > 0) {
1578 // String content= reference.getSource();
1579 // if (content != null) {
1580 // offset= range.getOffset() + content.indexOf(name);
1581 // length= name.length();
1584 // } else if (reference instanceof IPackageDeclaration) {
1585 // String name= ((IPackageDeclaration) reference).getElementName();
1586 // if (name != null && name.length() > 0) {
1587 // String content= reference.getSource();
1588 // if (content != null) {
1589 // offset= range.getOffset() + content.indexOf(name);
1590 // length= name.length();
1595 if (offset > -1 && length > 0) {
1596 sourceViewer.revealRange(offset, length);
1597 sourceViewer.setSelectedRange(offset, length);
1600 } catch (JavaModelException x) {
1601 } catch (IllegalArgumentException x) {
1603 if (textWidget != null)
1604 textWidget.setRedraw(true);
1607 } else if (moveCursor) {
1608 resetHighlightRange();
1611 markInNavigationHistory();
1614 public void setSelection(IJavaElement element) {
1616 if (element == null || element instanceof ICompilationUnit) { // || element instanceof IClassFile) {
1618 * If the element is an ICompilationUnit this unit is either the input
1619 * of this editor or not being displayed. In both cases, nothing should
1620 * happened. (http://dev.eclipse.org/bugs/show_bug.cgi?id=5128)
1625 IJavaElement corresponding = getCorrespondingElement(element);
1626 if (corresponding instanceof ISourceReference) {
1627 ISourceReference reference = (ISourceReference) corresponding;
1628 // set hightlight range
1629 setSelection(reference, true);
1630 // set outliner selection
1631 if (fOutlinePage != null) {
1632 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1633 fOutlinePage.select(reference);
1634 fOutlinePage.addSelectionChangedListener(fSelectionChangedListener);
1639 public synchronized void editingScriptStarted() {
1640 ++ fIgnoreOutlinePageSelection;
1643 public synchronized void editingScriptEnded() {
1644 -- fIgnoreOutlinePageSelection;
1647 public synchronized boolean isEditingScriptRunning() {
1648 return (fIgnoreOutlinePageSelection > 0);
1651 /** The <code>PHPEditor</code> implementation of this
1652 * <code>AbstractTextEditor</code> method performs gets
1653 * the java content outline page if request is for a an
1656 public Object getAdapter(Class required) {
1658 if (IContentOutlinePage.class.equals(required)) {
1659 if (fOutlinePage == null)
1660 fOutlinePage = createOutlinePage();
1661 return fOutlinePage;
1664 if (IEncodingSupport.class.equals(required))
1665 return fEncodingSupport;
1667 if (required == IShowInTargetList.class) {
1668 return new IShowInTargetList() {
1669 public String[] getShowInTargetIds() {
1670 return new String[] { JavaUI.ID_PACKAGES, IPageLayout.ID_OUTLINE, IPageLayout.ID_RES_NAV };
1676 return super.getAdapter(required);
1678 // public Object getAdapter(Class required) {
1679 // if (IContentOutlinePage.class.equals(required)) {
1680 // if (fOutlinePage == null) {
1681 // fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
1682 // if (getEditorInput() != null)
1683 // fOutlinePage.setInput(getEditorInput());
1685 // return fOutlinePage;
1688 // if (IEncodingSupport.class.equals(required))
1689 // return fEncodingSupport;
1691 // return super.getAdapter(required);
1694 protected void doSelectionChanged(SelectionChangedEvent event) {
1695 ISourceReference reference = null;
1697 ISelection selection = event.getSelection();
1698 Iterator iter = ((IStructuredSelection) selection).iterator();
1699 while (iter.hasNext()) {
1700 Object o = iter.next();
1701 if (o instanceof ISourceReference) {
1702 reference = (ISourceReference) o;
1707 if (!isActivePart() && PHPeclipsePlugin.getActivePage() != null)
1708 PHPeclipsePlugin.getActivePage().bringToTop(this);
1711 editingScriptStarted();
1712 setSelection(reference, !isActivePart());
1714 editingScriptEnded();
1718 * @see AbstractTextEditor#adjustHighlightRange(int, int)
1720 protected void adjustHighlightRange(int offset, int length) {
1724 IJavaElement element = getElementAt(offset);
1725 while (element instanceof ISourceReference) {
1726 ISourceRange range = ((ISourceReference) element).getSourceRange();
1727 if (offset < range.getOffset() + range.getLength() && range.getOffset() < offset + length) {
1728 setHighlightRange(range.getOffset(), range.getLength(), true);
1729 if (fOutlinePage != null) {
1730 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1731 fOutlinePage.select((ISourceReference) element);
1732 fOutlinePage.addSelectionChangedListener(fSelectionChangedListener);
1736 element = element.getParent();
1739 } catch (JavaModelException x) {
1740 PHPeclipsePlugin.log(x.getStatus());
1743 resetHighlightRange();
1745 protected boolean isActivePart() {
1746 IWorkbenchWindow window = getSite().getWorkbenchWindow();
1747 IPartService service = window.getPartService();
1748 IWorkbenchPart part = service.getActivePart();
1749 return part != null && part.equals(this);
1753 // public void openContextHelp() {
1754 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
1755 // ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
1756 // int pos = selection.getOffset();
1757 // String word = getFunctionName(doc, pos);
1758 // openContextHelp(word);
1761 // private void openContextHelp(String word) {
1765 // public static void open(String word) {
1766 // IHelp help = WorkbenchHelp.getHelpSupport();
1767 // if (help != null) {
1768 // IHelpResource helpResource = new PHPFunctionHelpResource(word);
1769 // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
1771 // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
1775 // private String getFunctionName(IDocument doc, int pos) {
1776 // Point word = PHPWordExtractor.findWord(doc, pos);
1777 // if (word != null) {
1779 // return doc.get(word.x, word.y).replace('_', '-');
1780 // } catch (BadLocationException e) {
1787 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1789 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1793 ISourceViewer sourceViewer = getSourceViewer();
1794 if (sourceViewer == null)
1797 String property = event.getProperty();
1799 if (PreferenceConstants.EDITOR_TAB_WIDTH.equals(property)) {
1800 Object value = event.getNewValue();
1801 if (value instanceof Integer) {
1802 sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
1803 } else if (value instanceof String) {
1804 sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
1809 if (OVERVIEW_RULER.equals(property)) {
1810 if (isOverviewRulerVisible())
1811 showOverviewRuler();
1813 hideOverviewRuler();
1817 if (LINE_NUMBER_RULER.equals(property)) {
1818 if (isLineNumberRulerVisible())
1819 showLineNumberRuler();
1821 hideLineNumberRuler();
1825 if (fLineNumberRulerColumn != null
1826 && (LINE_NUMBER_COLOR.equals(property) || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property) || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
1828 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
1831 if (isJavaEditorHoverProperty(property))
1832 updateHoverBehavior();
1834 if (BROWSER_LIKE_LINKS.equals(property)) {
1835 if (isBrowserLikeLinks())
1836 enableBrowserLikeLinks();
1838 disableBrowserLikeLinks();
1843 super.handlePreferenceStoreChanged(event);
1848 // * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1850 // protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1854 // ISourceViewer sourceViewer = getSourceViewer();
1855 // if (sourceViewer == null)
1858 // String property = event.getProperty();
1860 // // if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
1861 // // Object value= event.getNewValue();
1862 // // if (value instanceof Integer) {
1863 // // sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
1864 // // } else if (value instanceof String) {
1865 // // sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
1870 // if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
1871 // if (isLineNumberRulerVisible())
1872 // showLineNumberRuler();
1874 // hideLineNumberRuler();
1878 // if (fLineNumberRulerColumn != null
1879 // && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
1880 // || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
1881 // || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
1883 // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
1887 // super.handlePreferenceStoreChanged(event);
1891 // private boolean isJavaEditorHoverProperty(String property) {
1892 // return PreferenceConstants.EDITOR_DEFAULT_HOVER.equals(property)
1893 // || PreferenceConstants.EDITOR_NONE_HOVER.equals(property)
1894 // || PreferenceConstants.EDITOR_CTRL_HOVER.equals(property)
1895 // || PreferenceConstants.EDITOR_SHIFT_HOVER.equals(property)
1896 // || PreferenceConstants.EDITOR_CTRL_ALT_HOVER.equals(property)
1897 // || PreferenceConstants.EDITOR_CTRL_SHIFT_HOVER.equals(property)
1898 // || PreferenceConstants.EDITOR_CTRL_ALT_SHIFT_HOVER.equals(property)
1899 // || PreferenceConstants.EDITOR_ALT_SHIFT_HOVER.equals(property);
1903 * Shows the line number ruler column.
1905 private void showLineNumberRuler() {
1906 IVerticalRuler v = getVerticalRuler();
1907 if (v instanceof CompositeRuler) {
1908 CompositeRuler c = (CompositeRuler) v;
1909 c.addDecorator(1, createLineNumberRulerColumn());
1912 private boolean isJavaEditorHoverProperty(String property) {
1913 return PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS.equals(property);
1917 * Return whether the browser like links should be enabled
1918 * according to the preference store settings.
1919 * @return <code>true</code> if the browser like links should be enabled
1921 private boolean isBrowserLikeLinks() {
1922 IPreferenceStore store = getPreferenceStore();
1923 return store.getBoolean(BROWSER_LIKE_LINKS);
1927 * Enables browser like links.
1929 private void enableBrowserLikeLinks() {
1930 if (fMouseListener == null) {
1931 fMouseListener = new MouseClickListener();
1932 fMouseListener.install();
1937 * Disables browser like links.
1939 private void disableBrowserLikeLinks() {
1940 if (fMouseListener != null) {
1941 fMouseListener.uninstall();
1942 fMouseListener = null;
1946 * Handles a property change event describing a change
1947 * of the java core's preferences and updates the preference
1948 * related editor properties.
1950 * @param event the property change event
1952 protected void handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
1953 if (COMPILER_TASK_TAGS.equals(event.getProperty())) {
1954 ISourceViewer sourceViewer = getSourceViewer();
1955 if (sourceViewer != null
1956 && affectsTextPresentation(new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue())))
1957 sourceViewer.invalidateTextPresentation();
1962 * Return whether the line number ruler column should be
1963 * visible according to the preference store settings.
1964 * @return <code>true</code> if the line numbers should be visible
1966 private boolean isLineNumberRulerVisible() {
1967 IPreferenceStore store = getPreferenceStore();
1968 return store.getBoolean(LINE_NUMBER_RULER);
1971 * Hides the line number ruler column.
1973 private void hideLineNumberRuler() {
1974 IVerticalRuler v = getVerticalRuler();
1975 if (v instanceof CompositeRuler) {
1976 CompositeRuler c = (CompositeRuler) v;
1978 c.removeDecorator(1);
1979 } catch (Throwable e) {
1985 * @see AbstractTextEditor#handleCursorPositionChanged()
1987 protected void handleCursorPositionChanged() {
1988 super.handleCursorPositionChanged();
1989 if (!isEditingScriptRunning() && fUpdater != null)
1994 * Initializes the given line number ruler column from the preference store.
1995 * @param rulerColumn the ruler column to be initialized
1997 protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
1998 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
1999 IColorManager manager = textTools.getColorManager();
2001 IPreferenceStore store = getPreferenceStore();
2002 if (store != null) {
2006 if (store.contains(LINE_NUMBER_COLOR)) {
2007 if (store.isDefault(LINE_NUMBER_COLOR))
2008 rgb = PreferenceConverter.getDefaultColor(store, LINE_NUMBER_COLOR);
2010 rgb = PreferenceConverter.getColor(store, LINE_NUMBER_COLOR);
2012 rulerColumn.setForeground(manager.getColor(rgb));
2016 if (!store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
2017 if (store.contains(PREFERENCE_COLOR_BACKGROUND)) {
2018 if (store.isDefault(PREFERENCE_COLOR_BACKGROUND))
2019 rgb = PreferenceConverter.getDefaultColor(store, PREFERENCE_COLOR_BACKGROUND);
2021 rgb = PreferenceConverter.getColor(store, PREFERENCE_COLOR_BACKGROUND);
2024 rulerColumn.setBackground(manager.getColor(rgb));
2029 * Creates a new line number ruler column that is appropriately initialized.
2031 protected IVerticalRulerColumn createLineNumberRulerColumn() {
2032 fLineNumberRulerColumn = new LineNumberRulerColumn();
2033 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
2034 return fLineNumberRulerColumn;
2038 * @see AbstractTextEditor#createVerticalRuler()
2040 protected IVerticalRuler createVerticalRuler() {
2041 CompositeRuler ruler = new CompositeRuler();
2042 ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
2043 if (isLineNumberRulerVisible())
2044 ruler.addDecorator(1, createLineNumberRulerColumn());
2048 private static IRegion getSignedSelection(ITextViewer viewer) {
2050 StyledText text = viewer.getTextWidget();
2051 int caretOffset = text.getCaretOffset();
2052 Point selection = text.getSelection();
2056 if (caretOffset == selection.x) {
2057 offset = selection.y;
2058 length = selection.x - selection.y;
2062 offset = selection.x;
2063 length = selection.y - selection.x;
2066 return new Region(offset, length);
2069 /** Preference key for matching brackets */
2070 protected final static String MATCHING_BRACKETS = PreferenceConstants.EDITOR_MATCHING_BRACKETS;
2071 /** Preference key for matching brackets color */
2072 protected final static String MATCHING_BRACKETS_COLOR = PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
2073 /** Preference key for highlighting current line */
2074 protected final static String CURRENT_LINE = PreferenceConstants.EDITOR_CURRENT_LINE;
2075 /** Preference key for highlight color of current line */
2076 protected final static String CURRENT_LINE_COLOR = PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
2077 /** Preference key for showing print marging ruler */
2078 protected final static String PRINT_MARGIN = PreferenceConstants.EDITOR_PRINT_MARGIN;
2079 /** Preference key for print margin ruler color */
2080 protected final static String PRINT_MARGIN_COLOR = PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
2081 /** Preference key for print margin ruler column */
2082 protected final static String PRINT_MARGIN_COLUMN = PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
2083 /** Preference key for error indication */
2084 protected final static String ERROR_INDICATION = PreferenceConstants.EDITOR_PROBLEM_INDICATION;
2085 /** Preference key for error color */
2086 protected final static String ERROR_INDICATION_COLOR = PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
2087 /** Preference key for warning indication */
2088 protected final static String WARNING_INDICATION = PreferenceConstants.EDITOR_WARNING_INDICATION;
2089 /** Preference key for warning color */
2090 protected final static String WARNING_INDICATION_COLOR = PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
2091 /** Preference key for task indication */
2092 protected final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
2093 /** Preference key for task color */
2094 protected final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
2095 /** Preference key for bookmark indication */
2096 protected final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
2097 /** Preference key for bookmark color */
2098 protected final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
2099 /** Preference key for search result indication */
2100 protected final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
2101 /** Preference key for search result color */
2102 protected final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
2103 /** Preference key for unknown annotation indication */
2104 protected final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
2105 /** Preference key for unknown annotation color */
2106 protected final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
2107 /** Preference key for shwoing the overview ruler */
2108 protected final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
2109 /** Preference key for error indication in overview ruler */
2110 protected final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
2111 /** Preference key for warning indication in overview ruler */
2112 protected final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
2113 /** Preference key for task indication in overview ruler */
2114 protected final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
2115 /** Preference key for bookmark indication in overview ruler */
2116 protected final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
2117 /** Preference key for search result indication in overview ruler */
2118 protected final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
2119 /** Preference key for unknown annotation indication in overview ruler */
2120 protected final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
2121 // /** Preference key for compiler task tags */
2122 // private final static String COMPILER_TASK_TAGS= JavaCore.COMPILER_TASK_TAGS;
2123 /** Preference key for browser like links */
2124 private final static String BROWSER_LIKE_LINKS = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS;
2125 /** Preference key for key modifier of browser like links */
2126 private final static String BROWSER_LIKE_LINKS_KEY_MODIFIER = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS_KEY_MODIFIER;
2128 * Preference key for key modifier mask of browser like links.
2129 * The value is only used if the value of <code>EDITOR_BROWSER_LIKE_LINKS</code>
2130 * cannot be resolved to valid SWT modifier bits.
2134 private final static String BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK;
2136 private final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']' };
2138 private static boolean isBracket(char character) {
2139 for (int i = 0; i != BRACKETS.length; ++i)
2140 if (character == BRACKETS[i])
2145 private static boolean isSurroundedByBrackets(IDocument document, int offset) {
2146 if (offset == 0 || offset == document.getLength())
2150 return isBracket(document.getChar(offset - 1)) && isBracket(document.getChar(offset));
2152 } catch (BadLocationException e) {
2157 protected void configureSourceViewerDecorationSupport() {
2159 fSourceViewerDecorationSupport.setCharacterPairMatcher(fBracketMatcher);
2161 fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2162 AnnotationType.UNKNOWN,
2163 UNKNOWN_INDICATION_COLOR,
2165 UNKNOWN_INDICATION_IN_OVERVIEW_RULER,
2167 fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2168 AnnotationType.BOOKMARK,
2169 BOOKMARK_INDICATION_COLOR,
2170 BOOKMARK_INDICATION,
2171 BOOKMARK_INDICATION_IN_OVERVIEW_RULER,
2173 fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2174 AnnotationType.TASK,
2175 TASK_INDICATION_COLOR,
2177 TASK_INDICATION_IN_OVERVIEW_RULER,
2179 fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2180 AnnotationType.SEARCH,
2181 SEARCH_RESULT_INDICATION_COLOR,
2182 SEARCH_RESULT_INDICATION,
2183 SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER,
2185 fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2186 AnnotationType.WARNING,
2187 WARNING_INDICATION_COLOR,
2189 WARNING_INDICATION_IN_OVERVIEW_RULER,
2191 fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2192 AnnotationType.ERROR,
2193 ERROR_INDICATION_COLOR,
2195 ERROR_INDICATION_IN_OVERVIEW_RULER,
2198 fSourceViewerDecorationSupport.setCursorLinePainterPreferenceKeys(CURRENT_LINE, CURRENT_LINE_COLOR);
2199 fSourceViewerDecorationSupport.setMarginPainterPreferenceKeys(PRINT_MARGIN, PRINT_MARGIN_COLOR, PRINT_MARGIN_COLUMN);
2200 fSourceViewerDecorationSupport.setMatchingCharacterPainterPreferenceKeys(MATCHING_BRACKETS, MATCHING_BRACKETS_COLOR);
2202 fSourceViewerDecorationSupport.setSymbolicFontName(getFontPropertyPreferenceKey());
2206 * Jumps to the matching bracket.
2208 public void gotoMatchingBracket() {
2210 ISourceViewer sourceViewer = getSourceViewer();
2211 IDocument document = sourceViewer.getDocument();
2212 if (document == null)
2215 IRegion selection = getSignedSelection(sourceViewer);
2217 int selectionLength = Math.abs(selection.getLength());
2218 if (selectionLength > 1) {
2219 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$
2220 sourceViewer.getTextWidget().getDisplay().beep();
2225 int sourceCaretOffset = selection.getOffset() + selection.getLength();
2226 if (isSurroundedByBrackets(document, sourceCaretOffset))
2227 sourceCaretOffset -= selection.getLength();
2229 IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
2230 if (region == null) {
2231 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$
2232 sourceViewer.getTextWidget().getDisplay().beep();
2236 int offset = region.getOffset();
2237 int length = region.getLength();
2242 int anchor = fBracketMatcher.getAnchor();
2243 int targetOffset = (PHPPairMatcher.RIGHT == anchor) ? offset : offset + length - 1;
2245 boolean visible = false;
2246 if (sourceViewer instanceof ITextViewerExtension3) {
2247 ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
2248 visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
2250 IRegion visibleRegion = sourceViewer.getVisibleRegion();
2251 visible = (targetOffset >= visibleRegion.getOffset() && targetOffset < visibleRegion.getOffset() + visibleRegion.getLength());
2255 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$
2256 sourceViewer.getTextWidget().getDisplay().beep();
2260 if (selection.getLength() < 0)
2261 targetOffset -= selection.getLength();
2263 sourceViewer.setSelectedRange(targetOffset, selection.getLength());
2264 sourceViewer.revealRange(targetOffset, selection.getLength());
2267 * Ses the given message as error message to this editor's status line.
2268 * @param msg message to be set
2270 protected void setStatusLineErrorMessage(String msg) {
2271 IEditorStatusLine statusLine = (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
2272 if (statusLine != null)
2273 statusLine.setMessage(true, msg, null);
2277 * Returns a segmentation of the line of the given document appropriate for bidi rendering.
2278 * The default implementation returns only the string literals of a php code line as segments.
2280 * @param document the document
2281 * @param lineOffset the offset of the line
2282 * @return the line's bidi segmentation
2283 * @throws BadLocationException in case lineOffset is not valid in document
2285 public static int[] getBidiLineSegments(IDocument document, int lineOffset) throws BadLocationException {
2287 IRegion line = document.getLineInformationOfOffset(lineOffset);
2288 ITypedRegion[] linePartitioning = document.computePartitioning(lineOffset, line.getLength());
2290 List segmentation = new ArrayList();
2291 for (int i = 0; i < linePartitioning.length; i++) {
2292 if (IPHPPartitionScannerConstants.PHP_STRING.equals(linePartitioning[i].getType()))
2293 segmentation.add(linePartitioning[i]);
2296 if (segmentation.size() == 0)
2299 int size = segmentation.size();
2300 int[] segments = new int[size * 2 + 1];
2303 for (int i = 0; i < size; i++) {
2304 ITypedRegion segment = (ITypedRegion) segmentation.get(i);
2309 int offset = segment.getOffset() - lineOffset;
2310 if (offset > segments[j - 1])
2311 segments[j++] = offset;
2313 if (offset + segment.getLength() >= line.getLength())
2316 segments[j++] = offset + segment.getLength();
2319 if (j < segments.length) {
2320 int[] result = new int[j];
2321 System.arraycopy(segments, 0, result, 0, j);
2328 * Returns a segmentation of the given line appropriate for bidi rendering. The default
2329 * implementation returns only the string literals of a php code line as segments.
2331 * @param lineOffset the offset of the line
2332 * @param line the content of the line
2333 * @return the line's bidi segmentation
2335 protected int[] getBidiLineSegments(int lineOffset, String line) {
2336 IDocumentProvider provider = getDocumentProvider();
2337 if (provider != null && line != null && line.length() > 0) {
2338 IDocument document = provider.getDocument(getEditorInput());
2339 if (document != null)
2341 return getBidiLineSegments(document, lineOffset);
2342 } catch (BadLocationException x) {
2350 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
2352 // protected final ISourceViewer createSourceViewer(
2353 // Composite parent,
2354 // IVerticalRuler ruler,
2356 // ISourceViewer viewer = createJavaSourceViewer(parent, ruler, styles);
2357 // StyledText text = viewer.getTextWidget();
2358 // text.addBidiSegmentListener(new BidiSegmentListener() {
2359 // public void lineGetSegments(BidiSegmentEvent event) {
2360 // event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
2363 // // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
2366 protected final ISourceViewer createSourceViewer(Composite parent, IVerticalRuler verticalRuler, int styles) {
2368 ISharedTextColors sharedColors = PHPeclipsePlugin.getDefault().getJavaTextTools().getColorManager();
2370 fOverviewRuler = new OverviewRuler(fAnnotationAccess, VERTICAL_RULER_WIDTH, sharedColors);
2371 fOverviewRuler.addHeaderAnnotationType(AnnotationType.WARNING);
2372 fOverviewRuler.addHeaderAnnotationType(AnnotationType.ERROR);
2374 ISourceViewer viewer = createJavaSourceViewer(parent, verticalRuler, fOverviewRuler, isOverviewRulerVisible(), styles);
2376 StyledText text = viewer.getTextWidget();
2377 text.addBidiSegmentListener(new BidiSegmentListener() {
2378 public void lineGetSegments(BidiSegmentEvent event) {
2379 event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
2383 // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
2385 fSourceViewerDecorationSupport = new SourceViewerDecorationSupport(viewer, fOverviewRuler, fAnnotationAccess, sharedColors);
2386 configureSourceViewerDecorationSupport();
2391 protected void showOverviewRuler() {
2392 if (fOverviewRuler != null) {
2393 if (getSourceViewer() instanceof ISourceViewerExtension) {
2394 ((ISourceViewerExtension) getSourceViewer()).showAnnotationsOverview(true);
2395 fSourceViewerDecorationSupport.updateOverviewDecorations();
2400 protected void hideOverviewRuler() {
2401 if (getSourceViewer() instanceof ISourceViewerExtension) {
2402 fSourceViewerDecorationSupport.hideAnnotationOverview();
2403 ((ISourceViewerExtension) getSourceViewer()).showAnnotationsOverview(false);
2407 protected boolean isOverviewRulerVisible() {
2408 IPreferenceStore store = getPreferenceStore();
2409 return store.getBoolean(OVERVIEW_RULER);
2412 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
2414 protected ISourceViewer createJavaSourceViewer(
2416 IVerticalRuler ruler,
2417 IOverviewRuler overviewRuler,
2418 boolean isOverviewRulerVisible,
2420 return new SourceViewer(parent, ruler, overviewRuler, isOverviewRulerVisible(), styles);
2421 // return super.createSourceViewer(parent, ruler, styles);
2425 * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
2427 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
2428 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
2429 return textTools.affectsBehavior(event);
2433 * Jumps to the error next according to the given direction.
2435 public void gotoError(boolean forward) {
2437 ISelectionProvider provider = getSelectionProvider();
2439 ITextSelection s = (ITextSelection) provider.getSelection();
2440 Position errorPosition = new Position(0, 0);
2441 IJavaAnnotation nextError = getNextError(s.getOffset(), forward, errorPosition);
2443 if (nextError != null) {
2445 IMarker marker = null;
2446 if (nextError instanceof MarkerAnnotation)
2447 marker = ((MarkerAnnotation) nextError).getMarker();
2449 Iterator e = nextError.getOverlaidIterator();
2451 while (e.hasNext()) {
2452 Object o = e.next();
2453 if (o instanceof MarkerAnnotation) {
2454 marker = ((MarkerAnnotation) o).getMarker();
2461 if (marker != null) {
2462 IWorkbenchPage page = getSite().getPage();
2463 IViewPart view = view = page.findView("org.eclipse.ui.views.TaskList"); //$NON-NLS-1$
2464 if (view instanceof TaskList) {
2465 StructuredSelection ss = new StructuredSelection(marker);
2466 ((TaskList) view).setSelection(ss, true);
2470 selectAndReveal(errorPosition.getOffset(), errorPosition.getLength());
2471 setStatusLineErrorMessage(nextError.getMessage());
2475 setStatusLineErrorMessage(null);
2480 private IJavaAnnotation getNextError(int offset, boolean forward, Position errorPosition) {
2482 IJavaAnnotation nextError = null;
2483 Position nextErrorPosition = null;
2485 IDocument document = getDocumentProvider().getDocument(getEditorInput());
2486 int endOfDocument = document.getLength();
2489 IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput());
2490 Iterator e = new JavaAnnotationIterator(model, false);
2491 while (e.hasNext()) {
2493 IJavaAnnotation a = (IJavaAnnotation) e.next();
2494 if (a.hasOverlay() || !a.isProblem())
2497 Position p = model.getPosition((Annotation) a);
2498 if (!p.includes(offset)) {
2500 int currentDistance = 0;
2503 currentDistance = p.getOffset() - offset;
2504 if (currentDistance < 0)
2505 currentDistance = endOfDocument - offset + p.getOffset();
2507 currentDistance = offset - p.getOffset();
2508 if (currentDistance < 0)
2509 currentDistance = offset + endOfDocument - p.getOffset();
2512 if (nextError == null || currentDistance < distance) {
2513 distance = currentDistance;
2515 nextErrorPosition = p;
2520 if (nextErrorPosition != null) {
2521 errorPosition.setOffset(nextErrorPosition.getOffset());
2522 errorPosition.setLength(nextErrorPosition.getLength());