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