Search in sources :

Example 1 with TextAttribute

use of org.eclipse.jface.text.TextAttribute in project tdi-studio-se by Talend.

the class AbstractSQLScanner method adaptToColorChange.

private void adaptToColorChange(final Token token, final PropertyChangeEvent event) {
    RGB rgb = null;
    final Object value = event.getNewValue();
    if (value instanceof RGB) {
        rgb = (RGB) value;
    } else if (value instanceof String) {
        rgb = StringConverter.asRGB((String) value);
    }
    if (rgb != null) {
        final String property = event.getProperty();
        if (fColorManager instanceof IColorManagerExtension) {
            final IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
            ext.unbindColor(property);
            ext.bindColor(property, rgb);
        }
        final Object data = token.getData();
        if (data instanceof TextAttribute) {
            final TextAttribute oldAttr = (TextAttribute) data;
            token.setData(new TextAttribute(fColorManager.getColor(property), oldAttr.getBackground(), oldAttr.getStyle()));
        }
    }
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute) RGB(org.eclipse.swt.graphics.RGB)

Example 2 with TextAttribute

use of org.eclipse.jface.text.TextAttribute in project tdi-studio-se by Talend.

the class AbstractSQLScanner method addToken.

//$NON-NLS-1$
@SuppressWarnings("unchecked")
private void addToken(final String colorKey, final String styleKey) {
    final RGB rgb = PreferenceConverter.getColor(fPreferenceStore, colorKey);
    if (fColorManager instanceof IColorManagerExtension) {
        final IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
        ext.unbindColor(colorKey);
        ext.bindColor(colorKey, rgb);
    }
    final boolean bold = fPreferenceStore.getBoolean(styleKey);
    fTokenMap.put(colorKey, new Token(new TextAttribute(fColorManager.getColor(colorKey), null, bold ? SWT.BOLD : SWT.NORMAL)));
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute) Token(org.eclipse.jface.text.rules.Token) RGB(org.eclipse.swt.graphics.RGB)

Example 3 with TextAttribute

use of org.eclipse.jface.text.TextAttribute in project tdi-studio-se by Talend.

the class AbstractSQLScanner method adaptToStyleChange.

private void adaptToStyleChange(final Token token, final PropertyChangeEvent event) {
    boolean bold = false;
    final Object value = event.getNewValue();
    if (value instanceof Boolean) {
        bold = ((Boolean) value).booleanValue();
    } else if (value instanceof String) {
        final String s = (String) value;
        if (IPreferenceStore.TRUE.equals(s)) {
            bold = true;
        } else if (IPreferenceStore.FALSE.equals(s)) {
            bold = false;
        }
    }
    final Object data = token.getData();
    if (data instanceof TextAttribute) {
        final TextAttribute oldAttr = (TextAttribute) data;
        final boolean isBold = (oldAttr.getStyle() == SWT.BOLD);
        if (isBold != bold) {
            token.setData(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));
        }
    }
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute)

Example 4 with TextAttribute

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

the class SQLRuleManager method refreshRules.

