comparison 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
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
1 /*******************************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.text.DefaultTextHover;
14
15 import dwt.dwthelper.utils;
16
17 import java.util.Iterator;
18
19 import dwtx.core.runtime.Assert;
20 import dwtx.jface.text.source.Annotation;
21 import dwtx.jface.text.source.IAnnotationModel;
22 import dwtx.jface.text.source.ISourceViewer;
23 import dwtx.jface.text.source.ISourceViewerExtension2;
24
25 /**
26 * Standard implementation of {@link dwtx.jface.text.ITextHover}.
27 *
28 * @since 3.2
29 */
30 public class DefaultTextHover : ITextHover {
31
32 /** This hover's source viewer */
33 private ISourceViewer fSourceViewer;
34
35 /**
36 * Creates a new annotation hover.
37 *
38 * @param sourceViewer this hover's annotation model
39 */
40 public DefaultTextHover(ISourceViewer sourceViewer) {
41 Assert.isNotNull(sourceViewer);
42 fSourceViewer= sourceViewer;
43 }
44
45 /*
46 * @see dwtx.jface.text.ITextHover#getHoverInfo(dwtx.jface.text.ITextViewer, dwtx.jface.text.IRegion)
47 */
48 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
49 IAnnotationModel model= getAnnotationModel(fSourceViewer);
50 if (model is null)
51 return null;
52
53 Iterator e= model.getAnnotationIterator();
54 while (e.hasNext()) {
55 Annotation a= (Annotation) e.next();
56 if (isIncluded(a)) {
57 Position p= model.getPosition(a);
58 if (p !is null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
59 String msg= a.getText();
60 if (msg !is null && msg.trim().length() > 0)
61 return msg;
62 }
63 }
64 }
65
66 return null;
67 }
68
69 /*
70 * @see dwtx.jface.text.ITextHover#getHoverRegion(dwtx.jface.text.ITextViewer, int)
71 */
72 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
73 return findWord(textViewer.getDocument(), offset);
74 }
75
76 /**
77 * Tells whether the annotation should be included in
78 * the computation.
79 *
80 * @param annotation the annotation to test
81 * @return <code>true</code> if the annotation is included in the computation
82 */
83 protected bool isIncluded(Annotation annotation) {
84 return true;
85 }
86
87 private IAnnotationModel getAnnotationModel(ISourceViewer viewer) {
88 if (viewer instanceof ISourceViewerExtension2) {
89 ISourceViewerExtension2 extension= (ISourceViewerExtension2) viewer;
90 return extension.getVisualAnnotationModel();
91 }
92 return viewer.getAnnotationModel();
93 }
94
95 private IRegion findWord(IDocument document, int offset) {
96 int start= -2;
97 int end= -1;
98
99 try {
100
101 int pos= offset;
102 char c;
103
104 while (pos >= 0) {
105 c= document.getChar(pos);
106 if (!Character.isUnicodeIdentifierPart(c))
107 break;
108 --pos;
109 }
110
111 start= pos;
112
113 pos= offset;
114 int length= document.getLength();
115
116 while (pos < length) {
117 c= document.getChar(pos);
118 if (!Character.isUnicodeIdentifierPart(c))
119 break;
120 ++pos;
121 }
122
123 end= pos;
124
125 } catch (BadLocationException x) {
126 }
127
128 if (start >= -1 && end > -1) {
129 if (start is offset && end is offset)
130 return new Region(offset, 0);
131 else if (start is offset)
132 return new Region(start, end - start);
133 else
134 return new Region(start + 1, end - start - 1);
135 }
136
137 return null;
138 }
139 }