use of org.eclipse.xtext.formatting2.regionaccess.IWhitespace in project xtext-core by eclipse.
the class HiddenRegionReplacer method createReplacements.
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
AbstractFormatter2 formatter = context.getFormatter();
List<IHiddenRegionPart> hiddens = region.getParts();
if (hiddens.isEmpty()) {
return formatter.createWhitespaceReplacer(region, formatting).createReplacements(context);
} else if ((hiddens.size() == 1 && hiddens.get(0) instanceof IWhitespace)) {
return formatter.createWhitespaceReplacer(hiddens.get(0), formatting).createReplacements(context);
} else {
List<ITextReplacer> replacers = createReplacers(formatter);
applyHiddenRegionFormatting(replacers);
ITextReplacerContext current = context;
current.setNextReplacerIsChild();
for (ITextReplacer replacer : replacers) current = replacer.createReplacements(current.withReplacer(replacer));
return current;
}
}
use of org.eclipse.xtext.formatting2.regionaccess.IWhitespace 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.IWhitespace 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.<ITextSegment>singletonList(this);
} else {
ITextSegment lastWhitespace = null;
List<ITextSegment> result = Lists.newArrayList();
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.IWhitespace in project xtext-core by eclipse.
the class HiddenRegionPartAssociator method associate.
@Override
public void associate(IHiddenRegion region, BiConsumer<IHiddenRegionPart, HiddenRegionPartAssociation> handler) {
List<IHiddenRegionPart> parts = region.getParts();
if (!region.isMultiline()) {
// rather than one of the surrounding semantic tokens.
for (IHiddenRegionPart p : parts) {
handler.accept(p, HiddenRegionPartAssociation.CONTAINER);
}
} else {
int i = 0;
if (region.getPreviousSemanticRegion() != null) {
// collect whitespace and comments from the same line as belonging to the PREVIOUS semantic token
while (i < parts.size()) {
IHiddenRegionPart part = parts.get(i);
if (part.isMultiline()) {
if (part.getText().endsWith("\n")) {
handler.accept(part, HiddenRegionPartAssociation.PREVIOUS);
i++;
}
break;
} else {
handler.accept(part, HiddenRegionPartAssociation.PREVIOUS);
i++;
}
}
}
int j = parts.size() - 1;
if (region.getNextSemanticRegion() != null) {
// associated with the NEXT semantic token.
while (j >= i) {
IHiddenRegionPart part = parts.get(j);
if (part instanceof IWhitespace && part.getLineCount() > 2) {
break;
} else {
handler.accept(part, HiddenRegionPartAssociation.NEXT);
j--;
}
}
}
// Associate the remaining tokens with the CONTAINER
for (int k = i; k <= j; k++) {
IHiddenRegionPart part = parts.get(k);
handler.accept(part, HiddenRegionPartAssociation.CONTAINER);
}
}
}
Aggregations