org.eclipse.xtext.preferences.TypedPreferenceValues Java Examples

The following examples show how to use org.eclipse.xtext.preferences.TypedPreferenceValues. 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: 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 #2
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 #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);
}