X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Fwinterwell.markdown%2Fsrc%2Fwinterwell%2Fmarkdown%2Feditors%2FMDTextHover.java;fp=bundles%2Fwinterwell.markdown%2Fsrc%2Fwinterwell%2Fmarkdown%2Feditors%2FMDTextHover.java;h=04377a622aa36e544b6d84560bce71314e76f46f;hb=2531cdf245f42bce854d43f4d49a23983c79db96;hp=0000000000000000000000000000000000000000;hpb=857dbc869796d772864327ce02f19dc252b159fc;p=simantics%2Fplatform.git
diff --git a/bundles/winterwell.markdown/src/winterwell/markdown/editors/MDTextHover.java b/bundles/winterwell.markdown/src/winterwell/markdown/editors/MDTextHover.java
new file mode 100644
index 000000000..04377a622
--- /dev/null
+++ b/bundles/winterwell.markdown/src/winterwell/markdown/editors/MDTextHover.java
@@ -0,0 +1,81 @@
+/**
+ * (c) Winterwell 2010 and ThinkTank Mathematics 2007
+ */
+package winterwell.markdown.editors;
+
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextHover;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.Region;
+
+import winterwell.markdown.StringMethods;
+import winterwell.utils.containers.Pair;
+
+/**
+ *
+ *
+ * @author daniel
+ */
+public class MDTextHover implements ITextHover //, ITextHoverExtension
+{
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
+ */
+ public String getHoverInfo(ITextViewer textViewer, IRegion region) {
+ try {
+ IDocument doc = textViewer.getDocument();
+ String text = doc.get(region.getOffset(), region.getLength());
+ return ""+text+"";
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
+ */
+ public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
+ try {
+ IDocument doc = textViewer.getDocument();
+ int line = doc.getLineOfOffset(offset);
+ int lineOffset = doc.getLineOffset(line);
+ int lineLength = doc.getLineLength(line);
+ String text = doc.get(lineOffset, lineLength);
+ // Look for image tags
+ Pair altRegion;
+ Pair urlRegion =
+ StringMethods.findEnclosingRegion(text, offset-lineOffset, '(', ')');
+ if (urlRegion==null) {
+ altRegion = StringMethods.findEnclosingRegion(text, offset-lineOffset, '[', ']');
+ if (altRegion == null) return null;
+ urlRegion = StringMethods.findEnclosingRegion(text, altRegion.second, '(', ')');
+ } else {
+ altRegion = StringMethods.findEnclosingRegion(text, urlRegion.first-1, '[', ']');
+ }
+ if (urlRegion==null || altRegion==null) return null;
+ // Is it an image link?
+ if (text.charAt(altRegion.first-1) != '!') return null;
+ Region r = new Region(urlRegion.first+1+lineOffset, urlRegion.second-urlRegion.first-2);
+ return r;
+ } catch (Exception ex) {
+ return null;
+ }
+ }
+
+// public IInformationControlCreator getHoverControlCreator() {
+// return new IInformationControlCreator() {
+// public IInformationControl createInformationControl(Shell parent) {
+// int style= fIsFocusable ? SWT.V_SCROLL | SWT.H_SCROLL : SWT.NONE;
+//
+// if (BrowserInformationControl.isAvailable(parent)) {
+// final int shellStyle= SWT.TOOL | (fIsFocusable ? SWT.RESIZE : SWT.NO_TRIM);
+// return new BrowserInformationControl(parent, shellStyle, style, null);
+// }
+// return new DefaultInformationControl(parent, style, new HTMLTextPresenter());
+// }
+// };
+// }
+
+}