public void refreshRules(@Nullable DBPDataSource dataSource, IEditorInput editorInput) {
    boolean minimalRules = false;
    File file = EditorUtils.getLocalFileFromInput(editorInput);
    if (file != null && file.length() > MAX_FILE_LENGTH_FOR_RULES) {
        minimalRules = true;
    }
    /*final Color backgroundColor = null;unassigned || dataSource != null ?
            getColor(SQLConstants.CONFIG_COLOR_BACKGROUND, SWT.COLOR_WHITE) :
            getColor(SQLConstants.CONFIG_COLOR_DISABLED, SWT.COLOR_WIDGET_LIGHT_SHADOW);*/
    final IToken keywordToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final IToken typeToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DATATYPE), null, SWT.BOLD));
    final IToken stringToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_STRING), null, SWT.NORMAL));
    final IToken quotedToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DATATYPE), null, SWT.NORMAL));
    final IToken numberToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_NUMBER), null, SWT.NORMAL));
    final IToken commentToken = new SQLCommentToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_COMMENT), null, SWT.NORMAL));
    final SQLDelimiterToken delimiterToken = new SQLDelimiterToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DELIMITER, SWT.COLOR_RED), null, SWT.NORMAL));
    final SQLParameterToken parameterToken = new SQLParameterToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_PARAMETER, SWT.COLOR_DARK_BLUE), null, SWT.BOLD));
    final IToken otherToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_TEXT), null, SWT.NORMAL));
    final SQLBlockHeaderToken blockHeaderToken = new SQLBlockHeaderToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final SQLBlockBeginToken blockBeginToken = new SQLBlockBeginToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final SQLBlockEndToken blockEndToken = new SQLBlockEndToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final SQLBlockToggleToken blockToggleToken = new SQLBlockToggleToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DELIMITER), null, SWT.BOLD));
    setDefaultReturnToken(otherToken);
    List<IRule> rules = new ArrayList<>();
    SQLDialect dialect = syntaxManager.getDialect();
    // Add rule for single-line comments.
    for (String lineComment : dialect.getSingleLineComments()) {
        if (lineComment.startsWith("^")) {
            //$NON-NLS-1$
            rules.add(new LineCommentRule(lineComment, commentToken));
        } else {
            //$NON-NLS-1$
            rules.add(new EndOfLineRule(lineComment, commentToken));
        }
    }
    // Add rules for delimited identifiers and string literals.
    //char escapeChar = syntaxManager.getEscapeChar();
    String quoteSymbol = syntaxManager.getQuoteSymbol();
    if (quoteSymbol != null) {
        rules.add(new SingleLineRule(quoteSymbol, quoteSymbol, quotedToken, (char) 0));
    }
    if (quoteSymbol == null || !quoteSymbol.equals(SQLConstants.STR_QUOTE_SINGLE)) {
        rules.add(new MultiLineRule(SQLConstants.STR_QUOTE_SINGLE, SQLConstants.STR_QUOTE_SINGLE, stringToken, (char) 0));
    }
    if (quoteSymbol == null || !quoteSymbol.equals(SQLConstants.STR_QUOTE_DOUBLE)) {
        rules.add(new MultiLineRule(SQLConstants.STR_QUOTE_DOUBLE, SQLConstants.STR_QUOTE_DOUBLE, quotedToken, (char) 0));
    }
    // Add rules for multi-line comments
    Pair<String, String> multiLineComments = dialect.getMultiLineComments();
    if (multiLineComments != null) {
        rules.add(new MultiLineRule(multiLineComments.getFirst(), multiLineComments.getSecond(), commentToken, (char) 0, true));
    }
    // Add generic whitespace rule.
    rules.add(new WhitespaceRule(new TextWhiteSpaceDetector()));
    if (!minimalRules) {
        // Add numeric rule
        rules.add(new NumberRule(numberToken));
    }
    SQLDelimiterRule delimRule = new SQLDelimiterRule(syntaxManager.getStatementDelimiters(), delimiterToken);
    rules.add(delimRule);
    {
        // Delimiter redefine
        String delimRedefine = dialect.getScriptDelimiterRedefiner();
        if (!CommonUtils.isEmpty(delimRedefine)) {
            final SQLSetDelimiterToken setDelimiterToken = new SQLSetDelimiterToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_COMMAND), null, SWT.BOLD));
            rules.add(new SQLDelimiterSetRule(delimRedefine, setDelimiterToken, delimRule));
        }
    }
    if (!minimalRules) {
        // Add word rule for keywords, types, and constants.
        WordRule wordRule = new WordRule(new SQLWordDetector(), otherToken, true);
        for (String reservedWord : dialect.getReservedWords()) {
            wordRule.addWord(reservedWord, keywordToken);
        }
        if (dataSource != null) {
            for (String function : dialect.getFunctions(dataSource)) {
                wordRule.addWord(function, typeToken);
            }
            for (String type : dialect.getDataTypes(dataSource)) {
                wordRule.addWord(type, typeToken);
            }
        }
        final String blockHeaderString = dialect.getBlockHeaderString();
        if (!CommonUtils.isEmpty(blockHeaderString)) {
            wordRule.addWord(blockHeaderString, blockHeaderToken);
        }
        String[][] blockBounds = dialect.getBlockBoundStrings();
        if (blockBounds != null) {
            for (String[] block : blockBounds) {
                if (block.length != 2) {
                    continue;
                }
                wordRule.addWord(block[0], blockBeginToken);
                wordRule.addWord(block[1], blockEndToken);
            }
        }
        rules.add(wordRule);
    }
    final String blockToggleString = dialect.getBlockToggleString();
    if (!CommonUtils.isEmpty(blockToggleString)) {
        int divPos = blockToggleString.indexOf(SQLConstants.KEYWORD_PATTERN_CHARS);
        if (divPos != -1) {
            String prefix = blockToggleString.substring(0, divPos);
            String postfix = blockToggleString.substring(divPos + SQLConstants.KEYWORD_PATTERN_CHARS.length());
            WordPatternRule blockToggleRule = new WordPatternRule(new SQLWordDetector(), prefix, postfix, blockToggleToken);
            rules.add(blockToggleRule);
        } else {
            WordRule blockToggleRule = new WordRule(getWordOrSymbolDetector(blockToggleString), Token.UNDEFINED, true);
            blockToggleRule.addWord(blockToggleString, blockToggleToken);
            rules.add(blockToggleRule);
        }
    }
    if (!minimalRules) {
        // Parameter rule
        rules.add(new SQLParameterRule(syntaxManager, parameterToken));
    }
    IRule[] result = new IRule[rules.size()];
    rules.toArray(result);
    setRules(result);
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute) TextWhiteSpaceDetector(org.jkiss.dbeaver.ui.editors.text.TextWhiteSpaceDetector) LineCommentRule(org.jkiss.dbeaver.ui.editors.sql.syntax.rules.LineCommentRule) SQLParameterRule(org.jkiss.dbeaver.ui.editors.sql.syntax.rules.SQLParameterRule) SQLDelimiterRule(org.jkiss.dbeaver.ui.editors.sql.syntax.rules.SQLDelimiterRule) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) File(java.io.File) SQLDelimiterSetRule(org.jkiss.dbeaver.ui.editors.sql.syntax.rules.SQLDelimiterSetRule)

