Search in sources :

Example 71 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project dbeaver by serge-rider.

the class MorphDelimitedListHandler method execute.

@Override
public Object execute(ExecutionEvent executionEvent) throws ExecutionException {
    Shell activeShell = HandlerUtil.getActiveShell(executionEvent);
    BaseTextEditor textEditor = BaseTextEditor.getTextEditor(HandlerUtil.getActiveEditor(executionEvent));
    if (textEditor != null) {
        IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        ISelectionProvider provider = textEditor.getSelectionProvider();
        if (provider != null) {
            ISelection selection = provider.getSelection();
            if (selection instanceof ITextSelection) {
                ITextSelection textSelection = (ITextSelection) selection;
                if (textSelection.getLength() <= 0) {
                    UIUtils.showMessageBox(activeShell, "Morph text", "Text selection is empty. You need to select some text to morph", SWT.ICON_INFORMATION);
                    return null;
                }
                String formattedText = morphText(activeShell, textSelection.getText());
                if (formattedText != null) {
                    try {
                        document.replace(textSelection.getOffset(), textSelection.getLength(), formattedText);
                    } catch (BadLocationException e) {
                        DBWorkbench.getPlatformUI().showError("Morph text", "Error replacing text", e);
                    }
                }
            }
        }
    }
    return null;
}
Also used : ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) BaseTextEditor(org.jkiss.dbeaver.ui.editors.text.BaseTextEditor) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 72 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project dbeaver by serge-rider.

the class DatabaseLaunchContributionItem method extractSelectedObjects.

protected Object[] extractSelectedObjects() {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null) {
        return NO_OBJECTS;
    }
    ISelection selection = window.getSelectionService().getSelection();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structured = (IStructuredSelection) selection;
        Object[] array = structured.toArray();
        Object o = structured.getFirstElement();
        if (o instanceof IEditorPart) {
            IEditorPart part = (IEditorPart) o;
            array[0] = part.getEditorInput();
        }
        return array;
    }
    if (selection instanceof ITextSelection) {
        IWorkbenchPage activePage = window.getActivePage();
        if (activePage != null) {
            IEditorPart activeEditor = activePage.getActiveEditor();
            DBSObject databaseObject = DebugUI.extractDatabaseObject(activeEditor);
            if (databaseObject != null) {
                return new Object[] { databaseObject };
            }
        }
    }
    return NO_OBJECTS;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ISelection(org.eclipse.jface.viewers.ISelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 73 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project dbeaver by serge-rider.

the class ToggleProcedureBreakpointTarget method toggleLineBreakpoints.

@Override
public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
    IEditorPart editorPart = (IEditorPart) part;
    IResource resource = extractResource(editorPart, selection);
    if (resource == null) {
        return;
    }
    DBSObject databaseObject = DebugUI.extractDatabaseObject(editorPart);
    if (databaseObject == null) {
        return;
    }
    DBNDatabaseNode node = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(new VoidProgressMonitor(), databaseObject, false);
    if (node == null) {
        return;
    }
    String nodeItemPath = node.getNodeItemPath();
    ITextSelection textSelection = (ITextSelection) selection;
    int lineNumber = textSelection.getStartLine();
    IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(DBGConstants.MODEL_IDENTIFIER_DATABASE);
    for (IBreakpoint breakpoint : breakpoints) {
        if (breakpoint instanceof IDatabaseBreakpoint) {
            IDatabaseBreakpoint databaseBreakpoint = (IDatabaseBreakpoint) breakpoint;
            if (nodeItemPath.equals(databaseBreakpoint.getNodePath())) {
                if (((ILineBreakpoint) breakpoint).getLineNumber() == (lineNumber + 1)) {
                    DebugUITools.deleteBreakpoints(new IBreakpoint[] { breakpoint }, part.getSite().getShell(), null);
                    return;
                }
            }
        }
    }
    int charstart = -1, charend = -1;
    DBGBreakpointDescriptor breakpointDescriptor = GeneralUtils.adapt(databaseObject, DBGBreakpointDescriptor.class);
    if (breakpointDescriptor == null) {
        throw new CoreException(GeneralUtils.makeErrorStatus("Object '" + DBUtils.getObjectFullName(databaseObject, DBPEvaluationContext.UI) + "' doesn't support breakpoints"));
    }
    // create line breakpoint (doc line numbers start at 0)
    new DatabaseLineBreakpoint(databaseObject, node, resource, breakpointDescriptor, lineNumber + 1, charstart, charend, true);
}
Also used : DatabaseLineBreakpoint(org.jkiss.dbeaver.debug.core.breakpoints.DatabaseLineBreakpoint) IDatabaseBreakpoint(org.jkiss.dbeaver.debug.core.breakpoints.IDatabaseBreakpoint) IEditorPart(org.eclipse.ui.IEditorPart) DBGBreakpointDescriptor(org.jkiss.dbeaver.debug.DBGBreakpointDescriptor) ITextSelection(org.eclipse.jface.text.ITextSelection) ILineBreakpoint(org.eclipse.debug.core.model.ILineBreakpoint) IBreakpoint(org.eclipse.debug.core.model.IBreakpoint) DatabaseLineBreakpoint(org.jkiss.dbeaver.debug.core.breakpoints.DatabaseLineBreakpoint) IDatabaseBreakpoint(org.jkiss.dbeaver.debug.core.breakpoints.IDatabaseBreakpoint) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) CoreException(org.eclipse.core.runtime.CoreException) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IBreakpoint(org.eclipse.debug.core.model.IBreakpoint) IResource(org.eclipse.core.resources.IResource)

