Mercurial > projects > dwt-addons
comparison dwtx/jface/text/rules/RuleBasedPartitionScanner.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.RuleBasedPartitionScanner; | |
15 | |
16 import dwt.dwthelper.utils; | |
17 | |
18 | |
19 import dwtx.jface.text.IDocument; | |
20 | |
21 | |
22 /** | |
23 * Scanner that exclusively uses predicate rules. | |
24 * @since 2.0 | |
25 */ | |
26 public class RuleBasedPartitionScanner : BufferedRuleBasedScanner , IPartitionTokenScanner { | |
27 | |
28 /** The content type of the partition in which to resume scanning. */ | |
29 protected String fContentType; | |
30 /** The offset of the partition inside which to resume. */ | |
31 protected int fPartitionOffset; | |
32 | |
33 | |
34 /** | |
35 * Disallow setting the rules since this scanner | |
36 * exclusively uses predicate rules. | |
37 * | |
38 * @param rules the sequence of rules controlling this scanner | |
39 */ | |
40 public void setRules(IRule[] rules) { | |
41 throw new UnsupportedOperationException(); | |
42 } | |
43 | |
44 /* | |
45 * @see RuleBasedScanner#setRules(IRule[]) | |
46 */ | |
47 public void setPredicateRules(IPredicateRule[] rules) { | |
48 super.setRules(rules); | |
49 } | |
50 | |
51 /* | |
52 * @see ITokenScanner#setRange(IDocument, int, int) | |
53 */ | |
54 public void setRange(IDocument document, int offset, int length) { | |
55 setPartialRange(document, offset, length, null, -1); | |
56 } | |
57 | |
58 /* | |
59 * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String, int) | |
60 */ | |
61 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) { | |
62 fContentType= contentType; | |
63 fPartitionOffset= partitionOffset; | |
64 if (partitionOffset > -1) { | |
65 int delta= offset - partitionOffset; | |
66 if (delta > 0) { | |
67 super.setRange(document, partitionOffset, length + delta); | |
68 fOffset= offset; | |
69 return; | |
70 } | |
71 } | |
72 super.setRange(document, offset, length); | |
73 } | |
74 | |
75 /* | |
76 * @see ITokenScanner#nextToken() | |
77 */ | |
78 public IToken nextToken() { | |
79 | |
80 | |
81 if (fContentType is null || fRules is null) { | |
82 //don't try to resume | |
83 return super.nextToken(); | |
84 } | |
85 | |
86 // inside a partition | |
87 | |
88 fColumn= UNDEFINED; | |
89 bool resume= (fPartitionOffset > -1 && fPartitionOffset < fOffset); | |
90 fTokenOffset= resume ? fPartitionOffset : fOffset; | |
91 | |
92 IPredicateRule rule; | |
93 IToken token; | |
94 | |
95 for (int i= 0; i < fRules.length; i++) { | |
96 rule= (IPredicateRule) fRules[i]; | |
97 token= rule.getSuccessToken(); | |
98 if (fContentType.equals(token.getData())) { | |
99 token= rule.evaluate(this, resume); | |
100 if (!token.isUndefined()) { | |
101 fContentType= null; | |
102 return token; | |
103 } | |
104 } | |
105 } | |
106 | |
107 // haven't found any rule for this type of partition | |
108 fContentType= null; | |
109 if (resume) | |
110 fOffset= fPartitionOffset; | |
111 return super.nextToken(); | |
112 } | |
113 } |