Skip to content

Commit

Permalink
add support of editor selections #33
Browse files Browse the repository at this point in the history
fixes #33

Signed-off-by: Andre Bossert <[email protected]>
  • Loading branch information
anb0s committed Sep 27, 2020
1 parent 99e6aac commit 84e124a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.variables,
org.eclipse.jdt.core;resolution:=optional,
org.eclipse.cdt.ui;resolution:=optional,
org.eclipse.cdt.core;resolution:=optional
org.eclipse.cdt.core;resolution:=optional,
org.eclipse.ui.workbench.texteditor,
org.eclipse.jface.text
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: target/easyshell-library.jar
Expand Down
45 changes: 44 additions & 1 deletion plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
Expand All @@ -25,11 +27,14 @@
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.osgi.framework.Bundle;

import de.anbos.eclipse.easyshell.plugin.actions.ActionDelegate;
Expand All @@ -49,8 +54,24 @@ static public ISelection getResourceSelection(IWorkbenchPart part) {
ISelection selection = null;
if (part != null) {
if (part instanceof IEditorPart) {
// get file from part (can be only one)
Resource file = getResource((IEditorPart)part);
if (file != null) {
ITextEditor editor = null;
if (part instanceof ITextEditor) {
editor = (ITextEditor) part;
} else if (part instanceof MultiPageEditorPart) {
Object page = ((MultiPageEditorPart) part).getSelectedPage();
if (page instanceof ITextEditor) {
editor = (ITextEditor) page;
}
}
if (editor != null) {
ITextSelection sel = (ITextSelection)editor.getSelectionProvider().getSelection();
String text = sel.getText();
selection = getSelectionFromText(file, text);
}
// fallback to the selected part if no selection
if ((selection == null) && (file != null)) {
selection = new StructuredSelection(file);
}
} else {
Expand All @@ -64,6 +85,28 @@ static public ISelection getResourceSelection(IWorkbenchPart part) {
return selection;
}

private static ISelection getSelectionFromText(Resource partFile, String text) {
ISelection selection = null;
if (text != null && !text.isEmpty()) {
String lines[] = text.split("\\r?\\n");
List<Resource> resources = new ArrayList<Resource>();
for (String line : lines) {
String fileName = line.trim();
if (fileName != null && !fileName.isEmpty()) {
File file = new File(fileName);
if (!file.isAbsolute()) {
file = new File(partFile.getParentLocation(), file.getPath());
}
resources.add(new Resource(file));
}
}
if (!resources.isEmpty()) {
selection = new StructuredSelection(resources);
}
}
return selection;
}

static public Resource getResource(Object myObj) {
Object object = null;

Expand Down

0 comments on commit 84e124a

Please sign in to comment.