Search in sources :

Example 1 with WordRule

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

the class WordRuleTest method testBug144355.

/*
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=144355
	 */
@Test
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) Test(org.junit.Test)

Example 2 with WordRule

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

the class WordRuleTest method testBug175712_2.

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=175712
@Test
public void testBug175712_2() throws Exception {
    IRule[] rules = new IRule[2];
    IToken stepToken = new Token(new TextAttribute(null, null, SWT.BOLD));
    PatternRule stepRule = new PatternRule("(((", ")", stepToken, (char) 0, false);
    stepRule.setColumnConstraint(-1);
    rules[1] = stepRule;
    IToken titleToken = new Token(new TextAttribute(null, null, SWT.BOLD));
    WordRule wordRule = new WordRule(new SimpleWordDetector());
    wordRule.addWord("((", titleToken);
    rules[0] = wordRule;
    IDocument document = new Document("((\n((\n- Cheese\n- Wine");
    RuleBasedScanner scanner = new RuleBasedScanner();
    scanner.setRules(rules);
    scanner.setRange(document, 0, document.getLength());
    IToken defaultToken = new Token(this);
    scanner.setDefaultReturnToken(defaultToken);
    IToken token = scanner.nextToken();
    assertSame(titleToken, token);
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) PatternRule(org.eclipse.jface.text.rules.PatternRule) TextAttribute(org.eclipse.jface.text.TextAttribute) 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) IRule(org.eclipse.jface.text.rules.IRule) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 3 with WordRule

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

the class WordRuleTest method testBug175712_1.

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=175712
@Test
public void testBug175712_1() throws Exception {
    IRule[] rules = new IRule[2];
    IToken stepToken = new Token(new TextAttribute(null, null, SWT.BOLD));
    PatternRule stepRule = new PatternRule("(((", ")", stepToken, (char) 0, false);
    stepRule.setColumnConstraint(-1);
    rules[1] = stepRule;
    IToken titleToken = new Token(new TextAttribute(null, null, SWT.BOLD));
    WordRule wordRule = new WordRule(new SimpleWordDetector());
    wordRule.addWord("((", titleToken);
    rules[0] = wordRule;
    IDocument document = new Document("((( \n((\n- Cheese\n- Wine");
    RuleBasedScanner scanner = new RuleBasedScanner();
    scanner.setRules(rules);
    scanner.setRange(document, 0, document.getLength());
    IToken defaultToken = new Token(this);
    scanner.setDefaultReturnToken(defaultToken);
    IToken token = scanner.nextToken();
    assertSame(defaultToken, token);
    token = scanner.nextToken();
    assertSame(defaultToken, token);
    token = scanner.nextToken();
    assertSame(defaultToken, token);
    token = scanner.nextToken();
    assertSame(titleToken, token);
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) PatternRule(org.eclipse.jface.text.rules.PatternRule) TextAttribute(org.eclipse.jface.text.TextAttribute) 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) IRule(org.eclipse.jface.text.rules.IRule) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 4 with WordRule

use of org.eclipse.jface.text.rules.WordRule in project titan.EclipsePlug-ins by eclipse.

the class CodeScanner method getTTCNRules.

