1 package org.simantics.document.ui;
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;
18 public class CSSEditorTextHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {
20 private CSSTextEditorEnvironment sclTextEditorEnvironment;
22 public CSSEditorTextHover(ISourceViewer sourceViewer, CSSTextEditorEnvironment sclTextEditorEnvironment) {
23 this.sclTextEditorEnvironment = sclTextEditorEnvironment;
27 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
32 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
33 return findWord(textViewer.getDocument(), offset);
36 private static IRegion findWord(IDocument document, int offset) {
46 c= document.getChar(pos);
47 if (!Character.isUnicodeIdentifierPart(c))
55 int length= document.getLength();
57 while (pos < length) {
58 c= document.getChar(pos);
59 if (!Character.isUnicodeIdentifierPart(c))
66 } catch (BadLocationException x) {
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);
75 return new Region(start + 1, end - start - 1);
82 public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
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) {
96 public IInformationControlCreator getHoverControlCreator() {
97 AbstractReusableInformationControlCreator creator = new AbstractReusableInformationControlCreator() {
100 protected IInformationControl doCreateInformationControl(Shell parent) {
101 return new DefaultInformationControl(parent);