]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSEditorTextHover.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSEditorTextHover.java
1 package org.simantics.document.ui;\r
2 \r
3 import org.eclipse.jface.text.AbstractReusableInformationControlCreator;\r
4 import org.eclipse.jface.text.BadLocationException;\r
5 import org.eclipse.jface.text.DefaultInformationControl;\r
6 import org.eclipse.jface.text.IDocument;\r
7 import org.eclipse.jface.text.IInformationControl;\r
8 import org.eclipse.jface.text.IInformationControlCreator;\r
9 import org.eclipse.jface.text.IRegion;\r
10 import org.eclipse.jface.text.ITextHover;\r
11 import org.eclipse.jface.text.ITextHoverExtension;\r
12 import org.eclipse.jface.text.ITextHoverExtension2;\r
13 import org.eclipse.jface.text.ITextViewer;\r
14 import org.eclipse.jface.text.Region;\r
15 import org.eclipse.jface.text.source.ISourceViewer;\r
16 import org.eclipse.swt.widgets.Shell;\r
17 \r
18 public class CSSEditorTextHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {\r
19 \r
20     private CSSTextEditorEnvironment sclTextEditorEnvironment;\r
21     \r
22     public CSSEditorTextHover(ISourceViewer sourceViewer, CSSTextEditorEnvironment sclTextEditorEnvironment) {\r
23         this.sclTextEditorEnvironment = sclTextEditorEnvironment;\r
24     }\r
25 \r
26     @Override\r
27     public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {\r
28         return null;\r
29     }\r
30 \r
31     @Override\r
32     public IRegion getHoverRegion(ITextViewer textViewer, int offset) {\r
33         return findWord(textViewer.getDocument(), offset);\r
34     }\r
35     \r
36     private static IRegion findWord(IDocument document, int offset) {\r
37         int start= -2;\r
38         int end= -1;\r
39 \r
40         try {\r
41 \r
42             int pos= offset;\r
43             char c;\r
44 \r
45             while (pos >= 0) {\r
46                 c= document.getChar(pos);\r
47                 if (!Character.isUnicodeIdentifierPart(c))\r
48                     break;\r
49                 --pos;\r
50             }\r
51 \r
52             start= pos;\r
53 \r
54             pos= offset;\r
55             int length= document.getLength();\r
56 \r
57             while (pos < length) {\r
58                 c= document.getChar(pos);\r
59                 if (!Character.isUnicodeIdentifierPart(c))\r
60                     break;\r
61                 ++pos;\r
62             }\r
63 \r
64             end= pos;\r
65 \r
66         } catch (BadLocationException x) {\r
67         }\r
68 \r
69         if (start >= -1 && end > -1) {\r
70             if (start == offset && end == offset)\r
71                 return new Region(offset, 0);\r
72             else if (start == offset)\r
73                 return new Region(start, end - start);\r
74             else\r
75                 return new Region(start + 1, end - start - 1);\r
76         }\r
77 \r
78         return null;\r
79     }\r
80 \r
81     @Override\r
82     public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {\r
83         String info = null;\r
84         try {\r
85             IDocument document = textViewer.getDocument();\r
86             String text = document.get(hoverRegion.getOffset(), hoverRegion.getLength());\r
87             sclTextEditorEnvironment.updateEnvironment(document);\r
88             info = sclTextEditorEnvironment.getHoverInfo(text);\r
89         } catch (BadLocationException e) {\r
90             \r
91         }\r
92         return info;\r
93     }\r
94 \r
95     @Override\r
96     public IInformationControlCreator getHoverControlCreator() {\r
97         AbstractReusableInformationControlCreator creator = new AbstractReusableInformationControlCreator() {\r
98             \r
99             @Override\r
100             protected IInformationControl doCreateInformationControl(Shell parent) {\r
101                 return new DefaultInformationControl(parent);\r
102             }\r
103         };\r
104         return creator;\r
105     }\r
106 \r
107 }\r