Search in sources :

Example 11 with WordRule

use of org.eclipse.jface.text.rules.WordRule in project eclipse.platform.text by eclipse.

the class WordRuleTest method testBug163116.

/*
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=163116
	 */
@Test
public void testBug163116() throws Exception {
    IWordDetector detector = new IWordDetector() {

        @Override
        public boolean isWordPart(char c) {
            return true;
        }

        @Override
        public boolean isWordStart(char c) {
            return true;
        }
    };
    WordRule rule = new WordRule(detector, new Token(this));
    RuleBasedScanner scanner = new RuleBasedScanner();
    scanner.setRules(new IRule[] { rule });
    scanner.setRange(new Document(), 0, 0);
    IToken token = null;
    int i = 0;
    while (token != Token.EOF && i++ < 1000) token = scanner.nextToken();
    assertTrue(i < 1000);
}
Also used : IWordDetector(org.eclipse.jface.text.rules.IWordDetector) IToken(org.eclipse.jface.text.rules.IToken) IToken(org.eclipse.jface.text.rules.IToken) Token(org.eclipse.jface.text.rules.Token) WordRule(org.eclipse.jface.text.rules.WordRule) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) RuleBasedScanner(org.eclipse.jface.text.rules.RuleBasedScanner) Test(org.junit.Test)

Example 12 with WordRule

use of org.eclipse.jface.text.rules.WordRule in project webtools.sourceediting by eclipse.

the class WordRuleTest method testBug144355.

/*
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=144355
	 */
public void testBug144355() throws Exception {
    IWordDetector detector = new SimpleWordDetector();
    String defaultTokenString = "defaultToken";
    Token defaultToken = new Token(defaultTokenString);
    String testTokenStringNormal = "TestTokenString";
    String testTokenStringDifferentCapitalization = "TestTOKENString";
    String testTokenStringCompletelyDifferent = "XXX";
    Token normalToken = new Token(testTokenStringNormal);
    WordRule rule = new WordRule(detector, defaultToken, true);
    rule.addWord(testTokenStringNormal, normalToken);
    // scenario 1
    // pre: pass in a normal string ("TestTokenString")
    // post: expect the normal token to be returned
    RuleBasedScanner scanner = new RuleBasedScanner();
    scanner.setRules(new IRule[] { rule });
    scanner.setRange(new Document(testTokenStringNormal), 0, testTokenStringNormal.length());
    assertTrue(scanner.nextToken().getData().equals(testTokenStringNormal));
    // scenario 2
    // pre: pass in a normal string but different capitalization ("TestTOKENString")
    // post: expect the normal token to be returned
    scanner = new RuleBasedScanner();
    scanner.setRules(new IRule[] { rule });
    scanner.setRange(new Document(testTokenStringDifferentCapitalization), 0, testTokenStringDifferentCapitalization.length());
    assertTrue(scanner.nextToken().getData().equals(testTokenStringNormal));
    // scenario 3
    // pre: pass in a completely different string ("XXX")
    // post: expect the default token to be returned because the string can't be matched
    scanner = new RuleBasedScanner();
    scanner.setRules(new IRule[] { rule });
    scanner.setRange(new Document(testTokenStringCompletelyDifferent), 0, testTokenStringCompletelyDifferent.length());
    assertTrue(scanner.nextToken().getData().equals(defaultTokenString));
    WordRule ruleWithoutIgnoreCase = new WordRule(detector, defaultToken);
    ruleWithoutIgnoreCase.addWord(testTokenStringNormal, normalToken);
    // scenario 4
    // pre: pass in a normal string ("TestTokenString")
    // post: expect the normal token to be returned
    scanner = new RuleBasedScanner();
    scanner.setRules(new IRule[] { ruleWithoutIgnoreCase });
    scanner.setRange(new Document(testTokenStringNormal), 0, testTokenStringNormal.length());
    assertTrue(scanner.nextToken().getData().equals(testTokenStringNormal));
    // scenario 5
    // pre: pass in a normal string but different capitalization ("TestTOKENString")
    // post: expect the default token to be returned
    scanner = new RuleBasedScanner();
    scanner.setRules(new IRule[] { ruleWithoutIgnoreCase });
    scanner.setRange(new Document(testTokenStringDifferentCapitalization), 0, testTokenStringDifferentCapitalization.length());
    assertTrue(scanner.nextToken().getData().equals(defaultTokenString));
}
Also used : IWordDetector(org.eclipse.jface.text.rules.IWordDetector) IToken(org.eclipse.jface.text.rules.IToken) Token(org.eclipse.jface.text.rules.Token) WordRule(org.eclipse.jface.text.rules.WordRule) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) RuleBasedScanner(org.eclipse.jface.text.rules.RuleBasedScanner)

