Search in sources :

Example 26 with TextEdit

use of org.eclipse.text.edits.TextEdit in project che by eclipse.

the class TextChange method getPreviewContent.

/**
	 * Returns a preview of the text change clipped to a specific region.
	 * The preview is created by applying the text edits managed by the
	 * given array of {@link TextEditChangeGroup text edit change groups}.
	 * The region is determined as follows:
	 * <ul>
	 *   <li>if <code>expandRegionToFullLine</code> is <code>false</code>
	 *       then the parameter <code>region</code> determines the clipping.
	 *   </li>
	 *   <li>if <code>expandRegionToFullLine</code> is <code>true</code>
	 *       then the region determined by the parameter <code>region</code>
	 *       is extended to cover full lines.
	 *   </li>
	 *   <li>if <code>surroundingLines</code> &gt; 0 then the given number
	 *       of surrounding lines is added. The value of <code>surroundingLines
	 *       </code> is only considered if <code>expandRegionToFullLine</code>
	 *       is <code>true</code>
	 *   </li>
	 * </ul>
	 *
	 * @param changeGroups a set of change groups for which a preview is to be
	 *  generated
	 * @param region the starting region for the clipping
	 * @param expandRegionToFullLine if <code>true</code> is passed the region
	 *  is extended to cover full lines
	 * @param surroundingLines the number of surrounding lines to be added to
	 *  the clipping region. Is only considered if <code>expandRegionToFullLine
	 *  </code> is <code>true</code>
	 * @param pm a progress monitor to report progress or <code>null</code>
	 *  if no progress reporting is desired
	 *
	 * @return the current content of the text change clipped to a region
	 *  determined by the given parameters.
	 *
	 * @throws CoreException if an exception occurs while generating the preview
	 *
	 * @see #getCurrentContent(IRegion, boolean, int, IProgressMonitor)
	 *
	 * @since 3.2
	 */
public String getPreviewContent(TextEditBasedChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException {
    IRegion currentRegion = getRegion(changeGroups);
    Assert.isTrue(region.getOffset() <= currentRegion.getOffset() && currentRegion.getOffset() + currentRegion.getLength() <= region.getOffset() + region.getLength());
    // Make sure that all edits in the change groups are rooted under the edit the text change stand for.
    TextEdit root = getEdit();
    //$NON-NLS-1$
    Assert.isNotNull(root, "No root edit");
    for (int c = 0; c < changeGroups.length; c++) {
        TextEditBasedChangeGroup group = changeGroups[c];
        TextEdit[] edits = group.getTextEdits();
        for (int e = 0; e < edits.length; e++) {
        // TODO: enable once following bug is fixed
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=130909
        // Assert.isTrue(root == edits[e].getRoot(), "Wrong root edit"); //$NON-NLS-1$
        }
    }
    PreviewAndRegion result = getPreviewDocument(changeGroups, pm);
    int delta;
    if (result.region == null) {
        // all edits were delete edits so no new region
        delta = -currentRegion.getLength();
    } else {
        delta = result.region.getLength() - currentRegion.getLength();
    }
    return getContent(result.document, new Region(region.getOffset(), region.getLength() + delta), expandRegionToFullLine, surroundingLines);
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 27 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class ChangeSerializerTextEditComposer method endRecordingAndCompose.

protected TextEdit endRecordingAndCompose() {
    List<TextEdit> edits = Lists.newArrayList();
    super.endRecordChanges(change -> {
        collectChanges(change, edits);
    });
    if (edits.isEmpty()) {
        return null;
    }
    if (edits.size() == 1) {
        return edits.get(0);
    }
    MultiTextEdit multi = new MultiTextEdit();
    for (TextEdit e : edits) {
        multi.addChild(e);
    }
    return multi;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 28 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class ContentFormatter method format.

@Override
public void format(IDocument document, IRegion region) {
    IXtextDocument doc = (IXtextDocument) document;
    TextEdit r = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));
    try {
        if (r != null)
            r.apply(document);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 29 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class DefaultTextEditComposer method endRecording.

@Override
public TextEdit endRecording() {
    recording = false;
    TextEdit textEdit = getTextEdit();
    reset();
    return textEdit;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit)

Example 30 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class ReconcilingUnitOfWork method exec.

@Override
public T exec(XtextResource state) throws Exception {
    String original = document.get();
    DocumentChangeListener documentChangeListener = new DocumentChangeListener();
    T result;
    try {
        document.addDocumentListener(documentChangeListener);
        // lazy linking URIs might change, so resolve everything before applying any changes
        EcoreUtil2.resolveAll(state, CancelIndicator.NullImpl);
        composer.beginRecording(state);
        result = work.exec(state);
        final TextEdit edit = composer.endRecording();
        if (edit != null) {
            if (documentChangeListener.hasDocumentChanged())
                throw new IllegalStateException("Cannot modify document textually and semantically within the same unit of work");
            RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS | TextEdit.CREATE_UNDO);
            processor.performEdits();
        }
    } catch (RuntimeException e) {
        document.set(original);
        throw e;
    } catch (Exception e) {
        document.set(original);
        throw new RuntimeException(e);
    } finally {
        document.removeDocumentListener(documentChangeListener);
    }
    return result;
}
Also used : RewriteSessionEditProcessor(org.eclipse.jface.text.RewriteSessionEditProcessor) TextEdit(org.eclipse.text.edits.TextEdit)

Aggregations

TextEdit (org.eclipse.text.edits.TextEdit)190 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)113 Test (org.junit.Test)49 IDocument (org.eclipse.jface.text.IDocument)48 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)48 BadLocationException (org.eclipse.jface.text.BadLocationException)34 Document (org.eclipse.jface.text.Document)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 UndoEdit (org.eclipse.text.edits.UndoEdit)26 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)23 InsertEdit (org.eclipse.text.edits.InsertEdit)22 CoreException (org.eclipse.core.runtime.CoreException)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)19 DeleteEdit (org.eclipse.text.edits.DeleteEdit)17 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)16 ArrayList (java.util.ArrayList)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)15 TextEditGroup (org.eclipse.text.edits.TextEditGroup)15