use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class FormattingConfigBasedStream method collectLocators.
protected Set<ElementLocator> collectLocators(EObject ele) {
Set<ElementLocator> result = Sets.newHashSet(activeRangeLocators);
Collection<ElementLocator> loc = Sets.newHashSet();
if (ele instanceof AbstractElement)
for (ElementPattern pattern : matcher.matchNext((AbstractElement) ele)) loc.add(pattern.getLocator());
if ((last instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) last)) || (ele instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) ele)))
loc = collectLocatorsForComments(loc, last, ele);
last = ele;
for (ElementLocator locator : loc) if (locator.getType() == LocatorType.RANGE && !activeRangeLocators.add(locator))
activeRangeLocators.remove(locator);
result.addAll(loc);
for (ElementLocator locator : result) {
if (locator instanceof IndentationLocatorStart)
indentationLevel++;
else if (locator instanceof IndentationLocatorEnd)
indentationLevel--;
}
return result;
}
use of org.eclipse.xtext.AbstractRule 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.AbstractRule in project xtext-core by eclipse.
the class AbstractSemanticRegionsFinder method keywordPairs.
@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(String kw1, String kw2) {
Preconditions.checkNotNull(kw1);
Preconditions.checkNotNull(kw2);
Preconditions.checkArgument(!kw1.equals(kw2));
Predicate<ISemanticRegion> p1 = new KeywordPredicate(kw1);
Predicate<ISemanticRegion> p2 = new KeywordPredicate(kw2);
List<ISemanticRegion> all = findAll(Predicates.or(p1, p2));
Builder<Pair<ISemanticRegion, ISemanticRegion>> result = ImmutableList.builder();
LinkedList<ISemanticRegion> stack = new LinkedList<ISemanticRegion>();
for (ISemanticRegion region : all) {
if (p1.apply(region))
stack.push(region);
else {
AbstractRule regionRule = GrammarUtil.containingRule(region.getGrammarElement());
while (!stack.isEmpty()) {
ISemanticRegion candidate = stack.pop();
if (region.getSemanticElement() == candidate.getSemanticElement()) {
AbstractRule candidateRule = GrammarUtil.containingRule(candidate.getGrammarElement());
if (regionRule == candidateRule) {
result.add(Pair.of(candidate, region));
break;
}
}
}
}
}
return result.build();
}
use of org.eclipse.xtext.AbstractRule 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.AbstractRule in project xtext-core by eclipse.
the class GrammarUtilTest method testFindCurrentType_01.
@Test
public void testFindCurrentType_01() throws Exception {
this.with(XtextStandaloneSetup.class);
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
_builder.newLine();
_builder.append("generate g \'http://1\'");
_builder.newLine();
_builder.append("Rule:");
_builder.newLine();
_builder.append("\t");
_builder.append("Fragment;");
_builder.newLine();
_builder.append("fragment Fragment*: name=ID;");
_builder.newLine();
String model = _builder.toString();
final XtextResource r = this.getResourceFromString(model);
EObject _get = r.getContents().get(0);
final Grammar grammar = ((Grammar) _get);
final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
final AbstractElement fragmentCall = rule.getAlternatives();
final EClassifier currentType = GrammarUtil.findCurrentType(fragmentCall);
Assert.assertEquals("Rule", currentType.getName());
}
Aggregations