org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion Java Examples

The following examples show how to use org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(Keyword kw1, Keyword kw2) {
	Preconditions.checkNotNull(kw1);
	Preconditions.checkNotNull(kw2);
	Preconditions.checkArgument(kw1 != kw2);
	Predicate<ISemanticRegion> p1 = createPredicate(kw1);
	Predicate<ISemanticRegion> p2 = createPredicate(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 if (!stack.isEmpty())
			result.add(Pair.of(stack.pop(), region));
	}
	return result.build();
}
 
Example #2
Source File: ReferenceUpdater.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected ISemanticRegion getRegion(ITextRegionAccess access, IReferenceSnapshot ref) {
	XtextResource resource = access.getResource();
	URI objectUri = ref.getSourceEObjectUri();
	if (!resource.getURI().equals(objectUri.trimFragment())) {
		return null;
	}
	EObject object = resource.getEObject(objectUri.fragment());
	if (object == null) {
		return null;
	}
	ISemanticRegionsFinder finder = access.getExtensions().regionFor(object);
	int index = ref.getIndexInList();
	if (index < 0) {
		return finder.feature(ref.getEReference());
	} else {
		List<ISemanticRegion> list = finder.features(ref.getEReference());
		if (list != null && index < list.size()) {
			return list.get(index);
		}
	}
	return null;
}
 
Example #3
Source File: TestLanguageReferenceUpdater.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void updateReference(ITextRegionDiffBuilder rewriter, IUpdatableReference ref) {
	if (rewriter.isModified(ref.getReferenceRegion())) {
		return;
	}
	IScope scope = scopeProvider.getScope(ref.getSourceEObject(), ref.getEReference());
	ISemanticRegion region = ref.getReferenceRegion();
	QualifiedName oldName = nameConverter.toQualifiedName(region.getText());
	IEObjectDescription oldDesc = scope.getSingleElement(oldName);
	if (oldDesc != null && oldDesc.getEObjectOrProxy() == ref.getTargetEObject()) {
		return;
	}
	String newName = findValidName(ref, scope);
	if (newName != null) {
		if (oldName.getSegmentCount() > 1) {
			newName = oldName.skipLast(1).append(newName).toString();
		}
		rewriter.replace(region, newName);
	}
}
 
Example #4
Source File: TextRegionAccessToString.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected String toString(ITextSegment region) {
	String result;
	if (region instanceof IEObjectRegion)
		result = toString((IEObjectRegion) region);
	else if (region instanceof ISemanticRegion)
		result = toString((ISemanticRegion) region);
	else if (region instanceof IHiddenRegion)
		result = toString((IHiddenRegion) region);
	else if (region instanceof IWhitespace)
		result = toString((IWhitespace) region);
	else if (region instanceof IComment)
		result = toString((IComment) region);
	else if (region != null)
		result = region.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(region));
	else
		result = "null";
	if (hightlightOrigin && region == origin)
		return ">>>" + result + "<<<";
	return result;
}
 
Example #5
Source File: StringBasedTextRegionAccessDiffAppender.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public void copyAndAppend(ISemanticRegion substituteFirst, ISemanticRegion substituteLast) {
	if (!(this.last instanceof StringHiddenRegion)) {
		appendHiddenRegion(true);
	}
	ISequentialRegion current = substituteFirst;
	while (true) {
		if (current instanceof IHiddenRegion) {
			copyAndAppend((IHiddenRegion) current);
		} else if (current instanceof ISemanticRegion) {
			copyAndAppend((ISemanticRegion) current);
		}
		if (current == substituteLast) {
			break;
		}
		current = current.getNextSequentialRegion();
		if (current == null) {
			throw new IllegalStateException("last didn't match");
		}
	}
}
 
Example #6
Source File: ReferenceUpdater.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void updateReference(ITextRegionDiffBuilder rewriter, IUpdatableReference upd) {
	IUpdatableReference updatable = upd;
	if (rewriter.isModified(updatable.getReferenceRegion())) {
		return;
	}
	IScope scope = scopeProvider.getScope(updatable.getSourceEObject(), updatable.getEReference());
	ISemanticRegion region = updatable.getReferenceRegion();
	QualifiedName oldName = nameConverter.toQualifiedName(region.getText());
	IEObjectDescription oldDesc = scope.getSingleElement(oldName);
	if (oldDesc != null && oldDesc.getEObjectOrProxy() == updatable.getTargetEObject()) {
		return;
	}
	String newName = findValidName(updatable, scope);
	if (newName != null) {
		rewriter.replace(region, newName);
	}
}
 
