Search in sources :

Example 11 with FormatterRequest

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

the class Serializer method serialize.

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);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 12 with FormatterRequest

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

the class FormatterXpectMethod method formattedLines.

/**
 */
@Xpect
@ParameterParser(syntax = "arg1=INT")
public void formattedLines(@StringExpectation(whitespaceSensitive = true) IStringExpectation exp, // arg1
int lines, XpectInvocation inv, TargetSyntaxSupport syntax, ITextRegionAccess reg, ISetupInitializer<Preferences> prefInit) {
    ITextSegment region = getRegionForLines(reg, syntax.findFirstSemanticCharAfterStatement(inv), lines);
    Preferences prefs = new Preferences();
    // First put some defaults
    prefs.put(N4JSFormatterPreferenceKeys.FORMAT_PARENTHESIS, true);
    prefs.put(FormatterPreferenceKeys.lineSeparator, "\n");
    // Second init from concrete tests - will override defaults.
    prefInit.initialize(prefs);
    IFormatter2 formatter = formatterProvider.get();
    FormatterRequest request = formatterRequestProvider.get();
    request.setTextRegionAccess(reg);
    request.setExceptionHandler(ExceptionAcceptor.THROWING);
    // needed in case a check like this will be implemented:
    // org.eclipse.xtext.testing.formatter.FormatterTester.assertAllWhitespaceIsFormatted()
    request.setAllowIdentityEdits(true);
    request.setFormatUndefinedHiddenRegionsOnly(false);
    request.addRegion(region);
    request.setPreferences(prefs);
    List<ITextReplacement> replacements = formatter.format(request);
    String fmt = reg.getRewriter().renderToString(replacements);
    ITextSegment doc = reg.regionForDocument();
    int endIndex = region.getEndOffset() + (fmt.length() - doc.getLength()) - 1;
    String selection = fmt.substring(region.getOffset(), endIndex);
    exp.assertEquals(selection);
}
Also used : IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) Preferences(org.eclipse.n4js.xpect.config.Preferences) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 13 with FormatterRequest

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

the class FormatterFacade method format.

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);
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 14 with FormatterRequest

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

the class TextReplacerContext method addReplacement.

@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);
    }
}
Also used : ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) String(java.lang.String) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 15 with FormatterRequest

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

the class AbstractFormatter2 method format.

@Override
public final List<ITextReplacement> format(FormatterRequest request) {
    try {
        initialize(request);
        XtextResource xtextResource = request.getTextRegionAccess().getResource();
        IFormattableDocument document = createFormattableRootDocument();
        try {
            format(xtextResource, document);
        } catch (RegionTraceMissingException e) {
            document = handleTraceMissing(document, e);
        }
        List<ITextReplacement> rendered = document.renderToTextReplacements();
        List<ITextReplacement> postprocessed = postProcess(document, rendered);
        return postprocessed;
    } finally {
        reset();
    }
}
Also used : RegionTraceMissingException(org.eclipse.xtext.formatting2.internal.RegionTraceMissingException) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource)

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