From: khartlage Date: Fri, 29 Nov 2002 20:20:41 +0000 (+0000) Subject: Syntax Highlighting Prefs work now / Automatic parse on save option available X-Git-Url: http://git.phpeclipse.com?hp=f16805fd7d5ba77f8d16226f782d53e4905ca11d Syntax Highlighting Prefs work now / Automatic parse on save option available --- diff --git a/net.sourceforge.phpeclipse/src/junit/sourceforge/phpeclipse/PHPParserTestCase.java b/net.sourceforge.phpeclipse/src/junit/sourceforge/phpeclipse/PHPParserTestCase.java index bfead41..0227343 100644 --- a/net.sourceforge.phpeclipse/src/junit/sourceforge/phpeclipse/PHPParserTestCase.java +++ b/net.sourceforge.phpeclipse/src/junit/sourceforge/phpeclipse/PHPParserTestCase.java @@ -28,11 +28,16 @@ public class PHPParserTestCase extends TestCase { * Test the PHP Parser with different PHP snippets */ public void testPHPParser() { + checkHTML(""); checkHTML(""); checkHTML(""); checkHTML(" foo "); checkHTML(" "); + checkPHP("echo \"Test\", \"me\";"); + checkPHP("print (\"Test me\");"); + checkPHP("$s = <<"net.sourceforge.phpeclipse"). - */ - public static final String ID_PLUGIN= "net.sourceforge.phpeclipse"; //$NON-NLS-1$ - - //The shared instance. - private static PHPeclipsePlugin plugin; - //Resource bundle. - private ResourceBundle resourceBundle; - + */ + public static final String ID_PLUGIN = "net.sourceforge.phpeclipse"; //$NON-NLS-1$ + + //The shared instance. + private static PHPeclipsePlugin plugin; + //Resource bundle. + private ResourceBundle resourceBundle; + private PHPDocumentProvider fCompilationUnitDocumentProvider; - /** - * The Java virtual machine that we are running on. - */ - private static int jvm; - - /** MRJ 2.0 */ - private static final int MRJ_2_0 = 0; - - /** MRJ 2.1 or later */ - private static final int MRJ_2_1 = 1; - - /** Java on Mac OS X 10.0 (MRJ 3.0) */ - private static final int MRJ_3_0 = 3; - - /** MRJ 3.1 */ - private static final int MRJ_3_1 = 4; - - /** Windows NT */ - private static final int WINDOWS_NT = 5; - - /** Windows 9x */ - private static final int WINDOWS_9x = 6; - - /** JVM constant for any other platform */ - private static final int OTHER = -1; - /** - * The constructor. - */ - public PHPeclipsePlugin(IPluginDescriptor descriptor) { - super(descriptor); - plugin = this; - setJVM(); - try { - resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources"); - } catch (MissingResourceException x) { - resourceBundle = null; - } - } + /** + * The Java virtual machine that we are running on. + */ + private static int jvm; + + /** MRJ 2.0 */ + private static final int MRJ_2_0 = 0; + + /** MRJ 2.1 or later */ + private static final int MRJ_2_1 = 1; + + /** Java on Mac OS X 10.0 (MRJ 3.0) */ + private static final int MRJ_3_0 = 3; + + /** MRJ 3.1 */ + private static final int MRJ_3_1 = 4; + + /** Windows NT */ + private static final int WINDOWS_NT = 5; + + /** Windows 9x */ + private static final int WINDOWS_9x = 6; + + /** JVM constant for any other platform */ + private static final int OTHER = -1; + /** + * The constructor. + */ + public PHPeclipsePlugin(IPluginDescriptor descriptor) { + super(descriptor); + plugin = this; + setJVM(); + try { + resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources"); + } catch (MissingResourceException x) { + resourceBundle = null; + } + } // @TODO: refactor this into a better method name ! public PHPDocumentProvider getCompilationUnitDocumentProvider() { if (fCompilationUnitDocumentProvider == null) - fCompilationUnitDocumentProvider= new PHPDocumentProvider(); + fCompilationUnitDocumentProvider = new PHPDocumentProvider(); return fCompilationUnitDocumentProvider; } - - public static void setJVM() { - String osName = System.getProperty("os.name"); - - if (osName.startsWith("Mac OS")) { - String mrjVersion = System.getProperty("mrj.version"); - String majorMRJVersion = mrjVersion.substring(0, 3); - jvm = OTHER; - try { - - double version = Double.valueOf(majorMRJVersion).doubleValue(); - - if (version == 2) { - jvm = MRJ_2_0; - } else if (version >= 2.1 && version < 3) { - jvm = MRJ_2_1; - } else if (version == 3.0) { - jvm = MRJ_3_0; - } else if (version >= 3.1) { - jvm = MRJ_3_1; - } - - } catch (NumberFormatException nfe) { - - } - - } else if (osName.startsWith("Windows")) { - if (osName.indexOf("9") != -1) { - jvm = WINDOWS_9x; - } else { - jvm = WINDOWS_NT; - } - } - } - public static int getJVM() { - return jvm; - } - /** - * Returns the shared instance. - */ - public static PHPeclipsePlugin getDefault() { - return plugin; - } - - /** - * Returns the workspace instance. - */ - public static IWorkspace getWorkspace() { - return ResourcesPlugin.getWorkspace(); - } - + + public static void setJVM() { + String osName = System.getProperty("os.name"); + + if (osName.startsWith("Mac OS")) { + String mrjVersion = System.getProperty("mrj.version"); + String majorMRJVersion = mrjVersion.substring(0, 3); + jvm = OTHER; + try { + + double version = Double.valueOf(majorMRJVersion).doubleValue(); + + if (version == 2) { + jvm = MRJ_2_0; + } else if (version >= 2.1 && version < 3) { + jvm = MRJ_2_1; + } else if (version == 3.0) { + jvm = MRJ_3_0; + } else if (version >= 3.1) { + jvm = MRJ_3_1; + } + + } catch (NumberFormatException nfe) { + + } + + } else if (osName.startsWith("Windows")) { + if (osName.indexOf("9") != -1) { + jvm = WINDOWS_9x; + } else { + jvm = WINDOWS_NT; + } + } + } + public static int getJVM() { + return jvm; + } + /** + * Returns the shared instance. + */ + public static PHPeclipsePlugin getDefault() { + return plugin; + } + + /** + * Returns the workspace instance. + */ + public static IWorkspace getWorkspace() { + return ResourcesPlugin.getWorkspace(); + } + public static IWorkbenchPage getActivePage() { return getDefault().getActivePage(); } - + public static IWorkbenchWindow getActiveWorkbenchWindow() { return getDefault().getWorkbench().getActiveWorkbenchWindow(); } - + public static Shell getActiveWorkbenchShell() { return getActiveWorkbenchWindow().getShell(); } - + public static String getPluginId() { return getDefault().getDescriptor().getUniqueIdentifier(); } @@ -170,83 +170,88 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon public static void log(IStatus status) { getDefault().getLog().log(status); } - -// public static void logErrorMessage(String message) { -// log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null)); -// } -// -// public static void logErrorStatus(String message, IStatus status) { -// if (status == null) { -// logErrorMessage(message); -// return; -// } -// MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null); -// multi.add(status); -// log(multi); -// } -// -// public static void log(Throwable e) { -// log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$ -// } - + + // public static void logErrorMessage(String message) { + // log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null)); + // } + // + // public static void logErrorStatus(String message, IStatus status) { + // if (status == null) { + // logErrorMessage(message); + // return; + // } + // MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null); + // multi.add(status); + // log(multi); + // } + // + // public static void log(Throwable e) { + // log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$ + // } + public static boolean isDebug() { return getDefault().isDebugging(); } - + static IPath getInstallLocation() { return new Path(getDefault().getDescriptor().getInstallURL().getFile()); } - /** - * Returns the string from the plugin's resource bundle, - * or 'key' if not found. - */ - public static String getResourceString(String key) { - ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle(); - try { - return bundle.getString(key); - } catch (MissingResourceException e) { - return key; - } - } - - /** - * Returns the plugin's resource bundle, - */ - public ResourceBundle getResourceBundle() { - return resourceBundle; - } - - protected void initializeDefaultPreferences(IPreferenceStore store) { - // windows preferences: - store.setDefault(LOCALHOST_PREF, "http://localhost"); - - store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false"); + /** + * Returns the string from the plugin's resource bundle, + * or 'key' if not found. + */ + public static String getResourceString(String key) { + ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle(); + try { + return bundle.getString(key); + } catch (MissingResourceException e) { + return key; + } + } + + /** + * Returns the plugin's resource bundle, + */ + public ResourceBundle getResourceBundle() { + return resourceBundle; + } + + protected void initializeDefaultPreferences(IPreferenceStore store) { + // windows preferences: + store.setDefault(LOCALHOST_PREF, "http://localhost"); + + store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false"); store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true"); - if (jvm == WINDOWS_9x) { - store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}"); - } else if (jvm == WINDOWS_NT) { - store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}"); - } else { - store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}"); - } - if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) { + if (jvm == WINDOWS_9x) { + store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}"); + } else if (jvm == WINDOWS_NT) { + store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}"); + } else { + store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}"); + } + if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) { store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}"); store.setDefault(DOCUMENTROOT_PREF, "c:\\eclipse\\workspace"); - store.setDefault(MYSQL_PREF, "c:\\apache\\mysql\\bin\\mysqld.exe --standalone"); - store.setDefault(APACHE_START_PREF, "c:\\apache\\apache.exe -c \"DocumentRoot \"{0}\"\""); - store.setDefault(APACHE_STOP_PREF, "c:\\apache\\apache.exe -k shutdown"); - store.setDefault(APACHE_RESTART_PREF, "c:\\apache\\apache.exe -k restart"); - } else { + store.setDefault(MYSQL_PREF, "c:\\apache\\mysql\\bin\\mysqld.exe --standalone"); + store.setDefault(APACHE_START_PREF, "c:\\apache\\apache.exe -c \"DocumentRoot \"{0}\"\""); + store.setDefault(APACHE_STOP_PREF, "c:\\apache\\apache.exe -k shutdown"); + store.setDefault(APACHE_RESTART_PREF, "c:\\apache\\apache.exe -k restart"); + } else { store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}"); store.setDefault(DOCUMENTROOT_PREF, "/eclipse/workspace"); - store.setDefault(MYSQL_PREF, "/apache/mysql/bin/mysqld --standalone"); - store.setDefault(APACHE_START_PREF, "/apache/apache -c \"DocumentRoot \"{0}\"\""); - store.setDefault(APACHE_STOP_PREF, "/apache/apache.exe -k shutdown"); - store.setDefault(APACHE_RESTART_PREF, "/apache/apache -k restart"); - - } + store.setDefault(MYSQL_PREF, "/apache/mysql/bin/mysqld --standalone"); + store.setDefault(APACHE_START_PREF, "/apache/apache -c \"DocumentRoot \"{0}\"\""); + store.setDefault(APACHE_STOP_PREF, "/apache/apache.exe -k shutdown"); + store.setDefault(APACHE_RESTART_PREF, "/apache/apache -k restart"); + + } + + store.setDefault(PHP_PARSER_DEFAULT, "true"); + store.setDefault(PHP_INTERNAL_PARSER, "true"); + store.setDefault(PHP_EXTERNAL_PARSER, "false"); + store.setDefault(PHP_PARSE_ON_SAVE, "true"); // php syntax highlighting PreferenceConverter.setDefault(store, PHP_MULTILINE_COMMENT, PHPColorProvider.MULTI_LINE_COMMENT); PreferenceConverter.setDefault(store, PHP_SINGLELINE_COMMENT, PHPColorProvider.SINGLE_LINE_COMMENT); @@ -256,5 +261,5 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING); PreferenceConverter.setDefault(store, PHP_DEFAULT, PHPColorProvider.DEFAULT); - } + } } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPExternalParserAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPExternalParserAction.java index a7f1dc0..1e1c770 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPExternalParserAction.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPExternalParserAction.java @@ -11,31 +11,19 @@ Contributors: **********************************************************************/ package net.sourceforge.phpeclipse.actions; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Hashtable; import java.util.Iterator; -import java.util.StringTokenizer; -import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import net.sourceforge.phpeclipse.phpeditor.PHPParser; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; import org.eclipse.jface.action.IAction; -import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.texteditor.MarkerUtilities; public class PHPExternalParserAction implements IObjectActionDelegate { - private static final String PARSE_ERROR = "Parse error"; //$NON-NLS-1$ - private static final String WARNING = "Warning"; //$NON-NLS-1$ private IWorkbenchPart workbenchPart; /** @@ -68,9 +56,7 @@ public class PHPExternalParserAction implements IObjectActionDelegate { StructuredSelection selection = null; selection = (StructuredSelection) selectionProvider.getSelection(); - IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); - - Shell shell = null; + //Shell shell = null; Iterator iterator = null; iterator = selection.iterator(); while (iterator.hasNext()) { @@ -87,22 +73,7 @@ public class PHPExternalParserAction implements IObjectActionDelegate { case IResource.FILE : // single file: IFile file = (IFile) resource; - IPath path = file.getFullPath(); - - String filename = file.getLocation().toString(); - - String[] arguments = { filename }; - MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF)); - String command = form.format(arguments); - - String parserResult = PHPStartApacheAction.execute(command, "External parser: "); - - try { - // parse the buffer to find the errors and warnings - createMarkers(parserResult, file); - } catch (CoreException e) { - } - + PHPParser.phpExternalParse(file); } } } @@ -114,70 +85,4 @@ public class PHPExternalParserAction implements IObjectActionDelegate { public void selectionChanged(IAction action, ISelection selection) { } - /** - * Create markers according to the compiler output - */ - protected void createMarkers(String output, IFile file) throws CoreException { - // delete all markers - file.deleteMarkers(IMarker.PROBLEM, false, 0); - - int indx = 0; - int brIndx = 0; - boolean flag = true; - while ((brIndx = output.indexOf("
", indx)) != -1) { - // newer php error output (tested with 4.2.3) - scanLine(output, file, indx, brIndx); - indx = brIndx + 6; - flag = false; - } - if (flag) { - while ((brIndx = output.indexOf("
", indx)) != -1) { - // older php error output (tested with 4.2.3) - scanLine(output, file, indx, brIndx); - indx = brIndx + 4; - } - } - } - private void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException { - String current; - String outLineNumberString; - StringBuffer lineNumberBuffer = new StringBuffer(10); - char ch; - current = output.substring(indx, brIndx); - - if (current.indexOf(WARNING) != -1 || current.indexOf(PARSE_ERROR) != -1) { - int onLine = current.indexOf("on line "); - if (onLine != -1) { - lineNumberBuffer.delete(0, lineNumberBuffer.length()); - for (int i = onLine; i < current.length(); i++) { - ch = current.charAt(i); - if ('0' <= ch && '9' >= ch) { - lineNumberBuffer.append(ch); - } - } - - // String line = current.substring(current.indexOf(fullPath) + fullPath.length(), current.length()); - // String errorsLocation = line.substring(1, line.indexOf(":") - 1); //$NON-NLS-1$ - // String message = line.substring(line.indexOf(":") + 2, line.length() - 1); //$NON-NLS-1$ - - int lineNumber = Integer.parseInt(lineNumberBuffer.toString()); - - Hashtable attributes = new Hashtable(); - - current = current.replaceAll("\n", ""); - current = current.replaceAll("", ""); - current = current.replaceAll("", ""); - MarkerUtilities.setMessage(attributes, current); - - if (current.indexOf(PARSE_ERROR) != -1) - attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); - else if (current.indexOf(WARNING) != -1) - attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING)); - else - attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO)); - MarkerUtilities.setLineNumber(attributes, lineNumber); - MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM); - } - } - } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPDocumentProvider.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPDocumentProvider.java index 1c603b6..07af3cd 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPDocumentProvider.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPDocumentProvider.java @@ -21,7 +21,7 @@ import org.eclipse.ui.editors.text.FileDocumentProvider; import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner; /** - * The JavaDocumentProvider provides the IDocuments used by java editors. + * The PHPDocumentProvider provides the IDocuments used by java editors. */ public class PHPDocumentProvider extends FileDocumentProvider { 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 11cf34f..2c19dd5 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditor.java @@ -11,17 +11,22 @@ Contributors: IBM Corporation - Initial implementation Klaus Hartlage - www.eclipseproject.de **********************************************************************/ +import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner; import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.help.IHelp; import org.eclipse.help.IHelpResource; +import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.MenuManager; -import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.editors.text.TextEditor; @@ -29,7 +34,6 @@ import org.eclipse.ui.help.WorkbenchHelp; import org.eclipse.ui.texteditor.DefaultRangeIndicator; import org.eclipse.ui.texteditor.TextOperationAction; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; -import org.eclipse.jface.action.IAction; /** * Java specific text editor. */ @@ -78,9 +82,9 @@ public class PHPEditor extends TextEditor { super.dispose(); } - /** The JavaEditor implementation of this + /** The PHPEditor implementation of this * AbstractTextEditor method performs any extra - * revert behavior required by the java editor. + * revert behavior required by the php editor. */ public void doRevertToSaved() { super.doRevertToSaved(); @@ -88,23 +92,26 @@ public class PHPEditor extends TextEditor { fOutlinePage.update(); } - /** The JavaEditor implementation of this + /** The PHPEditor implementation of this * AbstractTextEditor method performs any extra - * save behavior required by the java editor. + * save behavior required by the php editor. */ public void doSave(IProgressMonitor monitor) { super.doSave(monitor); // compile or not, according to the user preferences - IAction a = PHPParserAction.getInstance(); - if (a != null) - a.run(); + IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) { + IAction a = PHPParserAction.getInstance(); + if (a != null) + a.run(); + } if (fOutlinePage != null) fOutlinePage.update(); } - /** The JavaEditor implementation of this + /** The PHPEditor implementation of this * AbstractTextEditor method performs any extra - * save as behavior required by the java editor. + * save as behavior required by the php editor. */ public void doSaveAs() { super.doSaveAs(); @@ -112,7 +119,7 @@ public class PHPEditor extends TextEditor { fOutlinePage.update(); } - /** The JavaEditor implementation of this + /** The PHPEditor implementation of this * AbstractTextEditor method performs sets the * input of the outline page after AbstractTextEditor has set input. */ @@ -193,5 +200,18 @@ public class PHPEditor extends TextEditor { setRangeIndicator(new DefaultRangeIndicator()); setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$ setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$ + // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider()); + + PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() { + public void propertyChange(PropertyChangeEvent event) { + PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner(); + if (scanner != null) { + scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider()); + } + if (getSourceViewer() != null) { + getSourceViewer().invalidateTextPresentation(); + } + } + }); } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditorEnvironment.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditorEnvironment.java index f50c26d..011feb4 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditorEnvironment.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPEditorEnvironment.java @@ -57,18 +57,18 @@ public class PHPEditorEnvironment { /** * Returns the singleton scanner. */ - public static RuleBasedScanner getPHPCodeScanner() { + public static PHPCodeScanner getPHPCodeScanner() { return fgCodeScanner; } - public static RuleBasedScanner getHTMLCodeScanner() { + public static HTMLCodeScanner getHTMLCodeScanner() { return fgHTMLCodeScanner; } /** * Returns the singleton color provider. */ - public static PHPColorProvider getJavaColorProvider() { + public static PHPColorProvider getPHPColorProvider() { return fgColorProvider; } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java index 91620bc..7da9027 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java @@ -10,17 +10,25 @@ Contributors: **********************************************************************/ package net.sourceforge.phpeclipse.phpeditor; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import net.sourceforge.phpeclipse.actions.PHPStartApacheAction; import net.sourceforge.phpeclipse.phpeditor.php.PHPKeywords; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.ui.texteditor.MarkerUtilities; public class PHPParser extends PHPKeywords { + // strings for external parser call + private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$ + private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$ public static final int ERROR = 2; public static final int WARNING = 1; @@ -90,6 +98,7 @@ public class PHPParser extends PHPKeywords { final static int TT_QUESTIONMARK = 61; final static int TT_DDOT2 = 62; final static int TT_AT = 63; + // final static int TT_HEREDOC = 64; final static int TT_DOLLAROPEN = 127; final static int TT_ARGOPEN = 128; @@ -194,6 +203,11 @@ public class PHPParser extends PHPKeywords { throw new SyntaxError(rowCount, chIndx - columnCount + 1, str.substring(columnCount, eol), error); } + private void throwSyntaxError(String error, int startRow) { + + throw new SyntaxError(startRow, 0, " ", error); + } + /** * Method Declaration. * @@ -306,6 +320,7 @@ public class PHPParser extends PHPKeywords { } else if (ch == '\'') { // read string until end boolean openString = true; + int startRow = rowCount; while (str.length() > chIndx) { ch = str.charAt(chIndx++); if (ch == '\\') { @@ -321,13 +336,14 @@ public class PHPParser extends PHPKeywords { } } if (openString) { - throwSyntaxError("Open string character \"'\" at end of file."); + throwSyntaxError("Open string character \"'\" at end of file.", startRow); } token = TT_STRING_CONSTANT; return; } else if (ch == '`') { // read string until end boolean openString = true; + int startRow = rowCount; while (str.length() > chIndx) { ch = str.charAt(chIndx++); if (ch == '\\') { @@ -343,7 +359,7 @@ public class PHPParser extends PHPKeywords { } } if (openString) { - throwSyntaxError("Open string character \"`\" at end of file."); + throwSyntaxError("Open string character \"`\" at end of file.", startRow); } setMarker("Other string delimiters prefered (found \"`\").", rowCount, PHPParser.INFO); token = TT_STRING_CONSTANT; @@ -596,12 +612,34 @@ public class PHPParser extends PHPKeywords { if (str.charAt(chIndx) == '<') { chIndx++; token = TT_LSHIFT; - if (str.length() > chIndx) { - if (str.charAt(chIndx) == '=') { - chIndx++; - token = TT_LSHIFTASSIGN; - break; + if (str.charAt(chIndx) == '<') { + // heredoc + int startRow = rowCount; + if (str.length() > chIndx) { + + ch = str.charAt(++chIndx); + if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '_')) { + chIndx++; + getIdentifier(); + token = TT_STRING_CONSTANT; + while (str.length() > chIndx) { + ch = str.charAt(chIndx++); + if (ch == '\n') { + if (str.length() >= chIndx + identifier.length()) { + if (str.substring(chIndx, chIndx + identifier.length()).equals(identifier)) { + chIndx += identifier.length(); + return; + } + } + } + } + } } + throwSyntaxError("Open heredoc syntax after operator '<<<'.", startRow); + } else if (str.charAt(chIndx) == '=') { + chIndx++; + token = TT_LSHIFTASSIGN; + break; } break; } @@ -1274,17 +1312,17 @@ public class PHPParser extends PHPKeywords { } } return; - } else if (token == TT_print) { - getNextToken(); - expression(); - if (token == TT_SEMICOLON) { - getNextToken(); - } else { - if (!phpEnd) { - throwSyntaxError("';' expected after 'print' statement."); - } - } - return; + // } else if (token == TT_print) { + // getNextToken(); + // expression(); + // if (token == TT_SEMICOLON) { + // getNextToken(); + // } else { + // if (!phpEnd) { + // throwSyntaxError("';' expected after 'print' statement."); + // } + // } + // return; } else if (token == TT_global || token == TT_static) { getNextToken(); @@ -1957,6 +1995,17 @@ public class PHPParser extends PHPKeywords { getNextToken(); } break; + case TT_print : + getNextToken(); + expression(); + // if (token == TT_SEMICOLON) { + // getNextToken(); + // } else { + // if (!phpEnd) { + // throwSyntaxError("';' expected after 'print' statement."); + // } + // } + break; case TT_list : getNextToken(); if (token == TT_ARGOPEN) { @@ -2507,5 +2556,91 @@ public class PHPParser extends PHPKeywords { throwSyntaxError("Constant expected."); } } + + /** + * Call the php parse command ( php -l -f <filename> ) + * and create markers according to the external parser output + */ + public static void phpExternalParse(IFile file) { + //IFile file = (IFile) resource; + IPath path = file.getFullPath(); + IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + String filename = file.getLocation().toString(); + + String[] arguments = { filename }; + MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF)); + String command = form.format(arguments); + + String parserResult = PHPStartApacheAction.execute(command, "External parser: "); + + try { + // parse the buffer to find the errors and warnings + createMarkers(parserResult, file); + } catch (CoreException e) { + } + } + + /** + * Create markers according to the external parser output + */ + private static void createMarkers(String output, IFile file) throws CoreException { + // delete all markers + file.deleteMarkers(IMarker.PROBLEM, false, 0); + + int indx = 0; + int brIndx = 0; + boolean flag = true; + while ((brIndx = output.indexOf("
", indx)) != -1) { + // newer php error output (tested with 4.2.3) + scanLine(output, file, indx, brIndx); + indx = brIndx + 6; + flag = false; + } + if (flag) { + while ((brIndx = output.indexOf("
", indx)) != -1) { + // older php error output (tested with 4.2.3) + scanLine(output, file, indx, brIndx); + indx = brIndx + 4; + } + } + } + + private static void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException { + String current; + String outLineNumberString; + StringBuffer lineNumberBuffer = new StringBuffer(10); + char ch; + current = output.substring(indx, brIndx); + + if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) { + int onLine = current.indexOf("on line "); + if (onLine != -1) { + lineNumberBuffer.delete(0, lineNumberBuffer.length()); + for (int i = onLine; i < current.length(); i++) { + ch = current.charAt(i); + if ('0' <= ch && '9' >= ch) { + lineNumberBuffer.append(ch); + } + } + int lineNumber = Integer.parseInt(lineNumberBuffer.toString()); + + Hashtable attributes = new Hashtable(); + + current = current.replaceAll("\n", ""); + current = current.replaceAll("", ""); + current = current.replaceAll("", ""); + MarkerUtilities.setMessage(attributes, current); + + if (current.indexOf(PARSE_ERROR_STRING) != -1) + attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); + else if (current.indexOf(PARSE_WARNING_STRING) != -1) + attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING)); + else + attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO)); + MarkerUtilities.setLineNumber(attributes, lineNumber); + MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM); + } + } + } } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java index dd34221..37fae97 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java @@ -15,20 +15,16 @@ Contributors: import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.Collections; -import java.util.Hashtable; import java.util.List; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.runtime.CoreException; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.Position; +import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.texteditor.ITextEditor; -import org.eclipse.ui.texteditor.MarkerUtilities; import org.eclipse.ui.texteditor.TextEditorAction; /** @@ -36,14 +32,6 @@ import org.eclipse.ui.texteditor.TextEditorAction; */ public class PHPParserAction extends TextEditorAction { - // public static final String EXE = "exe"; //$NON-NLS-1$ - // public static final String WINEXE = "winexe"; //$NON-NLS-1$ - // public static final String LIBRARY = "library"; //$NON-NLS-1$ - // public static final String MODULE = "module"; //$NON-NLS-1$ - - // private static final String ERROR = "error"; //$NON-NLS-1$ - // private static final String WARNING = "warning"; //$NON-NLS-1$ - private static PHPParserAction instance = new PHPParserAction(); protected IFile fileToParse; @@ -73,27 +61,22 @@ public class PHPParserAction extends TextEditorAction { // should throw an exception return; } - // first delete all the previous markers - fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0); - //IDocument document = getTextEditor().getDocumentProvider().getDocument(null); - //String text = document.get(); - - // String text = - // parse(document); - try { - InputStream iStream = fileToParse.getContents(); - // int c = iStream.read(); - parse(iStream); - iStream.close(); - } catch (IOException e) { + IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + if (store.getBoolean(PHPeclipsePlugin.PHP_PARSER_DEFAULT)) { + // first delete all the previous markers + fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0); + + try { + InputStream iStream = fileToParse.getContents(); + // int c = iStream.read(); + parse(iStream); + iStream.close(); + } catch (IOException e) { + } + } else { + PHPParser.phpExternalParse(fileToParse); } - // String message = "Test error"; - // int lineNumber = 1; - // - // // create marker for the error - // - // setMarker(message, lineNumber, fileToParse); } catch (CoreException e) { } @@ -120,19 +103,19 @@ public class PHPParserAction extends TextEditorAction { /** * Create marker for the parse error */ -// protected void setMarker(String message, int lineNumber) throws CoreException { -// -// Hashtable attributes = new Hashtable(); -// MarkerUtilities.setMessage(attributes, message); -// if (message.startsWith(ERROR)) -// attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); -// else if (message.startsWith(WARNING)) -// attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING)); -// else -// attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO)); -// MarkerUtilities.setLineNumber(attributes, lineNumber); -// MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM); -// } + // protected void setMarker(String message, int lineNumber) throws CoreException { + // + // Hashtable attributes = new Hashtable(); + // MarkerUtilities.setMessage(attributes, message); + // if (message.startsWith(ERROR)) + // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); + // else if (message.startsWith(WARNING)) + // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING)); + // else + // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO)); + // MarkerUtilities.setLineNumber(attributes, lineNumber); + // MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM); + // } // private String getIdentifier(InputStream iStream, int c) { // // int i = 0; @@ -168,8 +151,8 @@ public class PHPParserAction extends TextEditorAction { return; } String input = buf.toString(); - - PHPParser parser = new PHPParser(fileToParse); - parser.htmlParse(input); + + PHPParser parser = new PHPParser(fileToParse); + parser.htmlParse(input); } } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java index abea8df..2cb2397 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java @@ -95,7 +95,7 @@ public class PHPSourceViewerConfiguration extends SourceViewerConfiguration { assistant.setAutoActivationDelay(500); assistant.setProposalPopupOrientation(assistant.PROPOSAL_OVERLAY); assistant.setContextInformationPopupOrientation(assistant.CONTEXT_INFO_ABOVE); - assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getJavaColorProvider().getColor(new RGB(150, 150, 0))); + assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getPHPColorProvider().getColor(new RGB(150, 150, 0))); return assistant; } @@ -126,7 +126,7 @@ public class PHPSourceViewerConfiguration extends SourceViewerConfiguration { */ public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { - PHPColorProvider provider = PHPEditorEnvironment.getJavaColorProvider(); + PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider(); PresentationReconciler reconciler = new PresentationReconciler(); DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner()); diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCodeScanner.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCodeScanner.java index ccbc66a..bf89097 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCodeScanner.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCodeScanner.java @@ -39,7 +39,13 @@ import org.eclipse.jface.text.rules.WordRule; */ public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConstants { - private IToken variable; + private static Token variable; + private static Token keyword; + private static Token functionName; + private static Token string; + private static Token comment; + private static Token multi_comment; + private static Token other; private class PHPWordRule extends WordRule { private StringBuffer fBuffer = new StringBuffer(); @@ -88,34 +94,33 @@ public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConst } private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false", "null", "object", "array" }; - private TextAttribute fComment; - private TextAttribute fKeyword; - private TextAttribute fType; - private TextAttribute fString; + // private static TextAttribute fSingleLine; + // private static TextAttribute fMultiLine; + // private static TextAttribute fKeyword; + // private static TextAttribute fFunctionName; + // private static TextAttribute fString; + // private static TextAttribute fVariable; private PHPColorProvider fColorProvider; /** - * Creates a Java code scanner + * Creates a PHP code scanner */ public PHPCodeScanner(PHPColorProvider provider) { - final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); - IToken keyword = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)))); - IToken functionName = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)))); - IToken string = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)))); - IToken comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)))); - IToken multi_comment = - new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)))); - IToken other = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)))); + variable = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)))); + keyword = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)))); + functionName = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)))); + string = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)))); + comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)))); + multi_comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)))); + other = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)))); - variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE))); List rules = new ArrayList(); // Add rule for single line comments. rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$ - // EndOfLineRule endOfLine = new EndOfLineRule("#", comment); - // endOfLine.setColumnConstraint(0); rules.add(new EndOfLineRule("#", comment)); // Add rule for strings and character constants. @@ -142,4 +147,17 @@ public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConst rules.toArray(result); setRules(result); } + + public void updateToken(PHPColorProvider provider) { + final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + + variable.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)))); + keyword.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)))); + functionName.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)))); + string.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)))); + comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)))); + multi_comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)))); + other.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)))); + + } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java index 3df4172..bb6bd07 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java @@ -51,7 +51,8 @@ public class PHPKeywords { // "empty", // "array", // "isset", - "echo", "var", "as", "print", + "echo", "var", "as", + "print", // "unset", // "exit", "die", "and", "or", "xor", "list", @@ -145,9 +146,10 @@ public class PHPKeywords { // TT_empty, // TT_array, // TT_isset, - TT_echo, TT_var, TT_as, TT_print, + TT_echo, TT_var, TT_as, + TT_print, // TT_unset, - //TT_exit, TT_die, + // TT_exit, TT_die, TT_and, TT_or, TT_xor, TT_list, TT_null, TT_false, TT_true }; }