Search in sources :

Example 31 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project xtext-eclipse by eclipse.

the class TextViewerJoinLinesAction method run.

@Override
public void run() {
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    if (!canModifyViewer())
        return;
    IDocument document = viewer.getDocument();
    if (document == null)
        return;
    ITextSelection selection = getSelection(viewer);
    if (selection == null)
        return;
    int startLine = selection.getStartLine();
    int endLine = selection.getEndLine();
    try {
        int caretOffset = joinLines(document, startLine, endLine);
        if (caretOffset > -1) {
            StyledText widget = viewer.getTextWidget();
            widget.setRedraw(false);
            adjustHighlightRange(viewer, caretOffset, 0);
            viewer.revealRange(caretOffset, 0);
            viewer.setSelectedRange(caretOffset, 0);
            widget.setRedraw(true);
        }
    } catch (BadLocationException e) {
    // should not happen
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 32 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project xtext-eclipse by eclipse.

the class TextViewerMoveLinesAction method runWithEvent.

@Override
public void runWithEvent(Event event) {
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    if (!canModifyViewer())
        return;
    // get involved objects
    IDocument document = viewer.getDocument();
    if (document == null)
        return;
    StyledText widget = viewer.getTextWidget();
    if (widget == null)
        return;
    // get selection
    ITextSelection sel = (ITextSelection) viewer.getSelectionProvider().getSelection();
    if (sel.isEmpty())
        return;
    ITextSelection skippedLine = getSkippedLine(document, sel);
    if (skippedLine == null)
        return;
    try {
        ITextSelection movingArea = getMovingSelection(document, sel, viewer);
        // visible area, bail out
        if (!containedByVisibleRegion(movingArea, viewer) || !containedByVisibleRegion(skippedLine, viewer))
            return;
        // get the content to be moved around: the moving (selected) area and the skipped line
        String moving = movingArea.getText();
        String skipped = skippedLine.getText();
        if (moving == null || skipped == null || document.getLength() == 0)
            return;
        String delim;
        String insertion;
        int offset, deviation;
        if (fUpwards) {
            delim = document.getLineDelimiter(skippedLine.getEndLine());
            if (fCopy) {
                delim = TextUtilities.getDefaultLineDelimiter(document);
                insertion = moving + delim;
                offset = movingArea.getOffset();
                deviation = 0;
            } else {
                Assert.isNotNull(delim);
                insertion = moving + delim + skipped;
                offset = skippedLine.getOffset();
                deviation = -skippedLine.getLength() - delim.length();
            }
        } else {
            delim = document.getLineDelimiter(movingArea.getEndLine());
            if (fCopy) {
                if (delim == null) {
                    delim = TextUtilities.getDefaultLineDelimiter(document);
                    insertion = delim + moving;
                } else {
                    insertion = moving + delim;
                }
                offset = skippedLine.getOffset();
                deviation = movingArea.getLength() + delim.length();
            } else {
                Assert.isNotNull(delim);
                insertion = skipped + delim + moving;
                offset = movingArea.getOffset();
                deviation = skipped.length() + delim.length();
            }
        }
        // modify the document
        beginCompoundEdit();
        if (fCopy) {
            document.replace(offset, 0, insertion);
        } else {
            document.replace(offset, insertion.length(), insertion);
        }
        // move the selection along
        int selOffset = movingArea.getOffset() + deviation;
        int selLength = movingArea.getLength() + (fAddDelimiter ? delim.length() : 0);
        if (!(viewer instanceof ITextViewerExtension5))
            selLength = Math.min(selLength, viewer.getVisibleRegion().getOffset() + viewer.getVisibleRegion().getLength() - selOffset);
        else {
        // TODO need to check what is necessary in the projection case
        }
        selectAndReveal(viewer, selOffset, selLength);
    } catch (BadLocationException x) {
        // won't happen without concurrent modification - bail out
        return;
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 33 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project xtext-eclipse by eclipse.

the class TextViewerOperationAction method run.

/**
 * The <code>TextOperationAction</code> implementation of this <code>IAction</code> method runs the operation with
 * the current operation code.
 */
@Override
public void run() {
    if (fOperationCode == -1 || fOperationTarget == null)
        return;
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    if (!fRunsOnReadOnly && !canModifyViewer())
        return;
    Display display = null;
    Shell shell = viewer.getTextWidget().getShell();
    if (shell != null && !shell.isDisposed())
        display = shell.getDisplay();
    BusyIndicator.showWhile(display, new Runnable() {

        @Override
        public void run() {
            fOperationTarget.doOperation(fOperationCode);
        }
    });
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ITextViewer(org.eclipse.jface.text.ITextViewer) Display(org.eclipse.swt.widgets.Display)

Example 34 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project xtext-eclipse by eclipse.

the class TextViewerOperationAction method update.

/**
 * The <code>TextOperationAction</code> implementation of this <code>IUpdate</code> method discovers the operation
 * through the current editor's <code>ITextOperationTarget</code> adapter, and sets the enabled state accordingly.
 */
@Override
public void update() {
    if (!fAllowUpdate)
        return;
    super.update();
    if (!fRunsOnReadOnly && !canModifyViewer()) {
        setEnabled(false);
        return;
    }
    ITextViewer viewer = getTextViewer();
    if (fOperationTarget == null && viewer != null && fOperationCode != -1)
        fOperationTarget = viewer.getTextOperationTarget();
    boolean isEnabled = (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
    setEnabled(isEnabled);
}
Also used : ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 35 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project xtext-eclipse by eclipse.

the class TextViewerShiftAction method run.

/**
 * The <code>TextOperationAction</code> implementation of this <code>IAction</code> method runs the operation with
 * the current operation code.
 */
@Override
public void run() {
    if (fOperationCode == -1 || fOperationTarget == null)
        return;
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    if (!canModifyViewer())
        return;
    Display display = null;
    Shell shell = viewer.getTextWidget().getShell();
    if (shell != null && !shell.isDisposed())
        display = shell.getDisplay();
    BusyIndicator.showWhile(display, new Runnable() {

        @Override
        public void run() {
            fOperationTarget.doOperation(fOperationCode);
        }
    });
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ITextViewer(org.eclipse.jface.text.ITextViewer) Display(org.eclipse.swt.widgets.Display)

Aggregations

ITextViewer (org.eclipse.jface.text.ITextViewer)75 BadLocationException (org.eclipse.jface.text.BadLocationException)13 IDocument (org.eclipse.jface.text.IDocument)13 Region (org.eclipse.jface.text.Region)13 StyledText (org.eclipse.swt.custom.StyledText)13 IRegion (org.eclipse.jface.text.IRegion)11 ArrayList (java.util.ArrayList)9 Point (org.eclipse.swt.graphics.Point)9 Test (org.junit.Test)9 ITextSelection (org.eclipse.jface.text.ITextSelection)8 IEditorPart (org.eclipse.ui.IEditorPart)8 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)6 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)6 Document (org.eclipse.jface.text.Document)5 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)5 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)5 Display (org.eclipse.swt.widgets.Display)5 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)5 List (java.util.List)4 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)4