use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class InsertionPointFinder method findInsertionPoint.
public IHiddenRegion findInsertionPoint(ISerializationContext ctx, IEObjectRegion obj, AbstractElement ins) {
ISerState insertionState = findState(ctx, ins);
Set<AbstractElement> followers = collectAdjacent(insertionState, s -> s.getFollowers());
Set<AbstractElement> precendents = collectAdjacent(insertionState, s -> s.getPrecedents());
List<IAstRegion> regions = Lists.newArrayList(obj.getAstRegions());
if (regions.isEmpty()) {
return obj.getPreviousHiddenRegion();
}
if (followers.contains(regions.get(0).getGrammarElement())) {
return obj.getPreviousHiddenRegion();
}
if (precendents.contains(regions.get(regions.size() - 1).getGrammarElement())) {
return obj.getNextHiddenRegion();
}
for (int i = 0; i < regions.size() - 1; i++) {
IAstRegion leading = regions.get(i);
IAstRegion trailing = regions.get(i + 1);
if (precendents.contains(leading.getGrammarElement()) && followers.contains(trailing.getGrammarElement())) {
return leading.getNextHiddenRegion();
}
}
return null;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class InsertionPointFinder method collectAdjacent.
protected Set<AbstractElement> collectAdjacent(ISerState start, Function<ISerState, List<? extends ISerState>> followers) {
LinkedList<ISerState> todo = new LinkedList<>();
Set<ISerState> seen = Sets.newHashSet();
todo.addAll(followers.apply(start));
Set<AbstractElement> result = Sets.newLinkedHashSet();
while (!todo.isEmpty()) {
ISerState next = todo.pop();
if (!seen.add(next)) {
continue;
}
if (next.getType() == SerStateType.ELEMENT && !GrammarUtil.isUnassignedAction(next.getGrammarElement())) {
result.add(next.getGrammarElement());
} else {
todo.addAll(followers.apply(next));
}
}
return result;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class IndentationAwareCompletionPrefixProvider method getLastCompleteNodeByOffset.
protected INode getLastCompleteNodeByOffset(INode node, int offset) {
BidiTreeIterator<INode> iterator = node.getRootNode().getAsTreeIterable().iterator();
INode result = node;
ITextRegion candidateTextRegion = node.getTextRegion();
while (iterator.hasNext()) {
INode candidate = iterator.next();
ITextRegion textRegion = candidate.getTextRegion();
if (textRegion.getOffset() >= offset && !(textRegion.getOffset() == offset && textRegion.getLength() == 0)) {
if (!candidateTextRegion.equals(textRegion) && candidate instanceof ILeafNode && textRegion.getLength() + textRegion.getOffset() >= offset) {
break;
}
}
if ((candidate instanceof ILeafNode) && (candidate.getGrammarElement() == null || candidate.getGrammarElement() instanceof AbstractElement || candidate.getGrammarElement() instanceof ParserRule)) {
if (textRegion.getLength() == 0) {
if (candidateTextRegion.getOffset() + candidateTextRegion.getLength() < offset || candidateTextRegion.getLength() == 0 && candidateTextRegion.getOffset() <= offset) {
result = candidate;
candidateTextRegion = candidate.getTextRegion();
}
} else {
result = candidate;
candidateTextRegion = candidate.getTextRegion();
}
}
}
return result;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class BaseContentAssistParser method getInitializedUnorderedGroupHelper.
/**
* @since 2.14
*/
protected IUnorderedGroupHelper getInitializedUnorderedGroupHelper(FE element, InternalParser parser, UnorderedGroup group) {
final IUnorderedGroupHelper helper = parser.getUnorderedGroupHelper();
helper.enter(group);
for (AbstractElement consumed : element.getHandledUnorderedGroupElements()) {
parser.before(consumed);
helper.select(group, group.getElements().indexOf(consumed));
helper.returnFromSelection(group);
parser.after(consumed);
}
return helper;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class BaseContentAssistParser method getFollowElements.
public Collection<FE> getFollowElements(FE element) {
if (element.getLookAhead() <= 1)
throw new IllegalArgumentException("lookahead may not be less than or equal to 1");
Collection<FE> result = new ArrayList<FE>();
for (AbstractElement elementToParse : getElementsToParse(element)) {
elementToParse = unwrapSingleElementGroups(elementToParse);
String ruleName = getRuleName(elementToParse);
String[][] allRuleNames = getRequiredRuleNames(ruleName, element.getParamStack(), elementToParse);
for (String[] ruleNames : allRuleNames) {
for (int i = 0; i < ruleNames.length; i++) {
InternalParser parser = createParser();
parser.setUnorderedGroupHelper(createUnorderedGroupHelper());
parser.getUnorderedGroupHelper().initializeWith(parser);
ObservableXtextTokenStream tokens = setTokensFromFollowElement(parser, element);
tokens.setListener(parser);
parser.getGrammarElements().addAll(element.getTrace());
parser.getGrammarElements().add(elementToParse);
parser.getLocalTrace().addAll(element.getLocalTrace());
parser.getLocalTrace().add(elementToParse);
parser.getParamStack().addAll(element.getParamStack());
if (elementToParse instanceof UnorderedGroup && element.getGrammarElement() == elementToParse) {
UnorderedGroup group = (UnorderedGroup) elementToParse;
IUnorderedGroupHelper helper = getInitializedUnorderedGroupHelper(element, parser, group);
parser.setUnorderedGroupHelper(ignoreFirstEntrance(helper));
}
Collection<FE> elements = getFollowElements(parser, elementToParse, ruleNames, i);
result.addAll(elements);
}
}
}
return result;
}
Aggregations