From: khartlage Date: Mon, 8 Sep 2003 22:34:29 +0000 (+0000) Subject: Extended the builder with an index file generator for PHP class- and function-names. X-Git-Url: http://secure.phpeclipse.com Extended the builder with an index file generator for PHP class- and function-names. The index is stored in a "project.index" file for every project with php-nature. Right-mouse click in the PHP editor on an indexed class- or function-name should open the file, where the identifier is declared. --- diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/util/StreamUtil.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/util/StreamUtil.java new file mode 100644 index 0000000..cdfe20d --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/util/StreamUtil.java @@ -0,0 +1,35 @@ +/* + * Created on 06.09.2003 + * + */ +package net.sourceforge.phpdt.internal.ui.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * @author khartlage + * */ +public class StreamUtil { + public static void transferStreams(InputStream source, OutputStream destination) throws IOException { + try { + byte[] buffer = new byte[8192]; + while (true) { + int bytesRead = source.read(buffer); + if (bytesRead == -1) + break; + destination.write(buffer, 0, bytesRead); + } + } finally { + try { + source.close(); + } catch (IOException e) { + } + try { + destination.close(); + } catch (IOException e) { + } + } + } +} diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/util/StringUtil.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/util/StringUtil.java index 8ade751..0e36009 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/util/StringUtil.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/util/StringUtil.java @@ -30,4 +30,5 @@ public class StringUtil { buf.append(str.substring(lastindex)); return buf.toString(); } + } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/PHPeclipsePlugin.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/PHPeclipsePlugin.java index cb16be2..a414fd1 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/PHPeclipsePlugin.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/PHPeclipsePlugin.java @@ -11,6 +11,11 @@ Contributors: **********************************************************************/ package net.sourceforge.phpeclipse; +import java.io.File; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; + import net.sourceforge.phpdt.externaltools.internal.model.ColorManager; import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin; import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager; @@ -18,6 +23,10 @@ import net.sourceforge.phpdt.internal.ui.preferences.TemplatePreferencePage; import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry; import net.sourceforge.phpdt.ui.PreferenceConstants; import net.sourceforge.phpdt.ui.text.JavaTextTools; +import net.sourceforge.phpeclipse.builder.ExternalEditorInput; +import net.sourceforge.phpeclipse.builder.ExternalStorageDocumentProvider; +import net.sourceforge.phpeclipse.builder.FileStorage; +import net.sourceforge.phpeclipse.builder.IdentifierIndexManager; import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider; import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr; import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider; @@ -27,6 +36,7 @@ import net.sourceforge.phpeclipse.resourcesview.ResourceAdapterFactory; import org.eclipse.core.boot.BootLoader; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; @@ -40,22 +50,30 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.PreferenceConverter; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorDescriptor; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IEditorRegistry; +import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.eclipse.ui.texteditor.IDocumentProvider; +import org.eclipse.ui.texteditor.ITextEditor; /** * The main plugin class to be used in the desktop. */ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceConstants { - /** * The id of the PHP plugin (value "net.sourceforge.phpeclipse"). */ public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$ - public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature"; /** * id of builder - matches plugin.xml (concatenate pluginid.builderid) @@ -63,27 +81,10 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon public static final String BUILDER_INDEX_ID = PLUGIN_ID + ".indexbuilder"; public static final String BUILDER_PARSER_ID = PLUGIN_ID + ".parserbuilder"; - // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$ - public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$ - - public static final String PHPPARSER_NEW = "test.PHPParser"; - public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser"; - - /** Change this if you want to switch PHP Parser. */ - public static final String PHPPARSER = PHPPARSER_ORIGINAL; - - //The shared instance. - private static PHPeclipsePlugin plugin; + /** General debug flag*/ + public static final boolean DEBUG = false; private static ExternalToolsPlugin externalTools; - //Resource bundle. - //private ResourceBundle resourceBundle; - - private ImageDescriptorRegistry fImageDescriptorRegistry; - private PHPDocumentProvider fCompilationUnitDocumentProvider; - private IFile fLastEditorFile = null; - - private JavaTextTools fJavaTextTools; /** * The Java virtual machine that we are running on. @@ -102,17 +103,37 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon /** MRJ 3.1 */ private static final int MRJ_3_1 = 4; - /** Windows NT */ - private static final int WINDOWS_NT = 5; + /** JVM constant for any other platform */ + private static final int OTHER = -1; + + // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$ + public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$ + public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature"; + + public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser"; + + /** Change this if you want to switch PHP Parser. */ + public static final String PHPPARSER = PHPPARSER_ORIGINAL; + + public static final String PHPPARSER_NEW = "test.PHPParser"; + + //The shared instance. + private static PHPeclipsePlugin plugin; /** Windows 9x */ private static final int WINDOWS_9x = 6; - /** JVM constant for any other platform */ - private static final int OTHER = -1; + /** Windows NT */ + private static final int WINDOWS_NT = 5; + private PHPDocumentProvider fCompilationUnitDocumentProvider; + //Resource bundle. + //private ResourceBundle resourceBundle; - /** General debug flag*/ - public static final boolean DEBUG = false; + private ImageDescriptorRegistry fImageDescriptorRegistry; + private HashMap fIndexManagerMap = new HashMap(); + + private JavaTextTools fJavaTextTools; + private IFile fLastEditorFile = null; /** * The constructor. */ @@ -128,63 +149,50 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon // } } - public static ImageDescriptorRegistry getImageDescriptorRegistry() { - return getDefault().internalGetImageDescriptorRegistry(); + public static IWorkbenchPage getActivePage() { + return getDefault().internalGetActivePage(); } - private ImageDescriptorRegistry internalGetImageDescriptorRegistry() { - if (fImageDescriptorRegistry == null) - fImageDescriptorRegistry = new ImageDescriptorRegistry(); - return fImageDescriptorRegistry; - } - // @TODO: refactor this into a better method name ! - public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() { - if (fCompilationUnitDocumentProvider == null) - fCompilationUnitDocumentProvider = new PHPDocumentProvider(); - return fCompilationUnitDocumentProvider; + public static Shell getActiveWorkbenchShell() { + return getActiveWorkbenchWindow().getShell(); } - private 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) { + public static IWorkbenchWindow getActiveWorkbenchWindow() { + return getDefault().getWorkbench().getActiveWorkbenchWindow(); + } + /** + * Returns the shared instance. + */ + public static PHPeclipsePlugin getDefault() { + return plugin; + } - } + public static ImageDescriptorRegistry getImageDescriptorRegistry() { + return getDefault().internalGetImageDescriptorRegistry(); + } - } else if (osName.startsWith("Windows")) { - if (osName.indexOf("9") != -1) { - jvm = WINDOWS_9x; - } else { - jvm = WINDOWS_NT; - } - } + static IPath getInstallLocation() { + return new Path(getDefault().getDescriptor().getInstallURL().getFile()); } public static int getJVM() { return jvm; } + + public static String getPluginId() { + return getDefault().getDescriptor().getUniqueIdentifier(); + } + /** - * Returns the shared instance. + * Returns the standard display to be used. The method first checks, if + * the thread calling this method has an associated display. If so, this + * display is returned. Otherwise the method returns the default display. */ - public static PHPeclipsePlugin getDefault() { - return plugin; + public static Display getStandardDisplay() { + Display display = Display.getCurrent(); + if (display == null) { + display = Display.getDefault(); + } + return display; } // public static ExternalToolsPlugin getExternalTools() { @@ -197,31 +205,8 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon return ResourcesPlugin.getWorkspace(); } - public static IWorkbenchPage getActivePage() { - return getDefault().internalGetActivePage(); - } - - private IWorkbenchPage internalGetActivePage() { - IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow(); - if (window != null) - return window.getActivePage(); - return null; - } - - public static IWorkbenchWindow getActiveWorkbenchWindow() { - return getDefault().getWorkbench().getActiveWorkbenchWindow(); - } - - public static Shell getActiveWorkbenchShell() { - return getActiveWorkbenchWindow().getShell(); - } - - public static String getPluginId() { - return getDefault().getDescriptor().getUniqueIdentifier(); - } - - public static void log(IStatus status) { - getDefault().getLog().log(status); + public static boolean isDebug() { + return getDefault().isDebugging(); } // public static void logErrorMessage(String message) { @@ -246,16 +231,69 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null); log(status); } + + public static void log(IStatus status) { + getDefault().getLog().log(status); + } public static void log(Throwable e) { log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$ } - public static boolean isDebug() { - return getDefault().isDebugging(); + private 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; + } + } } - static IPath getInstallLocation() { - return new Path(getDefault().getDescriptor().getInstallURL().getFile()); + // TODO: refactor this into a better method name ! + public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() { + if (fCompilationUnitDocumentProvider == null) + fCompilationUnitDocumentProvider = new PHPDocumentProvider(); + return fCompilationUnitDocumentProvider; + } + + /** + * Get the identifier index manager for the given project + * + * @param iProject the current project + * @return + */ + public IdentifierIndexManager getIndexManager(IProject iProject) { + String indexFilename = iProject.getLocation() + File.separator + "project.index"; + IdentifierIndexManager indexManager = (IdentifierIndexManager) fIndexManagerMap.get(indexFilename); + if (indexManager == null) { + indexManager = new IdentifierIndexManager(indexFilename); + fIndexManagerMap.put(indexFilename, indexManager); + } + return indexManager; } public synchronized JavaTextTools getJavaTextTools() { @@ -264,6 +302,10 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon return fJavaTextTools; } + public IFile getLastEditorFile() { + return fLastEditorFile; + } + /** * Returns the string from the plugin's resource bundle, * or 'key' if not found. @@ -299,7 +341,7 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon } else if (windowsSystem.equals(BootLoader.WS_WIN32)) { store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}"); } else if (windowsSystem.equals(BootLoader.WS_CARBON)) { - // TODO How do we start Safari on Mac OS X ? + // TODO How do we start Safari on Mac OS X ? store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}"); } else { store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}"); @@ -412,17 +454,115 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon externalTools.initializeDefaultPreferences(store); } + private IWorkbenchPage internalGetActivePage() { + IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow(); + if (window != null) + return window.getActivePage(); + return null; + } + + private ImageDescriptorRegistry internalGetImageDescriptorRegistry() { + if (fImageDescriptorRegistry == null) + fImageDescriptorRegistry = new ImageDescriptorRegistry(); + return fImageDescriptorRegistry; + } + /** - * Returns the standard display to be used. The method first checks, if - * the thread calling this method has an associated display. If so, this - * display is returned. Otherwise the method returns the default display. + * Open a file in the Workbench that may or may not exist in the workspace. + * Must be run on the UI thread. + * @param filename + * @param line + * @throws CoreException + */ + public void openFileInTextEditor(String filename, int line, String findString) throws CoreException { + + // reject directories + if (new File(filename).isDirectory()) + return; + + IWorkbench workbench = PlatformUI.getWorkbench(); + IWorkbenchWindow window = workbench.getWorkbenchWindows()[0]; + IWorkbenchPage page = window.getActivePage(); + IPath path = new Path(filename); + + // If the file exists in the workspace, open it + IFile file = getWorkspace().getRoot().getFileForLocation(path); + IEditorPart editor; + ITextEditor textEditor; + if (file != null && file.exists()) { + editor = page.openEditor(file); + textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class); + } else { + // Otherwise open the stream directly + if (page == null) + return; + FileStorage storage = new FileStorage(path); + IEditorRegistry registry = getWorkbench().getEditorRegistry(); + IEditorDescriptor desc = registry.getDefaultEditor(filename); + if (desc == null) { + desc = registry.getDefaultEditor(); + } + IEditorInput input = new ExternalEditorInput(storage); + editor = page.openEditor(input, desc.getId()); + textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class); + + // If the storage provider is not ours, we can't guarantee read/write. + if (textEditor != null) { + IDocumentProvider documentProvider = textEditor.getDocumentProvider(); + if (!(documentProvider instanceof ExternalStorageDocumentProvider)) { + storage.setReadOnly(); + } + } + } + if (textEditor != null) { + // If a line number was given, go to it + if (line > 0) { + try { + line--; // document is 0 based + IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); + textEditor.selectAndReveal(document.getLineOffset(line), document.getLineLength(line)); + + } catch (BadLocationException e) { + // invalid text position -> do nothing + } + } + // If a string was given, go to it + if (findString != null) { + try { + IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); + int offset = document.search(0, findString, true, false, true); + textEditor.selectAndReveal(offset, findString.length()); + + } catch (BadLocationException e) { + // invalid text position -> do nothing + } + + } + } + } + + public void setLastEditorFile(IFile textEditor) { + this.fLastEditorFile = textEditor; + } + + /** + * @see org.eclipse.ui.plugin.AbstractUIPlugin#shutdown() */ - public static Display getStandardDisplay() { - Display display = Display.getCurrent(); - if (display == null) { - display = Display.getDefault(); + public void shutdown() throws CoreException { + super.shutdown(); + + // externalTools.shutDown(); + ColorManager.getDefault().dispose(); + + // save the information from the php index files if necessary + Collection collection = fIndexManagerMap.values(); + Iterator iterator = collection.iterator(); + IdentifierIndexManager indexManager = null; + while (iterator.hasNext()) { + indexManager = (IdentifierIndexManager) iterator.next(); + indexManager.writeFile(); } - return display; + } public void startup() throws CoreException { @@ -439,19 +579,4 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceCon }); } - /** - * @see org.eclipse.core.runtime.Plugin#shutdown() - */ - public void shutdown() throws CoreException { - // externalTools.shutDown(); - ColorManager.getDefault().dispose(); - } - - public void setLastEditorFile(IFile textEditor) { - this.fLastEditorFile = textEditor; - } - - public IFile getLastEditorFile() { - return fLastEditorFile; - } } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorActon.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorActon.java new file mode 100644 index 0000000..babeffc --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorActon.java @@ -0,0 +1,138 @@ +/********************************************************************** +Copyright (c) 2000, 2002 IBM Corp. and others. +All rights reserved. This program and the accompanying materials +are made available under the terms of the Common Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/cpl-v10.html + +Contributors: + Klaus Hartlage - www.eclipseproject.de +**********************************************************************/ +package net.sourceforge.phpeclipse.actions; + +import java.util.List; + +import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import net.sourceforge.phpeclipse.builder.IdentifierIndexManager; +import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation; +import net.sourceforge.phpeclipse.phpeditor.PHPEditor; +import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.TextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.graphics.Point; +import org.eclipse.ui.IEditorActionDelegate; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.actions.ActionDelegate; + +public class PHPOpenDeclarationEditorActon extends ActionDelegate implements IEditorActionDelegate { + + private IWorkbenchWindow fWindow; + private PHPEditor fEditor; + private IProject fProject; + + public void dispose() { + } + + public void init(IWorkbenchWindow window) { + this.fWindow = window; + } + + public void selectionChanged(IAction action, ISelection selection) { + if (!selection.isEmpty()) { + if (selection instanceof TextSelection) { + action.setEnabled(true); + } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) { + // + } + } + } + + public void run(IAction action) { + if (fEditor == null) { + IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor(); + if (targetEditor != null && (targetEditor instanceof PHPEditor)) { + fEditor = (PHPEditor) targetEditor; + } + } + if (fEditor != null) { + // determine the current Project from a (file-based) Editor + IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile(); + fProject = f.getProject(); +// System.out.println(fProject.toString()); + + ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection(); + IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); + int pos = selection.getOffset(); + String word = getPHPIdentifier(doc, pos); + // System.out.println(word); + if (word!=null && ! word.equals("")) { + IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject); + List list = indexManager.getLocations(word); + if (list!=null && list.size()>0) { + String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString(); + // TODO show all entries of the list in a dialog box + // at the moment allways the first entry will be opened + PHPIdentifierLocation location = (PHPIdentifierLocation)list.get(0); + String filename = workspaceLocation + location.getFilename(); +// System.out.println(filename); + try { + PHPeclipsePlugin.getDefault().openFileInTextEditor(filename, 0, word); + } catch (CoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + } + + public void setActiveEditor(IAction action, IEditorPart targetEditor) { + if (targetEditor != null && (targetEditor instanceof PHPEditor)) { + fEditor = (PHPEditor) targetEditor; + } + } + + // public static void openContextHelp(String word) { + // IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + // if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) { + // String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE), word }; + // MessageFormat form = new MessageFormat(store.getString(PHPHelpPlugin.PHP_CHM_COMMAND)); + // try { + // Runtime runtime = Runtime.getRuntime(); + // String command = form.format(arguments); + // + // runtime.exec(command); + // } catch (IOException e) { + // } + // } else { + // IHelp help = WorkbenchHelp.getHelpSupport(); + // if (help != null) { + // PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(word); + // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource); + // } else { + // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$ + // } + // } + // } + + private String getPHPIdentifier(IDocument doc, int pos) { + Point word = PHPWordExtractor.findWord(doc, pos); + if (word != null) { + try { + return doc.get(word.x, word.y); + } catch (BadLocationException e) { + } + } + return ""; + } +} diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ExternalEditorInput.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ExternalEditorInput.java new file mode 100644 index 0000000..0b08b11 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ExternalEditorInput.java @@ -0,0 +1,104 @@ +/* + * Created on 06.09.2003 + * + * To change the template for this generated file go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ +package net.sourceforge.phpeclipse.builder; + +import org.eclipse.core.resources.IStorage; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.IEditorRegistry; +import org.eclipse.ui.IPersistableElement; +import org.eclipse.ui.IStorageEditorInput; +import org.eclipse.ui.PlatformUI; + +/** + * An EditorInput for an external file. + */ +public class ExternalEditorInput implements IStorageEditorInput { + + IStorage externalFile; + + /** + * Two ExternalEditorInputs are equal if their IStorage's are equal. + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof ExternalEditorInput)) + return false; + ExternalEditorInput other = (ExternalEditorInput) obj; + return externalFile.equals(other.externalFile); + } + + /* + * @see IEditorInput#exists() + */ + public boolean exists() { + // External file can not be deleted + return true; + } + + /* + * @see IAdaptable#getAdapter(Class) + */ + public Object getAdapter(Class adapter) { + return null; + } + + /* + * @see IEditorInput#getContentType() + */ + public String getContentType() { + return externalFile.getFullPath().getFileExtension(); + } + + /* + * @see IEditorInput#getFullPath() + */ + public String getFullPath() { + return externalFile.getFullPath().toString(); + } + + /* + * @see IEditorInput#getImageDescriptor() + */ + public ImageDescriptor getImageDescriptor() { + IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry(); + return registry.getImageDescriptor(externalFile.getFullPath().getFileExtension()); + } + + /* + * @see IEditorInput#getName() + */ + public String getName() { + return externalFile.getName(); + } + + /* + * @see IEditorInput#getPersistable() + */ + public IPersistableElement getPersistable() { + return null; + } + + /* + * see IStorageEditorInput#getStorage() + */ + public IStorage getStorage() { + return externalFile; + } + + /* + * @see IEditorInput#getToolTipText() + */ + public String getToolTipText() { + return externalFile.getFullPath().toString(); + } + + public ExternalEditorInput(IStorage exFile) { + externalFile = exFile; + } +} diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ExternalStorageDocumentProvider.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ExternalStorageDocumentProvider.java new file mode 100644 index 0000000..ea8aae0 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ExternalStorageDocumentProvider.java @@ -0,0 +1,59 @@ +/* + * Created on 06.09.2003 + * + */ +package net.sourceforge.phpeclipse.builder; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import net.sourceforge.phpeclipse.PHPeclipsePlugin; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.text.IDocument; +import org.eclipse.ui.editors.text.StorageDocumentProvider; + +/** + * @author ed + * @version 1.0, May 19, 2003 + */ +public class ExternalStorageDocumentProvider extends StorageDocumentProvider { + + /* (non-Javadoc) + * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#doSaveDocument(org.eclipse.core.runtime.IProgressMonitor, java.lang.Object, org.eclipse.jface.text.IDocument, boolean) + */ + protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) + throws CoreException { + if (element instanceof ExternalEditorInput) { + ExternalEditorInput external = (ExternalEditorInput) element; + FileStorage storage = (FileStorage)external.getStorage(); + String encoding = getEncoding(element); + if (encoding == null) + encoding = getDefaultEncoding(); + try { + InputStream stream = new ByteArrayInputStream(document.get().getBytes(encoding)); + try { + // inform about the upcoming content change + fireElementStateChanging(element); + storage.setContents(stream, overwrite, true, monitor); + } catch (RuntimeException e) { + // inform about failure + fireElementStateChangeFailed(element); + throw e; + } + } catch (IOException e) { + IStatus s = new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.OK, e.getMessage(), e); + throw new CoreException(s); + } + + } else { + super.doSaveDocument(monitor, element, document, overwrite); + } + } + +} + diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/FileStorage.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/FileStorage.java new file mode 100644 index 0000000..1080cd9 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/FileStorage.java @@ -0,0 +1,125 @@ +/* + * Created on 06.09.2003 + * + * To change the template for this generated file go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ +package net.sourceforge.phpeclipse.builder; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import net.sourceforge.phpdt.internal.ui.util.StreamUtil; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; + +import org.eclipse.core.resources.IStorage; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.PlatformObject; +import org.eclipse.core.runtime.Status; + +/* + * (c) Copyright QNX Software Systems Ltd. 2002. + * All Rights Reserved. + */ + +/** + * + * @see IStorage + */ +public class FileStorage extends PlatformObject implements IStorage { + private boolean forceReadOnly; + private final IPath path; + private final File file; + + /** + * Two FileStorages are equal if their IPaths are equal. + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof FileStorage)) + return false; + FileStorage other = (FileStorage) obj; + return path.equals(other.path); + } + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IStorage#getContents() + */ + public InputStream getContents() throws CoreException { + try { + return new FileInputStream(file); + } catch (FileNotFoundException e) { + throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e)); + } + } + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IStorage#getFullPath() + */ + public IPath getFullPath() { + return this.path; + } + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IStorage#getName() + */ + public String getName() { + return this.path.lastSegment(); + } + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IStorage#isReadOnly() + */ + public boolean isReadOnly() { + return forceReadOnly || !file.canWrite(); + } + + /** + * Method FileStorage. + * @param path + */ + public FileStorage(IPath path) { + this.path = path; + this.file = path.toFile(); + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + return path.toOSString(); + } + + /** + * @param stream + * @param overwrite + * @param b + * @param monitor + */ + public void setContents(InputStream stream, boolean overwrite, boolean b, IProgressMonitor monitor) throws CoreException { + try { + StreamUtil.transferStreams(stream, new FileOutputStream(file)); + } catch (FileNotFoundException e) { + throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e)); + } catch (IOException e) { + throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e)); + } + } + + /** + * Some document providers (notably CompilationUnitDocumentProvider) + * can't handle read/write storage. + */ + public void setReadOnly() { + forceReadOnly = true; + } +} diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java new file mode 100644 index 0000000..dd2672b --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java @@ -0,0 +1,446 @@ +package net.sourceforge.phpeclipse.builder; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.StringTokenizer; + +import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; +import net.sourceforge.phpdt.core.compiler.InvalidInputException; +import net.sourceforge.phpdt.internal.compiler.parser.Scanner; +import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError; +import net.sourceforge.phpeclipse.mover.obfuscator.PHPIdentifier; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; + +/** + * Manages the identifer index information for a specific project + * + */ +public class IdentifierIndexManager { + + public class LineCreator implements ITerminalSymbols { + + private Scanner fScanner; + private int fToken; + + public LineCreator() { + fScanner = new Scanner(false, false); + } + /** + * gets the next token from input + */ + private void getNextToken() { + + try { + fToken = fScanner.getNextToken(); + if (Scanner.DEBUG) { + int currentEndPosition = fScanner.getCurrentTokenEndPosition(); + int currentStartPosition = fScanner.getCurrentTokenStartPosition(); + + System.out.print(currentStartPosition + "," + currentEndPosition + ": "); + System.out.println(fScanner.toStringAction(fToken)); + } + return; + } catch (InvalidInputException e) { + // ignore errors + } + fToken = TokenNameERROR; + } + + private void parseDeclarations(StringBuffer buf, boolean goBack) { + char[] ident; + int counter = 0; + + try { + while (fToken != TokenNameEOF && fToken != TokenNameERROR) { + if (fToken == TokenNamevar) { + getNextToken(); + if (fToken == TokenNameVariable) { + ident = fScanner.getCurrentIdentifierSource(); + buf.append("\tv"); + buf.append(ident); + + getNextToken(); + } + } else if (fToken == TokenNamefunction) { + getNextToken(); + if (fToken == TokenNameAND) { + getNextToken(); + } + if (fToken == TokenNameIdentifier) { + ident = fScanner.getCurrentIdentifierSource(); + buf.append("\tm"); + buf.append(ident); + getNextToken(); + parseDeclarations(buf, true); + } + } else if (fToken == TokenNameclass) { + getNextToken(); + if (fToken == TokenNameIdentifier) { + ident = fScanner.getCurrentIdentifierSource(); + buf.append("\tc"); + buf.append(ident); + getNextToken(); + + //skip tokens for classname, extends and others until we have the opening '{' + while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) { + getNextToken(); + } + parseDeclarations(buf, true); + } + } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) { + getNextToken(); + counter++; + } else if (fToken == TokenNameRBRACE) { + getNextToken(); + --counter; + if (counter == 0 && goBack) { + return; + } + } else { + getNextToken(); + } + } + } catch (SyntaxError e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void parseIdentifiers(char[] charArray, StringBuffer buf) { + char[] ident; + String identifier; + int counter = 0; + + fScanner.setSource(charArray); + fScanner.setPHPMode(false); + fToken = TokenNameEOF; + getNextToken(); + + try { + while (fToken != TokenNameEOF && fToken != TokenNameERROR) { + if (fToken == TokenNamefunction) { + getNextToken(); + if (fToken == TokenNameAND) { + getNextToken(); + } + if (fToken == TokenNameIdentifier) { + ident = fScanner.getCurrentIdentifierSource(); + buf.append("\tf"); + buf.append(ident); + getNextToken(); + parseDeclarations(buf, true); + } + } else if (fToken == TokenNameclass) { + getNextToken(); + if (fToken == TokenNameIdentifier) { + ident = fScanner.getCurrentIdentifierSource(); + buf.append("\tc"); + buf.append(ident); + getNextToken(); + + //skip fTokens for classname, extends and others until we have the opening '{' + while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) { + getNextToken(); + } + + parseDeclarations(buf, true); + + } + } else { + getNextToken(); + } + } + } catch (SyntaxError e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + private HashMap fFileMap; + private String fFilename; + private HashMap fIndentifierMap; + + public IdentifierIndexManager(String filename) { + fFilename = filename; + initialize(); + readFile(); + } + + /** + * Add the information for a given IFile resource + * + */ + public void addFile(IFile fileToParse) { + InputStream iStream; + LineCreator lineCreator = new LineCreator(); + try { + iStream = fileToParse.getContents(); + + StringBuffer buf = new StringBuffer(); + int c0; + try { + while ((c0 = iStream.read()) != (-1)) { + buf.append((char) c0); + } + } catch (IOException e) { + return; + } + + StringBuffer lineBuffer = new StringBuffer(); + // lineBuffer.append(fileToParse.getLocation().toString()); + lineBuffer.append(fileToParse.getFullPath().toString()); + int lineLength = lineBuffer.length(); + lineCreator.parseIdentifiers(buf.toString().toCharArray(), lineBuffer); + if (lineLength != lineBuffer.length()) { + addLine(lineBuffer.toString()); + } + } catch (CoreException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + /** + * Adds a line of the index file for function, class, class-method and class-variable names + * + * @param line + */ + private void addLine(String line) { + StringTokenizer tokenizer; + String phpFileName = null; + String token; + String identifier = null; + String classname = null; + PHPIdentifier phpIdentifier = null; + boolean tokenExists = false; + + tokenizer = new StringTokenizer(line, "\t"); + // first token contains the filename: + if (tokenizer.hasMoreTokens()) { + phpFileName = tokenizer.nextToken(); + //System.out.println(token); + } else { + return; + } + // all the other tokens are identifiers: + while (tokenizer.hasMoreTokens()) { + token = tokenizer.nextToken(); + //System.out.println(token); + switch (token.charAt(0)) { + case 'c' : // class name + identifier = token.substring(1); + classname = identifier; + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName); + break; + case 'f' : // function name + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName); + break; + case 'm' : //method inside a class + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname); + break; + case 'v' : // variable inside a class + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname); + break; + default : + identifier = null; + phpIdentifier = null; + classname = null; + } + if (identifier != null && phpIdentifier != null) { + tokenExists = true; + ArrayList list = (ArrayList) fIndentifierMap.get(identifier); + if (list == null) { + list = new ArrayList(); + list.add(phpIdentifier); + fIndentifierMap.put(identifier, list); + } else { + boolean flag = false; + for (int i = 0; i < list.size(); i++) { + if (list.get(i).equals(phpIdentifier)) { + flag = true; + break; + } + } + if (flag == false) { + list.add(phpIdentifier); + } + } + } + } + if (tokenExists) { + fFileMap.put(phpFileName, line); + } + } + + /** + * Change the information for a given IFile resource + * + */ + public void changeFile(IFile fileToParse) { + removeFile(fileToParse); + addFile(fileToParse); + } + + /** + * Get a list of all PHPIdentifierLocation object's associated with an identifier + * + * @param identifier + * @return + */ + public List getLocations(String identifier) { + return (List) fIndentifierMap.get(identifier); + } + + /** + * Initialize (i.e. clear) the current index information + * + */ + public void initialize() { + fIndentifierMap = new HashMap(); + fFileMap = new HashMap(); + } + + private void readFile() { + + FileReader fileReader; + try { + fileReader = new FileReader(fFilename); + + BufferedReader bufferedReader = new BufferedReader(fileReader); + + String line; + while (bufferedReader.ready()) { + // all entries for one file are in a line + // separated by tabs ! + line = bufferedReader.readLine(); + addLine(line); + } + + fileReader.close(); + } catch (FileNotFoundException e) { + // ignore this + // TODO DialogBox which asks the user if she/he likes to build new index? + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + /** + * Remove the information for a given IFile resource + * + */ + public void removeFile(IFile fileToParse) { + // String line = (String) fFileMap.get(fileToParse.getLocation().toString()); + String line = (String) fFileMap.get(fileToParse.getFullPath().toString()); + if (line != null) { + removeLine(line); + } + } + + /** + * Removes a line of the index file for function, class, class-method and class-variable names + * + * @param line + */ + private void removeLine(String line) { + StringTokenizer tokenizer; + String phpFileName = null; + String token; + String identifier = null; + String classname = null; + PHPIdentifier phpIdentifier = null; + boolean tokenExists = false; + + tokenizer = new StringTokenizer(line, "\t"); + // first token contains the filename: + if (tokenizer.hasMoreTokens()) { + phpFileName = tokenizer.nextToken(); + //System.out.println(token); + } else { + return; + } + // all the other tokens are identifiers: + while (tokenizer.hasMoreTokens()) { + token = tokenizer.nextToken(); + //System.out.println(token); + switch (token.charAt(0)) { + case 'c' : // class name + identifier = token.substring(1); + classname = identifier; + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName); + break; + case 'f' : // function name + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName); + break; + case 'm' : //method inside a class + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname); + break; + case 'v' : // variable inside a class + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname); + break; + default : + identifier = null; + phpIdentifier = null; + classname = null; + } + if (identifier != null && phpIdentifier != null) { + ArrayList list = (ArrayList) fIndentifierMap.get(identifier); + if (list == null) { + } else { + for (int i = 0; i < list.size(); i++) { + if (list.get(i).equals(phpIdentifier)) { + list.remove(i); + break; + } + } + if (list.size() == 0) { + fIndentifierMap.remove(identifier); + } + } + } + } + fFileMap.remove(phpFileName); + } + + /** + * Save the current index information in the projects index file + * + */ + public void writeFile() { + FileWriter fileWriter; + try { + fileWriter = new FileWriter(fFilename); + String line; + Collection collection = fFileMap.values(); + Iterator iterator = collection.iterator(); + while (iterator.hasNext()) { + line = (String) iterator.next(); + fileWriter.write(line + '\n'); + } + fileWriter.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/PHPIdentifierLocation.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/PHPIdentifierLocation.java new file mode 100644 index 0000000..f3fb463 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/PHPIdentifierLocation.java @@ -0,0 +1,60 @@ +package net.sourceforge.phpeclipse.builder; + +import net.sourceforge.phpeclipse.mover.obfuscator.PHPIdentifier; + +/** + * @author khartlage + * + */ +public class PHPIdentifierLocation extends PHPIdentifier { + private String fClassname; + private String fFilename; + + public PHPIdentifierLocation(String identifier, int type, String filename) { + this(identifier, type, filename, null); + } + + public PHPIdentifierLocation(String identifier, int type, String filename, String classname) { + super(identifier, type); + fFilename = filename; + fClassname = classname; + } + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) { + if (!(obj instanceof PHPIdentifierLocation)) { + return false; + } + return super.equals(obj)&&fFilename.equals(((PHPIdentifierLocation)obj).fFilename); + } + + /** + * @return + */ + public String getClassname() { + return fClassname; + } + + /** + * @return + */ + public String getFilename() { + return fFilename; + } + + /** + * @param string + */ + public void setClassname(String string) { + fClassname = string; + } + + /** + * @param string + */ + public void setFilename(String string) { + fFilename = string; + } + +} diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserBuilder.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserBuilder.java index 4b9babe..4fd5108 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserBuilder.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserBuilder.java @@ -1,9 +1,12 @@ package net.sourceforge.phpeclipse.builder; +import java.io.File; import java.util.Map; import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpeclipse.phpeditor.PHPParserAction; +import net.sourceforge.phpeclipse.resourcesview.PHPProject; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -35,8 +38,7 @@ public class ParserBuilder extends IncrementalProjectBuilder { /** * */ - protected IProject[] build(int kind, Map args, IProgressMonitor monitor) - throws CoreException { + protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException { monitor.beginTask("Parsing files", TOTAL_WORK); if (kind == IncrementalProjectBuilder.FULL_BUILD) { @@ -48,7 +50,7 @@ public class ParserBuilder extends IncrementalProjectBuilder { IResourceDelta delta = getDelta(getProject()); if (delta != null) { - delta.accept(new ParserVisitor(monitor)); + delta.accept(new ParserVisitor(getProject(), monitor)); } } @@ -62,8 +64,8 @@ public class ParserBuilder extends IncrementalProjectBuilder { * * @param iProject */ - public void processFull(IProject iProject, final IProgressMonitor monitor) { - + public void processFull(final IProject iProject, final IProgressMonitor monitor) { + final IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(iProject); // Create resource visitor logic IResourceVisitor myVisitor = new IResourceVisitor() { public boolean visit(IResource resource) throws CoreException { @@ -71,11 +73,14 @@ public class ParserBuilder extends IncrementalProjectBuilder { if (monitor.isCanceled()) { throw new OperationCanceledException(); } - if ((resource.getFileExtension() != null) - && PHPFileUtil.isPHPFile((IFile) resource)) { + if ((resource.getFileExtension() != null) && PHPFileUtil.isPHPFile((IFile) resource)) { monitor.worked(1); monitor.subTask("Parsing: " + resource.getFullPath()); + // check for parsing errors PHPParserAction.parseFile((IFile) resource); + // update indexfile for the project: + PHPProject nature = (PHPProject) iProject.getNature(PHPeclipsePlugin.PHP_NATURE_ID); + indexManager.addFile((IFile) resource); } } @@ -85,7 +90,14 @@ public class ParserBuilder extends IncrementalProjectBuilder { // Process the project using the visitor just created try { + +// if (iProject.hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) { +// thePHPProject = new PHPProject(); +// thePHPProject.setProject(iProject); +// } + indexManager.initialize(); iProject.accept(myVisitor); + indexManager.writeFile(); } catch (CoreException e) { e.printStackTrace(); } @@ -120,11 +132,7 @@ public class ParserBuilder extends IncrementalProjectBuilder { * *

