use of org.eclipse.xtext.formatting2.FormatterRequest 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.FormatterRequest in project xtext-core by eclipse.
the class FormatterTestHelper method assertFormatted.
public void assertFormatted(FormatterTestRequest req) {
checkNotNull(req);
checkNotNull(req.getToBeFormatted());
FormatterRequest request = req.getRequest();
checkArgument(request.getTextRegionAccess() == null);
String document = req.getToBeFormatted().toString();
XtextResource parsed = parse(document);
if (req.isAllowSyntaxErrors()) {
if (request.getExplicitExceptionHandler() == null) {
request.setExceptionHandler(ExceptionAcceptor.IGNORING);
}
} else {
assertNoSyntaxErrors(parsed);
if (request.getExplicitExceptionHandler() == null) {
request.setExceptionHandler(ExceptionAcceptor.THROWING);
}
}
request.setTextRegionAccess(createRegionAccess(parsed, req));
if (request.getPreferences() == null)
request.setPreferences(new MapBasedPreferenceValues(Maps.<String, String>newLinkedHashMap()));
List<ITextReplacement> replacements = createFormatter(req).format(request);
assertReplacementsAreInRegion(replacements, request.getRegions(), document);
if (!req.isAllowUnformattedWhitespace())
assertAllWhitespaceIsFormatted(request.getTextRegionAccess(), replacements);
String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
Assert.assertEquals(req.getExpectationOrToBeFormatted().toString(), formatted);
// TODO: assert formatting a second time only produces identity replacements
// TODO: assert formatting with undefined whitespace only
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-eclipse by eclipse.
the class FormatterTester method assertFormatted.
public void assertFormatted(FormatterTestRequest req) {
checkNotNull(req);
checkNotNull(req.getToBeFormatted());
FormatterRequest request = req.getRequest();
checkArgument(request.getTextRegionAccess() == null);
String document = req.getToBeFormatted().toString();
XtextResource parsed = parse(document);
if (req.isAllowSyntaxErrors()) {
request.setExceptionHandler(ExceptionAcceptor.IGNORING);
} else {
assertNoSyntaxErrors(parsed);
request.setExceptionHandler(ExceptionAcceptor.THROWING);
}
request.setTextRegionAccess(createRegionAccess(parsed, req));
if (request.getPreferences() == null)
request.setPreferences(new MapBasedPreferenceValues(Maps.<String, String>newLinkedHashMap()));
List<ITextReplacement> replacements = createFormatter(req).format(request);
assertReplacementsAreInRegion(replacements, request.getRegions(), document);
if (!req.isAllowUnformattedWhitespace())
assertAllWhitespaceIsFormatted(request.getTextRegionAccess(), replacements);
String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
Assert.assertEquals(req.getExpectationOrToBeFormatted().toString(), formatted);
// TODO: assert formatting a second time only produces identity replacements
// TODO: assert formatting with undefined whitespace only
}
use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-eclipse by eclipse.
the class ContentFormatter method exec.
protected TextEdit exec(IXtextDocument document, IRegion region, XtextResource resource) throws Exception {
try {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null && parseResult.getRootASTElement() != null) {
FormatterRequest request = requestProvider.get();
initRequest(document, region, resource, request);
IFormatter2 formatter = formatterProvider.get();
List<ITextReplacement> replacements = formatter.format(request);
final TextEdit mte = createTextEdit(replacements);
return mte;
}
} catch (Exception e) {
LOG.error("Error formatting " + resource.getURI() + ": " + e.getMessage(), e);
}
return new MultiTextEdit();
}
Aggregations