X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditor.java index ed60680..c86919b 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditor.java @@ -11,15 +11,23 @@ Contributors: IBM Corporation - Initial implementation Klaus Hartlage - www.eclipseproject.de **********************************************************************/ +import java.util.ArrayList; +import java.util.List; + import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup; +import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter; +import net.sourceforge.phpdt.internal.ui.text.JavaColorManager; import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher; +import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider; import net.sourceforge.phpdt.ui.actions.GenerateActionGroup; import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction; +import net.sourceforge.phpdt.ui.text.JavaTextTools; import net.sourceforge.phpeclipse.IPreferenceConstants; import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants; import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner; -import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.action.Action; @@ -28,12 +36,17 @@ import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.PreferenceConverter; import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DefaultInformationControl; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IInformationControl; +import org.eclipse.jface.text.IInformationControlCreator; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextOperationTarget; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.ITextViewerExtension3; +import org.eclipse.jface.text.ITypedRegion; import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.information.InformationPresenter; import org.eclipse.jface.text.source.AnnotationRulerColumn; import org.eclipse.jface.text.source.CompositeRuler; import org.eclipse.jface.text.source.ISourceViewer; @@ -42,29 +55,39 @@ import org.eclipse.jface.text.source.IVerticalRulerColumn; import org.eclipse.jface.text.source.LineNumberRulerColumn; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.BidiSegmentEvent; +import org.eclipse.swt.custom.BidiSegmentListener; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.actions.ActionContext; import org.eclipse.ui.actions.ActionGroup; -import org.eclipse.ui.editors.text.TextEditor; import org.eclipse.ui.texteditor.ContentAssistAction; import org.eclipse.ui.texteditor.DefaultRangeIndicator; +import org.eclipse.ui.texteditor.IDocumentProvider; import org.eclipse.ui.texteditor.IEditorStatusLine; import org.eclipse.ui.texteditor.ITextEditorActionConstants; +import org.eclipse.ui.texteditor.StatusTextEditor; import org.eclipse.ui.texteditor.TextOperationAction; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; /** * PHP specific text editor. */ -public class PHPEditor extends TextEditor { -// extends StatusTextEditor implements IViewPartInputProvider { // extends TextEditor { +public class PHPEditor + extends StatusTextEditor + implements IViewPartInputProvider { // extends TextEditor { // protected PHPActionGroup fActionGroups; /** The outline page */ private PHPContentOutlinePage fOutlinePage; - private IPreferenceStore phpPrefStore; + + // protected PHPSyntaxParserThread fValidationThread = null; + + // private IPreferenceStore fPHPPrefStore; /** The editor's bracket matcher */ private PHPPairMatcher fBracketMatcher; @@ -75,13 +98,25 @@ public class PHPEditor extends TextEditor { /** The standard action groups added to the menu */ private GenerateActionGroup fGenerateActionGroup; private CompositeActionGroup fContextMenuGroup; + + /** The information presenter. */ + private InformationPresenter fInformationPresenter; + /** * Default constructor. */ public PHPEditor() { super(); - setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$ - setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$ + JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools(); + setSourceViewerConfiguration( + new PHPSourceViewerConfiguration(textTools, this)); + setRangeIndicator(new DefaultRangeIndicator()); + setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore()); + + // if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE)) + // fUpdater= new OutlinePageSelectionUpdater(); + + initializeEditor(); } // // /** @@ -99,6 +134,38 @@ public class PHPEditor extends TextEditor { // return doc.get().toCharArray(); // } + /* + * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput() + */ + public Object getViewPartInput() { + return getEditorInput().getAdapter(IResource.class); + } + + /* + * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt. + * widgets.Composite) + */ + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + IInformationControlCreator informationControlCreator = + new IInformationControlCreator() { + public IInformationControl createInformationControl(Shell parent) { + boolean cutDown = false; + int style = cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL); + return new DefaultInformationControl( + parent, + SWT.RESIZE, + style, + new HTMLTextPresenter(cutDown)); + } + }; + + fInformationPresenter = new InformationPresenter(informationControlCreator); + fInformationPresenter.setSizeConstraints(60, 10, true, true); + fInformationPresenter.install(getSourceViewer()); + } + /** * Returns this document's complete text. * @@ -192,6 +259,16 @@ public class PHPEditor extends TextEditor { new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup }); // rg, // new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)}); + + // if (fValidationThread == null) { + // fValidationThread = + // new PHPSyntaxParserThread(this, getSourceViewer()); + // //Thread defaults + // + // fValidationThread.start(); + // } + // + // fValidationThread.setText(getSourceViewer().getTextWidget().getText()); } /** The PHPEditor implementation of this @@ -226,7 +303,7 @@ public class PHPEditor extends TextEditor { public void doSave(IProgressMonitor monitor) { super.doSave(monitor); // compile or not, according to the user preferences - IPreferenceStore store = phpPrefStore; + IPreferenceStore store = getPreferenceStore(); // fPHPPrefStore; if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) { IAction a = PHPParserAction.getInstance(); if (a != null) @@ -250,7 +327,7 @@ public class PHPEditor extends TextEditor { * AbstractTextEditor method performs sets the * input of the outline page after AbstractTextEditor has set input. */ - public void doSetInput(IEditorInput input) throws CoreException { + protected void doSetInput(IEditorInput input) throws CoreException { super.doSetInput(input); if (fOutlinePage != null) fOutlinePage.setInput(input); @@ -259,10 +336,10 @@ public class PHPEditor extends TextEditor { /* * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput() */ -// public Object getViewPartInput() { -// return getEditorInput().getAdapter(IFile.class); -// } - + // public Object getViewPartInput() { + // return getEditorInput().getAdapter(IFile.class); + // } + /** The PHPEditor implementation of this * AbstractTextEditor method adds any * PHPEditor specific entries. @@ -395,7 +472,8 @@ public class PHPEditor extends TextEditor { */ private boolean isLineNumberRulerVisible() { // IPreferenceStore store= getPreferenceStore(); - return phpPrefStore.getBoolean(IPreferenceConstants.LINE_NUMBER_RULER); + return getPreferenceStore().getBoolean( + IPreferenceConstants.LINE_NUMBER_RULER); } /** * Hides the line number ruler column. @@ -416,44 +494,45 @@ public class PHPEditor extends TextEditor { * @param rulerColumn the ruler column to be initialized */ protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) { - // JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools(); - PHPColorProvider manager = PHPEditorEnvironment.getPHPColorProvider(); + JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools(); + // PHPColorProvider manager = PHPEditorEnvironment.getPHPColorProvider(); + JavaColorManager manager = textTools.getColorManager(); - if (phpPrefStore != null) { + IPreferenceStore store = getPreferenceStore(); + if (store != null) { RGB rgb = null; // foreground color - if (phpPrefStore.contains(IPreferenceConstants.LINE_NUMBER_COLOR)) { - if (phpPrefStore.isDefault(IPreferenceConstants.LINE_NUMBER_COLOR)) + if (store.contains(IPreferenceConstants.LINE_NUMBER_COLOR)) { + if (store.isDefault(IPreferenceConstants.LINE_NUMBER_COLOR)) rgb = PreferenceConverter.getDefaultColor( - phpPrefStore, + store, IPreferenceConstants.LINE_NUMBER_COLOR); else rgb = PreferenceConverter.getColor( - phpPrefStore, + store, IPreferenceConstants.LINE_NUMBER_COLOR); } rulerColumn.setForeground(manager.getColor(rgb)); rgb = null; // background color - if (!phpPrefStore + if (!store .getBoolean( IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) { - if (phpPrefStore - .contains(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND)) { - if (phpPrefStore + if (store.contains(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND)) { + if (store .isDefault(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND)) rgb = PreferenceConverter.getDefaultColor( - phpPrefStore, + store, IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND); else rgb = PreferenceConverter.getColor( - phpPrefStore, + store, IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND); } rulerColumn.setBackground(manager.getColor(rgb)); @@ -486,16 +565,14 @@ public class PHPEditor extends TextEditor { * Method declared on TextEditor */ protected void initializeEditor() { + IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); PHPEditorEnvironment.connect(this); - setSourceViewerConfiguration(new PHPSourceViewerConfiguration(this)); - setRangeIndicator(new DefaultRangeIndicator()); setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$ setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$ // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider()); - phpPrefStore = PHPeclipsePlugin.getDefault().getPreferenceStore(); - phpPrefStore.addPropertyChangeListener(new IPropertyChangeListener() { + store.addPropertyChangeListener(new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner(); if (scanner != null) { @@ -516,7 +593,7 @@ public class PHPEditor extends TextEditor { } }); } - + private static IRegion getSignedSelection(ITextViewer viewer) { StyledText text = viewer.getTextWidget(); @@ -638,4 +715,106 @@ public class PHPEditor extends TextEditor { if (statusLine != null) statusLine.setMessage(true, msg, null); } + + /** + * Returns a segmentation of the line of the given document appropriate for bidi rendering. + * The default implementation returns only the string literals of a php code line as segments. + * + * @param document the document + * @param lineOffset the offset of the line + * @return the line's bidi segmentation + * @throws BadLocationException in case lineOffset is not valid in document + */ + public static int[] getBidiLineSegments(IDocument document, int lineOffset) throws BadLocationException { + + IRegion line= document.getLineInformationOfOffset(lineOffset); + ITypedRegion[] linePartitioning= document.computePartitioning(lineOffset, line.getLength()); + + List segmentation= new ArrayList(); + for (int i= 0; i < linePartitioning.length; i++) { + if (IPHPPartitionScannerConstants.PHP_STRING.equals(linePartitioning[i].getType())) + segmentation.add(linePartitioning[i]); + } + + + if (segmentation.size() == 0) + return null; + + int size= segmentation.size(); + int[] segments= new int[size * 2 + 1]; + + int j= 0; + for (int i= 0; i < size; i++) { + ITypedRegion segment= (ITypedRegion) segmentation.get(i); + + if (i == 0) + segments[j++]= 0; + + int offset= segment.getOffset() - lineOffset; + if (offset > segments[j - 1]) + segments[j++]= offset; + + if (offset + segment.getLength() >= line.getLength()) + break; + + segments[j++]= offset + segment.getLength(); + } + + if (j < segments.length) { + int[] result= new int[j]; + System.arraycopy(segments, 0, result, 0, j); + segments= result; + } + + return segments; + } + /** + * Returns a segmentation of the given line appropriate for bidi rendering. The default + * implementation returns only the string literals of a php code line as segments. + * + * @param lineOffset the offset of the line + * @param line the content of the line + * @return the line's bidi segmentation + */ + protected int[] getBidiLineSegments(int lineOffset, String line) { + IDocumentProvider provider = getDocumentProvider(); + if (provider != null && line != null && line.length() > 0) { + IDocument document = provider.getDocument(getEditorInput()); + if (document != null) + try { + return getBidiLineSegments(document, lineOffset); + } catch (BadLocationException x) { + // ignore + } + } + return null; + } + + /* + * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int) + */ + protected final ISourceViewer createSourceViewer( + Composite parent, + IVerticalRuler ruler, + int styles) { + ISourceViewer viewer = createJavaSourceViewer(parent, ruler, styles); + StyledText text = viewer.getTextWidget(); + text.addBidiSegmentListener(new BidiSegmentListener() { + public void lineGetSegments(BidiSegmentEvent event) { + event.segments = getBidiLineSegments(event.lineOffset, event.lineText); + } + }); + // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR); + return viewer; + } + + /* + * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int) + */ + protected ISourceViewer createJavaSourceViewer( + Composite parent, + IVerticalRuler ruler, + int styles) { + return super.createSourceViewer(parent, ruler, styles); + } }