Search in sources :

Example 1 with IComment

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

the class StringBasedTextRegionAccessDiffAppender method copyAndAppend.

public IHiddenRegionPart copyAndAppend(IHiddenRegionPart part) {
    StringHiddenRegion region;
    if (this.last instanceof StringHiddenRegion) {
        region = (StringHiddenRegion) this.last;
    } else {
        region = appendHiddenRegion(true);
    }
    String text = part.getText();
    int offset = result.append(text);
    if (part instanceof IComment) {
        IComment comment = ((IComment) part);
        AbstractRule grammarElement = (AbstractRule) comment.getGrammarElement();
        StringComment newComment = new StringComment(region, grammarElement, offset, text.length());
        region.addPart(newComment);
        recordDiff(part, newComment);
        return newComment;
    } else if (part instanceof IWhitespace) {
        IWhitespace ws = (IWhitespace) part;
        AbstractRule grammarElement = (AbstractRule) ws.getGrammarElement();
        StringWhitespace newWs = new StringWhitespace(region, grammarElement, offset, text.length());
        region.addPart(newWs);
        recordDiff(part, newWs);
        return newWs;
    }
    throw new IllegalStateException();
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 2 with IComment

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

the class AbstractFormatter2 method createCommentReplacer.

public ITextReplacer createCommentReplacer(IComment comment) {
    EObject grammarElement = comment.getGrammarElement();
    if (grammarElement instanceof AbstractRule) {
        String ruleName = ((AbstractRule) grammarElement).getName();
        if (ruleName.startsWith("ML"))
            return new MultilineCommentReplacer(comment, '*');
        if (ruleName.startsWith("SL")) {
            if (comment.getLineRegions().get(0).getIndentation().getLength() > 0)
                return new SinglelineDocCommentReplacer(comment, "//");
            else
                return new SinglelineCodeCommentReplacer(comment, "//");
        }
    }
    String elementName = new GrammarElementTitleSwitch().showQualified().showRule().doSwitch(grammarElement);
    throw new IllegalStateException("No " + ITextReplacer.class.getSimpleName() + " configured for " + elementName);
}
Also used : GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) MultilineCommentReplacer(org.eclipse.xtext.formatting2.internal.MultilineCommentReplacer) EObject(org.eclipse.emf.ecore.EObject) SinglelineCodeCommentReplacer(org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer) SinglelineDocCommentReplacer(org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 3 with IComment

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

Example 4 with IComment

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

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

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