2 * (c) Winterwell 2010 and ThinkTank Mathematics 2007
4 package winterwell.markdown.editors;
6 import org.eclipse.jface.text.IDocument;
7 import org.eclipse.jface.text.IRegion;
8 import org.eclipse.jface.text.ITextHover;
9 import org.eclipse.jface.text.ITextViewer;
10 import org.eclipse.jface.text.Region;
12 import winterwell.markdown.StringMethods;
13 import winterwell.utils.containers.Pair;
20 public class MDTextHover implements ITextHover //, ITextHoverExtension
24 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
26 public String getHoverInfo(ITextViewer textViewer, IRegion region) {
28 IDocument doc = textViewer.getDocument();
29 String text = doc.get(region.getOffset(), region.getLength());
30 return "<b>"+text+"</b>";
31 } catch (Exception e) {
37 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
39 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
41 IDocument doc = textViewer.getDocument();
42 int line = doc.getLineOfOffset(offset);
43 int lineOffset = doc.getLineOffset(line);
44 int lineLength = doc.getLineLength(line);
45 String text = doc.get(lineOffset, lineLength);
46 // Look for image tags
47 Pair<Integer> altRegion;
48 Pair<Integer> urlRegion =
49 StringMethods.findEnclosingRegion(text, offset-lineOffset, '(', ')');
50 if (urlRegion==null) {
51 altRegion = StringMethods.findEnclosingRegion(text, offset-lineOffset, '[', ']');
52 if (altRegion == null) return null;
53 urlRegion = StringMethods.findEnclosingRegion(text, altRegion.second, '(', ')');
55 altRegion = StringMethods.findEnclosingRegion(text, urlRegion.first-1, '[', ']');
57 if (urlRegion==null || altRegion==null) return null;
58 // Is it an image link?
59 if (text.charAt(altRegion.first-1) != '!') return null;
60 Region r = new Region(urlRegion.first+1+lineOffset, urlRegion.second-urlRegion.first-2);
62 } catch (Exception ex) {
67 // public IInformationControlCreator getHoverControlCreator() {
68 // return new IInformationControlCreator() {
69 // public IInformationControl createInformationControl(Shell parent) {
70 // int style= fIsFocusable ? SWT.V_SCROLL | SWT.H_SCROLL : SWT.NONE;
72 // if (BrowserInformationControl.isAvailable(parent)) {
73 // final int shellStyle= SWT.TOOL | (fIsFocusable ? SWT.RESIZE : SWT.NO_TRIM);
74 // return new BrowserInformationControl(parent, shellStyle, style, null);
76 // return new DefaultInformationControl(parent, style, new HTMLTextPresenter());