use of org.eclipse.jface.text.ITextSelection 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());
}
use of org.eclipse.jface.text.ITextSelection in project dbeaver by serge-rider.
the class NavigateObjectHandler method getCurrentHyperlink.
private IHyperlink getCurrentHyperlink(SQLEditorBase editor) {
SQLHyperlinkDetector hyperlinkDetector = new SQLHyperlinkDetector(editor, editor.getSyntaxManager());
ITextSelection selection = (ITextSelection) editor.getTextViewer().getSelection();
IRegion curRegion = new Region(selection.getOffset(), 0);
IHyperlink[] hyperLinks = hyperlinkDetector.detectHyperlinks(editor.getTextViewer(), curRegion, false);
return ArrayUtils.isEmpty(hyperLinks) ? null : hyperLinks[0];
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class RailroadSelectionLinker method textSelectionChanged.
public void textSelectionChanged(SelectionChangedEvent event) {
ISelection selection = event.getSelection();
if (selection instanceof ITextSelection && !selection.isEmpty()) {
ITextSelection textSelection = (ITextSelection) selection;
int offset = textSelection.getOffset();
IFigure figureToBeSelected = findFigureForTextOffset(view.getContents(), offset, null);
if (figureToBeSelected != null) {
selectFigure(figureToBeSelected);
view.reveal(figureToBeSelected);
}
}
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class AbstractAutoEditTest method assertState.
protected void assertState(XtextEditor editor, String string) {
int cursor = string.indexOf('|');
assertEquals(string.replace("|", ""), editor.getDocument().get());
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
assertEquals("unexpected cursor position:", cursor, selection.getOffset());
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class AbstractCursorHandlingTest method assertState.
protected void assertState(String string, XtextEditor editor) {
int cursor = string.indexOf('|');
assertEquals(string.replace("|", ""), editor.getDocument().get());
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
assertEquals("unexpected cursor position:", cursor, selection.getOffset());
}
Aggregations