From c8db5a1f507b6ddc9556b6b801d6707e06520c37 Mon Sep 17 00:00:00 2001 From: khartlage Date: Sun, 6 Jun 2004 18:30:04 +0000 Subject: [PATCH] Open PHP "include" relative to the projects root inside the editor (right mouse click) --- .../actions/PHPOpenDeclarationEditorAction.java | 155 +++++++++++++++++ .../actions/PHPOpenDeclarationEditorActon.java | 155 ----------------- .../actions/PHPOpenIncludeEditorAction.java | 178 ++++++++++++++++++++ 3 files changed, 333 insertions(+), 155 deletions(-) create mode 100644 net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorAction.java delete mode 100644 net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorActon.java create mode 100644 net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenIncludeEditorAction.java diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorAction.java new file mode 100644 index 0000000..a582f1f --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorAction.java @@ -0,0 +1,155 @@ +/********************************************************************** +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.jface.viewers.LabelProvider; +import org.eclipse.jface.window.Window; +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; +import org.eclipse.ui.dialogs.ListSelectionDialog; +import org.eclipse.ui.internal.dialogs.ListContentProvider; + +public class PHPOpenDeclarationEditorAction 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(); + // System.out.println(selection.getText()); + 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 always the first entry will be opened + if (list.size() > 1) { + ListSelectionDialog listSelectionDialog = + new ListSelectionDialog( + PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(), + list, + new ListContentProvider(), + new LabelProvider(), + "Select the resources to open."); + listSelectionDialog.setTitle("Multiple declarations found"); + if (listSelectionDialog.open() == Window.OK) { + Object[] locations = listSelectionDialog.getResult(); + if (locations != null) { + try { + for (int i = 0; i < locations.length; i++) { + PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i]; + String filename = workspaceLocation + location.getFilename(); + // System.out.println(filename); + if (location.getOffset() >= 0) { + PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length()); + } else { + PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word); + } + } + } catch (CoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } else { + try { + PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0); + String filename = workspaceLocation + location.getFilename(); + // System.out.println(filename); + if (location.getOffset() >= 0) { + PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length()); + } else { + PHPeclipsePlugin.getDefault().openFileAndFindString(filename, 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; + } + } + + 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/actions/PHPOpenDeclarationEditorActon.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorActon.java deleted file mode 100644 index 4d2e8e5..0000000 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenDeclarationEditorActon.java +++ /dev/null @@ -1,155 +0,0 @@ -/********************************************************************** -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.jface.viewers.LabelProvider; -import org.eclipse.jface.window.Window; -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; -import org.eclipse.ui.dialogs.ListSelectionDialog; -import org.eclipse.ui.internal.dialogs.ListContentProvider; - -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(); - // System.out.println(selection.getText()); - 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 always the first entry will be opened - if (list.size() > 1) { - ListSelectionDialog listSelectionDialog = - new ListSelectionDialog( - PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(), - list, - new ListContentProvider(), - new LabelProvider(), - "Select the resources to open."); - listSelectionDialog.setTitle("Multiple declarations found"); - if (listSelectionDialog.open() == Window.OK) { - Object[] locations = listSelectionDialog.getResult(); - if (locations != null) { - try { - for (int i = 0; i < locations.length; i++) { - PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i]; - String filename = workspaceLocation + location.getFilename(); - // System.out.println(filename); - if (location.getOffset() >= 0) { - PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length()); - } else { - PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word); - } - } - } catch (CoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - } else { - try { - PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0); - String filename = workspaceLocation + location.getFilename(); - // System.out.println(filename); - if (location.getOffset() >= 0) { - PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length()); - } else { - PHPeclipsePlugin.getDefault().openFileAndFindString(filename, 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; - } - } - - 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/actions/PHPOpenIncludeEditorAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenIncludeEditorAction.java new file mode 100644 index 0000000..1dcf6eb --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenIncludeEditorAction.java @@ -0,0 +1,178 @@ +/******************************************************************************* + * 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 net.sourceforge.phpdt.internal.compiler.parser.Scanner; +import net.sourceforge.phpeclipse.phpeditor.PHPEditor; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.Path; +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.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.actions.ActionDelegate; +import org.eclipse.ui.ide.IDE; + +public class PHPOpenIncludeEditorAction 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) { + // + } + } + } + private IWorkbenchPage getActivePage() { + IWorkbenchWindow workbenchWindow = fEditor.getEditorSite() + .getWorkbenchWindow(); + IWorkbenchPage page = workbenchWindow.getActivePage(); + return page; + } + public IContainer getWorkingLocation(IFileEditorInput editorInput) { + if (editorInput == null || editorInput.getFile() == null) { + return null; + } + return editorInput.getFile().getParent(); + } + private IFile getWikiFile(IProject project, IFileEditorInput editorInput, String relativeFilename) { +// IContainer container = getWorkingLocation(editorInput); +// String fullPath = project.getLocation().toString(); + Path path = new Path(relativeFilename); + IFile file = project.getFile(path); + return file; + } + + 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(); + // System.out.println(selection.getText()); + String relativeFilename = getPHPIncludeText(doc, pos); + // System.out.println(word); + if (relativeFilename != null && !relativeFilename.equals("")) { + + IFile file = getWikiFile(fProject, (IFileEditorInput) fEditor + .getEditorInput(), relativeFilename); + try { + // createNewFileIfNeeded(file); + // if + // (WikiPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) + // { + // saveIfNeeded(); + // getActivePage().reuseEditor(reusableEditor, new + // FileEditorInput(file)); + // } else { + IDE.openEditor(getActivePage(), file, true); + // redrawText(); + // } + } catch (Exception e) { + // WikiPlugin.getDefault().logAndReport(WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE), + // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), + // e); + } + + } + } + } + + public void setActiveEditor(IAction action, IEditorPart targetEditor) { + if (targetEditor != null && (targetEditor instanceof PHPEditor)) { + fEditor = (PHPEditor) targetEditor; + } + } + + private String getPHPIncludeText(IDocument doc, int pos) { + Point word = null; + int start = -1; + int end = -1; + + try { + + int position = pos; + char character; + + while (position >= 0) { + character = doc.getChar(position); + if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n')) + break; + --position; + } + + start = position; + + position = pos; + int length = doc.getLength(); + + while (position < length) { + character = doc.getChar(position); + if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n')) + break; + ++position; + } + + start++; + end = position; + + if (end > start) + word = new Point(start, end - start); + + } catch (BadLocationException x) { + } + + if (word != null) { + try { + return doc.get(word.x, word.y); + } catch (BadLocationException e) { + } + } + return ""; + } +} \ No newline at end of file -- 1.7.1