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