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.PreferenceConstants;
22 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
23 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
24 import net.sourceforge.phpdt.ui.text.IColorManager;
25 import net.sourceforge.phpdt.ui.text.JavaTextTools;
26 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.jface.action.Action;
33 import org.eclipse.jface.action.IAction;
34 import org.eclipse.jface.action.MenuManager;
35 import org.eclipse.jface.preference.IPreferenceStore;
36 import org.eclipse.jface.preference.PreferenceConverter;
37 import org.eclipse.jface.text.BadLocationException;
38 import org.eclipse.jface.text.DefaultInformationControl;
39 import org.eclipse.jface.text.IDocument;
40 import org.eclipse.jface.text.IInformationControl;
41 import org.eclipse.jface.text.IInformationControlCreator;
42 import org.eclipse.jface.text.IRegion;
43 import org.eclipse.jface.text.ITextHover;
44 import org.eclipse.jface.text.ITextOperationTarget;
45 import org.eclipse.jface.text.ITextViewer;
46 import org.eclipse.jface.text.ITextViewerExtension2;
47 import org.eclipse.jface.text.ITextViewerExtension3;
48 import org.eclipse.jface.text.ITypedRegion;
49 import org.eclipse.jface.text.Region;
50 import org.eclipse.jface.text.information.InformationPresenter;
51 import org.eclipse.jface.text.source.AnnotationRulerColumn;
52 import org.eclipse.jface.text.source.CompositeRuler;
53 import org.eclipse.jface.text.source.ISourceViewer;
54 import org.eclipse.jface.text.source.IVerticalRuler;
55 import org.eclipse.jface.text.source.IVerticalRulerColumn;
56 import org.eclipse.jface.text.source.LineNumberRulerColumn;
57 import org.eclipse.jface.text.source.SourceViewerConfiguration;
58 import org.eclipse.jface.util.PropertyChangeEvent;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.custom.BidiSegmentEvent;
61 import org.eclipse.swt.custom.BidiSegmentListener;
62 import org.eclipse.swt.custom.StyledText;
63 import org.eclipse.swt.graphics.Point;
64 import org.eclipse.swt.graphics.RGB;
65 import org.eclipse.swt.widgets.Composite;
66 import org.eclipse.swt.widgets.Shell;
67 import org.eclipse.ui.IEditorInput;
68 import org.eclipse.ui.actions.ActionContext;
69 import org.eclipse.ui.actions.ActionGroup;
70 import org.eclipse.ui.texteditor.ContentAssistAction;
71 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
72 import org.eclipse.ui.texteditor.IDocumentProvider;
73 import org.eclipse.ui.texteditor.IEditorStatusLine;
74 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
75 import org.eclipse.ui.texteditor.StatusTextEditor;
76 import org.eclipse.ui.texteditor.TextOperationAction;
77 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
79 * PHP specific text editor.
81 public class PHPEditor
82 extends StatusTextEditor
83 implements IViewPartInputProvider { // extends TextEditor {
85 /** Preference key for showing the line number ruler */
86 private final static String LINE_NUMBER_RULER =
87 PreferenceConstants.EDITOR_LINE_NUMBER_RULER;
88 /** Preference key for the foreground color of the line numbers */
89 private final static String LINE_NUMBER_COLOR =
90 PreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR;
91 /** Preference key for the link color */
92 private final static String LINK_COLOR =
93 PreferenceConstants.EDITOR_LINK_COLOR;
95 // protected PHPActionGroup fActionGroups;
96 /** The outline page */
97 private PHPContentOutlinePage fOutlinePage;
99 // protected PHPSyntaxParserThread fValidationThread = null;
101 // private IPreferenceStore fPHPPrefStore;
103 /** The editor's bracket matcher */
104 private PHPPairMatcher fBracketMatcher;
105 /** The line number ruler column */
106 private LineNumberRulerColumn fLineNumberRulerColumn;
108 protected CompositeActionGroup fActionGroups;
109 /** The standard action groups added to the menu */
110 private GenerateActionGroup fGenerateActionGroup;
111 private CompositeActionGroup fContextMenuGroup;
113 /** The information presenter. */
114 private InformationPresenter fInformationPresenter;
117 * Default constructor.
121 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
122 setSourceViewerConfiguration(
123 new PHPSourceViewerConfiguration(textTools, this));
124 setRangeIndicator(new DefaultRangeIndicator());
125 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
127 // if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE))
128 // fUpdater= new OutlinePageSelectionUpdater();
134 // * @see IMember#getCompilationUnit()
136 // public ICompilationUnit getCompilationUnit() {
140 // * @see org.phpeclipse.phpdt.internal.compiler.env.ICompilationUnit#getContents()
142 // public char[] getContents() {
143 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
145 // return doc.get().toCharArray();
149 * Update the hovering behavior depending on the preferences.
151 private void updateHoverBehavior() {
152 SourceViewerConfiguration configuration= getSourceViewerConfiguration();
153 String[] types= configuration.getConfiguredContentTypes(getSourceViewer());
155 for (int i= 0; i < types.length; i++) {
159 int[] stateMasks= configuration.getConfiguredTextHoverStateMasks(getSourceViewer(), t);
161 ISourceViewer sourceViewer= getSourceViewer();
162 if (sourceViewer instanceof ITextViewerExtension2) {
163 if (stateMasks != null) {
164 for (int j= 0; j < stateMasks.length; j++) {
165 int stateMask= stateMasks[j];
166 ITextHover textHover= configuration.getTextHover(sourceViewer, t, stateMask);
167 ((ITextViewerExtension2)sourceViewer).setTextHover(textHover, t, stateMask);
170 ITextHover textHover= configuration.getTextHover(sourceViewer, t);
171 ((ITextViewerExtension2)sourceViewer).setTextHover(textHover, t, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
174 sourceViewer.setTextHover(configuration.getTextHover(sourceViewer, t), t);
179 * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
181 public Object getViewPartInput() {
182 return getEditorInput().getAdapter(IResource.class);
186 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.
189 public void createPartControl(Composite parent) {
190 super.createPartControl(parent);
192 IInformationControlCreator informationControlCreator =
193 new IInformationControlCreator() {
194 public IInformationControl createInformationControl(Shell parent) {
195 boolean cutDown = false;
196 int style = cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL);
197 return new DefaultInformationControl(
201 new HTMLTextPresenter(cutDown));
205 fInformationPresenter = new InformationPresenter(informationControlCreator);
206 fInformationPresenter.setSizeConstraints(60, 10, true, true);
207 fInformationPresenter.install(getSourceViewer());
211 * Returns this document's complete text.
213 * @return the document's complete text
215 public String get() {
217 this.getDocumentProvider().getDocument(this.getEditorInput());
222 * Returns the standard action group of this editor.
224 protected ActionGroup getActionGroup() {
225 return fActionGroups;
228 public PHPContentOutlinePage getfOutlinePage() {
232 /** The <code>PHPEditor</code> implementation of this
233 * <code>AbstractTextEditor</code> method extend the
234 * actions to add those specific to the receiver
236 protected void createActions() {
237 super.createActions();
241 // "ContentAssistProposal",
242 // new TextOperationAction(
243 // PHPEditorMessages.getResourceBundle(),
244 // "ContentAssistProposal.",
246 // ISourceViewer.CONTENTASSIST_PROPOSALS));
247 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
248 action.setActionDefinitionId(
249 PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
250 setAction("ContentAssistProposal", action); //$NON-NLS-1$
254 new TextOperationAction(
255 PHPEditorMessages.getResourceBundle(),
258 ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
261 new TextOperationAction(
262 PHPEditorMessages.getResourceBundle(),
265 ITextOperationTarget.PREFIX);
266 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
267 setAction("Comment", action);
270 new TextOperationAction(
271 PHPEditorMessages.getResourceBundle(),
274 ITextOperationTarget.STRIP_PREFIX);
275 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
276 setAction("Uncomment", action);
278 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
279 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
280 setAction("Format", action); //$NON-NLS-1$
282 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
283 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
284 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
285 markAsStateDependentAction("Format", true); //$NON-NLS-1$
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;
348 if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
349 IAction a = PHPParserAction.getInstance();
353 if (fOutlinePage != null)
354 fOutlinePage.update();
357 /** The <code>PHPEditor</code> implementation of this
358 * <code>AbstractTextEditor</code> method performs any extra
359 * save as behavior required by the php editor.
361 public void doSaveAs() {
363 if (fOutlinePage != null)
364 fOutlinePage.update();
367 /** The <code>PHPEditor</code> implementation of this
368 * <code>AbstractTextEditor</code> method performs sets the
369 * input of the outline page after AbstractTextEditor has set input.
371 protected void doSetInput(IEditorInput input) throws CoreException {
372 super.doSetInput(input);
373 if (fOutlinePage != null)
374 fOutlinePage.setInput(input);
378 * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
380 // public Object getViewPartInput() {
381 // return getEditorInput().getAdapter(IFile.class);
384 /** The <code>PHPEditor</code> implementation of this
385 * <code>AbstractTextEditor</code> method adds any
386 * PHPEditor specific entries.
388 public void editorContextMenuAboutToShow(MenuManager menu) {
389 super.editorContextMenuAboutToShow(menu);
391 addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format"); //$NON-NLS-1$
393 ActionContext context =
394 new ActionContext(getSelectionProvider().getSelection());
395 fContextMenuGroup.setContext(context);
396 fContextMenuGroup.fillContextMenu(menu);
397 fContextMenuGroup.setContext(null);
400 protected void updateStateDependentActions() {
401 super.updateStateDependentActions();
402 fGenerateActionGroup.editorStateChanged();
405 /** The <code>PHPEditor</code> implementation of this
406 * <code>AbstractTextEditor</code> method performs gets
407 * the java content outline page if request is for a an
410 public Object getAdapter(Class required) {
411 if (IContentOutlinePage.class.equals(required)) {
412 if (fOutlinePage == null) {
413 fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
414 if (getEditorInput() != null)
415 fOutlinePage.setInput(getEditorInput());
419 return super.getAdapter(required);
422 // public void openContextHelp() {
423 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
424 // ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
425 // int pos = selection.getOffset();
426 // String word = getFunctionName(doc, pos);
427 // openContextHelp(word);
430 // private void openContextHelp(String word) {
434 // public static void open(String word) {
435 // IHelp help = WorkbenchHelp.getHelpSupport();
436 // if (help != null) {
437 // IHelpResource helpResource = new PHPFunctionHelpResource(word);
438 // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
440 // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
444 // private String getFunctionName(IDocument doc, int pos) {
445 // Point word = PHPWordExtractor.findWord(doc, pos);
446 // if (word != null) {
448 // return doc.get(word.x, word.y).replace('_', '-');
449 // } catch (BadLocationException e) {
456 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
458 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
462 ISourceViewer sourceViewer = getSourceViewer();
463 if (sourceViewer == null)
466 String property = event.getProperty();
468 if (PreferenceConstants.EDITOR_TAB_WIDTH.equals(property)) {
469 Object value = event.getNewValue();
470 if (value instanceof Integer) {
471 sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
472 } else if (value instanceof String) {
473 sourceViewer.getTextWidget().setTabs(
474 Integer.parseInt((String) value));
479 if (LINE_NUMBER_RULER.equals(property)) {
480 if (isLineNumberRulerVisible())
481 showLineNumberRuler();
483 hideLineNumberRuler();
487 if (fLineNumberRulerColumn != null
488 && (LINE_NUMBER_COLOR.equals(property)
489 || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
490 || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
492 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
495 if (isJavaEditorHoverProperty(property)) {
496 updateHoverBehavior();
500 super.handlePreferenceStoreChanged(event);
505 // * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
507 // protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
511 // ISourceViewer sourceViewer = getSourceViewer();
512 // if (sourceViewer == null)
515 // String property = event.getProperty();
517 // // if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
518 // // Object value= event.getNewValue();
519 // // if (value instanceof Integer) {
520 // // sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
521 // // } else if (value instanceof String) {
522 // // sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
527 // if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
528 // if (isLineNumberRulerVisible())
529 // showLineNumberRuler();
531 // hideLineNumberRuler();
535 // if (fLineNumberRulerColumn != null
536 // && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
537 // || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
538 // || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
540 // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
544 // super.handlePreferenceStoreChanged(event);
548 private boolean isJavaEditorHoverProperty(String property) {
549 return PreferenceConstants.EDITOR_DEFAULT_HOVER.equals(property)
550 || PreferenceConstants.EDITOR_NONE_HOVER.equals(property)
551 || PreferenceConstants.EDITOR_CTRL_HOVER.equals(property)
552 || PreferenceConstants.EDITOR_SHIFT_HOVER.equals(property)
553 || PreferenceConstants.EDITOR_CTRL_ALT_HOVER.equals(property)
554 || PreferenceConstants.EDITOR_CTRL_SHIFT_HOVER.equals(property)
555 || PreferenceConstants.EDITOR_CTRL_ALT_SHIFT_HOVER.equals(property)
556 || PreferenceConstants.EDITOR_ALT_SHIFT_HOVER.equals(property);
560 * Shows the line number ruler column.
562 private void showLineNumberRuler() {
563 IVerticalRuler v = getVerticalRuler();
564 if (v instanceof CompositeRuler) {
565 CompositeRuler c = (CompositeRuler) v;
566 c.addDecorator(1, createLineNumberRulerColumn());
571 * Return whether the line number ruler column should be
572 * visible according to the preference store settings.
573 * @return <code>true</code> if the line numbers should be visible
575 private boolean isLineNumberRulerVisible() {
576 IPreferenceStore store = getPreferenceStore();
577 return store.getBoolean(LINE_NUMBER_RULER);
580 * Hides the line number ruler column.
582 private void hideLineNumberRuler() {
583 IVerticalRuler v = getVerticalRuler();
584 if (v instanceof CompositeRuler) {
585 CompositeRuler c = (CompositeRuler) v;
587 c.removeDecorator(1);
588 } catch (Throwable e) {
594 * Initializes the given line number ruler column from the preference store.
595 * @param rulerColumn the ruler column to be initialized
597 protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
598 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
599 IColorManager manager = textTools.getColorManager();
601 IPreferenceStore store = getPreferenceStore();
606 if (store.contains(LINE_NUMBER_COLOR)) {
607 if (store.isDefault(LINE_NUMBER_COLOR))
608 rgb = PreferenceConverter.getDefaultColor(store, LINE_NUMBER_COLOR);
610 rgb = PreferenceConverter.getColor(store, LINE_NUMBER_COLOR);
612 rulerColumn.setForeground(manager.getColor(rgb));
616 if (!store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
617 if (store.contains(PREFERENCE_COLOR_BACKGROUND)) {
618 if (store.isDefault(PREFERENCE_COLOR_BACKGROUND))
620 PreferenceConverter.getDefaultColor(
622 PREFERENCE_COLOR_BACKGROUND);
625 PreferenceConverter.getColor(store, PREFERENCE_COLOR_BACKGROUND);
628 rulerColumn.setBackground(manager.getColor(rgb));
633 * Creates a new line number ruler column that is appropriately initialized.
635 protected IVerticalRulerColumn createLineNumberRulerColumn() {
636 fLineNumberRulerColumn = new LineNumberRulerColumn();
637 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
638 return fLineNumberRulerColumn;
642 * @see AbstractTextEditor#createVerticalRuler()
644 protected IVerticalRuler createVerticalRuler() {
645 CompositeRuler ruler = new CompositeRuler();
646 ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
647 if (isLineNumberRulerVisible())
648 ruler.addDecorator(1, createLineNumberRulerColumn());
653 * Method declared on TextEditor
655 protected void initializeEditor() {
656 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
657 // PHPEditorEnvironment.connect(this);
659 // store.addPropertyChangeListener(new IPropertyChangeListener() {
660 // public void propertyChange(PropertyChangeEvent event) {
661 // PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
662 // if (scanner != null) {
663 // scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
665 // if (getSourceViewer() != null) {
666 // getSourceViewer().invalidateTextPresentation();
669 // String property = event.getProperty();
670 // if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
671 // if (isLineNumberRulerVisible())
672 // showLineNumberRuler();
674 // hideLineNumberRuler();
681 private static IRegion getSignedSelection(ITextViewer viewer) {
683 StyledText text = viewer.getTextWidget();
684 int caretOffset = text.getCaretOffset();
685 Point selection = text.getSelection();
689 if (caretOffset == selection.x) {
690 offset = selection.y;
691 length = selection.x - selection.y;
695 offset = selection.x;
696 length = selection.y - selection.x;
699 return new Region(offset, length);
702 private final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']' };
704 private static boolean isBracket(char character) {
705 for (int i = 0; i != BRACKETS.length; ++i)
706 if (character == BRACKETS[i])
711 private static boolean isSurroundedByBrackets(
714 if (offset == 0 || offset == document.getLength())
718 return isBracket(document.getChar(offset - 1))
719 && isBracket(document.getChar(offset));
721 } catch (BadLocationException e) {
726 * Jumps to the matching bracket.
728 public void gotoMatchingBracket() {
730 if (fBracketMatcher == null)
731 fBracketMatcher = new PHPPairMatcher(BRACKETS);
733 ISourceViewer sourceViewer = getSourceViewer();
734 IDocument document = sourceViewer.getDocument();
735 if (document == null)
738 IRegion selection = getSignedSelection(sourceViewer);
740 int selectionLength = Math.abs(selection.getLength());
741 if (selectionLength > 1) {
742 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$
743 sourceViewer.getTextWidget().getDisplay().beep();
748 int sourceCaretOffset = selection.getOffset() + selection.getLength();
749 if (isSurroundedByBrackets(document, sourceCaretOffset))
750 sourceCaretOffset -= selection.getLength();
752 IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
753 if (region == null) {
754 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$
755 sourceViewer.getTextWidget().getDisplay().beep();
759 int offset = region.getOffset();
760 int length = region.getLength();
765 int anchor = fBracketMatcher.getAnchor();
767 (PHPPairMatcher.RIGHT == anchor) ? offset : offset + length - 1;
769 boolean visible = false;
770 if (sourceViewer instanceof ITextViewerExtension3) {
771 ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
772 visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
774 IRegion visibleRegion = sourceViewer.getVisibleRegion();
776 (targetOffset >= visibleRegion.getOffset()
777 && targetOffset < visibleRegion.getOffset() + visibleRegion.getLength());
781 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$
782 sourceViewer.getTextWidget().getDisplay().beep();
786 if (selection.getLength() < 0)
787 targetOffset -= selection.getLength();
789 sourceViewer.setSelectedRange(targetOffset, selection.getLength());
790 sourceViewer.revealRange(targetOffset, selection.getLength());
793 * Ses the given message as error message to this editor's status line.
794 * @param msg message to be set
796 protected void setStatusLineErrorMessage(String msg) {
797 IEditorStatusLine statusLine =
798 (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
799 if (statusLine != null)
800 statusLine.setMessage(true, msg, null);
804 * Returns a segmentation of the line of the given document appropriate for bidi rendering.
805 * The default implementation returns only the string literals of a php code line as segments.
807 * @param document the document
808 * @param lineOffset the offset of the line
809 * @return the line's bidi segmentation
810 * @throws BadLocationException in case lineOffset is not valid in document
812 public static int[] getBidiLineSegments(IDocument document, int lineOffset)
813 throws BadLocationException {
815 IRegion line = document.getLineInformationOfOffset(lineOffset);
816 ITypedRegion[] linePartitioning =
817 document.computePartitioning(lineOffset, line.getLength());
819 List segmentation = new ArrayList();
820 for (int i = 0; i < linePartitioning.length; i++) {
821 if (IPHPPartitionScannerConstants
823 .equals(linePartitioning[i].getType()))
824 segmentation.add(linePartitioning[i]);
827 if (segmentation.size() == 0)
830 int size = segmentation.size();
831 int[] segments = new int[size * 2 + 1];
834 for (int i = 0; i < size; i++) {
835 ITypedRegion segment = (ITypedRegion) segmentation.get(i);
840 int offset = segment.getOffset() - lineOffset;
841 if (offset > segments[j - 1])
842 segments[j++] = offset;
844 if (offset + segment.getLength() >= line.getLength())
847 segments[j++] = offset + segment.getLength();
850 if (j < segments.length) {
851 int[] result = new int[j];
852 System.arraycopy(segments, 0, result, 0, j);
859 * Returns a segmentation of the given line appropriate for bidi rendering. The default
860 * implementation returns only the string literals of a php code line as segments.
862 * @param lineOffset the offset of the line
863 * @param line the content of the line
864 * @return the line's bidi segmentation
866 protected int[] getBidiLineSegments(int lineOffset, String line) {
867 IDocumentProvider provider = getDocumentProvider();
868 if (provider != null && line != null && line.length() > 0) {
869 IDocument document = provider.getDocument(getEditorInput());
870 if (document != null)
872 return getBidiLineSegments(document, lineOffset);
873 } catch (BadLocationException x) {
881 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
883 protected final ISourceViewer createSourceViewer(
885 IVerticalRuler ruler,
887 ISourceViewer viewer = createJavaSourceViewer(parent, ruler, styles);
888 StyledText text = viewer.getTextWidget();
889 text.addBidiSegmentListener(new BidiSegmentListener() {
890 public void lineGetSegments(BidiSegmentEvent event) {
891 event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
894 // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
899 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
901 protected ISourceViewer createJavaSourceViewer(
903 IVerticalRuler ruler,
905 return super.createSourceViewer(parent, ruler, styles);
909 * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
911 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
912 JavaTextTools textTools= PHPeclipsePlugin.getDefault().getJavaTextTools();
913 return textTools.affectsBehavior(event);