Java Code Examples for org.eclipse.xtext.formatting2.IFormatter2#format()
The following examples show how to use
org.eclipse.xtext.formatting2.IFormatter2#format() .
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 |
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: ContentFormatter.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
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(); }
Example 3
Source File: FormattingService.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
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 4
Source File: Serializer.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
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); }