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