Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Kurtakov2017-04-13 05:36:35 -0400
committerAlexander Kurtakov2017-04-13 05:36:35 -0400
commit48bb69ab06aaeda08e08d220faa677ff002db461 (patch)
tree5cbafa1a996b82cfd241a551c302d09ede4b0d70
parentdffc6039d803e16e64902f716632d4c424b345c4 (diff)
downloadorg.eclipse.dltk.sh-48bb69ab06aaeda08e08d220faa677ff002db461.tar.gz
org.eclipse.dltk.sh-48bb69ab06aaeda08e08d220faa677ff002db461.tar.xz
org.eclipse.dltk.sh-48bb69ab06aaeda08e08d220faa677ff002db461.zip
Bug 515239 - Add hyperlink for sourced files
Add hyperlink for 'source other.sh' in the same project. Works for './other.sh', 'dir/other.sh', '../other.sh' and etc. cases. Change-Id: I47c11935d0652071cfc8bdd4edbc5798eb718c9d Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
-rw-r--r--plugins/org.eclipse.dltk.sh.ui/META-INF/MANIFEST.MF4
-rw-r--r--plugins/org.eclipse.dltk.sh.ui/plugin.xml15
-rw-r--r--plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/editor/ShellSourceViewerConfiguration.java11
-rw-r--r--plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/hyperlink/SourceHyperlinkDetector.java145
4 files changed, 173 insertions, 2 deletions
diff --git a/plugins/org.eclipse.dltk.sh.ui/META-INF/MANIFEST.MF b/plugins/org.eclipse.dltk.sh.ui/META-INF/MANIFEST.MF
index c9af866..fe71697 100644
--- a/plugins/org.eclipse.dltk.sh.ui/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.dltk.sh.ui/META-INF/MANIFEST.MF
@@ -16,7 +16,9 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.debug.ui,
org.eclipse.search,
org.eclipse.linuxtools.man.core,
- org.eclipse.core.expressions
+ org.eclipse.core.expressions,
+ org.eclipse.ui.workbench.texteditor,
+ org.eclipse.ui.ide
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Vendor: Eclipse DLTK
diff --git a/plugins/org.eclipse.dltk.sh.ui/plugin.xml b/plugins/org.eclipse.dltk.sh.ui/plugin.xml
index 875e7c8..6147366 100644
--- a/plugins/org.eclipse.dltk.sh.ui/plugin.xml
+++ b/plugins/org.eclipse.dltk.sh.ui/plugin.xml
@@ -287,6 +287,21 @@
</command>
</menuContribution>
</extension>
+ <extension
+ point="org.eclipse.ui.workbench.texteditor.hyperlinkDetectors">
+ <hyperlinkDetector
+ id="org.eclipse.dltk.sh.SourceHyperlinkDetector"
+ targetId="org.eclipse.dltk.shell.code"
+ class="org.eclipse.dltk.sh.internal.ui.text.hyperlink.SourceHyperlinkDetector"
+ name="Shell source Hyperlink Detector">
+ </hyperlinkDetector>
+ </extension>
+ <extension
+ point="org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets">
+ <target id="org.eclipse.dltk.shell.code" name="Shell script editor">
+ <context type="org.eclipse.dltk.sh.internal.ui.editor.ShellScriptEditor" />
+ </target>
+ </extension>
<extension point="org.eclipse.dltk.ui.folding">
<structureProvider
diff --git a/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/editor/ShellSourceViewerConfiguration.java b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/editor/ShellSourceViewerConfiguration.java
index 9a36002..bb2b8fb 100644
--- a/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/editor/ShellSourceViewerConfiguration.java
+++ b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/editor/ShellSourceViewerConfiguration.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2016 Red Hat Inc. and others.
+ * Copyright (c) 2009, 2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -11,7 +11,9 @@
package org.eclipse.dltk.sh.internal.ui.editor;
import java.util.ArrayList;
+import java.util.Map;
+import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.dltk.sh.internal.ui.Activator;
import org.eclipse.dltk.sh.internal.ui.IShellColorConstants;
import org.eclipse.dltk.sh.internal.ui.ShellContentAssistPreference;
@@ -230,4 +232,11 @@ public class ShellSourceViewerConfiguration extends ScriptSourceViewerConfigurat
return IShellPartitions.CONTENT_TYPES;
}
+ @Override
+ protected Map<String, IAdaptable> getHyperlinkDetectorTargets(final ISourceViewer sourceViewer) {
+ final Map<String, IAdaptable> targets = super.getHyperlinkDetectorTargets(sourceViewer);
+ targets.put("org.eclipse.dltk.shell.code", getEditor()); //$NON-NLS-1$
+ return targets;
+ }
+
}
diff --git a/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/hyperlink/SourceHyperlinkDetector.java b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/hyperlink/SourceHyperlinkDetector.java
new file mode 100644
index 0000000..a2f2235
--- /dev/null
+++ b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/hyperlink/SourceHyperlinkDetector.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Red Hat Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Alexander Kurtakov - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.dltk.sh.internal.ui.text.hyperlink;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
+import org.eclipse.jface.text.hyperlink.IHyperlink;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.part.FileEditorInput;
+import org.eclipse.ui.texteditor.ITextEditor;
+
+public class SourceHyperlinkDetector extends AbstractHyperlinkDetector {
+
+ private static final String SOURCE = "source";
+
+ @Override
+ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion inputRegion,
+ boolean canShowMultipleHyperlinks) {
+ if ((inputRegion == null) || (textViewer == null)) {
+ return null;
+ }
+ try {
+ final IDocument doc = textViewer.getDocument();
+ final int lineNumber = doc.getLineOfOffset(inputRegion.getOffset());
+ final IRegion region = doc.getLineInformation(lineNumber);
+ final String line = doc.get(region.getOffset(), region.getLength());
+ if ((line != null) && (line.length() != 0)) {
+ final IHyperlink link = checkLine(region.getOffset(), line);
+ if (link != null) {
+ return new IHyperlink[] { link };
+ }
+ }
+ } catch (BadLocationException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /**
+ * Checks if the specified line matches the <code>require</code> statement
+ *
+ * @param offset
+ * @param line
+ */
+ private IHyperlink checkLine(int offset, final String line) {
+ int begin = 0;
+ int end = line.length();
+ while ((begin < end) && Character.isWhitespace(line.charAt(begin))) {
+ ++begin;
+ }
+ while ((begin < end) && Character.isWhitespace(line.charAt(end - 1))) {
+ --end;
+ }
+ if (((begin + SOURCE.length()) < end) && line.startsWith(SOURCE, begin)) {
+ begin += SOURCE.length();
+ while ((begin < end) && Character.isWhitespace(line.charAt(begin))) {
+ ++begin;
+ }
+ if (((begin + 2) < end) && (line.charAt(begin) == '(') && (line.charAt(end - 1) == ')')) {
+ ++begin;
+ --end;
+ while ((begin < end) && Character.isWhitespace(line.charAt(begin))) {
+ ++begin;
+ }
+ while ((begin < end) && Character.isWhitespace(line.charAt(end - 1))) {
+ --end;
+ }
+ }
+
+ return createLink(offset, line, begin, end);
+ }
+ return null;
+ }
+
+ /**
+ * Creates {@link IHyperlink} instance.
+ *
+ * This method is extracted to simplify testing.
+ *
+ * @param offset
+ * @param line
+ * @param begin
+ * @param end
+ * @return
+ */
+ private IHyperlink createLink(int offset, final String line, int begin, int end) {
+ final ITextEditor editor = getAdapter(ITextEditor.class);
+ if (editor != null) {
+ final String requiredFile = line.substring(begin, end);
+ final Region region = new Region(offset + begin, end - begin);
+ IContainer container = ((FileEditorInput) getAdapter(ITextEditor.class).getEditorInput()).getFile()
+ .getParent();
+ IResource resourceToOpen = container.findMember(requiredFile);
+ if ((resourceToOpen != null) && (resourceToOpen.getType() == IResource.FILE)) {
+ return new IHyperlink() {
+
+ @Override
+ public void open() {
+ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ try {
+ IDE.openEditor(page, (IFile) resourceToOpen);
+ } catch (PartInitException e) {
+ }
+
+ }
+
+ @Override
+ public String getTypeLabel() {
+ return "Shell: source";
+ }
+
+ @Override
+ public String getHyperlinkText() {
+ return requiredFile;
+ }
+
+ @Override
+ public IRegion getHyperlinkRegion() {
+ return region;
+ }
+ };
+ }
+ }
+ return null;
+ }
+
+}

Back to the top