Search in sources :

Example 86 with ITextRegionAccess

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

the class RegionAccessTestHelper method operator_tripleEquals.

public void operator_tripleEquals(final ITextRegionAccess access, final CharSequence expectation) {
    if ((access instanceof ITextRegionAccessDiff)) {
        this.assertDiffs(((ITextRegionAccessDiff) access));
    }
    TextRegionAccessToString _cfg = this.cfg(new TextRegionAccessToString().withRegionAccess(access));
    final String tra1 = (_cfg + "\n");
    Assert.assertEquals(Strings.toPlatformLineSeparator(expectation), Strings.toPlatformLineSeparator(tra1));
}
Also used : ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)

Example 87 with ITextRegionAccess

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

the class RegionAccessTestHelper method modify.

public ITextRegionAccess modify(final ITextRegionAccess base, final Procedure1<? super ITextRegionDiffBuilder> modify) {
    final StringBasedTextRegionAccessDiffBuilder fac = new StringBasedTextRegionAccessDiffBuilder(base);
    modify.apply(fac);
    final StringBasedTextRegionAccessDiff modified = fac.create();
    return modified;
}
Also used : StringBasedTextRegionAccessDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiffBuilder) StringBasedTextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiff)

Example 88 with ITextRegionAccess

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess 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 = findRegion(lines, inv, syntax, reg);
    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 89 with ITextRegionAccess

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project n4js by eclipse.

the class N4MultilineCommentReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    if (!multiline)
        return context;
    IComment comment = getComment();
    ITextRegionAccess access = comment.getTextRegionAccess();
    List<ILineRegion> lines = comment.getLineRegions();
    String oldIndentation = lines.get(0).getIndentation().getText();
    String indentationString = context.getIndentationString();
    String newIndentation = indentationString + " " + prefix;
    String newIndentation2 = newIndentation + " ";
    for (int i = 1; i < lines.size() - 1; i++) {
        ITextSegment line = lines.get(i);
        String text = line.getText();
        int prefixOffset = prefixOffset(text);
        ITextSegment target;
        if (prefixOffset >= 0)
            target = access.regionForOffset(line.getOffset(), prefixOffset + 1);
        else if (text.startsWith(oldIndentation))
            target = access.regionForOffset(line.getOffset(), oldIndentation.length() + nonWhiteSpaceOffset(text.substring(oldIndentation.length())));
        else
            target = access.regionForOffset(line.getOffset(), /* 0 */
            nonWhiteSpaceOffset(text));
        if (line.getEndOffset() - target.getEndOffset() > 0) {
            // content left in line
            context.addReplacement(target.replaceWith(newIndentation2));
        } else {
            // no content left
            context.addReplacement(target.replaceWith(newIndentation));
        }
    }
    if (lines.size() > 1) {
        ILineRegion line = lines.get(lines.size() - 1);
        context.addReplacement(line.getIndentation().replaceWith(indentationString + " "));
    }
    return context;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 90 with ITextRegionAccess

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project n4js by eclipse.

the class FixedMultilineCommentReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    if (!multiline)
        return context;
    IComment comment = getComment();
    ITextRegionAccess access = comment.getTextRegionAccess();
    List<ILineRegion> lines = comment.getLineRegions();
    String oldIndentation = lines.get(0).getIndentation().getText();
    String indentationString = context.getIndentationString();
    String newIndentation = indentationString;
    for (int i = 1; i < lines.size() - 1; i++) {
        ITextSegment line = lines.get(i);
        String text = line.getText();
        ITextSegment target;
        if (text.startsWith(oldIndentation))
            target = access.regionForOffset(line.getOffset(), oldIndentation.length());
        else
            target = access.regionForOffset(line.getOffset(), 0);
        context.addReplacement(target.replaceWith(newIndentation));
    }
    if (lines.size() > 1) {
        ILineRegion line = lines.get(lines.size() - 1);
        context.addReplacement(line.getIndentation().replaceWith(indentationString + " "));
    }
    return context;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Aggregations

ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)75 Test (org.junit.Test)39 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)17 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)17 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)17 OptionalChildList (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList)12 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)11 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)11 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)11 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)9 EObject (org.eclipse.emf.ecore.EObject)8 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)8 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)8 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)7 XtextResource (org.eclipse.xtext.resource.XtextResource)7 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)6 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)4 String (java.lang.String)3 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)3