Search in sources :

Example 1 with SQLCommentToken

use of org.jkiss.dbeaver.ui.editors.sql.syntax.tokens.SQLCommentToken in project dbeaver by serge-rider.

the class CopyUnformattedTextAction method run.

@Override
public void run() {
    if (sqlEditor == null) {
        return;
    }
    ITextSelection selection = (ITextSelection) sqlEditor.getSelectionProvider().getSelection();
    IDocument document = sqlEditor.getDocumentProvider().getDocument(sqlEditor.getEditorInput());
    if (document == null) {
        return;
    }
    int startPos, endPos;
    if (selection.getLength() > 1) {
        startPos = selection.getOffset();
        endPos = startPos + selection.getLength();
    } else {
        startPos = 0;
        endPos = document.getLength();
    }
    StringBuilder result = new StringBuilder();
    SQLRuleManager ruleManager = sqlEditor.getRuleManager();
    SQLDialect dialect = sqlEditor.getSyntaxManager().getDialect();
    ruleManager.setRange(document, startPos, endPos - startPos);
    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 = TextUtils.compactWhiteSpaces(comment);
                result.append(comment);
            } else {
                lastWhitespace = false;
                result.append(document.get(tokenOffset, tokenLength));
            }
        }
    } catch (BadLocationException e) {
        // dump
        e.printStackTrace();
    }
    UIUtils.setClipboardContents(Display.getCurrent(), TextTransfer.getInstance(), result.toString().trim());
}
Also used : SQLRuleManager(org.jkiss.dbeaver.ui.editors.sql.syntax.SQLRuleManager) ITextSelection(org.eclipse.jface.text.ITextSelection) IToken(org.eclipse.jface.text.rules.IToken) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLCommentToken(org.jkiss.dbeaver.ui.editors.sql.syntax.tokens.SQLCommentToken) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 IToken (org.eclipse.jface.text.rules.IToken)1 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)1 SQLRuleManager (org.jkiss.dbeaver.ui.editors.sql.syntax.SQLRuleManager)1 SQLCommentToken (org.jkiss.dbeaver.ui.editors.sql.syntax.tokens.SQLCommentToken)1