Example #7
Source File: SemanticRegionIterable.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Iterator<ISemanticRegion> iterator() {
	return new AbstractIterator<ISemanticRegion>() {
		private ISemanticRegion next = first;

		@Override
		protected ISemanticRegion computeNext() {
			if (next == null)
				return endOfData();
			ISemanticRegion result = next;
			next = next.getNextSemanticRegion();
			if (result == last)
				next = null;
			return result;
		}
	};
}
 
Example #8
Source File: XbaseFormatter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _format(final XFeatureCall expr, @Extension final IFormattableDocument format) {
  this.formatFeatureCallTypeParameters(expr, format);
  boolean _isExplicitOperationCall = expr.isExplicitOperationCall();
  if (_isExplicitOperationCall) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    final ISemanticRegion open = format.prepend(this.textRegionExtensions.regionFor(expr).keyword(this.grammar.getXFeatureCallAccess().getExplicitOperationCallLeftParenthesisKeyword_3_0_0()), _function);
    final ISemanticRegion close = this.textRegionExtensions.regionFor(expr).keyword(this.grammar.getXFeatureCallAccess().getRightParenthesisKeyword_3_2());
    this.formatFeatureCallParams(expr.getFeatureCallArguments(), open, close, format);
  } else {
    EList<XExpression> _featureCallArguments = expr.getFeatureCallArguments();
    for (final XExpression arg : _featureCallArguments) {
      this.format(arg, format);
    }
  }
}
 
Example #9
Source File: XbaseFormatter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _format(final XCollectionLiteral literal, @Extension final IFormattableDocument document) {
  final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
    it.noSpace();
  };
  document.append(this.textRegionExtensions.regionFor(literal).keyword("#"), _function);
  ISemanticRegion _elvis = null;
  ISemanticRegion _keyword = this.textRegionExtensions.regionFor(literal).keyword("[");
  if (_keyword != null) {
    _elvis = _keyword;
  } else {
    ISemanticRegion _keyword_1 = this.textRegionExtensions.regionFor(literal).keyword("{");
    _elvis = _keyword_1;
  }
  final ISemanticRegion open = _elvis;
  ISemanticRegion _elvis_1 = null;
  ISemanticRegion _keyword_2 = this.textRegionExtensions.regionFor(literal).keyword("]");
  if (_keyword_2 != null) {
    _elvis_1 = _keyword_2;
  } else {
    ISemanticRegion _keyword_3 = this.textRegionExtensions.regionFor(literal).keyword("}");
    _elvis_1 = _keyword_3;
  }
  final ISemanticRegion close = _elvis_1;
  this.formatCommaSeparatedList(literal.getElements(), open, close, document);
}
 
Example #10
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static boolean fixBug(IHiddenRegion hiddenRegion) {
	boolean needBugFix = true;
	final ISemanticRegion semanticRegion = hiddenRegion.getNextSemanticRegion();
	if (semanticRegion != null) {
		final EObject element = semanticRegion.getGrammarElement();
		if (element instanceof Keyword
				&& Strings.equal(((Keyword) element).getValue(), "}")) { //$NON-NLS-1$
			needBugFix = false;
		}
	}
	return needBugFix;
}
 
Example #11
Source File: SemanticRegionFinderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void regionForAssignment() throws Exception {
	Mixed mixed = parseAs("6 (foo)", Mixed.class);
	ISemanticRegionsFinder finder = toAccess(mixed).regionForEObject(mixed).getRegionFor();
	ISemanticRegion actual = finder
			.assignment(grammarAccess.getMixedAccess().getNameAssignment_2_2_0());
	List<ISemanticRegion> actuals = finder
			.assignments(grammarAccess.getMixedAccess().getNameAssignment_2_2_0());
	assertEquals("foo", actual, actuals);
}
 