*/ - public void setInitializationData( - IConfigurationElement config, - String propertyName, - Object data) - throws CoreException { + public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException { super.setInitializationData(config, propertyName, data); } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserVisitor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserVisitor.java index 5dbc801..01c5902 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserVisitor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/ParserVisitor.java @@ -1,9 +1,11 @@ package net.sourceforge.phpeclipse.builder; import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpeclipse.phpeditor.PHPParserAction; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceDelta; import org.eclipse.core.resources.IResourceDeltaVisitor; @@ -18,15 +20,18 @@ import org.eclipse.core.runtime.OperationCanceledException; */ public class ParserVisitor implements IResourceDeltaVisitor { final IProgressMonitor fMonitor; - public ParserVisitor(IProgressMonitor monitor) { + final IProject fProject; + public ParserVisitor(IProject iProject, IProgressMonitor monitor) { fMonitor = monitor; + fProject = iProject; } - - protected void checkCancel() { - if (fMonitor.isCanceled()) { - throw new OperationCanceledException(); - } - } + + protected void checkCancel() { + if (fMonitor.isCanceled()) { + throw new OperationCanceledException(); + } + } + /** * Visits the given resource delta. * @@ -38,33 +43,51 @@ public class ParserVisitor implements IResourceDeltaVisitor { IResource resource = delta.getResource(); int resourceType = resource.getType(); - checkCancel(); - + checkCancel(); + + final IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject); + switch (delta.getKind()) { case IResourceDelta.ADDED : if (resourceType == IResource.FILE) { - if ((resource.getFileExtension() != null) - && PHPFileUtil.isPHPFile((IFile) resource)) { - fMonitor.worked(1); - fMonitor.subTask("Parsing: " + resource.getFullPath()); + if ((resource.getFileExtension() != null) && PHPFileUtil.isPHPFile((IFile) resource)) { + fMonitor.worked(1); + fMonitor.subTask("Adding: " + resource.getFullPath()); + + // check for parsing errors PHPParserAction.parseFile((IFile) resource); + // update indexfile for the project: + indexManager.addFile((IFile) resource); } } break; case IResourceDelta.CHANGED : if (resourceType == IResource.FILE) { - if ((resource.getFileExtension() != null) - && PHPFileUtil.isPHPFile((IFile) resource)) { - fMonitor.worked(1); - fMonitor.subTask("Parsing: " + resource.getFullPath()); + if ((resource.getFileExtension() != null) && PHPFileUtil.isPHPFile((IFile) resource)) { + fMonitor.worked(1); + fMonitor.subTask("Changing: " + resource.getFullPath()); + + //check for parsing errors PHPParserAction.parseFile((IFile) resource); + // update indexfile for the project: + indexManager.changeFile((IFile) resource); } } break; case IResourceDelta.REMOVED : + if (resourceType == IResource.FILE) { + if ((resource.getFileExtension() != null) && PHPFileUtil.isPHPFile((IFile) resource)) { + fMonitor.worked(1); + fMonitor.subTask("Removing: " + resource.getFullPath()); + + // update indexfile for the project: + indexManager.removeFile((IFile) resource); + } } + break; + } return true; // carry on } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPAnalyzer.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPAnalyzer.java index bc7b049..a9c401d 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPAnalyzer.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPAnalyzer.java @@ -32,6 +32,7 @@ public class PHPAnalyzer extends DefaultMover implements ITerminalSymbols { * be synchronized */ protected byte[] bytes = new byte[1024]; + /** * Creates a PHPAnalyzer * @param console reports error to the PHPConsole diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPIdentifier.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPIdentifier.java index a097a2e..5c59457 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPIdentifier.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPIdentifier.java @@ -3,20 +3,17 @@ package net.sourceforge.phpeclipse.mover.obfuscator; /** * @author khartlage * - * To change this generated comment edit the template variable "typecomment": - * Window>Preferences>Java>ObfuscatorIgnores. - * To enable and disable the creation of type comments go to - * Window>Preferences>Java>Code Generation. */ public class PHPIdentifier { - + public final static int CLASS = 1; public final static int FUNCTION = 2; public final static int VARIABLE = 3; - + public final static int METHOD = 4; + private int fType; private String fIdentifier; - + public PHPIdentifier(String identifier, int type) { fType = type; fIdentifier = identifier; @@ -25,15 +22,15 @@ public class PHPIdentifier { public boolean isClass() { return fType == CLASS; } - + public boolean isFuncton() { return fType == FUNCTION; } - + public boolean isVariable() { return fType == VARIABLE; } - + public void setType(int fType) { this.fType = fType; } @@ -49,5 +46,16 @@ public class PHPIdentifier { public String getIdentifier() { return fIdentifier; } - + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) { + if (!(obj instanceof PHPIdentifier)) { + return false; + } + return ((PHPIdentifier) obj).fType == fType && + ((PHPIdentifier) obj).fIdentifier.equals(fIdentifier); + } + } 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 fbef0f3..f5cbca0 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java @@ -58,53 +58,53 @@ public class PHPParserAction extends TextEditorAction { */ public void run() { boolean phpFlag = false; - - // try { - fileToParse = getPHPFile(); - parseFile(fileToParse); + + // try { + fileToParse = getPHPFile(); + parseFile(fileToParse); } - public static void parseFile(IFile fileToParse) { - boolean phpFlag = false; - try { - - if (fileToParse == null) { - // should never happen - System.err.println("Error : no file in the editor"); - // should throw an exception - return; - } - String name = fileToParse.getName().toLowerCase(); - for (int i = 0; i"); - Iterator pathEntriesIterator = loadPathEntries.iterator(); + Iterator pathEntriesIterator = fLoadPathEntries.iterator(); while (pathEntriesIterator.hasNext()) { LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next(); @@ -221,8 +144,73 @@ public class PHPProject implements IProjectNature, PHPElement { return buffer.toString(); } + public IProject getProject() { + return fProject; + } + + protected IProject getProject(String name) { + return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name); + } + + public List getReferencedProjects() { + List referencedProjects = new ArrayList(); + + Iterator iterator = getLoadPathEntries().iterator(); + while (iterator.hasNext()) { + LoadPathEntry pathEntry = (LoadPathEntry) iterator.next(); + if (pathEntry.getType() == LoadPathEntry.TYPE_PROJECT) + referencedProjects.add(pathEntry.getProject()); + } + + return referencedProjects; + } + public IResource getUnderlyingResource() { return fProject; } + protected void loadLoadPathEntries() { + fLoadPathEntries = new ArrayList(); + + IFile loadPathsFile = getLoadPathEntriesFile(); + + XMLReader reader = null; + try { + reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); + reader.setContentHandler(getLoadPathEntriesContentHandler()); + reader.parse(new InputSource(loadPathsFile.getContents())); + } catch (Exception e) { + //the file is nonextant or unreadable + } + } + + public void removeLoadPathEntry(IProject anotherPHPProject) { + Iterator entries = getLoadPathEntries().iterator(); + while (entries.hasNext()) { + LoadPathEntry entry = (LoadPathEntry) entries.next(); + if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) { + getLoadPathEntries().remove(entry); + fScratched = true; + break; + } + } + } + + public void save() throws CoreException { + if (fScratched) { + InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes()); + IFile loadPathsFile = getLoadPathEntriesFile(); + if (!loadPathsFile.exists()) + loadPathsFile.create(xmlPath, true, null); + else + loadPathsFile.setContents(xmlPath, true, false, null); + + fScratched = false; + } + } + + public void setProject(IProject aProject) { + fProject = aProject; + } + }