Example 5 with TextAttribute

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

the class XMLConfiguration method getXMLScanner.

XMLScanner getXMLScanner() {
    XMLScanner scanner = new XMLScanner(colorManager);
    scanner.setDefaultReturnToken(new Token(new TextAttribute(colorManager.getColor(COLOR_DEFAULT))));
    return scanner;
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute) Token(org.eclipse.jface.text.rules.Token)

Aggregations

TextAttribute (org.eclipse.jface.text.TextAttribute)139 Token (org.eclipse.jface.text.rules.Token)45 RGB (org.eclipse.swt.graphics.RGB)37 Color (org.eclipse.swt.graphics.Color)35 IToken (org.eclipse.jface.text.rules.IToken)22 StyleRange (org.eclipse.swt.custom.StyleRange)20 IPresentationReconciler (org.eclipse.jface.text.presentation.IPresentationReconciler)15 PresentationReconciler (org.eclipse.jface.text.presentation.PresentationReconciler)15 DefaultDamagerRepairer (org.eclipse.jface.text.rules.DefaultDamagerRepairer)13 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)12 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)12 IRule (org.eclipse.jface.text.rules.IRule)10 RuleBasedScanner (org.eclipse.jface.text.rules.RuleBasedScanner)9 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)8 ArrayList (java.util.ArrayList)7 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)6 Document (org.eclipse.jface.text.Document)4 IDocument (org.eclipse.jface.text.IDocument)4 MultiLineRule (org.eclipse.jface.text.rules.MultiLineRule)4 PatternRule (org.eclipse.jface.text.rules.PatternRule)4