|
From: <mir...@us...> - 2006-07-28 19:17:15
|
Revision: 1545 Author: mirkostocker Date: 2006-07-27 11:29:13 -0700 (Thu, 27 Jul 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1545&view=rev Log Message: ----------- Added a first version of the AST viewer plugin, I had some problems with automatically updating the view, so this currently needs to be done by hand (with the refresh icon). I also plan to make it configureable, so you could select the nodes you don't want to see (for example NewlineNode). Feel free to contact me if you have any suggestions for further functionality! Added Paths: ----------- trunk/org.rubypeople.rdt.astviewer/ trunk/org.rubypeople.rdt.astviewer/.classpath trunk/org.rubypeople.rdt.astviewer/.project trunk/org.rubypeople.rdt.astviewer/.settings/ trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.core.prefs trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.ui.prefs trunk/org.rubypeople.rdt.astviewer/META-INF/ trunk/org.rubypeople.rdt.astviewer/META-INF/MANIFEST.MF trunk/org.rubypeople.rdt.astviewer/bin/ trunk/org.rubypeople.rdt.astviewer/build.properties trunk/org.rubypeople.rdt.astviewer/icons/ trunk/org.rubypeople.rdt.astviewer/icons/ast_view.gif trunk/org.rubypeople.rdt.astviewer/icons/refresh.gif trunk/org.rubypeople.rdt.astviewer/plugin.xml trunk/org.rubypeople.rdt.astviewer/src/ trunk/org.rubypeople.rdt.astviewer/src/org/ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/Activator.java trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/AstView.java trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeObject.java trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeParent.java trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewContentProvider.java trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewLabelProvider.java Added: trunk/org.rubypeople.rdt.astviewer/.classpath =================================================================== --- trunk/org.rubypeople.rdt.astviewer/.classpath (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/.classpath 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry sourcepath="/org.jruby/src" kind="lib" path="/org.rubypeople.rdt.core/lib/jruby.jar"/> + <classpathentry kind="output" path="bin"/> +</classpath> Added: trunk/org.rubypeople.rdt.astviewer/.project =================================================================== --- trunk/org.rubypeople.rdt.astviewer/.project (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/.project 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.rubypeople.rdt.astviewer</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> Added: trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.core.prefs =================================================================== --- trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.core.prefs (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.core.prefs 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,12 @@ +#Thu Jul 27 01:45:30 CEST 2006 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 Added: trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.ui.prefs =================================================================== --- trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.ui.prefs (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/.settings/org.eclipse.jdt.ui.prefs 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,3 @@ +#Thu Jul 27 01:45:30 CEST 2006 +eclipse.preferences.version=1 +internal.default.compliance=default Added: trunk/org.rubypeople.rdt.astviewer/META-INF/MANIFEST.MF =================================================================== --- trunk/org.rubypeople.rdt.astviewer/META-INF/MANIFEST.MF (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/META-INF/MANIFEST.MF 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,19 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Astviewer Plug-in +Bundle-SymbolicName: org.rubypeople.rdt.astviewer; singleton:=true +Bundle-Version: 0.1.0 +Bundle-Activator: org.rubypeople.rdt.astviewer.Activator +Bundle-Localization: plugin +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime, + org.rubypeople.rdt.ui, + org.eclipse.core.resources, + org.eclipse.ui.ide, + org.eclipse.jface.text, + org.rubypeople.rdt.core, + org.eclipse.ui.editors, + org.eclipse.ui.workbench.texteditor +Eclipse-LazyStart: true +Export-Package: org.rubypeople.rdt.astviewer, + org.rubypeople.rdt.astviewer.views Added: trunk/org.rubypeople.rdt.astviewer/build.properties =================================================================== --- trunk/org.rubypeople.rdt.astviewer/build.properties (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/build.properties 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,11 @@ +source.. = src/ +output.. = bin/ +bin.includes = plugin.xml,\ + META-INF/,\ + .,\ + icons/ +src.includes = META-INF/,\ + src/,\ + plugin.xml,\ + icons/,\ + build.properties Added: trunk/org.rubypeople.rdt.astviewer/icons/ast_view.gif =================================================================== (Binary files differ) Property changes on: trunk/org.rubypeople.rdt.astviewer/icons/ast_view.gif ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/org.rubypeople.rdt.astviewer/icons/refresh.gif =================================================================== (Binary files differ) Property changes on: trunk/org.rubypeople.rdt.astviewer/icons/refresh.gif ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/org.rubypeople.rdt.astviewer/plugin.xml =================================================================== --- trunk/org.rubypeople.rdt.astviewer/plugin.xml (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/plugin.xml 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.2"?> +<plugin> + + <extension + point="org.eclipse.ui.views"> + <category + name="Ruby" + id="org.rubypeople.rdt.astviewer"> + </category> + <view + name="AST View" + icon="icons/ast_view.gif" + category="org.rubypeople.rdt.astviewer" + class="org.rubypeople.rdt.astviewer.views.AstView" + id="org.rubypeople.rdt.astviewer.views.AstView"> + </view> + </extension> + <extension + point="org.eclipse.ui.perspectiveExtensions"> + <perspectiveExtension + targetID="org.eclipse.ui.resourcePerspective"> + <view + ratio="0.5" + relative="org.eclipse.ui.views.TaskList" + relationship="right" + id="org.rubypeople.rdt.astviewer.views.AstView"> + </view> + </perspectiveExtension> + </extension> + +</plugin> Added: trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/Activator.java =================================================================== --- trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/Activator.java (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/Activator.java 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,89 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2006 Mirko Stocker <me...@mi...> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.rubypeople.rdt.astviewer; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.rubypeople.rdt.astviewer"; + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + plugin = this; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + + /** + * Returns an image descriptor for the image file at the given + * plug-in relative path + * + * @param path the path + * @return the image descriptor + */ + public static ImageDescriptor getImageDescriptor(String path) { + return imageDescriptorFromPlugin(PLUGIN_ID, path); + } +} Added: trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/AstView.java =================================================================== --- trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/AstView.java (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/AstView.java 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,263 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2006 Mirko Stocker <me...@mi...> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.rubypeople.rdt.astviewer.views; + +import java.io.InputStream; +import java.net.URL; + +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.IMenuListener; +import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.action.IToolBarManager; +import org.eclipse.jface.action.MenuManager; +import org.eclipse.jface.action.Separator; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.source.SourceViewer; +import org.eclipse.jface.viewers.DoubleClickEvent; +import org.eclipse.jface.viewers.IDoubleClickListener; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.SashForm; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.TreeItem; +import org.eclipse.ui.IActionBars; +import org.eclipse.ui.IWorkbenchActionConstants; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.DrillDownAdapter; +import org.eclipse.ui.part.ViewPart; +import org.jruby.ast.Node; +import org.rubypeople.rdt.internal.ui.rubyeditor.RubyEditor; + +public class AstView extends ViewPart { + private TreeViewer viewer; + private DrillDownAdapter drillDownAdapter; + private Action refreshAction; + private Action doubleClickAction; + private Action clickAction; + private ViewContentProvider viewContentProvider; + private SashForm sashForm; + private SourceViewer detailsViewer; + + public static final String PLUGIN_ID = "org.rubypeople.rdt.astviewer"; + public static final String ICON_PATH = "icons/"; + + public void createPartControl(Composite parent) { + sashForm = new SashForm(parent, SWT.NONE); + sashForm.setOrientation(SWT.VERTICAL); + + viewer = new TreeViewer(sashForm, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); + drillDownAdapter = new DrillDownAdapter(viewer); + viewContentProvider = new ViewContentProvider(getViewSite()); + viewer.setContentProvider(viewContentProvider); + viewer.setLabelProvider(new ViewLabelProvider()); + + viewer.setInput(getViewSite()); + makeActions(); + hookContextMenu(); + hookClickAction(); + hookDoubleClickAction(); + contributeToActionBars(); + //setupListeners(); + viewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS); + + detailsViewer= new SourceViewer(sashForm, null, SWT.V_SCROLL | SWT.H_SCROLL); + detailsViewer.setEditable(false); + detailsViewer.setDocument(new Document()); + Control control = detailsViewer.getControl(); + GridData gd = new GridData(GridData.FILL_BOTH); + control.setLayoutData(gd); + + sashForm.setWeights(new int[]{90, 10}); + } + +// Tried to automatically update the view, but could not figure out yet how to do it properly. +// private void setupListeners(){ +// +// final IPartListener partListener = new IPartListener(){ +// public void partActivated(IWorkbenchPart part) { +// partWasActivated(); +// } +// public void partBroughtToTop(IWorkbenchPart part) {} +// public void partClosed(IWorkbenchPart part) {} +// public void partDeactivated(IWorkbenchPart part) {} +// public void partOpened(IWorkbenchPart part) { +// partWasActivated(); +// } +// }; +// +// PlatformUI.getWorkbench().getActiveWorkbenchWindow().addPageListener(new IPageListener(){ +// +// public void pageActivated(final IWorkbenchPage page) { +// page.addPartListener(partListener); +// } +// +// public void pageClosed(IWorkbenchPage page) {} +// +// public void pageOpened(IWorkbenchPage page) { +// page.addPartListener(partListener); +// }}); +// } +// +// private void partWasActivated() { +// if(viewContentProvider.updateContent()) { +// viewer.setInput(getViewSite()); +// viewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS); +// } +// } + + private void hookContextMenu() { + MenuManager menuMgr = new MenuManager("#PopupMenu"); + menuMgr.setRemoveAllWhenShown(true); + menuMgr.addMenuListener(new IMenuListener() { + public void menuAboutToShow(IMenuManager manager) { + AstView.this.fillContextMenu(manager); + } + }); + Menu menu = menuMgr.createContextMenu(viewer.getControl()); + viewer.getControl().setMenu(menu); + getSite().registerContextMenu(menuMgr, viewer); + } + + private void contributeToActionBars() { + IActionBars bars = getViewSite().getActionBars(); + fillLocalPullDown(bars.getMenuManager()); + fillLocalToolBar(bars.getToolBarManager()); + } + + private void fillLocalPullDown(IMenuManager manager) { + manager.add(refreshAction); + } + + private void fillContextMenu(IMenuManager manager) { + manager.add(refreshAction); + drillDownAdapter.addNavigationActions(manager); + // Other plug-ins can contribute there actions here + manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); + } + + private void fillLocalToolBar(IToolBarManager manager) { + manager.add(refreshAction); + drillDownAdapter.addNavigationActions(manager); + } + + private void makeActions() { + refreshAction = new Action() { + public void run() { + viewContentProvider.forceUpdateContent(); + viewer.setInput(getViewSite()); + viewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS); + } + }; + + refreshAction.setText("Refresh the AST View"); + refreshAction.setToolTipText("Performs a complete refresh over the whole AST."); + refreshAction.setImageDescriptor(ImageDescriptor.createFromURL(getImageUrl("refresh.gif"))); + } + + private static URL getImageUrl(String relative) { + return Platform.find(Platform.getBundle(PLUGIN_ID), new Path(ICON_PATH + relative)); + } + + private void setSelection(RubyEditor editor, Node n) { + if (n == null) + return; + editor.selectAndReveal(n.getPosition().getStartOffset(), n.getPosition().getEndOffset() - n.getPosition().getStartOffset() + 1); + } + + private String formatedPosition(Node n) { + if(n == null) + return ""; + + StringBuilder posString = new StringBuilder(); + posString.append("Lines ["); + posString.append(n.getPosition().getStartLine()); + posString.append(":"); + posString.append(n.getPosition().getEndLine()); + posString.append("], Offset ["); + posString.append(n.getPosition().getStartOffset()); + posString.append(":"); + posString.append(n.getPosition().getEndOffset()); + posString.append("]"); + return posString.toString(); + } + + private void hookClickAction() { + + clickAction = new Action(){ + public void run() { + detailsViewer.getDocument().set(formatedPosition(getSelectedNode())); + } + }; + + viewer.addSelectionChangedListener(new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { + clickAction.run(); + } + }); + } + + private Node getSelectedNode() { + TreeItem[] selection = viewer.getTree().getSelection(); + if(selection.length <= 0) + return null; + return ((TreeObject) selection[0].getData()).getNode(); + } + + private RubyEditor getEditor() { + return (RubyEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); + } + + private void hookDoubleClickAction() { + + doubleClickAction = new Action(){ + public void run() { + setSelection(getEditor(), getSelectedNode()); + } + }; + + viewer.addDoubleClickListener(new IDoubleClickListener() { + public void doubleClick(DoubleClickEvent event) { + doubleClickAction.run(); + } + }); + } + + public void setFocus() { + viewer.getControl().setFocus(); + } +} \ No newline at end of file Added: trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeObject.java =================================================================== --- trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeObject.java (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeObject.java 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,59 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2006 Mirko Stocker <me...@mi...> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.rubypeople.rdt.astviewer.views; + +import org.eclipse.core.runtime.IAdaptable; +import org.jruby.ast.Node; + +class TreeObject implements IAdaptable { + private Node node; + private TreeParent parent; + + public Node getNode() { + return node; + } + public TreeObject(Node n) { + this.node = n; + } + public String getName() { + return node.getClass().toString(); + } + public void setParent(TreeParent parent) { + this.parent = parent; + } + public TreeParent getParent() { + return parent; + } + public String toString() { + return getName(); + } + public Object getAdapter(Class key) { + return null; + } +} \ No newline at end of file Added: trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeParent.java =================================================================== --- trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeParent.java (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/TreeParent.java 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,55 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2006 Mirko Stocker <me...@mi...> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.rubypeople.rdt.astviewer.views; + +import java.util.ArrayList; + +import org.jruby.ast.Node; + +class TreeParent extends TreeObject { + private ArrayList<TreeObject> children; + public TreeParent(Node n) { + super(n); + children = new ArrayList<TreeObject>(); + } + public void addChild(TreeObject child) { + children.add(child); + child.setParent(this); + } + public void removeChild(TreeObject child) { + children.remove(child); + child.setParent(null); + } + public TreeObject [] getChildren() { + return (TreeObject [])children.toArray(new TreeObject[children.size()]); + } + public boolean hasChildren() { + return children.size() > 0; + } +} \ No newline at end of file Added: trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewContentProvider.java =================================================================== --- trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewContentProvider.java (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewContentProvider.java 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,183 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2006 Mirko Stocker <me...@mi...> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.rubypeople.rdt.astviewer.views; + +import java.io.InputStreamReader; +import java.util.Iterator; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.IViewSite; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.jruby.ast.Node; +import org.jruby.common.NullWarnings; +import org.jruby.lexer.yacc.LexerSource; +import org.jruby.parser.DefaultRubyParser; +import org.jruby.parser.RubyParserConfiguration; +import org.jruby.parser.RubyParserPool; +import org.rubypeople.rdt.internal.ui.rubyeditor.RubyEditor; + +class ViewContentProvider implements IStructuredContentProvider, ITreeContentProvider { + private TreeParent invisibleRoot; + + private IViewSite viewSite; + + private RubyEditor editor; + + private DefaultRubyParser parser; + + public ViewContentProvider(IViewSite viewSite) { + this.viewSite = viewSite; + parser = RubyParserPool.getInstance().borrowParser(); + parser.setWarnings(new NullWarnings()); + parser.init(new RubyParserConfiguration()); + } + + public void inputChanged(Viewer v, Object oldInput, Object newInput) { + } + + public void dispose() { + RubyParserPool.getInstance().returnParser(parser); + } + + protected IFile getFile() + { + return ((IFileEditorInput) editor.getEditorInput()).getFile(); + } + + public Object[] getElements(Object parent) { + if (parent.equals(viewSite)) { + if (invisibleRoot == null) + initialize(); + return getChildren(invisibleRoot); + } + return getChildren(parent); + } + + public Object getParent(Object child) { + if (child instanceof TreeObject) { + return ((TreeObject) child).getParent(); + } + return null; + } + + public Object[] getChildren(Object parent) { + if (parent instanceof TreeParent) { + return ((TreeParent) parent).getChildren(); + } + return new Object[0]; + } + + public boolean hasChildren(Object parent) { + if (parent instanceof TreeParent) + return ((TreeParent) parent).hasChildren(); + return false; + } + + public Node getRootNode() { + LexerSource lexerSource; + try { + lexerSource = new LexerSource(getFile().getName(), new InputStreamReader(getFile().getContents())); + return parser.parse(lexerSource).getAST(); + } catch (CoreException e) { + e.printStackTrace(); + } + return null; + } + + private void initialize() { + updateContent(); + } + + private void buildTree(TreeParent parent, Node n) { + + if(n == null) + return; + + //we could just skip newline and scope nodes here. should make it configureable +// if(n instanceof NewlineNode) +// n = ((NewlineNode) n).getNextNode(); +// else if(n instanceof ScopeNode) +// n = ((ScopeNode) n).getBodyNode(); + + if(n.childNodes().size() <= 0) { + parent.addChild(new TreeObject(n)); + } else { + TreeParent new_parent = new TreeParent(n); + parent.addChild(new_parent); + Iterator it = n.childNodes().iterator(); + while(it.hasNext()) { + buildTree(new_parent, (Node) it.next()); + } + } + } + + private IWorkbenchPage getActiveWorkbenchPage() { + return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + } + + private boolean editorChanged() { + if(getActiveWorkbenchPage() == null) + return false; + return getEditor() != (RubyEditor) getActiveWorkbenchPage().getActiveEditor(); + } + + public boolean forceUpdateContent() { + if(getActiveWorkbenchPage() == null) + return false; + + setEditor((RubyEditor) getActiveWorkbenchPage().getActiveEditor()); + + invisibleRoot = new TreeParent(null); + buildTree(invisibleRoot, getRootNode()); + + return true; + } + + public boolean updateContent() { + + if(editorChanged()) + return forceUpdateContent(); + else + return false; + } + + private void setEditor(RubyEditor editor) { + this.editor = editor; + } + + private RubyEditor getEditor() { + return editor; + } +} Added: trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewLabelProvider.java =================================================================== --- trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewLabelProvider.java (rev 0) +++ trunk/org.rubypeople.rdt.astviewer/src/org/rubypeople/rdt/astviewer/views/ViewLabelProvider.java 2006-07-27 18:29:13 UTC (rev 1545) @@ -0,0 +1,53 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2006 Mirko Stocker <me...@mi...> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.rubypeople.rdt.astviewer.views; + +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.swt.graphics.Image; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; + +class ViewLabelProvider extends LabelProvider { + + public String getText(Object obj) { + String text = obj.toString().substring(obj.toString().lastIndexOf('.') + 1); + + if (text.endsWith("Node")) + text = text.substring(0, text.length() - "Node".length()); + + return text; + } + + public Image getImage(Object obj) { + String imageKey = ISharedImages.IMG_OBJ_ELEMENT; + if (obj instanceof TreeParent) + imageKey = ISharedImages.IMG_OBJ_FOLDER; + return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey); + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |