Search in sources :

Example 1 with IRewriteTarget

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

the class QuickDiffRestoreAction method run.

@Override
public void run() {
    ITextEditor editor = getTextEditor();
    if (editor == null || !validateEditorInputState())
        return;
    IRewriteTarget target = editor.getAdapter(IRewriteTarget.class);
    if (target != null)
        target.beginCompoundChange();
    runCompoundChange();
    if (target != null)
        target.endCompoundChange();
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget)

Example 2 with IRewriteTarget

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

the class MoveLinesAction method endCompoundEdit.

/**
 * Ends the compound change.
 */
private void endCompoundEdit() {
    ITextEditor editor = getTextEditor();
    if (!fEditInProgress || editor == null)
        return;
    IRewriteTarget target = editor.getAdapter(IRewriteTarget.class);
    if (target != null) {
        target.endCompoundChange();
    }
    fEditInProgress = false;
}
Also used : IRewriteTarget(org.eclipse.jface.text.IRewriteTarget)

Example 3 with IRewriteTarget

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

the class CompletionProposalPopup 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
 * @param offset the offset
 * @since 2.1
 */
void insertProposal(ICompletionProposal p, char trigger, int stateMask, final int offset) {
    fInserting = true;
    IRewriteTarget target = null;
    IEditingSupport helper = new IEditingSupport() {

        @Override
        public boolean isOriginator(DocumentEvent event, IRegion focus) {
            return focus.getOffset() <= offset && focus.getOffset() + focus.getLength() >= offset;
        }

        @Override
        public boolean ownsFocusShell() {
            return false;
        }
    };
    try {
        IDocument document = fContentAssistSubjectControlAdapter.getDocument();
        if (fViewer instanceof ITextViewerExtension) {
            ITextViewerExtension extension = (ITextViewerExtension) fViewer;
            target = extension.getRewriteTarget();
        }
        if (target != null)
            target.beginCompoundChange();
        if (fViewer instanceof IEditingSupportRegistry) {
            IEditingSupportRegistry registry = (IEditingSupportRegistry) fViewer;
            registry.register(helper);
        }
        if (p instanceof ICompletionProposalExtension2 && fViewer != null) {
            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);
        }
        fireAppliedEvent(p);
        Point selection = p.getSelection(document);
        if (selection != null) {
            fContentAssistSubjectControlAdapter.setSelectedRange(selection.x, selection.y);
            fContentAssistSubjectControlAdapter.revealRange(selection.x, selection.y);
        }
        IContextInformation info = p.getContextInformation();
        if (info != null) {
            int contextInformationOffset;
            if (p instanceof ICompletionProposalExtension) {
                ICompletionProposalExtension e = (ICompletionProposalExtension) p;
                contextInformationOffset = e.getContextInformationPosition();
            } else {
                if (selection == null)
                    selection = fContentAssistSubjectControlAdapter.getSelectedRange();
                contextInformationOffset = selection.x + selection.y;
            }
            fContentAssistant.showContextInformation(info, contextInformationOffset);
        } else
            fContentAssistant.showContextInformation(null, -1);
    } finally {
        if (target != null)
            target.endCompoundChange();
        if (fViewer instanceof IEditingSupportRegistry) {
            IEditingSupportRegistry registry = (IEditingSupportRegistry) fViewer;
            registry.unregister(helper);
        }
        fInserting = false;
    }
}
Also used : IEditingSupport(org.eclipse.jface.text.IEditingSupport) ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) IEditingSupportRegistry(org.eclipse.jface.text.IEditingSupportRegistry) Point(org.eclipse.swt.graphics.Point) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point)

Example 4 with IRewriteTarget

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

the class LinkedModeUI method beginCompoundChangeIfNeeded.

private void beginCompoundChangeIfNeeded() {
    if (!fHasOpenCompoundChange) {
        ITextViewerExtension extension = (ITextViewerExtension) fCurrentTarget.getViewer();
        IRewriteTarget target = extension.getRewriteTarget();
        target.beginCompoundChange();
        fHasOpenCompoundChange = true;
    }
}
Also used : ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget)

Example 5 with IRewriteTarget

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

the class AbstractTextEditor method installTextDragAndDrop.

/**
 * Installs text drag and drop on the given source viewer.
 *
 * @param viewer the viewer
 * @since 3.3
 */
