]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/winterwell.markdown/src/winterwell/markdown/editors/MDTextHover.java
Tycho compilation changes for SVN version also.
[simantics/platform.git] / bundles / winterwell.markdown / src / winterwell / markdown / editors / MDTextHover.java
1 /**
2  * (c) Winterwell 2010 and ThinkTank Mathematics 2007
3  */
4 package winterwell.markdown.editors;
5
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;
11
12 import winterwell.markdown.StringMethods;
13 import winterwell.utils.containers.Pair;
14
15 /**
16  * 
17  *
18  * @author daniel
19  */
20 public class MDTextHover implements ITextHover //, ITextHoverExtension 
21 {
22
23         /* (non-Javadoc)
24          * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
25          */
26         public String getHoverInfo(ITextViewer textViewer, IRegion region) {
27                 try {
28                         IDocument doc = textViewer.getDocument();
29                         String text = doc.get(region.getOffset(), region.getLength());
30                         return "<b>"+text+"</b>";
31                 } catch (Exception e) {
32                         return null;
33                 }               
34         }
35
36         /* (non-Javadoc)
37          * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
38          */
39         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
40                 try {
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, '(', ')');
54                         } else {
55                                 altRegion = StringMethods.findEnclosingRegion(text, urlRegion.first-1, '[', ']');
56                         }
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);
61                         return r;
62                 } catch (Exception ex) {
63                         return null;
64                 }
65         }
66
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;
71 //                              
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);
75 //                  }
76 //                              return new DefaultInformationControl(parent, style, new HTMLTextPresenter());
77 //                      }                                               
78 //              };
79 //      }
80
81 }