Search in sources :

Example 1 with SQLCommentToken

use of org.jkiss.dbeaver.model.sql.parser.tokens.SQLCommentToken in project dbeaver by dbeaver.

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 2 with SQLCommentToken

use of org.jkiss.dbeaver.model.sql.parser.tokens.SQLCommentToken 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)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)2 IToken (org.eclipse.jface.text.rules.IToken)2 NotNull (org.jkiss.code.NotNull)2 SQLCommentToken (org.jkiss.dbeaver.model.sql.parser.tokens.SQLCommentToken)2