use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project n4js by eclipse.
the class FixedContentFormatter method createTextEdit.
@Override
protected TextEdit createTextEdit(List<ITextReplacement> replacements) {
if (replacements.isEmpty()) {
return new ReplaceEdit(0, 0, "");
}
ITextRegionAccess regionAccess = replacements.get(0).getTextRegionAccess();
String newDocument = regionAccess.getRewriter().renderToString(replacements);
return new ReplaceEdit(0, regionAccess.regionForDocument().getLength(), newDocument);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-xtend by eclipse.
the class FormatterFacade method format.
public String format(final String xtendCode) {
try {
final XtextResourceSet resourceSet = new XtextResourceSet();
Resource _createResource = this.resourceFactory.createResource(URI.createURI("synthetic://to-be-formatted.xtend"));
final XtextResource resource = ((XtextResource) _createResource);
EList<Resource> _resources = resourceSet.getResources();
_resources.add(resource);
StringInputStream _stringInputStream = new StringInputStream(xtendCode);
resource.load(_stringInputStream, CollectionLiterals.<Object, Object>emptyMap());
final ITextRegionAccess regionAccess = this.regionAccessBuilder.get().forNodeModel(resource).create();
FormatterRequest _formatterRequest = new FormatterRequest();
final Procedure1<FormatterRequest> _function = (FormatterRequest it) -> {
it.setAllowIdentityEdits(false);
it.setTextRegionAccess(regionAccess);
it.setPreferences(TypedPreferenceValues.castOrWrap(this.cfgProvider.getPreferenceValues(resource)));
};
FormatterRequest request = ObjectExtensions.<FormatterRequest>operator_doubleArrow(_formatterRequest, _function);
List<ITextReplacement> replacements = this.formatter.format(request);
return regionAccess.getRewriter().renderToString(replacements);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class RegionDiffFormatter method format.
public List<ITextReplacement> format(ITextRegionAccess regions) {
IFormatter2 fmt = getFormatter();
if (regions instanceof ITextRegionAccessDiff) {
ITextRegionAccessDiff diff = (ITextRegionAccessDiff) regions;
FormatterRequest request = createFormatterRequest(diff);
List<ITextReplacement> replacements = fmt.format(request);
List<ITextReplacement> merged = mergeReplacements(diff, replacements);
return merged;
} else {
FormatterRequest request = createFormatterRequest(regions);
List<ITextReplacement> replacements = fmt.format(request);
return replacements;
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class RelatedXtextResourceUpdater method applyChange.
@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
XtextResource res = (XtextResource) lifecycleManager.openAndApplyReferences(getResourceSet(), getResource());
if (!referenceUpdater.isAffected(deltas, getResource())) {
return;
}
ITextRegionAccess base = textRegionBuilderProvider.get().forNodeModel(res).create();
ITextRegionDiffBuilder rewriter = new StringBasedTextRegionAccessDiffBuilder(base);
ReferenceUpdaterContext context = new ReferenceUpdaterContext(deltas, rewriter, getResource());
referenceUpdater.update(context);
if (!context.getModifications().isEmpty()) {
ChangeRecorder rec = new ChangeRecorder(res);
for (Runnable run : context.getModifications()) {
run.run();
}
ChangeDescription recording = rec.endRecording();
ResourceSet rs = res.getResourceSet();
ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, Collections.emptyList(), recording);
ResourceRecording recordedResource = tree.getRecordedResource(res);
if (recordedResource != null) {
serializer.serializeChanges(recordedResource, rewriter);
}
}
for (IUpdatableReference upd : context.getUpdatableReferences()) {
referenceUpdater.updateReference(rewriter, upd);
}
ITextRegionAccessDiff rewritten = rewriter.create();
List<ITextReplacement> rep = formatter.format(rewritten);
TextDocumentChange change = new TextDocumentChange(rewritten, getResource().getUri(), rep);
changeAcceptor.accept(change);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class FormattingService method format.
/**
* @since 2.14
*/
public List<TextEdit> format(final XtextResource resource, final Document document, final int offset, final int length, final FormattingOptions options) {
String indent = this.indentationInformation.getIndentString();
if ((options != null)) {
boolean _isInsertSpaces = options.isInsertSpaces();
if (_isInsertSpaces) {
indent = Strings.padEnd("", options.getTabSize(), ' ');
}
}
if ((this.formatter2Provider != null)) {
final MapBasedPreferenceValues preferences = new MapBasedPreferenceValues();
preferences.put("indentation", indent);
TextRegion _textRegion = new TextRegion(offset, length);
final List<ITextReplacement> replacements = this.format2(resource, _textRegion, preferences);
final Function1<ITextReplacement, TextEdit> _function = (ITextReplacement r) -> {
return this.toTextEdit(document, r.getReplacementText(), r.getOffset(), r.getLength());
};
return IterableExtensions.<TextEdit>toList(ListExtensions.<ITextReplacement, TextEdit>map(replacements, _function));
} else {
return CollectionLiterals.<TextEdit>newArrayList();
}
}
Aggregations