public static List<IRule> getTTCNRules(final ColorManager colorManager) {
    IToken singleLineComment = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_COMMENTS);
    IToken multiLineComment = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_COMMENTS);
    IToken keyword = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TTCN3_KEYWORDS);
    IToken templateMatch = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TEMPLATE_MATCH);
    IToken types = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TYPE);
    IToken timerOp = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TIMER_OP);
    IToken portOp = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_PORT_OP);
    IToken configOp = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_CONFIG_OP);
    IToken verdictOp = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_VERDICT_OP);
    IToken sutOp = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_SUT_OP);
    IToken functionOp = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_FUNCTION_OP);
    IToken predefinedOp = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_PREDEFINED_OP);
    IToken booleanConst = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_BOOLEAN_CONST);
    IToken verdictConst = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TTCN3_VERDICT_CONST);
    IToken otherConst = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_OTHER_CONST);
    IToken macro = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_PREPROCESSOR);
    IToken visibility = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_VISIBILITY_OP);
    IToken string = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_STRINGS);
    IToken other = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_NORMAL_TEXT);
    List<IRule> rules = new ArrayList<IRule>();
    rules.add(new EndOfLineRule("//", singleLineComment));
    rules.add(new MultiLineRule("/*", "*/", multiLineComment, '\0', true));
    rules.add(new WhitespaceRule(new WhiteSpaceDetector()));
    rules.add(new TTCN3StringDetectionPatternRule(string));
    rules.add(new StringDetectionPatternRule("'", new char[][] { { '\'', 'B' }, { '\'', 'H' }, { '\'', 'O' } }, string));
    WordRule wordRule = new WordRule(new WordDetector(), other);
    for (String element : CodeScanner.KEYWORDS) {
        wordRule.addWord(element, keyword);
    }
    for (String element : CodeScanner.TEMPLATE_MATCH) {
        wordRule.addWord(element, templateMatch);
    }
    for (String element : CodeScanner.TYPES) {
        wordRule.addWord(element, types);
    }
    for (String element : CodeScanner.TIMER_OPERATIONS) {
        wordRule.addWord(element, timerOp);
    }
    for (String element : CodeScanner.PORT_OPERATIONS) {
        wordRule.addWord(element, portOp);
    }
    for (String element : CodeScanner.CONFIGURATION_OPERATIONS) {
        wordRule.addWord(element, configOp);
    }
    for (String element : CodeScanner.VERDICT_OPERATIONS) {
        wordRule.addWord(element, verdictOp);
    }
    for (String element : CodeScanner.SUT_OPERATION) {
        wordRule.addWord(element, sutOp);
    }
    for (String element : CodeScanner.FUNCTION_OPERATIONS) {
        wordRule.addWord(element, functionOp);
    }
    for (String element : CodeScanner.PREDEFINED_OPERATIONS) {
        wordRule.addWord(element, predefinedOp);
    }
    for (String element : CodeScanner.BOOLEAN_CONSTANTS) {
        wordRule.addWord(element, booleanConst);
    }
    for (String element : CodeScanner.VERDICT_CONSTANT) {
        wordRule.addWord(element, verdictConst);
    }
    for (String element : CodeScanner.OTHER_CONSTANT) {
        wordRule.addWord(element, otherConst);
    }
    for (String element : CodeScanner.VISIBILITY_MODIFIERS) {
        wordRule.addWord(element, visibility);
    }
    rules.add(wordRule);
    WordRule macroRule = new WordRule(new MacroDetector(), other);
    for (String element : CodeScanner.MACROS) {
        macroRule.addWord(element, macro);
    }
    rules.add(macroRule);
    WordRule titanSpecificKeywordsRule = new WordRule(new TitanSpecificKeywordDetector(), other);
    for (String element : CodeScanner.TITANSPECIFICKEYWORDS) {
        // looks like a standard keyword
        titanSpecificKeywordsRule.addWord(element, keyword);
    }
    rules.add(titanSpecificKeywordsRule);
    return rules;
}
Also used : ArrayList(java.util.ArrayList) MultiLineRule(org.eclipse.jface.text.rules.MultiLineRule) StringDetectionPatternRule(org.eclipse.titan.designer.editors.StringDetectionPatternRule) WhitespaceRule(org.eclipse.jface.text.rules.WhitespaceRule) WordRule(org.eclipse.jface.text.rules.WordRule) IRule(org.eclipse.jface.text.rules.IRule) WhiteSpaceDetector(org.eclipse.titan.designer.editors.WhiteSpaceDetector) IToken(org.eclipse.jface.text.rules.IToken) EndOfLineRule(org.eclipse.jface.text.rules.EndOfLineRule)

Example 5 with WordRule

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

the class WordRuleTest method testBug175712_1.

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=175712
public void testBug175712_1() throws Exception {
    IRule[] rules = new IRule[2];
    IToken stepToken = new Token(new TextAttribute(null, null, SWT.BOLD));
    PatternRule stepRule = new PatternRule("(((", ")", stepToken, (char) 0, false);
    stepRule.setColumnConstraint(-1);
    rules[1] = stepRule;
    IToken titleToken = new Token(new TextAttribute(null, null, SWT.BOLD));
    WordRule wordRule = new WordRule(new SimpleWordDetector());
    wordRule.addWord("((", titleToken);
    rules[0] = wordRule;
    IDocument document = new Document("((( \n((\n- Cheese\n- Wine");
    RuleBasedScanner scanner = new RuleBasedScanner();
    scanner.setRules(rules);
    scanner.setRange(document, 0, document.getLength());
    IToken defaultToken = new Token(this);
    scanner.setDefaultReturnToken(defaultToken);
    IToken token = scanner.nextToken();
    assertSame(defaultToken, token);
    token = scanner.nextToken();
    assertSame(defaultToken, token);
    token = scanner.nextToken();
    assertSame(defaultToken, token);
    token = scanner.nextToken();
    assertSame(titleToken, token);
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) PatternRule(org.eclipse.jface.text.rules.PatternRule) TextAttribute(org.eclipse.jface.text.TextAttribute) 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) IRule(org.eclipse.jface.text.rules.IRule) IDocument(org.eclipse.jface.text.IDocument)

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