protected void installTextDragAndDrop(final ISourceViewer viewer) {
    if (viewer == null || fIsTextDragAndDropInstalled)
        return;
    final IDragAndDropService dndService = getSite().getService(IDragAndDropService.class);
    if (dndService == null)
        return;
    final StyledText st = viewer.getTextWidget();
    // Install drag source
    final ISelectionProvider selectionProvider = viewer.getSelectionProvider();
    final DragSource source = new DragSource(st, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {

        String fSelectedText;

        Point fSelection;

        @Override
        public void dragStart(DragSourceEvent event) {
            fTextDragAndDropToken = null;
            try {
                fSelection = st.getSelection();
                event.doit = isLocationSelected(new Point(event.x, event.y));
                ISelection selection = selectionProvider.getSelection();
                if (selection instanceof ITextSelection)
                    fSelectedText = ((ITextSelection) selection).getText();
                else
                    // fallback to widget
                    fSelectedText = st.getSelectionText();
            } catch (IllegalArgumentException ex) {
                event.doit = false;
            }
        }

        private boolean isLocationSelected(Point point) {
            // FIXME: https://bugs.eclipse.org/bugs/show_bug.cgi?id=260922
            if (isBlockSelectionModeEnabled())
                return false;
            int offset = st.getOffsetAtPoint(point);
            Point p = st.getLocationAtOffset(offset);
            if (p.x > point.x)
                offset--;
            return offset >= fSelection.x && offset < fSelection.y;
        }

        @Override
        public void dragSetData(DragSourceEvent event) {
            event.data = fSelectedText;
            // Can be any non-null object
            fTextDragAndDropToken = this;
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            try {
                if (event.detail == DND.DROP_MOVE && validateEditorInputState()) {
                    Point newSelection = st.getSelection();
                    int length = fSelection.y - fSelection.x;
                    int delta = 0;
                    if (newSelection.x < fSelection.x)
                        delta = length;
                    // $NON-NLS-1$
                    st.replaceTextRange(fSelection.x + delta, length, "");
                    if (fTextDragAndDropToken == null) {
                        // Move in same editor - end compound change
                        IRewriteTarget target = getAdapter(IRewriteTarget.class);
                        if (target != null)
                            target.endCompoundChange();
                    }
                }
            } finally {
                fTextDragAndDropToken = null;
            }
        }
    });
    // Install drag target
    DropTargetListener dropTargetListener = new DropTargetAdapter() {

        private Point fSelection;

        @Override
        public void dragEnter(DropTargetEvent event) {
            fTextDragAndDropToken = null;
            fSelection = st.getSelection();
            if (event.detail == DND.DROP_DEFAULT) {
                if ((event.operations & DND.DROP_MOVE) != 0) {
                    event.detail = DND.DROP_MOVE;
                } else if ((event.operations & DND.DROP_COPY) != 0) {
                    event.detail = DND.DROP_COPY;
                } else {
                    event.detail = DND.DROP_NONE;
                }
            }
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT) {
                if ((event.operations & DND.DROP_MOVE) != 0) {
                    event.detail = DND.DROP_MOVE;
                } else if ((event.operations & DND.DROP_COPY) != 0) {
                    event.detail = DND.DROP_COPY;
                } else {
                    event.detail = DND.DROP_NONE;
                }
            }
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            event.feedback |= DND.FEEDBACK_SCROLL;
        }

        @Override
        public void drop(DropTargetEvent event) {
            try {
                if (fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
                    // Move in same editor
                    int caretOffset = st.getCaretOffset();
                    if (fSelection.x <= caretOffset && caretOffset <= fSelection.y) {
                        event.detail = DND.DROP_NONE;
                        return;
                    }
                    // Start compound change
                    IRewriteTarget target = getAdapter(IRewriteTarget.class);
                    if (target != null)
                        target.beginCompoundChange();
                }
                if (!validateEditorInputState()) {
                    event.detail = DND.DROP_NONE;
                    return;
                }
                String text = (String) event.data;
                if (isBlockSelectionModeEnabled()) {
                // FIXME fix block selection and DND
                // if (fTextDNDColumnSelection != null && fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
                // // DND_MOVE within same editor - remove origin before inserting
                // Rectangle newSelection= st.getColumnSelection();
                // st.replaceColumnSelection(fTextDNDColumnSelection, ""); //$NON-NLS-1$
                // st.replaceColumnSelection(newSelection, text);
                // st.setColumnSelection(newSelection.x, newSelection.y, newSelection.x + fTextDNDColumnSelection.width - fTextDNDColumnSelection.x, newSelection.y + fTextDNDColumnSelection.height - fTextDNDColumnSelection.y);
                // } else {
                // Point newSelection= st.getSelection();
                // st.insert(text);
                // IDocument document= getDocumentProvider().getDocument(getEditorInput());
                // int startLine= st.getLineAtOffset(newSelection.x);
                // int startColumn= newSelection.x - st.getOffsetAtLine(startLine);
                // int endLine= startLine + document.computeNumberOfLines(text);
                // int endColumn= startColumn + TextUtilities.indexOf(document.getLegalLineDelimiters(), text, 0)[0];
                // st.setColumnSelection(startColumn, startLine, endColumn, endLine);
                // }
                } else {
                    Point newSelection = st.getSelection();
                    try {
                        int modelOffset = widgetOffset2ModelOffset(viewer, newSelection.x);
                        viewer.getDocument().replace(modelOffset, 0, text);
                    } catch (BadLocationException e) {
                        return;
                    }
                    st.setSelectionRange(newSelection.x, text.length());
                }
            } finally {
                fTextDragAndDropToken = null;
            }
        }
    };
    dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] { TextTransfer.getInstance() }, dropTargetListener);
    fIsTextDragAndDropInstalled = true;
}
Also used : DragSourceAdapter(org.eclipse.swt.dnd.DragSourceAdapter) StyledText(org.eclipse.swt.custom.StyledText) IDragAndDropService(org.eclipse.ui.dnd.IDragAndDropService) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DragSource(org.eclipse.swt.dnd.DragSource) Point(org.eclipse.swt.graphics.Point) ITextSelection(org.eclipse.jface.text.ITextSelection) Point(org.eclipse.swt.graphics.Point) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) ISelection(org.eclipse.jface.viewers.ISelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

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