Example #12
Source File: SemanticRegionFinderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void regionForKeyword() throws Exception {
	Mixed mixed = parseAs("6 (foo)", Mixed.class);
	ISemanticRegionsFinder finder = toAccess(mixed).regionForEObject(mixed).getRegionFor();
	ISemanticRegion actual = finder
			.keyword(grammarAccess.getMixedAccess().getLeftParenthesisKeyword_0());
	List<ISemanticRegion> actuals = finder
			.keywords(grammarAccess.getMixedAccess().getLeftParenthesisKeyword_0());
	assertEquals("(", actual, actuals);
}
 
Example #13
Source File: AbstractRegionAccess.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterable<ISemanticRegion> allSemanticRegions(EObject object) {
	AbstractEObjectRegion region = regionForEObject(object);
	if (region == null)
		return Collections.emptyList();
	return region.getAllSemanticRegions();
}
 
Example #14
Source File: SemanticRegionFinderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void regionForRuleCallAssignedDataType() throws Exception {
	Mixed mixed = parseAs("6 (datatype foo)", Mixed.class);
	ISemanticRegionsFinder finder = toAccess(mixed).regionForEObject(mixed).getRegionFor();
	ISemanticRegion actual = finder.ruleCall(grammarAccess.getMixedAccess()
			.getDatatypeDatatypeParserRuleCall_2_2_2_0());
	ISemanticRegion actuals = finder.ruleCall(grammarAccess.getMixedAccess()
			.getDatatypeDatatypeParserRuleCall_2_2_2_0());
	Assert.assertEquals("datatype foo", actual, actuals);
}
 
Example #15
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ISemanticRegion surround(ISemanticRegion token, Procedure1<? super IHiddenRegionFormatter> beforeAndAfter) {
	if (token != null) {
		IHiddenRegion previous = token.getPreviousHiddenRegion();
		IHiddenRegion next = token.getNextHiddenRegion();
		set(previous, next, beforeAndAfter);
	}
	return token;
}
 
Example #16
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ISemanticRegion prepend(ISemanticRegion token, Procedure1<? super IHiddenRegionFormatter> before) {
	if (token != null) {
		IHiddenRegion gap = token.getPreviousHiddenRegion();
		set(gap, before);
	}
	return token;
}
 
Example #17
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T1 extends ISemanticRegion, T2 extends ISemanticRegion> //
Pair<T1, T2> interior(T1 first, T2 second, Procedure1<? super IHiddenRegionFormatter> init) {
	if (first != null && second != null) {
		set(first.getNextHiddenRegion(), second.getPreviousHiddenRegion(), init);
	}
	return Pair.of(first, second);
}
 
Example #18
Source File: RichStringToLineModel.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void announceNextLiteral(final RichStringLiteral object) {
  super.announceNextLiteral(object);
  if (((this.lastLiteralEndOffset > 0) && (this.contentStartOffset < 0))) {
    this.contentStartOffset = this.lastLiteralEndOffset;
  }
  IEObjectRegion _regionForEObject = this.nodeModelAccess.regionForEObject(object);
  ISemanticRegionsFinder _regionFor = null;
  if (_regionForEObject!=null) {
    _regionFor=_regionForEObject.getRegionFor();
  }
  ISemanticRegion _feature = null;
  if (_regionFor!=null) {
    _feature=_regionFor.feature(XbasePackage.Literals.XSTRING_LITERAL__VALUE);
  }
  final ISemanticRegion node = _feature;
  if ((node != null)) {
    int _offset = node.getOffset();
    int _literalPrefixLenght = this.literalPrefixLenght(node);
    int _plus = (_offset + _literalPrefixLenght);
    this.offset = _plus;
    int _endOffset = node.getEndOffset();
    int _literalPostfixLenght = this.literalPostfixLenght(node);
    int _minus = (_endOffset - _literalPostfixLenght);
    this.lastLiteralEndOffset = _minus;
  }
  this.content = true;
}
 
