Search in sources :

Example 36 with ITextRegionAccess

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

the class TextRegionAccessToString method toString.

@Override
public String toString() {
    List<ITextSegment> list = toTokenAndGapList();
    if (list.isEmpty())
        return "(empty)";
    ITextRegionAccess access = list.get(0).getTextRegionAccess();
    DiffColumn diff = new DiffColumn(access);
    TextRegionListToString result = new TextRegionListToString();
    if (!hideColumnExplanation) {
        if (diff.isDiff()) {
            result.add("Columns: 1:offset 2:length 3:diff 4:kind 5: text 6:grammarElement", false);
        } else {
            result.add("Columns: 1:offset 2:length 3:kind 4: text 5:grammarElement", false);
        }
        result.add("Kind: H=IHiddenRegion S=ISemanticRegion B/E=IEObjectRegion", false);
        result.add("", false);
    }
    appendRegions(result, list, diff, false);
    diff.appendDiffs(result, this);
    return result.toString();
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 37 with ITextRegionAccess

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

the class TextRegionsInTextToString method toString.

@Override
public String toString() {
    try {
        ITextRegionAccess access = getTextRegionAccess();
        ITextSegment frame = getFrame();
        if (access == null || frame == null)
            return box(title, "(no changes)");
        StringBuilder builder = new StringBuilder();
        String vizualized = access.getRewriter().renderToString(frame, items);
        builder.append(box(title, vizualized));
        return builder.toString();
    } catch (Exception e) {
        return box("error", e.getMessage() + "\n" + Throwables.getStackTraceAsString(e));
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 38 with ITextRegionAccess

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

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

the class Serializer method serializeToRegions.

public ITextRegionAccess serializeToRegions(EObject obj) {
    checkNotNull(obj, "obj must not be null.");
    ISerializationContext context = getIContext(obj);
    TextRegionAccessBuilder builder = textRegionBuilderProvider.get();
    ISerializationDiagnostic.Acceptor errors = ISerializationDiagnostic.EXCEPTION_THROWING_ACCEPTOR;
    serialize(context, obj, builder.forSequence(context, obj), errors);
    ITextRegionAccess regionAccess = builder.create();
    return regionAccess;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ISerializationDiagnostic(org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic) TextRegionAccessBuilder(org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 40 with ITextRegionAccess

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

the class FormatterXpectMethod method getRegionForLines.

private ITextSegment getRegionForLines(ITextRegionAccess regions, int offset, int lines) {
    ILineRegion firstLine = regions.regionForLineAtOffset(offset);
    ILineRegion lastLine = firstLine;
    for (int i = 1; i < lines; i++) {
        ILineRegion next = lastLine.getNextLine();
        if (next != null) {
            lastLine = next;
        } else {
            break;
        }
    }
    int firstLineOffset = firstLine.getOffset();
    ITextSegment region = regions.regionForOffset(firstLineOffset, (lastLine.getEndOffset() - firstLineOffset) + 1);
    return region;
}
Also used : ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Aggregations

ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)71 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)37 Test (org.junit.Test)37 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)21 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)17 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)16 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)16 EObject (org.eclipse.emf.ecore.EObject)14 OptionalChildList (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList)12 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)11 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)10 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)10 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)9 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)9 EList (org.eclipse.emf.common.util.EList)8 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)7 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)7 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)6 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)6 XtextResource (org.eclipse.xtext.resource.XtextResource)5