use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class FormatterTestHelper method assertReplacementsAreInRegion.
protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
Set<ITextReplacement> invalid = Sets.newHashSet();
ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
if (allowed.contains(r))
continue ALLOWED;
invalid.add(r);
}
if (!invalid.isEmpty()) {
String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class RecordingXtextResourceUpdater method applyChange.
@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
Resource resource = snapshot.getResource();
ResourceSet rs = resource.getResourceSet();
ReferenceUpdaterContext ctx = new ReferenceUpdaterContext(deltas, document);
if (serializer.isUpdateCrossReferences()) {
referenceUpdater.update(ctx);
for (Runnable run : ctx.getModifications()) {
run.run();
}
}
ChangeDescription recording = recorder.endRecording();
if (recording != null) {
List<IResourceSnapshot> snapshots = Collections.singletonList(snapshot);
ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, snapshots, recording);
ResourceRecording recordedResource = tree.getRecordedResource(resource);
partialSerializer.serializeChanges(recordedResource, document);
}
List<IUpdatableReference> updatableReferences = ctx.getUpdatableReferences();
for (IUpdatableReference upd : updatableReferences) {
referenceUpdater.updateReference(document, upd);
}
ITextRegionAccessDiff rewritten = document.create();
List<ITextReplacement> rep = formatter.format(rewritten);
URI oldUri = snapshot.getURI();
TextDocumentChange change = new TextDocumentChange(rewritten, oldUri, rep);
changeAcceptor.accept(change);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class RegionDiffFormatter method mergeReplacements.
protected List<ITextReplacement> mergeReplacements(ITextRegionAccessDiff regions, List<ITextReplacement> rep) {
ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
List<ITextReplacement> result = Lists.newArrayList();
for (ITextSegmentDiff r : regions.getRegionDifferences()) {
ITextSegment original = r.getOriginalRegion();
ITextSegment modified = r.getModifiedRegion();
List<ITextReplacement> local = Lists.newArrayList();
for (ITextReplacement re : rep) {
if (modified.contains(re)) {
local.add(re);
}
}
String text;
if (local.isEmpty()) {
text = modified.getText();
} else {
text = regions.getRewriter().renderToString(modified, local);
}
ITextReplacement replacement = rewriter.createReplacement(original.getOffset(), original.getLength(), text);
result.add(replacement);
}
return result;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class CodeActionService method recordWorkspaceEdit.
private WorkspaceEdit recordWorkspaceEdit(final Document doc, final XtextResource resource, final IChangeSerializer.IModification<Resource> mod) {
try {
final XtextResourceSet rs = new XtextResourceSet();
final Resource copy = rs.createResource(resource.getURI());
String _text = resource.getParseResult().getRootNode().getText();
StringInputStream _stringInputStream = new StringInputStream(_text);
copy.load(_stringInputStream, CollectionLiterals.<Object, Object>emptyMap());
this.serializer.<Resource>addModification(copy, mod);
final ArrayList<IEmfResourceChange> documentchanges = CollectionLiterals.<IEmfResourceChange>newArrayList();
this.serializer.applyModifications(CollectionBasedAcceptor.<IEmfResourceChange>of(documentchanges));
WorkspaceEdit _workspaceEdit = new WorkspaceEdit();
final Procedure1<WorkspaceEdit> _function = (WorkspaceEdit it) -> {
Iterable<ITextDocumentChange> _filter = Iterables.<ITextDocumentChange>filter(documentchanges, ITextDocumentChange.class);
for (final ITextDocumentChange documentchange : _filter) {
{
final Function1<ITextReplacement, TextEdit> _function_1 = (ITextReplacement replacement) -> {
TextEdit _textEdit = new TextEdit();
final Procedure1<TextEdit> _function_2 = (TextEdit it_1) -> {
it_1.setNewText(replacement.getReplacementText());
Position _position = doc.getPosition(replacement.getOffset());
Position _position_1 = doc.getPosition(replacement.getEndOffset());
Range _range = new Range(_position, _position_1);
it_1.setRange(_range);
};
return ObjectExtensions.<TextEdit>operator_doubleArrow(_textEdit, _function_2);
};
final List<TextEdit> edits = ListExtensions.<ITextReplacement, TextEdit>map(documentchange.getReplacements(), _function_1);
it.getChanges().put(documentchange.getNewURI().toString(), edits);
}
}
};
return ObjectExtensions.<WorkspaceEdit>operator_doubleArrow(_workspaceEdit, _function);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextRegionRewriter method renderToAppendable.
@Override
public void renderToAppendable(ITextSegment input, Iterable<? extends ITextReplacement> rep, Appendable result) throws IOException {
int offset = input.getOffset();
String text = input.getText();
List<ITextReplacement> list = new TextReplacementList<ITextReplacement>(rep);
Collections.sort(list);
int lastOffset = 0;
for (ITextReplacement r : list) {
result.append(text.subSequence(lastOffset, r.getOffset() - offset));
result.append(r.getReplacementText());
lastOffset = (r.getOffset() - offset) + r.getLength();
}
if (lastOffset <= text.length())
result.append(text.subSequence(lastOffset, text.length()));
}
Aggregations