Search in sources :

Example 61 with IToken

use of org.eclipse.jface.text.rules.IToken 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)

Example 62 with IToken

use of org.eclipse.jface.text.rules.IToken in project hale by halestudio.

the class GroovyPartitionScanner method createRules.

public static List<IRule> createRules(boolean withColor) {
    IPreferenceStore store = GroovyUIPlugin.getDefault().getPreferenceStore();
    Object objComment;
    Object objdComment;
    Object objmString;
    Object objsString;
    Object objsComment;
    if (withColor) {
        RGB rgb = PreferenceConverter.getColor(store, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
        objComment = objmString = objsString = objsComment = objdComment = new TextAttribute(new Color(null, rgb), null, SWT.ITALIC);
    } else {
        objComment = JAVA_MULTI_LINE_COMMENT;
        objmString = GROOVY_MULTILINE_STRINGS;
        objsString = JAVA_STRING;
        objsComment = JAVA_SINGLE_LINE_COMMENT;
        objdComment = JAVA_DOC;
    }
    IToken comment = new Token(objComment);
    IToken mString = new Token(objmString);
    IToken sString = new Token(objsString);
    IToken sComment = new Token(objsComment);
    IToken jdoc = new Token(objdComment);
    List<IRule> rules = new ArrayList<IRule>();
    // Add rule for single line comments.
    rules.add(new EndOfLineRule("//", sComment));
    // Add rule for strings and character constants.
    rules.add(new MultiLineRule("'''", "'''", mString));
    rules.add(new MultiLineRule("\"\"\"", "\"\"\"", mString));
    // GRECLIPSE-1111 do not eagerly match these kinds of multiline strings
    rules.add(new SingleLineRule("\"", "\"", sString, '\\'));
    rules.add(new SingleLineRule("'", "'", sString, '\\'));
    // GRECLIPSE-1203 make dollar slashies optionally highlighted
    if (store.getBoolean(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_SLASHY_STRINGS)) {
        rules.add(new MultiLineRule("$/", "/$", mString, '\0', false));
    }
    // Add special case word rule.
    rules.add(new WordPredicateRule(comment));
    // Add rule for JavaDoc
    // $NON-NLS-1$ //$NON-NLS-2$
    rules.add(new MultiLineRule("/**", "*/", jdoc, (char) 0, true));
    // Add rules for multi-line comments
    // $NON-NLS-1$ //$NON-NLS-2$
    rules.add(new MultiLineRule("/*", "*/", comment, (char) 0, true));
    return rules;
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute) Color(org.eclipse.swt.graphics.Color) ArrayList(java.util.ArrayList) MultiLineRule(org.eclipse.jface.text.rules.MultiLineRule) IToken(org.eclipse.jface.text.rules.IToken) Token(org.eclipse.jface.text.rules.Token) RGB(org.eclipse.swt.graphics.RGB) IRule(org.eclipse.jface.text.rules.IRule) IToken(org.eclipse.jface.text.rules.IToken) SingleLineRule(org.eclipse.jface.text.rules.SingleLineRule) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) EndOfLineRule(org.eclipse.jface.text.rules.EndOfLineRule)

Example 63 with IToken

use of org.eclipse.jface.text.rules.IToken in project dbeaver by serge-rider.

the class HTMLSQLConverter method convertText.

@NotNull
@Override
public String convertText(@NotNull SQLDialect dialect, @NotNull SQLSyntaxManager syntaxManager, @NotNull SQLRuleScanner ruleManager, @NotNull IDocument document, int startPos, int length, @NotNull Map<String, Object> options) {
    StringBuilder result = new StringBuilder();
    ruleManager.setRange(document, startPos, length);
    try {
        result.append("<pre>");
        for (; ; ) {
            IToken token = ruleManager.nextToken();
            if (token.isEOF()) {
                break;
            }
            int tokenOffset = ruleManager.getTokenOffset();
            final int tokenLength = ruleManager.getTokenLength();
            boolean hasSpan = false;
            Object data = token.getData();
            if (data instanceof TextAttribute) {
                result.append("<span style='");
                TextAttribute ta = (TextAttribute) data;
                if (ta.getBackground() != null) {
                    result.append("background-color:").append(toHex(ta.getBackground())).append(";");
                }
                if (ta.getForeground() != null) {
                    result.append("color:").append(toHex(ta.getForeground())).append(";");
                }
                if ((ta.getStyle() & SWT.BOLD) == SWT.BOLD) {
                    result.append("font-weight:bold;");
                }
                if ((ta.getStyle() & SWT.ITALIC) == SWT.ITALIC) {
                    result.append("font-style: italic;");
                }
                // ta.getStyle()
                result.append("'>");
                hasSpan = true;
            }
            result.append(document.get(tokenOffset, tokenLength));
            if (hasSpan) {
                result.append("</span>");
            }
        }
        result.append("</pre>");
    } catch (BadLocationException e) {
        log.error("Error converting SQL to HTML", e);
    }
    return result.toString().trim();
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) TextAttribute(org.eclipse.jface.text.TextAttribute) BadLocationException(org.eclipse.jface.text.BadLocationException) NotNull(org.jkiss.code.NotNull)

Example 64 with IToken

use of org.eclipse.jface.text.rules.IToken in project dbeaver by serge-rider.