Example 74 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project dbeaver by serge-rider.

the class SQLEditorPropertyTester method test.

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (!(receiver instanceof SQLEditorBase)) {
        return false;
    }
    SQLEditor editor = (SQLEditor) receiver;
    final Control editorControl = editor.getEditorControl();
    if (editorControl == null) {
        return false;
    }
    boolean hasConnection = editor.getDataSourceContainer() != null;
    switch(property) {
        case PROP_CAN_EXECUTE:
            // Do not check hasActiveQuery - sometimes jface don't update action enablement after cursor change/typing
            return true;
        /* && (!"statement".equals(expectedValue) || editor.hasActiveQuery())*/
        case PROP_CAN_EXPLAIN:
            return hasConnection && GeneralUtils.adapt(editor.getDataSource(), DBCQueryPlanner.class) != null;
        case PROP_CAN_NAVIGATE:
            {
                // Check whether some word is under cursor
                ISelectionProvider selectionProvider = editor.getSelectionProvider();
                if (selectionProvider == null) {
                    return false;
                }
                ITextSelection selection = (ITextSelection) selectionProvider.getSelection();
                IDocument document = editor.getDocument();
                return selection != null && document != null && !new SQLIdentifierDetector(editor.getSyntaxManager().getDialect(), editor.getSyntaxManager().getStructSeparator(), editor.getSyntaxManager().getIdentifierQuoteStrings()).detectIdentifier(document, new Region(selection.getOffset(), selection.getLength())).isEmpty();
            }
        case PROP_CAN_EXPORT:
            return hasConnection && editor.hasActiveQuery();
        case PROP_HAS_SELECTION:
            {
                ISelection selection = editor.getSelectionProvider().getSelection();
                return selection instanceof ITextSelection && ((ITextSelection) selection).getLength() > 0;
            }
        case PROP_FOLDING_ENABLED:
            return editor.isFoldingEnabled();
        case PROP_FOLDING_SUPPORTED:
            return editor.getAnnotationModel() != null;
    }
    return false;
}
Also used : Control(org.eclipse.swt.widgets.Control) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) SQLIdentifierDetector(org.jkiss.dbeaver.model.sql.parser.SQLIdentifierDetector) Region(org.eclipse.jface.text.Region) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument)

Example 75 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonContentAssistProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
    if (!(viewer.getDocument() instanceof JsonDocument)) {
        return super.computeCompletionProposals(viewer, documentOffset);
    }
    maybeSwitchScope(documentOffset);
    final JsonDocument document = (JsonDocument) viewer.getDocument();
    final ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    int line = 0, lineOffset = 0, column = 0;
    try {
        line = document.getLineOfOffset(documentOffset);
        lineOffset = document.getLineOffset(line);
        column = selection.getOffset() - lineOffset;
    } catch (BadLocationException e) {
    }
    final String prefix = extractPrefix(viewer, documentOffset);
    // column to resolve the path
    if (!prefix.isEmpty()) {
        column -= prefix.length();
    }
    Model model = document.getModel(documentOffset - prefix.length());
    currentPath = model.getPath(line, column);
    isRefCompletion = referenceProposalProvider.canProvideProposal(model, currentPath);
    Collection<Proposal> p;
    if (isRefCompletion) {
        updateStatus(model);
        p = referenceProposalProvider.getProposals(currentPath, document, currentScope);
    } else {
        clearStatus();
        p = proposalProvider.getProposals(currentPath, model, prefix);
    }
    final Collection<ICompletionProposal> proposals = getCompletionProposals(p, prefix, documentOffset);
    // compute template proposals
    if (!isRefCompletion) {
        final ICompletionProposal[] templateProposals = super.computeCompletionProposals(viewer, documentOffset);
        if (templateProposals != null && templateProposals.length > 0) {
            proposals.addAll(Lists.newArrayList(templateProposals));
        }
    }
    return proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Model(com.reprezen.swagedit.core.model.Model) StyledString(org.eclipse.jface.viewers.StyledString) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)205 ISelection (org.eclipse.jface.viewers.ISelection)65 IDocument (org.eclipse.jface.text.IDocument)52 BadLocationException (org.eclipse.jface.text.BadLocationException)46 IRegion (org.eclipse.jface.text.IRegion)34 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 Region (org.eclipse.jface.text.Region)31 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)17 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 Point (org.eclipse.swt.graphics.Point)13 IResource (org.eclipse.core.resources.IResource)12 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)12 IFile (org.eclipse.core.resources.IFile)11 StyledText (org.eclipse.swt.custom.StyledText)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 Template (org.eclipse.jface.text.templates.Template)10