use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess 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.ITextRegionAccess 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.ITextRegionAccess in project xtext-xtend by eclipse.
the class RichStringFormatter method setSpace.
protected void setSpace(final IFormattableDocument doc, final int offset, final int length, final String space) {
IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
it.setSpace(space);
};
final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
AbstractFormatter2 _formatter = doc.getFormatter();
ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
doc.addReplacer(replacer);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-core by eclipse.
the class PartialSerializer method findRegions.
protected List<IAstRegion> findRegions(IEObjectRegion owner, FeatureChange change) {
EStructuralFeature feature = change.getFeature();
if (feature instanceof EReference && ((EReference) feature).isContainment()) {
ITextRegionAccess access = owner.getTextRegionAccess();
Set<EObject> children = Sets.newHashSet();
for (ListChange lc : change.getListChanges()) {
children.addAll(lc.getReferenceValues());
}
for (Object obj : (List<?>) owner.getSemanticElement().eGet(feature)) {
children.add((EObject) obj);
}
List<IEObjectRegion> result = Lists.newArrayList();
for (EObject obj : children) {
IEObjectRegion region = access.regionForEObject(obj);
if (region != null) {
result.add(region);
}
}
Collections.sort(result, (a, b) -> a.getOffset() - b.getOffset());
return ImmutableList.copyOf(result);
} else {
return ImmutableList.copyOf(owner.getRegionFor().features(feature));
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess 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;
}
}
Aggregations