org.eclipse.xtext.formatting2.FormatterRequest Java Examples
The following examples show how to use
org.eclipse.xtext.formatting2.FormatterRequest.
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: ConvertJavaCode.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
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 #4
Source File: FormatterSmokeSuite.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
@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 #5
Source File: FormatterFacade.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
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 #6
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 #7
Source File: ContentFormatter.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
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 #8
Source File: FormatterTester.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
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 #9
Source File: FormatterTester.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
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 #10
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); }
Example #11
Source File: FormatterTestHelper.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
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 #12
Source File: TextReplacerContext.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override public void addReplacement(ITextReplacement replacement) { Preconditions.checkNotNull(replacer); ITextSegment replacerRegion = replacer.getRegion(); FormatterRequest request = document.getRequest(); if (!replacerRegion.contains(replacement)) { String frameTitle = replacer.getClass().getSimpleName(); ITextSegment frameRegion = replacer.getRegion(); String replacerTitle = replacement.getReplacementText(); RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, (ITextSegment) replacement)); request.getExceptionHandler().accept(exception); return; } if (!isInRequestedRange(replacement)) { return; } if (!isInUndefinedRegion(replacement)) { if (request.isFormatUndefinedHiddenRegionsOnly()) { return; } if (!request.allowIdentityEdits() && isIdentityEdit(replacement)) { return; } } try { replacements.add(replacement); } catch (ConflictingRegionsException e) { request.getExceptionHandler().accept(e); } }
Example #13
Source File: RegionDiffFormatter.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
protected FormatterRequest createFormatterRequest(ITextRegionAccessDiff rewritten) { FormatterRequest request = formatterRequest.get(); request.setTextRegionAccess(rewritten); request.setRegions(collectRegionsToFormat(rewritten)); return request; }
Example #14
Source File: RegionDiffFormatter.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
protected FormatterRequest createFormatterRequest(ITextRegionAccess rewritten) { FormatterRequest request = formatterRequest.get(); request.setTextRegionAccess(rewritten); return request; }
Example #15
Source File: FormatterTestRequest.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Inject public FormatterTestRequest setRequest(FormatterRequest request) { this.request = request; return this; }
Example #16
Source File: FormatterTestRequest.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
public FormatterRequest getRequest() { return request; }
Example #17
Source File: RootDocument.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override public FormatterRequest getRequest() { return formatter.getRequest(); }
Example #18
Source File: HiddenRegionFormatting.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override public FormatterRequest getRequest() { return formatter.getRequest(); }
Example #19
Source File: SingleHiddenRegionFormatter.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override public FormatterRequest getRequest() { return formatting.getRequest(); }
Example #20
Source File: SubDocument.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override public FormatterRequest getRequest() { return parent.getRequest(); }
Example #21
Source File: DoubleHiddenRegionFormatter.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override public FormatterRequest getRequest() { return first.getRequest(); }
Example #22
Source File: FormatterTestRequest.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
@Inject public FormatterTestRequest setRequest(FormatterRequest request) { this.request = request; return this; }
Example #23
Source File: FormatterTestRequest.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
public FormatterRequest getRequest() { return request; }