Java Code Examples for org.eclipse.xtext.formatting2.FormatterRequest#setTextRegionAccess()

The following examples show how to use org.eclipse.xtext.formatting2.FormatterRequest#setTextRegionAccess() . 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: FormattingService.java    From xtext-web with Eclipse Public License 2.0 6 votes vote down vote up
protected String format2(XtextResource resource, ITextRegion selection, ITypedPreferenceValues preferences) {
	FormatterRequest request = formatterRequestProvider.get();
	request.setAllowIdentityEdits(false);
	request.setFormatUndefinedHiddenRegionsOnly(false);
	if (selection != null) {
		request.setRegions(Lists.newArrayList(selection));
	}
	if (preferences != null) {
		request.setPreferences(preferences);
	}
	ITextRegionAccess regionAccess = regionBuilder.forNodeModel(resource).create();
	request.setTextRegionAccess(regionAccess);
	IFormatter2 formatter2 = formatter2Provider.get();
	List<ITextReplacement> replacements = formatter2.format(request);
	if (selection != null) {
		return regionAccess.getRewriter().renderToString(
				new TextSegment(regionAccess, selection.getOffset(), selection.getLength()), replacements);
	} else {
		return regionAccess.getRewriter().renderToString(replacements);
	}
}
 
Example 2
Source File: ConvertJavaCode.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
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;
	}
}
 
Example 3
Source File: FormatterFacade.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
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);
  }
}
 
Example 4
Source File: FormattingService.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected List<ITextReplacement> format2(XtextResource resource, ITextRegion selection,
		ITypedPreferenceValues preferences) {
	FormatterRequest request = formatterRequestProvider.get();
	request.setAllowIdentityEdits(false);
	request.setFormatUndefinedHiddenRegionsOnly(false);
	if (selection != null) {
		request.setRegions(Collections.singletonList(selection));
	}
	if (preferences != null) {
		request.setPreferences(preferences);
	}
	ITextRegionAccess regionAccess = regionBuilder.forNodeModel(resource).create();
	request.setTextRegionAccess(regionAccess);
	IFormatter2 formatter2 = formatter2Provider.get();
	List<ITextReplacement> replacements = formatter2.format(request);
	return replacements;
}
 
Example 5
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 6
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 7
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 8
Source File: Serializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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);
}
 
Example 9
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 10
Source File: RegionDiffFormatter.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected FormatterRequest createFormatterRequest(ITextRegionAccess rewritten) {
	FormatterRequest request = formatterRequest.get();
	request.setTextRegionAccess(rewritten);
	return request;
}
 
Example 11
Source File: RegionDiffFormatter.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected FormatterRequest createFormatterRequest(ITextRegionAccessDiff rewritten) {
	FormatterRequest request = formatterRequest.get();
	request.setTextRegionAccess(rewritten);
	request.setRegions(collectRegionsToFormat(rewritten));
	return request;
}