From a4e2ae0c593c66b2f22dfcd4d7399c0e82c427e8 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sun, 24 Sep 2023 11:00:57 +0800 Subject: [PATCH 1/2] Add menu item --- microbat/plugin.xml | 878 ++++++++++++++++++++++---------------------- 1 file changed, 444 insertions(+), 434 deletions(-) diff --git a/microbat/plugin.xml b/microbat/plugin.xml index f6afdaa74..7017a271f 100644 --- a/microbat/plugin.xml +++ b/microbat/plugin.xml @@ -1,434 +1,444 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From f4fc01b2348db0cf83b33a2fd37922a748c40fb1 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Thu, 28 Sep 2023 20:58:01 +0800 Subject: [PATCH 2/2] Add basic project detection logic --- microbat/plugin.xml | 7 +- .../handler/StartDebugRightClickHandler.java | 166 ++++++++++++++++++ 2 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 microbat/src/main/microbat/handler/StartDebugRightClickHandler.java diff --git a/microbat/plugin.xml b/microbat/plugin.xml index 7017a271f..452bcefc6 100644 --- a/microbat/plugin.xml +++ b/microbat/plugin.xml @@ -169,6 +169,11 @@ id="microbat.stopdebugpilot" name="Stop Debug Pilot"> + + @@ -372,7 +377,7 @@ allPopups="false" locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions"> diff --git a/microbat/src/main/microbat/handler/StartDebugRightClickHandler.java b/microbat/src/main/microbat/handler/StartDebugRightClickHandler.java new file mode 100644 index 000000000..5623f7202 --- /dev/null +++ b/microbat/src/main/microbat/handler/StartDebugRightClickHandler.java @@ -0,0 +1,166 @@ +package microbat.handler; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.internal.core.PackageFragment; +import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.ISelectionService; +import org.eclipse.ui.internal.Workbench; + +import microbat.util.Settings; + +/** + * Handler for right click + * @author Gabau + * + */ +public class StartDebugRightClickHandler extends AbstractHandler { + + // basic data class for execution context + class RightClickHandlerExecutionContext { + private final IProject contextProject; + private final String entryPath; + private final String entryMethod; + + private final boolean isJunit = false; + // doesn't appear to have a method of detecting + // this though + public RightClickHandlerExecutionContext( + IProject contextProject, String entryPath, String entryMethod) { + this.contextProject = contextProject; + this.entryPath = entryPath; + this.entryMethod = entryMethod; + } + + public String getEntryPath() { + return entryPath; + } + + public String getEntryMethod() { + return entryMethod; + } + + public IProject getContextProject() { + return contextProject; + } + + public void updateSettings() { + Settings.launchClass = this.entryPath; + Settings.isRunTest = this.isJunit; + Settings.projectName = this.getContextProject().getName(); + + } + } + + // todo: update to have multiple sub menus where we can run main vs run junit + // todo: do not use the settings to deal with + // the method handling -> deal with it programmatically + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + // TODO Auto-generated method stub + IProject currentProject = getCurrentProject(); + System.out.println(currentProject); + String prevProject = Settings.projectName; + String prevLaunchClass = Settings.launchClass; + boolean prevTestSetting = Settings.isRunTest; + // set the settings to run the selected project + + RightClickHandlerExecutionContext ctxt = getContext(); + ctxt.updateSettings(); +// new StartDebugHandler().execute(event); + + Settings.projectName = prevProject; + Settings.launchClass = prevLaunchClass; + Settings.isRunTest = prevTestSetting; + + + + return null; + } + + // basic mvp -> run with the main function for the + // current project + // do this by modifying microbat config + // then run to run the specified code + public RightClickHandlerExecutionContext getContext() { + ISelectionService selectionService = + Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService(); + + ISelection selection = selectionService.getSelection(); + String launchClass = null; + String entryPath = null; + IProject project = null; + if(selection instanceof IStructuredSelection) { + Object element = ((IStructuredSelection)selection).getFirstElement(); + + if (element instanceof IResource) { + project= ((IResource)element).getProject(); + } else if (element instanceof PackageFragmentRootContainer) { + PackageFragmentRootContainer rootContainer = ((PackageFragmentRootContainer) element); + IJavaProject jProject = rootContainer.getJavaProject(); + + project = jProject.getProject(); + } else if (element instanceof IJavaElement) { + IJavaElement eJavaElement = ((IJavaElement)element); + IJavaProject jProject= eJavaElement.getJavaProject(); + PackageFragment parent = (PackageFragment) eJavaElement.getParent(); + if (parent == null) { + entryPath = eJavaElement.getElementName() + .substring(0, eJavaElement.getElementName().length() - 5); + } else { + StringBuilder stringBuilder = new StringBuilder(); + for (String s: parent.names) { + stringBuilder.append(s); + stringBuilder.append('.'); + } + stringBuilder.append(eJavaElement.getElementName() + .substring(0, eJavaElement.getElementName().length() - 5)); + entryPath = stringBuilder.toString(); + } + + + + project = jProject.getProject(); + } + } + return new RightClickHandlerExecutionContext(project, entryPath, null); + } + + + // only gets the current project -> need to + // get the method information to perform + // run as + public static IProject getCurrentProject(){ + ISelectionService selectionService = + Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService(); + + ISelection selection = selectionService.getSelection(); + + IProject project = null; + if(selection instanceof IStructuredSelection) { + Object element = ((IStructuredSelection)selection).getFirstElement(); + + if (element instanceof IResource) { + project= ((IResource)element).getProject(); + } else if (element instanceof PackageFragmentRootContainer) { + PackageFragmentRootContainer rootContainer = ((PackageFragmentRootContainer) element); + IJavaProject jProject = rootContainer.getJavaProject(); + + project = jProject.getProject(); + } else if (element instanceof IJavaElement) { + IJavaProject jProject= ((IJavaElement)element).getJavaProject(); + project = jProject.getProject(); + } + } + return project; + } + + +}