use of org.jkiss.dbeaver.ui.editors.sql.syntax.SQLRuleManager 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());
}
Aggregations