Example 13 with WordRule

use of org.eclipse.jface.text.rules.WordRule in project webtools.sourceediting by eclipse.

the class JSPELCodeScanner method initializeRules.

public void initializeRules() {
    List rules = new ArrayList();
    // Add generic whitespace rule.
    rules.add(new WhitespaceRule(new JSPELWhitespaceDetector()));
    // Add word rule for keywords, types, and constants.
    WordRule wordRule = new WordRule(new JSPELWordDetector(), fDefaultToken);
    for (int i = 0; i < fgKeywords.length; i++) wordRule.addWord(fgKeywords[i], fKeywordToken);
    for (int i = 0; i < fgConstants.length; i++) wordRule.addWord(fgConstants[i], fTypeToken);
    rules.add(wordRule);
    IRule[] result = new IRule[rules.size()];
    rules.toArray(result);
    setRules(result);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) WhitespaceRule(org.eclipse.jface.text.rules.WhitespaceRule) WordRule(org.eclipse.jface.text.rules.WordRule) IRule(org.eclipse.jface.text.rules.IRule)

Example 14 with WordRule

use of org.eclipse.jface.text.rules.WordRule in project syncope by apache.

the class JavaScriptScanner method createRules.

/**
 * Creates the list of <code>IRule</code>.
 * If you have to customize rules, override this method.
 *
 * @return the list of <code>IRule</code>
 */
protected List<IRule> createRules() {
    IToken normal = new Token(new TextAttribute(new Color(Display.getCurrent(), IHTMLColorConstants.FOREGROUND)));
    IToken string = new Token(new TextAttribute(new Color(Display.getCurrent(), IHTMLColorConstants.JAVA_STRING)));
    IToken comment = new Token(new TextAttribute(new Color(Display.getCurrent(), IHTMLColorConstants.JAVA_COMMENT)));
    IToken keyword = new Token(new TextAttribute(new Color(Display.getCurrent(), IHTMLColorConstants.JAVA_KEYWORD)));
    List<IRule> rules = new ArrayList<IRule>();
    rules.add(new SingleLineRule("\"", "\"", string, '\\'));
    rules.add(new SingleLineRule("'", "'", string, '\\'));
    rules.add(new SingleLineRule("\\//", null, normal));
    rules.add(new EndOfLineRule("//", comment));
    WordRule wordRule = new WordRule(new JavaWordDetector(), normal);
    for (int i = 0; i < KEYWORDS.length; i++) {
        wordRule.addWord(KEYWORDS[i], keyword);
    }
    rules.add(wordRule);
    return rules;
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) SingleLineRule(org.eclipse.jface.text.rules.SingleLineRule) TextAttribute(org.eclipse.jface.text.TextAttribute) Color(org.eclipse.swt.graphics.Color) ArrayList(java.util.ArrayList) IToken(org.eclipse.jface.text.rules.IToken) Token(org.eclipse.jface.text.rules.Token) WordRule(org.eclipse.jface.text.rules.WordRule) IRule(org.eclipse.jface.text.rules.IRule) EndOfLineRule(org.eclipse.jface.text.rules.EndOfLineRule)

Aggregations

WordRule (org.eclipse.jface.text.rules.WordRule)14 IToken (org.eclipse.jface.text.rules.IToken)11 IRule (org.eclipse.jface.text.rules.IRule)10 Token (org.eclipse.jface.text.rules.Token)10 Document (org.eclipse.jface.text.Document)8 IDocument (org.eclipse.jface.text.IDocument)8 RuleBasedScanner (org.eclipse.jface.text.rules.RuleBasedScanner)8 ArrayList (java.util.ArrayList)6 TextAttribute (org.eclipse.jface.text.TextAttribute)5 IWordDetector (org.eclipse.jface.text.rules.IWordDetector)4 PatternRule (org.eclipse.jface.text.rules.PatternRule)4 SingleLineRule (org.eclipse.jface.text.rules.SingleLineRule)4 Test (org.junit.Test)4 List (java.util.List)3 EndOfLineRule (org.eclipse.jface.text.rules.EndOfLineRule)3 MultiLineRule (org.eclipse.jface.text.rules.MultiLineRule)3 WhitespaceRule (org.eclipse.jface.text.rules.WhitespaceRule)3 Color (org.eclipse.swt.graphics.Color)1 StringDetectionPatternRule (org.eclipse.titan.designer.editors.StringDetectionPatternRule)1 WhiteSpaceDetector (org.eclipse.titan.designer.editors.WhiteSpaceDetector)1