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.List;
17 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
18 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
19 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
20 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
21 import net.sourceforge.phpdt.ui.IContextMenuConstants;
22 import net.sourceforge.phpdt.ui.PreferenceConstants;
23 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
24 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
25 import net.sourceforge.phpdt.ui.text.IColorManager;
26 import net.sourceforge.phpdt.ui.text.JavaTextTools;
27 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
28 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.action.GroupMarker;
35 import org.eclipse.jface.action.MenuManager;
36 import org.eclipse.jface.action.Separator;
37 import org.eclipse.jface.preference.IPreferenceStore;
38 import org.eclipse.jface.preference.PreferenceConverter;
39 import org.eclipse.jface.text.BadLocationException;
40 import org.eclipse.jface.text.DefaultInformationControl;
41 import org.eclipse.jface.text.IDocument;
42 import org.eclipse.jface.text.IInformationControl;
43 import org.eclipse.jface.text.IInformationControlCreator;
44 import org.eclipse.jface.text.IRegion;
45 import org.eclipse.jface.text.ITextHover;
46 import org.eclipse.jface.text.ITextOperationTarget;
47 import org.eclipse.jface.text.ITextViewer;
48 import org.eclipse.jface.text.ITextViewerExtension2;
49 import org.eclipse.jface.text.ITextViewerExtension3;
50 import org.eclipse.jface.text.ITypedRegion;
51 import org.eclipse.jface.text.Region;
52 import org.eclipse.jface.text.information.InformationPresenter;
53 import org.eclipse.jface.text.source.AnnotationRulerColumn;
54 import org.eclipse.jface.text.source.CompositeRuler;
55 import org.eclipse.jface.text.source.ISourceViewer;
56 import org.eclipse.jface.text.source.IVerticalRuler;
57 import org.eclipse.jface.text.source.IVerticalRulerColumn;
58 import org.eclipse.jface.text.source.LineNumberRulerColumn;
59 import org.eclipse.jface.text.source.SourceViewerConfiguration;
60 import org.eclipse.jface.util.PropertyChangeEvent;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.custom.BidiSegmentEvent;
63 import org.eclipse.swt.custom.BidiSegmentListener;
64 import org.eclipse.swt.custom.StyledText;
65 import org.eclipse.swt.graphics.Point;
66 import org.eclipse.swt.graphics.RGB;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Shell;
69 import org.eclipse.ui.IEditorInput;
70 import org.eclipse.ui.actions.ActionContext;
71 import org.eclipse.ui.actions.ActionGroup;
72 import org.eclipse.ui.texteditor.ContentAssistAction;
73 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
74 import org.eclipse.ui.texteditor.IDocumentProvider;
75 import org.eclipse.ui.texteditor.IEditorStatusLine;
76 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
77 import org.eclipse.ui.texteditor.StatusTextEditor;
78 import org.eclipse.ui.texteditor.TextOperationAction;
79 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
81 * PHP specific text editor.
83 public class PHPEditor
84 extends StatusTextEditor
85 implements IViewPartInputProvider { // extends TextEditor {
87 /** Preference key for showing the line number ruler */
88 private final static String LINE_NUMBER_RULER =
89 PreferenceConstants.EDITOR_LINE_NUMBER_RULER;
90 /** Preference key for the foreground color of the line numbers */
91 private final static String LINE_NUMBER_COLOR =
92 PreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR;
93 /** Preference key for the link color */
94 private final static String LINK_COLOR =
95 PreferenceConstants.EDITOR_LINK_COLOR;
97 // protected PHPActionGroup fActionGroups;
98 /** The outline page */
99 private AbstractContentOutlinePage fOutlinePage;
101 // protected PHPSyntaxParserThread fValidationThread = null;
103 // private IPreferenceStore fPHPPrefStore;
105 /** The editor's bracket matcher */
106 private PHPPairMatcher fBracketMatcher;
107 /** The line number ruler column */
108 private LineNumberRulerColumn fLineNumberRulerColumn;
110 protected CompositeActionGroup fActionGroups;
111 /** The standard action groups added to the menu */
112 protected GenerateActionGroup fGenerateActionGroup;
113 protected CompositeActionGroup fContextMenuGroup;
115 /** The information presenter. */
116 private InformationPresenter fInformationPresenter;
119 * Default constructor.
123 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
124 setSourceViewerConfiguration(
125 new PHPSourceViewerConfiguration(textTools, this));
126 setRangeIndicator(new DefaultRangeIndicator());
127 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
129 // if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE))
130 // fUpdater= new OutlinePageSelectionUpdater();
136 // * @see IMember#getCompilationUnit()
138 // public ICompilationUnit getCompilationUnit() {
142 // * @see org.phpeclipse.phpdt.internal.compiler.env.ICompilationUnit#getContents()
144 // public char[] getContents() {
145 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
147 // return doc.get().toCharArray();
151 * Update the hovering behavior depending on the preferences.
153 private void updateHoverBehavior() {
154 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
155 String[] types = configuration.getConfiguredContentTypes(getSourceViewer());
157 for (int i = 0; i < types.length; i++) {
162 configuration.getConfiguredTextHoverStateMasks(getSourceViewer(), t);
164 ISourceViewer sourceViewer = getSourceViewer();
165 if (sourceViewer instanceof ITextViewerExtension2) {
166 if (stateMasks != null) {
167 for (int j = 0; j < stateMasks.length; j++) {
168 int stateMask = stateMasks[j];
169 ITextHover textHover =
170 configuration.getTextHover(sourceViewer, t, stateMask);
171 ((ITextViewerExtension2) sourceViewer).setTextHover(
177 ITextHover textHover = configuration.getTextHover(sourceViewer, t);
178 ((ITextViewerExtension2) sourceViewer).setTextHover(
181 ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
184 sourceViewer.setTextHover(
185 configuration.getTextHover(sourceViewer, t),
191 * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
193 public Object getViewPartInput() {
194 return getEditorInput().getAdapter(IResource.class);
198 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.
201 public void createPartControl(Composite parent) {
202 super.createPartControl(parent);
204 IInformationControlCreator informationControlCreator =
205 new IInformationControlCreator() {
206 public IInformationControl createInformationControl(Shell parent) {
207 boolean cutDown = false;
208 int style = cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL);
209 return new DefaultInformationControl(
213 new HTMLTextPresenter(cutDown));
217 fInformationPresenter = new InformationPresenter(informationControlCreator);
218 fInformationPresenter.setSizeConstraints(60, 10, true, true);
219 fInformationPresenter.install(getSourceViewer());
223 * Returns this document's complete text.
225 * @return the document's complete text
227 public String get() {
229 this.getDocumentProvider().getDocument(this.getEditorInput());
234 * Returns the standard action group of this editor.
236 protected ActionGroup getActionGroup() {
237 return fActionGroups;
240 public AbstractContentOutlinePage getfOutlinePage() {
244 /** The <code>PHPEditor</code> implementation of this
245 * <code>AbstractTextEditor</code> method extend the
246 * actions to add those specific to the receiver
248 protected void createActions() {
249 super.createActions();
255 new TextOperationAction(
256 PHPEditorMessages.getResourceBundle(),
259 ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
261 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
262 action.setActionDefinitionId(
263 PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
264 setAction("ContentAssistProposal", action); //$NON-NLS-1$
265 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
266 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
268 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
269 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
270 setAction("Comment", action); //$NON-NLS-1$
271 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
272 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
274 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
275 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
276 setAction("Uncomment", action); //$NON-NLS-1$
277 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
278 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
280 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
281 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
282 setAction("Format", action); //$NON-NLS-1$
283 markAsStateDependentAction("Format", true); //$NON-NLS-1$
284 markAsSelectionDependentAction("Format", true); //$NON-NLS-1$
285 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
287 action = new GotoMatchingBracketAction(this);
288 action.setActionDefinitionId(
289 PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
290 setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action);
292 fGenerateActionGroup =
293 new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
296 new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
298 // We have to keep the context menu group separate to have better control over positioning
300 new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
302 // new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)});
304 // if (fValidationThread == null) {
305 // fValidationThread =
306 // new PHPSyntaxParserThread(this, getSourceViewer());
309 // fValidationThread.start();
312 // fValidationThread.setText(getSourceViewer().getTextWidget().getText());
315 /** The <code>PHPEditor</code> implementation of this
316 * <code>AbstractTextEditor</code> method performs any extra
317 * disposal actions required by the php editor.
319 public void dispose() {
320 // PHPEditorEnvironment.disconnect(this);
321 if (fOutlinePage != null)
322 fOutlinePage.setInput(null);
324 if (fActionGroups != null)
325 fActionGroups.dispose();
330 /** The <code>PHPEditor</code> implementation of this
331 * <code>AbstractTextEditor</code> method performs any extra
332 * revert behavior required by the php editor.
334 public void doRevertToSaved() {
335 super.doRevertToSaved();
336 if (fOutlinePage != null)
337 fOutlinePage.update();
340 /** The <code>PHPEditor</code> implementation of this
341 * <code>AbstractTextEditor</code> method performs any extra
342 * save behavior required by the php editor.
344 public void doSave(IProgressMonitor monitor) {
345 super.doSave(monitor);
346 // compile or not, according to the user preferences
347 IPreferenceStore store = getPreferenceStore(); // fPHPPrefStore;
349 // the parse on save was changed to the eclipse "builders" concept
350 // if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
351 // IAction a = PHPParserAction.getInstance();
356 // if (SWT.getPlatform().equals("win32")) {
357 // IAction a = ShowExternalPreviewAction.getInstance();
361 if (fOutlinePage != null)
362 fOutlinePage.update();
365 /** The <code>PHPEditor</code> implementation of this
366 * <code>AbstractTextEditor</code> method performs any extra
367 * save as behavior required by the php editor.
369 public void doSaveAs() {
371 if (fOutlinePage != null)
372 fOutlinePage.update();
375 /** The <code>PHPEditor</code> implementation of this
376 * <code>AbstractTextEditor</code> method performs sets the
377 * input of the outline page after AbstractTextEditor has set input.
379 protected void doSetInput(IEditorInput input) throws CoreException {
380 super.doSetInput(input);
381 if (fOutlinePage != null)
382 fOutlinePage.setInput(input);
386 * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
388 // public Object getViewPartInput() {
389 // return getEditorInput().getAdapter(IFile.class);
392 /** The <code>PHPEditor</code> implementation of this
393 * <code>AbstractTextEditor</code> method adds any
394 * PHPEditor specific entries.
396 public void editorContextMenuAboutToShow(MenuManager menu) {
397 super.editorContextMenuAboutToShow(menu);
399 ITextEditorActionConstants.GROUP_UNDO,
400 new Separator(IContextMenuConstants.GROUP_OPEN));
402 IContextMenuConstants.GROUP_OPEN,
403 new GroupMarker(IContextMenuConstants.GROUP_SHOW));
405 ActionContext context =
406 new ActionContext(getSelectionProvider().getSelection());
407 fContextMenuGroup.setContext(context);
408 fContextMenuGroup.fillContextMenu(menu);
409 fContextMenuGroup.setContext(null);
410 // addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format"); //$NON-NLS-1$
412 // ActionContext context =
413 // new ActionContext(getSelectionProvider().getSelection());
414 // fContextMenuGroup.setContext(context);
415 // fContextMenuGroup.fillContextMenu(menu);
416 // fContextMenuGroup.setContext(null);
419 protected void updateStateDependentActions() {
420 super.updateStateDependentActions();
421 fGenerateActionGroup.editorStateChanged();
424 /** The <code>PHPEditor</code> implementation of this
425 * <code>AbstractTextEditor</code> method performs gets
426 * the java content outline page if request is for a an
429 public Object getAdapter(Class required) {
430 if (IContentOutlinePage.class.equals(required)) {
431 if (fOutlinePage == null) {
432 fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
433 if (getEditorInput() != null)
434 fOutlinePage.setInput(getEditorInput());
438 return super.getAdapter(required);
441 // public void openContextHelp() {
442 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
443 // ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
444 // int pos = selection.getOffset();
445 // String word = getFunctionName(doc, pos);
446 // openContextHelp(word);
449 // private void openContextHelp(String word) {
453 // public static void open(String word) {
454 // IHelp help = WorkbenchHelp.getHelpSupport();
455 // if (help != null) {
456 // IHelpResource helpResource = new PHPFunctionHelpResource(word);
457 // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
459 // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
463 // private String getFunctionName(IDocument doc, int pos) {
464 // Point word = PHPWordExtractor.findWord(doc, pos);
465 // if (word != null) {
467 // return doc.get(word.x, word.y).replace('_', '-');
468 // } catch (BadLocationException e) {
475 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
477 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
481 ISourceViewer sourceViewer = getSourceViewer();
482 if (sourceViewer == null)
485 String property = event.getProperty();
487 if (PreferenceConstants.EDITOR_TAB_WIDTH.equals(property)) {
488 Object value = event.getNewValue();
489 if (value instanceof Integer) {
490 sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
491 } else if (value instanceof String) {
492 sourceViewer.getTextWidget().setTabs(
493 Integer.parseInt((String) value));
498 if (LINE_NUMBER_RULER.equals(property)) {
499 if (isLineNumberRulerVisible())
500 showLineNumberRuler();
502 hideLineNumberRuler();
506 if (fLineNumberRulerColumn != null
507 && (LINE_NUMBER_COLOR.equals(property)
508 || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
509 || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
511 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
514 if (isJavaEditorHoverProperty(property)) {
515 updateHoverBehavior();
519 super.handlePreferenceStoreChanged(event);
524 // * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
526 // protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
530 // ISourceViewer sourceViewer = getSourceViewer();
531 // if (sourceViewer == null)
534 // String property = event.getProperty();
536 // // if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
537 // // Object value= event.getNewValue();
538 // // if (value instanceof Integer) {
539 // // sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
540 // // } else if (value instanceof String) {
541 // // sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
546 // if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
547 // if (isLineNumberRulerVisible())
548 // showLineNumberRuler();
550 // hideLineNumberRuler();
554 // if (fLineNumberRulerColumn != null
555 // && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
556 // || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
557 // || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
559 // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
563 // super.handlePreferenceStoreChanged(event);
567 private boolean isJavaEditorHoverProperty(String property) {
568 return PreferenceConstants.EDITOR_DEFAULT_HOVER.equals(property)
569 || PreferenceConstants.EDITOR_NONE_HOVER.equals(property)
570 || PreferenceConstants.EDITOR_CTRL_HOVER.equals(property)
571 || PreferenceConstants.EDITOR_SHIFT_HOVER.equals(property)
572 || PreferenceConstants.EDITOR_CTRL_ALT_HOVER.equals(property)
573 || PreferenceConstants.EDITOR_CTRL_SHIFT_HOVER.equals(property)
574 || PreferenceConstants.EDITOR_CTRL_ALT_SHIFT_HOVER.equals(property)
575 || PreferenceConstants.EDITOR_ALT_SHIFT_HOVER.equals(property);
579 * Shows the line number ruler column.
581 private void showLineNumberRuler() {
582 IVerticalRuler v = getVerticalRuler();
583 if (v instanceof CompositeRuler) {
584 CompositeRuler c = (CompositeRuler) v;
585 c.addDecorator(1, createLineNumberRulerColumn());
590 * Return whether the line number ruler column should be
591 * visible according to the preference store settings.
592 * @return <code>true</code> if the line numbers should be visible
594 private boolean isLineNumberRulerVisible() {
595 IPreferenceStore store = getPreferenceStore();
596 return store.getBoolean(LINE_NUMBER_RULER);
599 * Hides the line number ruler column.
601 private void hideLineNumberRuler() {
602 IVerticalRuler v = getVerticalRuler();
603 if (v instanceof CompositeRuler) {
604 CompositeRuler c = (CompositeRuler) v;
606 c.removeDecorator(1);
607 } catch (Throwable e) {
613 * Initializes the given line number ruler column from the preference store.
614 * @param rulerColumn the ruler column to be initialized
616 protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
617 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
618 IColorManager manager = textTools.getColorManager();
620 IPreferenceStore store = getPreferenceStore();
625 if (store.contains(LINE_NUMBER_COLOR)) {
626 if (store.isDefault(LINE_NUMBER_COLOR))
627 rgb = PreferenceConverter.getDefaultColor(store, LINE_NUMBER_COLOR);
629 rgb = PreferenceConverter.getColor(store, LINE_NUMBER_COLOR);
631 rulerColumn.setForeground(manager.getColor(rgb));
635 if (!store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
636 if (store.contains(PREFERENCE_COLOR_BACKGROUND)) {
637 if (store.isDefault(PREFERENCE_COLOR_BACKGROUND))
639 PreferenceConverter.getDefaultColor(
641 PREFERENCE_COLOR_BACKGROUND);
644 PreferenceConverter.getColor(store, PREFERENCE_COLOR_BACKGROUND);
647 rulerColumn.setBackground(manager.getColor(rgb));
652 * Creates a new line number ruler column that is appropriately initialized.
654 protected IVerticalRulerColumn createLineNumberRulerColumn() {
655 fLineNumberRulerColumn = new LineNumberRulerColumn();
656 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
657 return fLineNumberRulerColumn;
661 * @see AbstractTextEditor#createVerticalRuler()
663 protected IVerticalRuler createVerticalRuler() {
664 CompositeRuler ruler = new CompositeRuler();
665 ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
666 if (isLineNumberRulerVisible())
667 ruler.addDecorator(1, createLineNumberRulerColumn());
672 * Method declared on TextEditor
674 protected void initializeEditor() {
675 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
676 // PHPEditorEnvironment.connect(this);
678 // store.addPropertyChangeListener(new IPropertyChangeListener() {
679 // public void propertyChange(PropertyChangeEvent event) {
680 // PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
681 // if (scanner != null) {
682 // scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
684 // if (getSourceViewer() != null) {
685 // getSourceViewer().invalidateTextPresentation();
688 // String property = event.getProperty();
689 // if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
690 // if (isLineNumberRulerVisible())
691 // showLineNumberRuler();
693 // hideLineNumberRuler();
700 private static IRegion getSignedSelection(ITextViewer viewer) {
702 StyledText text = viewer.getTextWidget();
703 int caretOffset = text.getCaretOffset();
704 Point selection = text.getSelection();
708 if (caretOffset == selection.x) {
709 offset = selection.y;
710 length = selection.x - selection.y;
714 offset = selection.x;
715 length = selection.y - selection.x;
718 return new Region(offset, length);
721 private final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']' };
723 private static boolean isBracket(char character) {
724 for (int i = 0; i != BRACKETS.length; ++i)
725 if (character == BRACKETS[i])
730 private static boolean isSurroundedByBrackets(
733 if (offset == 0 || offset == document.getLength())
737 return isBracket(document.getChar(offset - 1))
738 && isBracket(document.getChar(offset));
740 } catch (BadLocationException e) {
745 * Jumps to the matching bracket.
747 public void gotoMatchingBracket() {
749 if (fBracketMatcher == null)
750 fBracketMatcher = new PHPPairMatcher(BRACKETS);
752 ISourceViewer sourceViewer = getSourceViewer();
753 IDocument document = sourceViewer.getDocument();
754 if (document == null)
757 IRegion selection = getSignedSelection(sourceViewer);
759 int selectionLength = Math.abs(selection.getLength());
760 if (selectionLength > 1) {
761 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$
762 sourceViewer.getTextWidget().getDisplay().beep();
767 int sourceCaretOffset = selection.getOffset() + selection.getLength();
768 if (isSurroundedByBrackets(document, sourceCaretOffset))
769 sourceCaretOffset -= selection.getLength();
771 IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
772 if (region == null) {
773 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$
774 sourceViewer.getTextWidget().getDisplay().beep();
778 int offset = region.getOffset();
779 int length = region.getLength();
784 int anchor = fBracketMatcher.getAnchor();
786 (PHPPairMatcher.RIGHT == anchor) ? offset : offset + length - 1;
788 boolean visible = false;
789 if (sourceViewer instanceof ITextViewerExtension3) {
790 ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
791 visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
793 IRegion visibleRegion = sourceViewer.getVisibleRegion();
795 (targetOffset >= visibleRegion.getOffset()
796 && targetOffset < visibleRegion.getOffset() + visibleRegion.getLength());
800 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$
801 sourceViewer.getTextWidget().getDisplay().beep();
805 if (selection.getLength() < 0)
806 targetOffset -= selection.getLength();
808 sourceViewer.setSelectedRange(targetOffset, selection.getLength());
809 sourceViewer.revealRange(targetOffset, selection.getLength());
812 * Ses the given message as error message to this editor's status line.
813 * @param msg message to be set
815 protected void setStatusLineErrorMessage(String msg) {
816 IEditorStatusLine statusLine =
817 (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
818 if (statusLine != null)
819 statusLine.setMessage(true, msg, null);
823 * Returns a segmentation of the line of the given document appropriate for bidi rendering.
824 * The default implementation returns only the string literals of a php code line as segments.
826 * @param document the document
827 * @param lineOffset the offset of the line
828 * @return the line's bidi segmentation
829 * @throws BadLocationException in case lineOffset is not valid in document
831 public static int[] getBidiLineSegments(IDocument document, int lineOffset)
832 throws BadLocationException {
834 IRegion line = document.getLineInformationOfOffset(lineOffset);
835 ITypedRegion[] linePartitioning =
836 document.computePartitioning(lineOffset, line.getLength());
838 List segmentation = new ArrayList();
839 for (int i = 0; i < linePartitioning.length; i++) {
840 if (IPHPPartitionScannerConstants
842 .equals(linePartitioning[i].getType()))
843 segmentation.add(linePartitioning[i]);
846 if (segmentation.size() == 0)
849 int size = segmentation.size();
850 int[] segments = new int[size * 2 + 1];
853 for (int i = 0; i < size; i++) {
854 ITypedRegion segment = (ITypedRegion) segmentation.get(i);
859 int offset = segment.getOffset() - lineOffset;
860 if (offset > segments[j - 1])
861 segments[j++] = offset;
863 if (offset + segment.getLength() >= line.getLength())
866 segments[j++] = offset + segment.getLength();
869 if (j < segments.length) {
870 int[] result = new int[j];
871 System.arraycopy(segments, 0, result, 0, j);
878 * Returns a segmentation of the given line appropriate for bidi rendering. The default
879 * implementation returns only the string literals of a php code line as segments.
881 * @param lineOffset the offset of the line
882 * @param line the content of the line
883 * @return the line's bidi segmentation
885 protected int[] getBidiLineSegments(int lineOffset, String line) {
886 IDocumentProvider provider = getDocumentProvider();
887 if (provider != null && line != null && line.length() > 0) {
888 IDocument document = provider.getDocument(getEditorInput());
889 if (document != null)
891 return getBidiLineSegments(document, lineOffset);
892 } catch (BadLocationException x) {
900 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
902 protected final ISourceViewer createSourceViewer(
904 IVerticalRuler ruler,
906 ISourceViewer viewer = createJavaSourceViewer(parent, ruler, styles);
907 StyledText text = viewer.getTextWidget();
908 text.addBidiSegmentListener(new BidiSegmentListener() {
909 public void lineGetSegments(BidiSegmentEvent event) {
910 event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
913 // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
918 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
920 protected ISourceViewer createJavaSourceViewer(
922 IVerticalRuler ruler,
924 return super.createSourceViewer(parent, ruler, styles);
928 * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
930 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
931 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
932 return textTools.affectsBehavior(event);