Search in sources :

Example 16 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.

the class RegionDiffFormatter method format.

public List<ITextReplacement> format(ITextRegionAccess regions) {
    IFormatter2 fmt = getFormatter();
    if (regions instanceof ITextRegionAccessDiff) {
        ITextRegionAccessDiff diff = (ITextRegionAccessDiff) regions;
        FormatterRequest request = createFormatterRequest(diff);
        List<ITextReplacement> replacements = fmt.format(request);
        List<ITextReplacement> merged = mergeReplacements(diff, replacements);
        return merged;
    } else {
        FormatterRequest request = createFormatterRequest(regions);
        List<ITextReplacement> replacements = fmt.format(request);
        return replacements;
    }
}
Also used : ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 17 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.

the class FormatterTestHelper method assertFormatted.

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
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) XtextResource(org.eclipse.xtext.resource.XtextResource) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 18 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-eclipse by eclipse.

the class FormatterTester method assertFormatted.

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
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) XtextResource(org.eclipse.xtext.resource.XtextResource) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 19 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-eclipse by eclipse.

the class ContentFormatter method exec.

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();
}
Also used : IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) IParseResult(org.eclipse.xtext.parser.IParseResult) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) BadLocationException(org.eclipse.jface.text.BadLocationException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Aggregations

FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)17 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)13 XtextResource (org.eclipse.xtext.resource.XtextResource)7 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)5 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)4 IDList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList)4 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)4 Test (org.junit.Test)4 Resource (org.eclipse.emf.ecore.resource.Resource)3 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)3 Joiner (com.google.common.base.Joiner)2 Lists (com.google.common.collect.Lists)2 Inject (com.google.inject.Inject)2 Provider (com.google.inject.Provider)2 List (java.util.List)2 URI (org.eclipse.emf.common.util.URI)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2