diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/rules/RuleBasedPartitionScanner.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+
+module dwtx.jface.text.rules.RuleBasedPartitionScanner;
+
+import dwt.dwthelper.utils;
+
+
+import dwtx.jface.text.IDocument;
+
+
+/**
+ * Scanner that exclusively uses predicate rules.
+ * @since 2.0
+ */
+public class RuleBasedPartitionScanner : BufferedRuleBasedScanner , IPartitionTokenScanner {
+
+    /** The content type of the partition in which to resume scanning. */
+    protected String fContentType;
+    /** The offset of the partition inside which to resume. */
+    protected int fPartitionOffset;
+
+
+    /**
+     * Disallow setting the rules since this scanner
+     * exclusively uses predicate rules.
+     * 
+     * @param rules the sequence of rules controlling this scanner
+     */
+    public void setRules(IRule[] rules) {
+        throw new UnsupportedOperationException();
+    }
+
+    /*
+     * @see RuleBasedScanner#setRules(IRule[])
+     */
+    public void setPredicateRules(IPredicateRule[] rules) {
+        super.setRules(rules);
+    }
+
+    /*
+     * @see ITokenScanner#setRange(IDocument, int, int)
+     */
+    public void setRange(IDocument document, int offset, int length) {
+        setPartialRange(document, offset, length, null, -1);
+    }
+
+    /*
+     * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String, int)
+     */
+    public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
+        fContentType= contentType;
+        fPartitionOffset= partitionOffset;
+        if (partitionOffset > -1) {
+            int delta= offset - partitionOffset;
+            if (delta > 0) {
+                super.setRange(document, partitionOffset, length + delta);
+                fOffset= offset;
+                return;
+            }
+        }
+        super.setRange(document, offset, length);
+    }
+
+    /*
+     * @see ITokenScanner#nextToken()
+     */
+    public IToken nextToken() {
+
+
+        if (fContentType is null || fRules is null) {
+            //don't try to resume
+            return super.nextToken();
+        }
+
+        // inside a partition
+
+        fColumn= UNDEFINED;
+        bool resume= (fPartitionOffset > -1 && fPartitionOffset < fOffset);
+        fTokenOffset= resume ? fPartitionOffset : fOffset;
+
+        IPredicateRule rule;
+        IToken token;
+
+        for (int i= 0; i < fRules.length; i++) {
+            rule= (IPredicateRule) fRules[i];
+            token= rule.getSuccessToken();
+            if (fContentType.equals(token.getData())) {
+                token= rule.evaluate(this, resume);
+                if (!token.isUndefined()) {
+                    fContentType= null;
+                    return token;
+                }
+            }
+        }
+
+        // haven't found any rule for this type of partition
+        fContentType= null;
+        if (resume)
+            fOffset= fPartitionOffset;
+        return super.nextToken();
+    }
+}