comparison dwtx/jface/text/rules/DefaultDamagerRepairer.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) 2000, 2006 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
14 module dwtx.jface.text.rules.DefaultDamagerRepairer;
15
16 import dwt.dwthelper.utils;
17
18
19
20
21
22 import dwt.DWT;
23 import dwt.custom.StyleRange;
24 import dwtx.core.runtime.Assert;
25 import dwtx.jface.text.BadLocationException;
26 import dwtx.jface.text.DocumentEvent;
27 import dwtx.jface.text.IDocument;
28 import dwtx.jface.text.IRegion;
29 import dwtx.jface.text.ITypedRegion;
30 import dwtx.jface.text.Region;
31 import dwtx.jface.text.TextAttribute;
32 import dwtx.jface.text.TextPresentation;
33 import dwtx.jface.text.presentation.IPresentationDamager;
34 import dwtx.jface.text.presentation.IPresentationRepairer;
35
36
37 /**
38 * A standard implementation of a syntax driven presentation damager
39 * and presentation repairer. It uses a token scanner to scan
40 * the document and to determine its damage and new text presentation.
41 * The tokens returned by the scanner are supposed to return text attributes
42 * as their data.
43 *
44 * @see ITokenScanner
45 * @since 2.0
46 */
47 public class DefaultDamagerRepairer : IPresentationDamager, IPresentationRepairer {
48
49
50 /** The document this object works on */
51 protected IDocument fDocument;
52 /** The scanner it uses */
53 protected ITokenScanner fScanner;
54 /** The default text attribute if non is returned as data by the current token */
55 protected TextAttribute fDefaultTextAttribute;
56
57 /**
58 * Creates a damager/repairer that uses the given scanner and returns the given default
59 * text attribute if the current token does not carry a text attribute.
60 *
61 * @param scanner the token scanner to be used
62 * @param defaultTextAttribute the text attribute to be returned if non is specified by the current token,
63 * may not be <code>null</code>
64 *
65 * @deprecated use DefaultDamagerRepairer(ITokenScanner) instead
66 */
67 public DefaultDamagerRepairer(ITokenScanner scanner, TextAttribute defaultTextAttribute) {
68
69 Assert.isNotNull(defaultTextAttribute);
70
71 fScanner= scanner;
72 fDefaultTextAttribute= defaultTextAttribute;
73 }
74
75 /**
76 * Creates a damager/repairer that uses the given scanner. The scanner may not be <code>null</code>
77 * and is assumed to return only token that carry text attributes.
78 *
79 * @param scanner the token scanner to be used, may not be <code>null</code>
80 */
81 public DefaultDamagerRepairer(ITokenScanner scanner) {
82
83 Assert.isNotNull(scanner);
84
85 fScanner= scanner;
86 fDefaultTextAttribute= new TextAttribute(null);
87 }
88
89 /*
90 * @see IPresentationDamager#setDocument(IDocument)
91 * @see IPresentationRepairer#setDocument(IDocument)
92 */
93 public void setDocument(IDocument document) {
94 fDocument= document;
95 }
96
97
98 //---- IPresentationDamager
99
100 /**
101 * Returns the end offset of the line that contains the specified offset or
102 * if the offset is inside a line delimiter, the end offset of the next line.
103 *
104 * @param offset the offset whose line end offset must be computed
105 * @return the line end offset for the given offset
106 * @exception BadLocationException if offset is invalid in the current document
107 */
108 protected int endOfLineOf(int offset) throws BadLocationException {
109
110 IRegion info= fDocument.getLineInformationOfOffset(offset);
111 if (offset <= info.getOffset() + info.getLength())
112 return info.getOffset() + info.getLength();
113
114 int line= fDocument.getLineOfOffset(offset);
115 try {
116 info= fDocument.getLineInformation(line + 1);
117 return info.getOffset() + info.getLength();
118 } catch (BadLocationException x) {
119 return fDocument.getLength();
120 }
121 }
122
123 /*
124 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, bool)
125 */
126 public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, bool documentPartitioningChanged) {
127
128 if (!documentPartitioningChanged) {
129 try {
130
131 IRegion info= fDocument.getLineInformationOfOffset(e.getOffset());
132 int start= Math.max(partition.getOffset(), info.getOffset());
133
134 int end= e.getOffset() + (e.getText() is null ? e.getLength() : e.getText().length());
135
136 if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
137 // optimize the case of the same line
138 end= info.getOffset() + info.getLength();
139 } else
140 end= endOfLineOf(end);
141
142 end= Math.min(partition.getOffset() + partition.getLength(), end);
143 return new Region(start, end - start);
144
145 } catch (BadLocationException x) {
146 }
147 }
148
149 return partition;
150 }
151
152 //---- IPresentationRepairer
153
154 /*
155 * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
156 */
157 public void createPresentation(TextPresentation presentation, ITypedRegion region) {
158
159 if (fScanner is null) {
160 // will be removed if deprecated constructor will be removed
161 addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute);
162 return;
163 }
164
165 int lastStart= region.getOffset();
166 int length= 0;
167 bool firstToken= true;
168 IToken lastToken= Token.UNDEFINED;
169 TextAttribute lastAttribute= getTokenTextAttribute(lastToken);
170
171 fScanner.setRange(fDocument, lastStart, region.getLength());
172
173 while (true) {
174 IToken token= fScanner.nextToken();
175 if (token.isEOF())
176 break;
177
178 TextAttribute attribute= getTokenTextAttribute(token);
179 if (lastAttribute !is null && lastAttribute.equals(attribute)) {
180 length += fScanner.getTokenLength();
181 firstToken= false;
182 } else {
183 if (!firstToken)
184 addRange(presentation, lastStart, length, lastAttribute);
185 firstToken= false;
186 lastToken= token;
187 lastAttribute= attribute;
188 lastStart= fScanner.getTokenOffset();
189 length= fScanner.getTokenLength();
190 }
191 }
192
193 addRange(presentation, lastStart, length, lastAttribute);
194 }
195
196 /**
197 * Returns a text attribute encoded in the given token. If the token's
198 * data is not <code>null</code> and a text attribute it is assumed that
199 * it is the encoded text attribute. It returns the default text attribute
200 * if there is no encoded text attribute found.
201 *
202 * @param token the token whose text attribute is to be determined
203 * @return the token's text attribute
204 */
205 protected TextAttribute getTokenTextAttribute(IToken token) {
206 Object data= token.getData();
207 if (data instanceof TextAttribute)
208 return (TextAttribute) data;
209 return fDefaultTextAttribute;
210 }
211
212 /**
213 * Adds style information to the given text presentation.
214 *
215 * @param presentation the text presentation to be extended
216 * @param offset the offset of the range to be styled
217 * @param length the length of the range to be styled
218 * @param attr the attribute describing the style of the range to be styled
219 */
220 protected void addRange(TextPresentation presentation, int offset, int length, TextAttribute attr) {
221 if (attr !is null) {
222 int style= attr.getStyle();
223 int fontStyle= style & (DWT.ITALIC | DWT.BOLD | DWT.NORMAL);
224 StyleRange styleRange= new StyleRange(offset, length, attr.getForeground(), attr.getBackground(), fontStyle);
225 styleRange.strikeout= (style & TextAttribute.STRIKETHROUGH) !is 0;
226 styleRange.underline= (style & TextAttribute.UNDERLINE) !is 0;
227 styleRange.font= attr.getFont();
228 presentation.addStyleRange(styleRange);
229 }
230 }
231 }
232
233