Example #19
Source File: SemanticRegionInIterableFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ImmutableList<ISemanticRegion> findAll(Predicate<ISemanticRegion> predicate) {
	Builder<ISemanticRegion> builder = ImmutableList.builder();
	for (ISemanticRegion region : regions)
		if (predicate.apply(region))
			builder.add(region);
	return builder.build();
}
 
Example #20
Source File: SemanticRegionInIterableFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ISemanticRegion findFirst(Predicate<ISemanticRegion> predicate) {
	for (ISemanticRegion region : regions)
		if (predicate.apply(region))
			return region;
	return null;
}
 
Example #21
Source File: StringBasedTextRegionAccessDiffAppender.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public StringBasedTextRegionAccessDiff finish() {
	if (this.last instanceof ISemanticRegion) {
		appendHiddenRegion(false);
	}
	updateEObjectRegions();
	if (diffFirstOriginal != null && diffFirstCopy != null && diffFirstCopy != null) {
		IHiddenRegion orig = result.getOriginalTextRegionAccess().regionForRootEObject().getNextHiddenRegion();
		result.append(new SequentialRegionDiff(diffFirstOriginal, orig, diffFirstCopy, this.last));
		diffFirstCopy = null;
		diffFirstOriginal = null;
	}
	return result;
}
 
Example #22
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ISemanticRegion append(ISemanticRegion token, Procedure1<? super IHiddenRegionFormatter> after) {
	if (token != null) {
		IHiddenRegion gap = token.getNextHiddenRegion();
		set(gap, after);
	}
	return token;
}
 
Example #23
Source File: SemanticRegionMatcher.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ImmutableList<ISemanticRegion> findAll(Predicate<ISemanticRegion> predicate) {
	ISemanticRegion element = findFirst(predicate);
	if (element == null)
		return ImmutableList.of();
	else
		return ImmutableList.of(element);
}
 
Example #24
Source File: PureXbaseFormatter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void _format(final XBlockExpression xBlockExpression, @Extension final IFormattableDocument document) {
  final Consumer<ISemanticRegion> _function = (ISemanticRegion it) -> {
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
      it_1.newLine();
    };
    document.append(it, _function_1);
  };
  this.textRegionExtensions.regionFor(xBlockExpression).keywords(this._pureXbaseGrammarAccess.getSpecialBlockExpressionAccess().getSemicolonKeyword_1_1()).forEach(_function);
  EList<XExpression> _expressions = xBlockExpression.getExpressions();
  for (final XExpression xExpression : _expressions) {
    document.<XExpression>format(xExpression);
  }
}
 
Example #25
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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();
}
 
Example #26
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean apply(ISemanticRegion input) {
	if (input == null)
		return false;
	EObject element = input.getGrammarElement();
	Assignment assignment = GrammarUtil.containingAssignment(element);
	if (assignment == null || !name.equals(assignment.getFeature()))
		return false;
	EObject semanticElement = input.getSemanticElement();
	return type.isInstance(semanticElement);
}
 
Example #27
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Predicate<ISemanticRegion> createPredicate(AbstractElement... ele) {
	Set<AbstractElement> result = Sets.newHashSet();
	for (AbstractElement e : ele)
		collectMatchableElements(e, result);
	switch (result.size()) {
		case 0:
			return Predicates.alwaysFalse();
		case 1:
			return new GrammarElementPredicate(result.iterator().next());
		default:
			return new GrammarElementsPredicate(result);
	}
}
 
Example #28
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Predicate<ISemanticRegion> createPredicate(AbstractElement ele) {
	switch (ele.eClass().getClassifierID()) {
		case XtextPackage.RULE_CALL:
			assertNoEObjectRuleCall((RuleCall) ele);
		case XtextPackage.KEYWORD:
			return new GrammarElementPredicate(ele);
	}
	return createPredicate(new AbstractElement[] { ele });
}
 
Example #29
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean apply(ISemanticRegion input) {
	if (input == null)
		return false;
	EObject element = input.getGrammarElement();
	return element instanceof RuleCall && rules.contains(((RuleCall) element).getRule());
}
 
Example #30
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean apply(ISemanticRegion input) {
	if (input == null)
		return false;
	EObject element = input.getGrammarElement();
	return element instanceof RuleCall && ((RuleCall) element).getRule() == rule;
}