Search in sources :

Example 11 with IRewriteTarget

use of org.eclipse.jface.text.IRewriteTarget in project eclipse.platform.text by eclipse.

the class CompletionProposalPopup2 method insertProposal.

/**
 * Applies the given proposal at the given offset. The given character is the
 * one that triggered the insertion of this proposal.
 *
 * @param p the completion proposal
 * @param trigger the trigger character
 * @param stateMask the state mask of the keyboard event triggering the insertion
 * @param offset the offset
 * @since 2.1
 */
private void insertProposal(ICompletionProposal p, char trigger, int stateMask, final int offset) {
    fInserting = true;
    IRewriteTarget target = null;
    IEditingSupportRegistry registry = null;
    try {
        IDocument document = fViewer.getDocument();
        if (fViewer instanceof ITextViewerExtension) {
            ITextViewerExtension extension = (ITextViewerExtension) fViewer;
            target = extension.getRewriteTarget();
        }
        if (target != null)
            target.beginCompoundChange();
        if (fViewer instanceof IEditingSupportRegistry) {
            registry = (IEditingSupportRegistry) fViewer;
            registry.register(fModificationEditingSupport);
        }
        if (p instanceof ICompletionProposalExtension2) {
            ICompletionProposalExtension2 e = (ICompletionProposalExtension2) p;
            e.apply(fViewer, trigger, stateMask, offset);
        } else if (p instanceof ICompletionProposalExtension) {
            ICompletionProposalExtension e = (ICompletionProposalExtension) p;
            e.apply(document, trigger, offset);
        } else {
            p.apply(document);
        }
        Point selection = p.getSelection(document);
        if (selection != null) {
            fViewer.setSelectedRange(selection.x, selection.y);
            fViewer.revealRange(selection.x, selection.y);
        }
        IContextInformation info = p.getContextInformation();
        if (info != null) {
            int position;
            if (p instanceof ICompletionProposalExtension) {
                ICompletionProposalExtension e = (ICompletionProposalExtension) p;
                position = e.getContextInformationPosition();
            } else {
                if (selection == null)
                    selection = fViewer.getSelectedRange();
                position = selection.x + selection.y;
            }
            fContentAssistant.showContextInformation(info, position);
        }
        fContentAssistant.fireProposalChosen(p);
    } finally {
        if (target != null)
            target.endCompoundChange();
        if (registry != null)
            registry.unregister(fModificationEditingSupport);
        fInserting = false;
    }
}
Also used : ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) IEditingSupportRegistry(org.eclipse.jface.text.IEditingSupportRegistry) ICompletionProposalExtension(org.eclipse.jface.text.contentassist.ICompletionProposalExtension) IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) ICompletionProposalExtension2(org.eclipse.jface.text.contentassist.ICompletionProposalExtension2) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point)

Example 12 with IRewriteTarget

use of org.eclipse.jface.text.IRewriteTarget in project eclipse.platform.text by eclipse.

the class LinkedModeUI method endCompoundChangeIfNeeded.

private void endCompoundChangeIfNeeded() {
    if (fHasOpenCompoundChange) {
        ITextViewerExtension extension = (ITextViewerExtension) fCurrentTarget.getViewer();
        IRewriteTarget target = extension.getRewriteTarget();
        target.endCompoundChange();
        fHasOpenCompoundChange = false;
    }
}
Also used : ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget)

Example 13 with IRewriteTarget

use of org.eclipse.jface.text.IRewriteTarget in project linuxtools by eclipse.

the class IndentHandler method execute.

@Override
public Object execute(ExecutionEvent event) {
    // the framework
    if (!isEnabled())
        return null;
    ITextEditor editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
    if (editor == null || !editor.isEditable()) {
        return null;
    }
    ITextSelection selection = getSelection(editor);
    final IDocument document = getDocument(editor);
    if (document != null) {
        final int offset = selection.getOffset();
        final int length = selection.getLength();
        final Position end = new Position(offset + length);
        final int firstLine, nLines;
        fCaretOffset = -1;
        try {
            firstLine = document.getLineOfOffset(offset);
            // check for marginal (zero-length) lines
            int minusOne = length == 0 ? 0 : 1;
            nLines = document.getLineOfOffset(offset + length - minusOne) - firstLine + 1;
            document.addPosition(end);
        } catch (BadLocationException e) {
            // will only happen on concurrent modification
            // $NON-NLS-1$
            IDEPlugin.log(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, IStatus.OK, "", e));
            return null;
        }
        Runnable runnable = () -> {
            IRewriteTarget target = editor.getAdapter(IRewriteTarget.class);
            if (target != null) {
                target.beginCompoundChange();
            }
            try {
                STPHeuristicScanner scanner = new STPHeuristicScanner(document);
                STPIndenter indenter = new STPIndenter(document, scanner, getProject(editor));
                final boolean multiLine = nLines > 1;
                boolean hasChanged = false;
                for (int i = 0; i < nLines; i++) {
                    hasChanged |= indentLine(document, firstLine + i, offset, indenter, scanner, multiLine);
                }
                // update caret position: move to new position when
                // indenting just one line
                // keep selection when indenting multiple
                int newOffset, newLength;
                if (!fIsTabAction && multiLine) {
                    newOffset = offset;
                    newLength = end.getOffset() - offset;
                } else {
                    newOffset = fCaretOffset;
                    newLength = 0;
                }
                // but not when we had a single line non-tab invocation
                if (newOffset != -1 && (hasChanged || newOffset != offset || newLength != length))
                    selectAndReveal(editor, newOffset, newLength);
            } catch (BadLocationException e) {
                // will only happen on concurrent modification
                IDEPlugin.log(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, IStatus.OK, "ConcurrentModification in IndentAction", // $NON-NLS-1$
                e));
            } finally {
                document.removePosition(end);
                if (target != null) {
                    target.endCompoundChange();
                }
            }
        };
        if (nLines > 50) {
            Display display = editor.getEditorSite().getWorkbenchWindow().getShell().getDisplay();
            BusyIndicator.showWhile(display, runnable);
        } else {
            runnable.run();
        }
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) STPIndenter(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPIndenter) Position(org.eclipse.jface.text.Position) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) STPHeuristicScanner(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPHeuristicScanner) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) Display(org.eclipse.swt.widgets.Display)

Aggregations

IRewriteTarget (org.eclipse.jface.text.IRewriteTarget)13 IDocument (org.eclipse.jface.text.IDocument)5 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)4 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Point (org.eclipse.swt.graphics.Point)3 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)3 IEditingSupportRegistry (org.eclipse.jface.text.IEditingSupportRegistry)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 DocumentEvent (org.eclipse.jface.text.DocumentEvent)1 IEditingSupport (org.eclipse.jface.text.IEditingSupport)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1 ICompletionProposalExtension (org.eclipse.jface.text.contentassist.ICompletionProposalExtension)1 ICompletionProposalExtension2 (org.eclipse.jface.text.contentassist.ICompletionProposalExtension2)1 IContextInformation (org.eclipse.jface.text.contentassist.IContextInformation)1 ISelection (org.eclipse.jface.viewers.ISelection)1