use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart 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.IHiddenRegionPart 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.IHiddenRegionPart in project xtext-core by eclipse.
the class TextReplacerContext method isInUndefinedRegion.
protected boolean isInUndefinedRegion(ITextReplacement repl) {
ITextSegment replacerRegion = replacer.getRegion();
IHiddenRegion hidden = null;
if (replacerRegion instanceof IHiddenRegionPart) {
hidden = ((IHiddenRegionPart) replacerRegion).getHiddenRegion();
} else if (replacerRegion instanceof IHiddenRegion) {
hidden = (IHiddenRegion) replacerRegion;
}
return hidden == null || hidden.isUndefined();
}
use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart in project xtext-core by eclipse.
the class TextRegionAccessToString method toTokenAndGapList.
protected List<ITextSegment> toTokenAndGapList() {
int range = this.highlightOrigin ? 4 : (Integer.MAX_VALUE / 2);
ITextSegment first = null;
ITextSegment current = origin;
for (int i = 0; i < range && current != null; i++) {
first = current;
if (current instanceof ITextRegionAccess)
current = ((ITextRegionAccess) current).regionForRootEObject().getPreviousHiddenRegion();
else if (current instanceof ISequentialRegion)
current = ((ISequentialRegion) current).getPreviousHiddenRegion();
else if (current instanceof IHiddenRegionPart)
current = ((IHiddenRegionPart) current).getHiddenRegion();
else
throw new IllegalStateException("Unexpected Type: " + current.getClass());
}
if (first == null)
return Collections.emptyList();
List<ITextSegment> result = new ArrayList<>();
ITextSegment currentRegion = first;
for (int i = 0; i <= (range * 2) && currentRegion != null; i++) {
result.add(currentRegion);
if (currentRegion instanceof ISemanticRegion)
currentRegion = ((ISemanticRegion) currentRegion).getNextHiddenRegion();
else if (currentRegion instanceof IHiddenRegion)
currentRegion = ((IHiddenRegion) currentRegion).getNextSemanticRegion();
else
throw new IllegalStateException("Unexpected Type: " + currentRegion.getClass());
}
return result;
}
use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart 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);
}
}
Aggregations