Search in sources :

Example 21 with ITextSegment

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

the class ConditionalReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    context.setNextReplacerIsChild();
    for (ISubFormatter formatter : subFormatters) {
        try {
            ITextSegment region = getRegion();
            SubDocument subDocument = new SubDocument(region, getDocument());
            for (ITextReplacer replacer : replacers) subDocument.addReplacer(replacer);
            formatter.format(subDocument);
            ITextReplacerContext first = context.withReplacer(subDocument);
            ITextReplacerContext last = subDocument.createReplacements(first);
            return last;
        } catch (FormattingNotApplicableException e) {
        // no need to do anything.
        // Try the next SubFormatter until one doens't throw a FormattingNotApplicableException
        }
    }
    throw new FormattingNotApplicableException();
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ISubFormatter(org.eclipse.xtext.formatting2.ISubFormatter) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) FormattingNotApplicableException(org.eclipse.xtext.formatting2.FormattingNotApplicableException) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 22 with ITextSegment

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

the class HiddenRegionReplacer method createReplacers.

protected List<ITextReplacer> createReplacers(AbstractFormatter2 formatter) {
    List<ITextSegment> regions = region.getAlternatingMergedSpaceAndComments();
    List<ITextReplacer> replacers = Lists.newArrayListWithCapacity(regions.size());
    for (ITextSegment region : regions) {
        if (region instanceof IComment)
            replacers.add(formatter.createCommentReplacer((IComment) region));
        else
            replacers.add(formatter.createWhitespaceReplacer(region, formatter.createHiddenRegionFormatting()));
    }
    return replacers;
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 23 with ITextSegment

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

the class MultilineCommentReplacer 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 + " ";
    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());
        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)

Example 24 with ITextSegment

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

the class SinglelineCommentReplacer method getFirstSpace.

protected ITextSegment getFirstSpace() {
    IComment comment = getComment();
    String text = comment.getText();
    if (!text.startsWith(prefix))
        return null;
    int start = prefix.length();
    for (int i = start; i < text.length(); i++) {
        char charAt = text.charAt(i);
        if (!Character.isWhitespace(charAt) || charAt == '\r' || charAt == '\n')
            return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, i - start);
    }
    return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, text.length() - start);
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) TextSegment(org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 25 with ITextSegment

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

the class TextReplacerContext method isWrapSincePrevious.

@Override
public boolean isWrapSincePrevious() {
    ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
    ITextSegment region = getRegion(0);
    ITextSegment previousRegion = getRegion(1);
    if (previousRegion != null) {
        int offset = previousRegion.getEndOffset();
        String between = access.textForOffset(offset, region.getOffset() - offset);
        if (between.contains("\n")) {
            return true;
        }
    }
    return false;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) String(java.lang.String)

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