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> > 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);
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations