]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSEditorTextHover.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSEditorTextHover.java
diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSEditorTextHover.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSEditorTextHover.java
new file mode 100644 (file)
index 0000000..445c2a1
--- /dev/null
@@ -0,0 +1,107 @@
+package org.simantics.document.ui;\r
+\r
+import org.eclipse.jface.text.AbstractReusableInformationControlCreator;\r
+import org.eclipse.jface.text.BadLocationException;\r
+import org.eclipse.jface.text.DefaultInformationControl;\r
+import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.IInformationControl;\r
+import org.eclipse.jface.text.IInformationControlCreator;\r
+import org.eclipse.jface.text.IRegion;\r
+import org.eclipse.jface.text.ITextHover;\r
+import org.eclipse.jface.text.ITextHoverExtension;\r
+import org.eclipse.jface.text.ITextHoverExtension2;\r
+import org.eclipse.jface.text.ITextViewer;\r
+import org.eclipse.jface.text.Region;\r
+import org.eclipse.jface.text.source.ISourceViewer;\r
+import org.eclipse.swt.widgets.Shell;\r
+\r
+public class CSSEditorTextHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {\r
+\r
+    private CSSTextEditorEnvironment sclTextEditorEnvironment;\r
+    \r
+    public CSSEditorTextHover(ISourceViewer sourceViewer, CSSTextEditorEnvironment sclTextEditorEnvironment) {\r
+        this.sclTextEditorEnvironment = sclTextEditorEnvironment;\r
+    }\r
+\r
+    @Override\r
+    public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public IRegion getHoverRegion(ITextViewer textViewer, int offset) {\r
+        return findWord(textViewer.getDocument(), offset);\r
+    }\r
+    \r
+    private static IRegion findWord(IDocument document, int offset) {\r
+        int start= -2;\r
+        int end= -1;\r
+\r
+        try {\r
+\r
+            int pos= offset;\r
+            char c;\r
+\r
+            while (pos >= 0) {\r
+                c= document.getChar(pos);\r
+                if (!Character.isUnicodeIdentifierPart(c))\r
+                    break;\r
+                --pos;\r
+            }\r
+\r
+            start= pos;\r
+\r
+            pos= offset;\r
+            int length= document.getLength();\r
+\r
+            while (pos < length) {\r
+                c= document.getChar(pos);\r
+                if (!Character.isUnicodeIdentifierPart(c))\r
+                    break;\r
+                ++pos;\r
+            }\r
+\r
+            end= pos;\r
+\r
+        } catch (BadLocationException x) {\r
+        }\r
+\r
+        if (start >= -1 && end > -1) {\r
+            if (start == offset && end == offset)\r
+                return new Region(offset, 0);\r
+            else if (start == offset)\r
+                return new Region(start, end - start);\r
+            else\r
+                return new Region(start + 1, end - start - 1);\r
+        }\r
+\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {\r
+        String info = null;\r
+        try {\r
+            IDocument document = textViewer.getDocument();\r
+            String text = document.get(hoverRegion.getOffset(), hoverRegion.getLength());\r
+            sclTextEditorEnvironment.updateEnvironment(document);\r
+            info = sclTextEditorEnvironment.getHoverInfo(text);\r
+        } catch (BadLocationException e) {\r
+            \r
+        }\r
+        return info;\r
+    }\r
+\r
+    @Override\r
+    public IInformationControlCreator getHoverControlCreator() {\r
+        AbstractReusableInformationControlCreator creator = new AbstractReusableInformationControlCreator() {\r
+            \r
+            @Override\r
+            protected IInformationControl doCreateInformationControl(Shell parent) {\r
+                return new DefaultInformationControl(parent);\r
+            }\r
+        };\r
+        return creator;\r
+    }\r
+\r
+}\r