diff dwtx/jface/text/DefaultTextHover.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/DefaultTextHover.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,139 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.text.DefaultTextHover;
+
+import dwt.dwthelper.utils;
+
+import java.util.Iterator;
+
+import dwtx.core.runtime.Assert;
+import dwtx.jface.text.source.Annotation;
+import dwtx.jface.text.source.IAnnotationModel;
+import dwtx.jface.text.source.ISourceViewer;
+import dwtx.jface.text.source.ISourceViewerExtension2;
+
+/**
+ * Standard implementation of {@link dwtx.jface.text.ITextHover}.
+ * 
+ * @since 3.2
+ */
+public class DefaultTextHover : ITextHover {
+
+    /** This hover's source viewer */
+    private ISourceViewer fSourceViewer;
+
+    /**
+     * Creates a new annotation hover.
+     * 
+     * @param sourceViewer this hover's annotation model
+     */
+    public DefaultTextHover(ISourceViewer sourceViewer)  {
+        Assert.isNotNull(sourceViewer);
+        fSourceViewer= sourceViewer;
+    }
+    
+    /*
+     * @see dwtx.jface.text.ITextHover#getHoverInfo(dwtx.jface.text.ITextViewer, dwtx.jface.text.IRegion)
+     */
+    public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
+        IAnnotationModel model= getAnnotationModel(fSourceViewer);
+        if (model is null)
+            return null;
+        
+        Iterator e= model.getAnnotationIterator();
+        while (e.hasNext()) {
+            Annotation a= (Annotation) e.next();
+            if (isIncluded(a)) {
+                Position p= model.getPosition(a);
+                if (p !is null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
+                    String msg= a.getText();
+                    if (msg !is null && msg.trim().length() > 0)
+                        return msg;
+                }
+            }
+        }
+        
+        return null;
+    }
+
+    /*
+     * @see dwtx.jface.text.ITextHover#getHoverRegion(dwtx.jface.text.ITextViewer, int)
+     */
+    public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
+        return findWord(textViewer.getDocument(), offset);
+    }
+    
+    /**
+     * Tells whether the annotation should be included in
+     * the computation.
+     * 
+     * @param annotation the annotation to test
+     * @return <code>true</code> if the annotation is included in the computation
+     */
+    protected bool isIncluded(Annotation annotation) {
+        return true;
+    }
+    
+    private IAnnotationModel getAnnotationModel(ISourceViewer viewer) {
+        if (viewer instanceof ISourceViewerExtension2) {
+            ISourceViewerExtension2 extension= (ISourceViewerExtension2) viewer;
+            return extension.getVisualAnnotationModel();
+        }
+        return viewer.getAnnotationModel();
+    }
+    
+    private IRegion findWord(IDocument document, int offset) {
+        int start= -2;
+        int end= -1;
+
+        try {
+
+            int pos= offset;
+            char c;
+
+            while (pos >= 0) {
+                c= document.getChar(pos);
+                if (!Character.isUnicodeIdentifierPart(c))
+                    break;
+                --pos;
+            }
+
+            start= pos;
+
+            pos= offset;
+            int length= document.getLength();
+
+            while (pos < length) {
+                c= document.getChar(pos);
+                if (!Character.isUnicodeIdentifierPart(c))
+                    break;
+                ++pos;
+            }
+
+            end= pos;
+
+        } catch (BadLocationException x) {
+        }
+
+        if (start >= -1 && end > -1) {
+            if (start is offset && end is offset)
+                return new Region(offset, 0);
+            else if (start is offset)
+                return new Region(start, end - start);
+            else
+                return new Region(start + 1, end - start - 1);
+        }
+
+        return null;
+    }
+}