Search in sources :

Example 1 with ITextSelection

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());
}
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)

Example 2 with ITextSelection

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];
}
Also used : SQLHyperlinkDetector(org.jkiss.dbeaver.ui.editors.sql.syntax.SQLHyperlinkDetector) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITextSelection(org.eclipse.jface.text.ITextSelection) IRegion(org.eclipse.jface.text.IRegion)

Example 3 with ITextSelection

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);
        }
    }
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection) IFigure(org.eclipse.draw2d.IFigure)

Example 4 with ITextSelection

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());
}
Also used : Point(org.eclipse.swt.graphics.Point) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 5 with ITextSelection

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());
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)200 ISelection (org.eclipse.jface.viewers.ISelection)64 IDocument (org.eclipse.jface.text.IDocument)50 BadLocationException (org.eclipse.jface.text.BadLocationException)45 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 IRegion (org.eclipse.jface.text.IRegion)33 Region (org.eclipse.jface.text.Region)29 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)26 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)20 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)16 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 IResource (org.eclipse.core.resources.IResource)12 IFile (org.eclipse.core.resources.IFile)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)11 Template (org.eclipse.jface.text.templates.Template)10 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)10 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)10