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