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();
}
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);
}
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;
}
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;
}
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;
}
Aggregations