use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.
the class Serializer method serialize.
protected void serialize(EObject obj, Appendable appendable, SaveOptions options) throws IOException {
ITextRegionAccess regionAccess = serializeToRegions(obj);
FormatterRequest request = formatterRequestProvider.get();
request.setFormatUndefinedHiddenRegionsOnly(!options.isFormatting());
request.setTextRegionAccess(regionAccess);
IFormatter2 formatter2 = formatter2Provider.get();
List<ITextReplacement> replacements = formatter2.format(request);
regionAccess.getRewriter().renderToAppendable(replacements, appendable);
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project n4js by eclipse.
the class FormatterXpectMethod method formattedLines.
/**
*/
@Xpect
@ParameterParser(syntax = "arg1=INT")
public void formattedLines(@StringExpectation(whitespaceSensitive = true) IStringExpectation exp, // arg1
int lines, XpectInvocation inv, TargetSyntaxSupport syntax, ITextRegionAccess reg, ISetupInitializer<Preferences> prefInit) {
ITextSegment region = getRegionForLines(reg, syntax.findFirstSemanticCharAfterStatement(inv), lines);
Preferences prefs = new Preferences();
// First put some defaults
prefs.put(N4JSFormatterPreferenceKeys.FORMAT_PARENTHESIS, true);
prefs.put(FormatterPreferenceKeys.lineSeparator, "\n");
// Second init from concrete tests - will override defaults.
prefInit.initialize(prefs);
IFormatter2 formatter = formatterProvider.get();
FormatterRequest request = formatterRequestProvider.get();
request.setTextRegionAccess(reg);
request.setExceptionHandler(ExceptionAcceptor.THROWING);
// needed in case a check like this will be implemented:
// org.eclipse.xtext.testing.formatter.FormatterTester.assertAllWhitespaceIsFormatted()
request.setAllowIdentityEdits(true);
request.setFormatUndefinedHiddenRegionsOnly(false);
request.addRegion(region);
request.setPreferences(prefs);
List<ITextReplacement> replacements = formatter.format(request);
String fmt = reg.getRewriter().renderToString(replacements);
ITextSegment doc = reg.regionForDocument();
int endIndex = region.getEndOffset() + (fmt.length() - doc.getLength()) - 1;
String selection = fmt.substring(region.getOffset(), endIndex);
exp.assertEquals(selection);
}
use of org.eclipse.xtext.formatting2.FormatterRequest 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.FormatterRequest in project xtext-core by eclipse.
the class TextReplacerContext method addReplacement.
@Override
public void addReplacement(ITextReplacement replacement) {
Preconditions.checkNotNull(replacer);
ITextSegment replacerRegion = replacer.getRegion();
FormatterRequest request = document.getRequest();
if (!replacerRegion.contains(replacement)) {
String frameTitle = replacer.getClass().getSimpleName();
ITextSegment frameRegion = replacer.getRegion();
String replacerTitle = replacement.getReplacementText();
RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, (ITextSegment) replacement));
request.getExceptionHandler().accept(exception);
return;
}
if (!isInRequestedRange(replacement)) {
return;
}
if (!isInUndefinedRegion(replacement)) {
if (request.isFormatUndefinedHiddenRegionsOnly()) {
return;
}
if (!request.allowIdentityEdits() && isIdentityEdit(replacement)) {
return;
}
}
try {
replacements.add(replacement);
} catch (ConflictingRegionsException e) {
request.getExceptionHandler().accept(e);
}
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.
the class AbstractFormatter2 method format.
@Override
public final List<ITextReplacement> format(FormatterRequest request) {
try {
initialize(request);
XtextResource xtextResource = request.getTextRegionAccess().getResource();
IFormattableDocument document = createFormattableRootDocument();
try {
format(xtextResource, document);
} catch (RegionTraceMissingException e) {
document = handleTraceMissing(document, e);
}
List<ITextReplacement> rendered = document.renderToTextReplacements();
List<ITextReplacement> postprocessed = postProcess(document, rendered);
return postprocessed;
} finally {
reset();
}
}
Aggregations