Search in sources :

Example 11 with ITextSegment

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

the class FormattableDocument method addReplacer.

@Override
public void addReplacer(ITextReplacer replacer) {
    if (!this.getRegion().contains(replacer.getRegion())) {
        String frameTitle = getClass().getSimpleName();
        ITextSegment frameRegion = getRegion();
        String replacerTitle = replacer.getClass().getSimpleName();
        ITextSegment replacerRegion = replacer.getRegion();
        RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, replacerRegion));
        getRequest().getExceptionHandler().accept(exception);
        return;
    }
    try {
        getReplacers().add(replacer, getFormatter().createTextReplacerMerger());
    } catch (ConflictingRegionsException e) {
        getRequest().getExceptionHandler().accept(e);
    }
}
Also used : HiddenRegionFormattingToString(org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 12 with ITextSegment

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

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

the class FormattableDocumentTest method autoWrapRewrite.

@Test
public void autoWrapRewrite() {
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
            it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(10));
        };
        it.preferences(_function_1);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("kwlist  kw1  kw2");
        _builder.newLine();
        it.setToBeFormatted(_builder);
        final GenericFormatter<KWList> _function_2 = new GenericFormatter<KWList>() {

            @Override
            protected void format(final KWList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
                    it_1.autowrap();
                    final IAutowrapFormatter _function_1 = (ITextSegment region, IHiddenRegionFormatting wrapped, IFormattableDocument doc) -> {
                        final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it_2) -> {
                            it_2.setSpace("!");
                        };
                        doc.append(regions.regionFor(model).keyword("kw1"), _function_2);
                    };
                    it_1.setOnAutowrap(_function_1);
                    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it_2) -> {
                        it_2.setSpace("@");
                        it_2.lowPriority();
                    };
                    document.append(regions.regionFor(model).keyword("kw1"), _function_2);
                };
                document.append(regions.regionFor(model).keyword("kwlist"), _function);
            }
        };
        it.setFormatter(_function_2);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("kwlist");
        _builder_1.newLine();
        _builder_1.append("kw1!kw2");
        _builder_1.newLine();
        it.setExpectation(_builder_1);
    };
    this._genericFormatterTester.assertFormatted(_function);
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) KWList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.KWList) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) IAutowrapFormatter(org.eclipse.xtext.formatting2.IAutowrapFormatter) Test(org.junit.Test)

Example 14 with ITextSegment

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

the class RegionSetTest method test.

private void test(final CharSequence expectation, final Procedure1<? super TestableTextSegmentSet> test) {
    Function<ITextSegment, ITextSegment> _identity = Functions.<ITextSegment>identity();
    Function<Object, String> _stringFunction = Functions.toStringFunction();
    ArrayListTextSegmentSet<ITextSegment> _arrayListTextSegmentSet = new ArrayListTextSegmentSet<ITextSegment>(_identity, _stringFunction, true);
    final TestableTextSegmentSet set = new TestableTextSegmentSet(_arrayListTextSegmentSet);
    try {
        test.apply(set);
    } catch (final Throwable _t) {
        if (_t instanceof Throwable) {
            final Throwable t = (Throwable) _t;
            t.printStackTrace();
            Assert.fail();
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
    Assert.assertEquals(expectation.toString(), set.toString());
}
Also used : ArrayListTextSegmentSet(org.eclipse.xtext.formatting2.internal.ArrayListTextSegmentSet) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TestableTextSegmentSet(org.eclipse.xtext.formatting2.internal.TestableTextSegmentSet)

Example 15 with ITextSegment

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

the class RegionDiffFormatter method collectRegionsToFormat.

protected Collection<ITextRegion> collectRegionsToFormat(ITextRegionAccessDiff regions) {
    List<ITextRegion> result = Lists.newArrayList();
    for (ITextSegmentDiff diff : regions.getRegionDifferences()) {
        int offset = diff.getModifiedFirstRegion().getOffset();
        int length = diff.getModifiedLastRegion().getEndOffset() - offset;
        ITextSegment region = regions.regionForOffset(offset, length);
        result.add(region);
    }
    return result;
}
Also used : ITextSegmentDiff(org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Aggregations

ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)30 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)10 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)8 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)5 String (java.lang.String)4 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)4 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)3 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)3 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)3 ITextRegionRewriter (org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter)3 ITextSegmentDiff (org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff)3 IAutowrapFormatter (org.eclipse.xtext.formatting2.IAutowrapFormatter)2 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)2 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)2 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)2 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)2 HiddenRegionFormattingToString (org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2