Search in sources :

Example 6 with IHiddenRegionFormatting

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

the class HiddenRegionReplacer method applyHiddenRegionFormatting.

/**
 * This method is when we have multiple TextReplacers for this HiddenRegion because the HiddenRegion contains
 * comments. It applies the formatting configuration from {@link #formatting} to the {@link WhitespaceReplacer} that
 * surround the comment.
 */
protected void applyHiddenRegionFormatting(List<ITextReplacer> replacers) {
    IHiddenRegionFormatting separator = findWhitespaceThatSeparatesSemanticRegions(replacers).getFormatting();
    // 1. apply indentation
    Integer inc = formatting.getIndentationIncrease();
    Integer dec = formatting.getIndentationDecrease();
    if (inc != null && dec != null) {
        ((WhitespaceReplacer) replacers.get(0)).getFormatting().setIndentationIncrease(inc);
        ((WhitespaceReplacer) replacers.get(replacers.size() - 1)).getFormatting().setIndentationDecrease(dec);
    } else if (inc != null) {
        separator.setIndentationIncrease(inc);
    } else if (dec != null) {
        separator.setIndentationDecrease(dec);
    }
    // 2. apply NewLine and space configuration to the first whitespace region that follows or contains a linewrap.
    separator.setNewLinesDefault(formatting.getNewLineDefault());
    separator.setNewLinesMax(formatting.getNewLineMax());
    separator.setNewLinesMin(formatting.getNewLineMin());
    separator.setSpace(formatting.getSpace());
    // 3. apply the 'priority' configuration for all and set a default formatting
    for (ITextReplacer replacer : replacers) if (replacer instanceof WhitespaceReplacer) {
        IHiddenRegionFormatting formatting2 = ((WhitespaceReplacer) replacer).getFormatting();
        formatting2.setPriority(formatting.getPriority());
        if (formatting2 != separator) {
            formatting2.setSpace(replacer.getRegion().getOffset() > 0 ? " " : "");
            formatting2.setNewLinesMin(0);
            formatting2.setNewLinesMax(1);
        }
    }
    // 4. make sure whitespace before and after multiline comments introduce a NewLine
    for (int i = 0; i < replacers.size(); i++) {
        ITextReplacer replacer = replacers.get(i);
        if (replacer instanceof CommentReplacer) {
            CommentReplacer commentReplacer = ((CommentReplacer) replacer);
            WhitespaceReplacer leading = (WhitespaceReplacer) replacers.get(i - 1);
            WhitespaceReplacer trailing = (WhitespaceReplacer) replacers.get(i + 1);
            commentReplacer.configureWhitespace(leading, trailing);
        }
    }
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer)

Example 7 with IHiddenRegionFormatting

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

the class TextReplacerMerger method mergeHiddenRegionReplacers.

protected ITextReplacer mergeHiddenRegionReplacers(List<? extends ITextReplacer> conflicting) {
    List<IHiddenRegionFormatting> formattings = Lists.newArrayList();
    IHiddenRegion region = null;
    for (ITextReplacer replacer : conflicting) {
        if (replacer instanceof HiddenRegionReplacer) {
            HiddenRegionReplacer hiddenRegionReplacer = (HiddenRegionReplacer) replacer;
            formattings.add(hiddenRegionReplacer.getFormatting());
            if (region == null)
                region = hiddenRegionReplacer.getRegion();
            else if (region != hiddenRegionReplacer.getRegion())
                return null;
        } else
            return null;
    }
    IHiddenRegionFormatting mergedFormatting = merger.merge(formattings);
    if (mergedFormatting != null)
        return formatter.createHiddenRegionReplacer(region, mergedFormatting);
    return null;
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Example 8 with IHiddenRegionFormatting

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

the class FormattableDocumentTest method autoWrapInsert.

@Test
public void autoWrapInsert() {
    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) -> {
                        String _property = System.getProperty("line.separator");
                        String _plus = ("@" + _property);
                        it_2.setSpace(_plus);
                    };
                    document.append(regions.regionFor(model).keyword("kw2"), _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)

Aggregations

IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)8 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)5 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 IAutowrapFormatter (org.eclipse.xtext.formatting2.IAutowrapFormatter)2 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)2 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)2 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)2 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)2 KWList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.KWList)2 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)2 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)2 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)2 TextSegment (org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)2 MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)2 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)2 Test (org.junit.Test)2 FormattingNotApplicableException (org.eclipse.xtext.formatting2.FormattingNotApplicableException)1 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)1