use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-eclipse by eclipse.
the class ContentFormatter method initRequest.
protected void initRequest(IXtextDocument document, IRegion region, XtextResource resource, FormatterRequest request) {
ITextRegion textRegion = new TextRegion(region.getOffset(), region.getLength());
request.setAllowIdentityEdits(false);
request.setFormatUndefinedHiddenRegionsOnly(false);
request.setRegions(singletonList(textRegion));
ITextRegionAccess tokenAccess = regionBuilder.forNodeModel(resource).create();
IPreferenceValues preferenceValues = preferencesProvider.getPreferenceValues(resource);
request.setPreferences(TypedPreferenceValues.castOrWrap(preferenceValues));
request.setTextRegionAccess(tokenAccess);
if (tokenAccess.hasSyntaxError())
request.setExceptionHandler(ExceptionAcceptor.IGNORING);
else
request.setExceptionHandler(ExceptionAcceptor.LOGGING);
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.
the class FormattingConflictTest method enableDebugTracingFalse.
@Test
public void enableDebugTracingFalse() {
final Wrapper<Throwable> wrapper = new Wrapper<Throwable>();
final Wrapper<Integer> execution = new Wrapper<Integer>(Integer.valueOf(0));
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist a");
_builder.newLine();
it.setToBeFormatted(_builder);
final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
Integer _get = execution.get();
int _plus = ((_get).intValue() + 1);
execution.set(Integer.valueOf(_plus));
final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
it_1.setSpace(" ");
};
document.append(regions.regionFor(model).keyword("idlist"), _function);
final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
it_1.setSpace("\t");
};
document.append(regions.regionFor(model).keyword("idlist"), _function_1);
}
};
it.setFormatter(_function_1);
FormatterRequest _request = it.getRequest();
_request.setEnableDebugTracing(false);
FormatterRequest _request_1 = it.getRequest();
final IAcceptor<Exception> _function_2 = (Exception e) -> {
wrapper.set(e);
};
_request_1.setExceptionHandler(_function_2);
};
this._genericFormatterTester.assertFormatted(_function);
Throwable _get = wrapper.get();
final ConflictingRegionsException exception = ((ConflictingRegionsException) _get);
Assert.assertEquals(2, (execution.get()).intValue());
Assert.assertEquals(2, exception.getTraces().size());
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.
the class FormattingConflictTest method enableDebugTracingTrue.
@Test
public void enableDebugTracingTrue() {
final Wrapper<Throwable> wrapper = new Wrapper<Throwable>();
final Wrapper<Integer> execution = new Wrapper<Integer>(Integer.valueOf(0));
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist a");
_builder.newLine();
it.setToBeFormatted(_builder);
final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
Integer _get = execution.get();
int _plus = ((_get).intValue() + 1);
execution.set(Integer.valueOf(_plus));
final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
it_1.setSpace(" ");
};
document.append(regions.regionFor(model).keyword("idlist"), _function);
final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
it_1.setSpace("\t");
};
document.append(regions.regionFor(model).keyword("idlist"), _function_1);
}
};
it.setFormatter(_function_1);
FormatterRequest _request = it.getRequest();
_request.setEnableDebugTracing(true);
FormatterRequest _request_1 = it.getRequest();
final IAcceptor<Exception> _function_2 = (Exception e) -> {
wrapper.set(e);
};
_request_1.setExceptionHandler(_function_2);
};
this._genericFormatterTester.assertFormatted(_function);
Throwable _get = wrapper.get();
final ConflictingRegionsException exception = ((ConflictingRegionsException) _get);
Assert.assertEquals(1, (execution.get()).intValue());
Assert.assertEquals(2, exception.getTraces().size());
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project n4js by eclipse.
the class N4MFFormatterTest method formatted.
/**
* @param expectation
* the expected formatted code
* @param resource
* the Xtext resource to be formatted
* @param offset
* the optional start offset from where formatting should be applied
* @param to
* the optional to offset to which formatting should be applied
*/
@ParameterParser(syntax = "('from' arg2=OFFSET 'to' arg3=OFFSET)?")
@Xpect
public void formatted(@StringExpectation(whitespaceSensitive = true) IStringExpectation expectation, @ThisResource XtextResource resource, int offset, int to) {
ICompositeNode rootNode = resource.getParseResult().getRootNode();
FormatterRequest request = requestProvider.get().setTextRegionAccess(regionAccessBuilderProvider.get().forNodeModel(resource).create());
int totalLength = rootNode.getTotalLength();
if (offset >= 0 && to > offset) {
request.setRegions(Collections.singleton(new TextRegion(offset, to - offset)));
} else {
request.setRegions(Collections.singleton(new TextRegion(offset, totalLength - offset)));
}
List<ITextReplacement> replacements = formatter.format(request);
String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
if (offset >= 0 && to > offset) {
formatted = formatted.substring(offset, formatted.length() - (totalLength - to));
} else {
formatted = formatted.substring(offset);
}
if (isUnixEnding()) {
formatted = formatted.replaceAll("\r\n", "\n");
} else if (isWindowsEnding()) {
if (!rootNode.getText().contains("\r\n")) {
formatted = formatted.replaceAll("\r\n", "\n");
} else {
formatted = formatted.replaceAll("(!\r)\n", "\r\n");
}
}
expectation.assertEquals(formatted);
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-xtend by eclipse.
the class ConvertJavaCode method formatXtendCode.
private String formatXtendCode(IFile xtendFile, final String xtendCode) {
try {
XtextResource resource = (XtextResource) createResource(xtendFile, xtendCode);
ITextRegionAccess regionAccess = regionAccessBuilder.get().forNodeModel(resource).create();
FormatterRequest request = new FormatterRequest();
request.setAllowIdentityEdits(false);
request.setTextRegionAccess(regionAccess);
request.setPreferences(TypedPreferenceValues.castOrWrap(cfgProvider.getPreferenceValues(resource)));
List<ITextReplacement> replacements = formatter.format(request);
String formatted = regionAccess.getRewriter().renderToString(replacements);
return formatted;
} catch (Exception e) {
LOG.error("Formatting step canceled due to an exception.", e);
return null;
}
}
Aggregations