Search in sources :

Example 1 with FormatterRequest

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

the class ContentFormatter method initRequest.

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);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IPreferenceValues(org.eclipse.xtext.preferences.IPreferenceValues) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 2 with FormatterRequest

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

the class FormattingConflictTest method enableDebugTracingFalse.

@Test
public void enableDebugTracingFalse() {
    final Wrapper<Throwable> wrapper = new Wrapper<Throwable>();
    final Wrapper<Integer> execution = new Wrapper<Integer>(Integer.valueOf(0));
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("idlist a");
        _builder.newLine();
        it.setToBeFormatted(_builder);
        final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                Integer _get = execution.get();
                int _plus = ((_get).intValue() + 1);
                execution.set(Integer.valueOf(_plus));
                final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace(" ");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function);
                final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace("\t");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function_1);
            }
        };
        it.setFormatter(_function_1);
        FormatterRequest _request = it.getRequest();
        _request.setEnableDebugTracing(false);
        FormatterRequest _request_1 = it.getRequest();
        final IAcceptor<Exception> _function_2 = (Exception e) -> {
            wrapper.set(e);
        };
        _request_1.setExceptionHandler(_function_2);
    };
    this._genericFormatterTester.assertFormatted(_function);
    Throwable _get = wrapper.get();
    final ConflictingRegionsException exception = ((ConflictingRegionsException) _get);
    Assert.assertEquals(2, (execution.get()).intValue());
    Assert.assertEquals(2, exception.getTraces().size());
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) IAcceptor(org.eclipse.xtext.util.IAcceptor) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 3 with FormatterRequest

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

the class FormattingConflictTest method enableDebugTracingTrue.

@Test
public void enableDebugTracingTrue() {
    final Wrapper<Throwable> wrapper = new Wrapper<Throwable>();
    final Wrapper<Integer> execution = new Wrapper<Integer>(Integer.valueOf(0));
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("idlist a");
        _builder.newLine();
        it.setToBeFormatted(_builder);
        final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                Integer _get = execution.get();
                int _plus = ((_get).intValue() + 1);
                execution.set(Integer.valueOf(_plus));
                final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace(" ");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function);
                final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace("\t");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function_1);
            }
        };
        it.setFormatter(_function_1);
        FormatterRequest _request = it.getRequest();
        _request.setEnableDebugTracing(true);
        FormatterRequest _request_1 = it.getRequest();
        final IAcceptor<Exception> _function_2 = (Exception e) -> {
            wrapper.set(e);
        };
        _request_1.setExceptionHandler(_function_2);
    };
    this._genericFormatterTester.assertFormatted(_function);
    Throwable _get = wrapper.get();
    final ConflictingRegionsException exception = ((ConflictingRegionsException) _get);
    Assert.assertEquals(1, (execution.get()).intValue());
    Assert.assertEquals(2, exception.getTraces().size());
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) IAcceptor(org.eclipse.xtext.util.IAcceptor) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 4 with FormatterRequest

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

the class N4MFFormatterTest method formatted.

/**
 * @param expectation
 *            the expected formatted code
 * @param resource
 *            the Xtext resource to be formatted
 * @param offset
 *            the optional start offset from where formatting should be applied
 * @param to
 *            the optional to offset to which formatting should be applied
 */
@ParameterParser(syntax = "('from' arg2=OFFSET 'to' arg3=OFFSET)?")
@Xpect
public void formatted(@StringExpectation(whitespaceSensitive = true) IStringExpectation expectation, @ThisResource XtextResource resource, int offset, int to) {
    ICompositeNode rootNode = resource.getParseResult().getRootNode();
    FormatterRequest request = requestProvider.get().setTextRegionAccess(regionAccessBuilderProvider.get().forNodeModel(resource).create());
    int totalLength = rootNode.getTotalLength();
    if (offset >= 0 && to > offset) {
        request.setRegions(Collections.singleton(new TextRegion(offset, to - offset)));
    } else {
        request.setRegions(Collections.singleton(new TextRegion(offset, totalLength - offset)));
    }
    List<ITextReplacement> replacements = formatter.format(request);
    String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
    if (offset >= 0 && to > offset) {
        formatted = formatted.substring(offset, formatted.length() - (totalLength - to));
    } else {
        formatted = formatted.substring(offset);
    }
    if (isUnixEnding()) {
        formatted = formatted.replaceAll("\r\n", "\n");
    } else if (isWindowsEnding()) {
        if (!rootNode.getText().contains("\r\n")) {
            formatted = formatted.replaceAll("\r\n", "\n");
        } else {
            formatted = formatted.replaceAll("(!\r)\n", "\r\n");
        }
    }
    expectation.assertEquals(formatted);
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 5 with FormatterRequest

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

the class ConvertJavaCode method formatXtendCode.

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;
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException)

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