org.eclipse.xtext.util.ExceptionAcceptor Java Examples

The following examples show how to use org.eclipse.xtext.util.ExceptionAcceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FormatterSmokeSuite.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void processFile(String data) throws Exception {
	byte[] hash = ((MessageDigest) messageDigest.clone()).digest(data.getBytes("UTF-8"));
	if (seen.add(new BigInteger(hash))) {
		XtextResourceSet resourceSet = resourceSetProvider.get();
		resourceSet.setClasspathURIContext(classLoader);
		XtendFile file = parseHelper.parse(data, resourceSet);
		if (file != null) {
			try {
				XtextResource resource = (XtextResource) file.eResource();
				ITextRegionAccess regions = regionBuilder.get().forNodeModel(resource).create();
				FormatterRequest request = new FormatterRequest().setTextRegionAccess(regions);
				request.setExceptionHandler(ExceptionAcceptor.IGNORING);
				formatter.format(request);
			} catch (Exception e) {
				e.printStackTrace();
				ComparisonFailure error = new ComparisonFailure(e.getMessage(), data, "");
				error.setStackTrace(e.getStackTrace());
				throw error;
			}
		}
	}
}
 
Example #2
Source File: FormatterTester.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
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
}
 
Example #3
Source File: ContentFormatter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
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);
}
 
Example #4
Source File: FormatterTester.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
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
}
 
Example #5
Source File: FormatterRequest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Sets the {@link #textRegionAccess}. If the region has syntax errors and no explicit {@link ExceptionAcceptor} is
 * configured yet, the {@link ExceptionAcceptor#IGNORING ignoring acceptor} will be configured.
 */
public FormatterRequest setTextRegionAccess(ITextRegionAccess tokens) {
	if (tokens.hasSyntaxError() && this.exceptionHandler == null)
		this.exceptionHandler = ExceptionAcceptor.IGNORING;
	this.textRegionAccess = tokens;
	return this;
}
 
Example #6
Source File: FormatterTestHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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
}
 
Example #7
Source File: FormatterRequest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @see #exceptionHandler
 */
public IAcceptor<Exception> getExceptionHandler() {
	if (exceptionHandler == null)
		return ExceptionAcceptor.LOGGING;
	return exceptionHandler;
}