the class UnformattedSQLConverter method convertText.

@NotNull
@Override
public String convertText(@NotNull SQLDialect dialect, @NotNull SQLSyntaxManager syntaxManager, @NotNull SQLRuleScanner ruleManager, @NotNull IDocument document, int startPos, int length, @NotNull Map<String, Object> options) {
    StringBuilder result = new StringBuilder();
    ruleManager.setRange(document, startPos, length);
    String[] singleLineComments = dialect.getSingleLineComments();
    Pair<String, String> multiLineComments = dialect.getMultiLineComments();
    boolean lastWhitespace = false;
    try {
        for (; ; ) {
            IToken token = ruleManager.nextToken();
            if (token.isEOF()) {
                break;
            }
            int tokenOffset = ruleManager.getTokenOffset();
            final int tokenLength = ruleManager.getTokenLength();
            if (token.isWhitespace()) {
                if (!lastWhitespace) {
                    result.append(' ');
                }
                lastWhitespace = true;
            } else if (token instanceof SQLCommentToken) {
                String comment = document.get(tokenOffset, tokenLength);
                for (String slc : singleLineComments) {
                    if (comment.startsWith(slc)) {
                        if (multiLineComments != null) {
                            comment = multiLineComments.getFirst() + comment.substring(slc.length()) + multiLineComments.getSecond();
                        }
                        break;
                    }
                }
                comment = CommonUtils.compactWhiteSpaces(comment);
                result.append(comment);
            } else {
                lastWhitespace = false;
                result.append(document.get(tokenOffset, tokenLength));
            }
        }
    } catch (BadLocationException e) {
        log.error("Error unformatting SQL", e);
    }
    return result.toString().trim();
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) SQLCommentToken(org.jkiss.dbeaver.model.sql.parser.tokens.SQLCommentToken) BadLocationException(org.eclipse.jface.text.BadLocationException) NotNull(org.jkiss.code.NotNull)

Example 65 with IToken

use of org.eclipse.jface.text.rules.IToken in project dbeaver by dbeaver.

the class HTMLSQLConverter method convertText.

@NotNull
@Override
public String convertText(@NotNull SQLDialect dialect, @NotNull SQLSyntaxManager syntaxManager, @NotNull SQLRuleScanner ruleManager, @NotNull IDocument document, int startPos, int length, @NotNull Map<String, Object> options) {
    StringBuilder result = new StringBuilder();
    ruleManager.setRange(document, startPos, length);
    try {
        result.append("<pre>");
        for (; ; ) {
            IToken token = ruleManager.nextToken();
            if (token.isEOF()) {
                break;
            }
            int tokenOffset = ruleManager.getTokenOffset();
            final int tokenLength = ruleManager.getTokenLength();
            boolean hasSpan = false;
            Object data = token.getData();
            if (data instanceof TextAttribute) {
                result.append("<span style='");
                TextAttribute ta = (TextAttribute) data;
                if (ta.getBackground() != null) {
                    result.append("background-color:").append(toHex(ta.getBackground())).append(";");
                }
                if (ta.getForeground() != null) {
                    result.append("color:").append(toHex(ta.getForeground())).append(";");
                }
                if ((ta.getStyle() & SWT.BOLD) == SWT.BOLD) {
                    result.append("font-weight:bold;");
                }
                if ((ta.getStyle() & SWT.ITALIC) == SWT.ITALIC) {
                    result.append("font-style: italic;");
                }
                // ta.getStyle()
                result.append("'>");
                hasSpan = true;
            }
            result.append(document.get(tokenOffset, tokenLength));
            if (hasSpan) {
                result.append("</span>");
            }
        }
        result.append("</pre>");
    } catch (BadLocationException e) {
        log.error("Error converting SQL to HTML", e);
    }
    return result.toString().trim();
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) TextAttribute(org.eclipse.jface.text.TextAttribute) BadLocationException(org.eclipse.jface.text.BadLocationException) NotNull(org.jkiss.code.NotNull)

Aggregations

IToken (org.eclipse.jface.text.rules.IToken)77 Token (org.eclipse.jface.text.rules.Token)25 Test (org.junit.Test)21 IDocument (org.eclipse.jface.text.IDocument)17 TextAttribute (org.eclipse.jface.text.TextAttribute)16 Document (org.eclipse.jface.text.Document)15 BadLocationException (org.eclipse.jface.text.BadLocationException)13 MultiLineRule (org.eclipse.jface.text.rules.MultiLineRule)13 IPartitionTokenScanner (org.eclipse.jface.text.rules.IPartitionTokenScanner)10 IPredicateRule (org.eclipse.jface.text.rules.IPredicateRule)10 IRule (org.eclipse.jface.text.rules.IRule)10 RuleBasedPartitionScanner (org.eclipse.jface.text.rules.RuleBasedPartitionScanner)10 ArrayList (java.util.ArrayList)9 WordRule (org.eclipse.jface.text.rules.WordRule)8 SingleLineRule (org.eclipse.jface.text.rules.SingleLineRule)6 NotNull (org.jkiss.code.NotNull)6 RuleBasedScanner (org.eclipse.jface.text.rules.RuleBasedScanner)5 Color (org.eclipse.swt.graphics.Color)5 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)4 TypedPosition (org.eclipse.jface.text.TypedPosition)4