Search in sources :

Example 6 with IComment

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

the class SinglelineCommentReplacer method hasEmptyBody.

protected boolean hasEmptyBody() {
    IComment comment = getComment();
    String text = comment.getText();
    if (!text.startsWith(prefix))
        return false;
    int start = prefix.length();
    for (int i = start; i < text.length(); i++) {
        char charAt = text.charAt(i);
        if (!Character.isWhitespace(charAt))
            return false;
    }
    return true;
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment)

Example 7 with IComment

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

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

the class AbstractHiddenRegion method collectAlternatingSpaceAndComments.

protected List<ITextSegment> collectAlternatingSpaceAndComments(boolean includeComments) {
    List<IHiddenRegionPart> parts = getParts();
    if (parts.isEmpty()) {
        return Collections.singletonList(this);
    } else {
        ITextSegment lastWhitespace = null;
        List<ITextSegment> result = new ArrayList<>();
        for (IHiddenRegionPart part : parts) {
            if (part instanceof IWhitespace) {
                if (lastWhitespace == null) {
                    result.add(part);
                    lastWhitespace = part;
                } else {
                    int mergedLength = lastWhitespace.getLength() + part.getLength();
                    lastWhitespace = new TextSegment(access, lastWhitespace.getOffset(), mergedLength);
                    result.set(result.size() - 1, lastWhitespace);
                }
            } else if (part instanceof IComment) {
                if (lastWhitespace == null) {
                    result.add(new TextSegment(access, part.getOffset(), 0));
                } else {
                    lastWhitespace = null;
                }
                if (includeComments) {
                    result.add(part);
                }
            }
        }
        if (lastWhitespace == null) {
            result.add(new TextSegment(access, getEndOffset(), 0));
        }
        return ImmutableList.copyOf(result);
    }
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) ArrayList(java.util.ArrayList) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace)

Example 9 with IComment

use of org.eclipse.xtext.formatting2.regionaccess.IComment 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)

Aggregations

IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)8 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)6 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)3 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)3 AbstractRule (org.eclipse.xtext.AbstractRule)2 IWhitespace (org.eclipse.xtext.formatting2.regionaccess.IWhitespace)2 ArrayList (java.util.ArrayList)1 EObject (org.eclipse.emf.ecore.EObject)1 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)1 MultilineCommentReplacer (org.eclipse.xtext.formatting2.internal.MultilineCommentReplacer)1 SinglelineCodeCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer)1 SinglelineDocCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer)1 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)1 TextSegment (org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)1 GrammarElementTitleSwitch (org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch)1