Search in sources :

Example 16 with ITextReplacement

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

the class FormattableDocument method applyTextReplacements.

// TODO: use org.eclipse.xtext.formatting2.TextReplacements
protected String applyTextReplacements(Iterable<ITextReplacement> replacements) {
    ITextSegment region = getRegion();
    String input = region.getText();
    ArrayList<ITextReplacement> list = Lists.newArrayList(replacements);
    Collections.sort(list);
    int startOffset = region.getOffset();
    int lastOffset = 0;
    StringBuilder result = new StringBuilder();
    for (ITextReplacement r : list) {
        int offset = r.getOffset() - startOffset;
        result.append(input.subSequence(lastOffset, offset));
        result.append(r.getReplacementText());
        lastOffset = offset + r.getLength();
    }
    result.append(input.subSequence(lastOffset, input.length()));
    return result.toString();
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) HiddenRegionFormattingToString(org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Example 17 with ITextReplacement

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

the class FormatterReplacementsTest method testUndefinedIdentityEditsAreNotFiltered.

@Test
public void testUndefinedIdentityEditsAreNotFiltered() {
    final GenericFormatter<IDList> _function = new GenericFormatter<IDList>() {

        @Override
        protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
            final Consumer<ISemanticRegion> _function = (ISemanticRegion it) -> {
                final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace("");
                };
                document.prepend(it, _function_1);
            };
            regions.regionFor(model).ruleCallsTo(FormatterReplacementsTest.this._formatterTestLanguageGrammarAccess.getIDRule()).forEach(_function);
        }
    };
    final GenericFormatter<IDList> formatter = _function;
    IDList _createIDList = this.fact.createIDList();
    final Procedure1<IDList> _function_1 = (IDList it) -> {
        EList<String> _ids = it.getIds();
        _ids.add("a");
    };
    final IDList model = ObjectExtensions.<IDList>operator_doubleArrow(_createIDList, _function_1);
    Resource _createResource = this.resFact.createResource(URI.createURI("foo.ext"));
    final Procedure1<Resource> _function_2 = (Resource it) -> {
        EList<Resource> _resources = new XtextResourceSet().getResources();
        _resources.add(it);
        EList<EObject> _contents = it.getContents();
        _contents.add(model);
    };
    ObjectExtensions.<Resource>operator_doubleArrow(_createResource, _function_2);
    final FormatterRequest request = this.requestProvider.get();
    request.setAllowIdentityEdits(false);
    request.setTextRegionAccess(this.serializer.serializeToRegions(model));
    final List<ITextReplacement> replacements = formatter.format(request);
    final Function1<ITextReplacement, String> _function_3 = (ITextReplacement it) -> {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("\"");
        String _text = it.getText();
        _builder.append(_text);
        _builder.append("\" -> \"");
        String _replacementText = it.getReplacementText();
        _builder.append(_replacementText);
        _builder.append("\"");
        _builder.append("\n");
        return _builder.toString();
    };
    final String actual = IterableExtensions.join(ListExtensions.<ITextReplacement, String>map(replacements, _function_3));
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("\"\" -> \"\"");
    _builder.newLine();
    _builder.append("\"\" -> \"\"");
    _builder.newLine();
    _builder.append("\"\" -> \"\"");
    _builder.newLine();
    final String expected = _builder.toString();
    Assert.assertEquals(expected, actual);
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Extension(org.eclipse.xtext.xbase.lib.Extension) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) EList(org.eclipse.emf.common.util.EList) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 18 with ITextReplacement

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

the class FormatterReplacementsTest method testIdentityEditsAreFiltered.

@Test
public void testIdentityEditsAreFiltered() {
    try {
        final GenericFormatter<IDList> _function = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                final Consumer<ISemanticRegion> _function = (ISemanticRegion it) -> {
                    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
                        it_1.autowrap();
                        it_1.oneSpace();
                    };
                    document.prepend(it, _function_1);
                };
                regions.regionFor(model).ruleCallsTo(FormatterReplacementsTest.this._formatterTestLanguageGrammarAccess.getIDRule()).forEach(_function);
            }
        };
        final GenericFormatter<IDList> formatter = _function;
        final IDList parsed = this.parseHelper.parse("idlist  aaa bbb");
        final FormatterRequest request = this.requestProvider.get();
        request.setAllowIdentityEdits(false);
        Resource _eResource = parsed.eResource();
        request.setTextRegionAccess(this.regionBuilder.forNodeModel(((XtextResource) _eResource)).create());
        final List<ITextReplacement> replacements = formatter.format(request);
        final Function1<ITextReplacement, String> _function_1 = (ITextReplacement it) -> {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("\"");
            String _text = it.getText();
            _builder.append(_text);
            _builder.append("\" -> \"");
            String _replacementText = it.getReplacementText();
            _builder.append(_replacementText);
            _builder.append("\"");
            _builder.append("\n");
            return _builder.toString();
        };
        final String actual = IterableExtensions.join(ListExtensions.<ITextReplacement, String>map(replacements, _function_1));
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("\"  \" -> \" \"");
        _builder.newLine();
        final String expected = _builder.toString();
        Assert.assertEquals(expected, actual);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Extension(org.eclipse.xtext.xbase.lib.Extension) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 19 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement 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 20 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement 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)

Aggregations

ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)35 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)13 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)9 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)7 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)7 Resource (org.eclipse.emf.ecore.resource.Resource)5 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 String (java.lang.String)4 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)4 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)4 ITextRegion (org.eclipse.xtext.util.ITextRegion)4 URI (org.eclipse.emf.common.util.URI)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)3 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)3 List (java.util.List)2