Java Code Examples for org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion

The following examples show how to use org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion. These examples are extracted from open source projects. 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 Project: xtext-extras   Source File: FormatterTester.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
	List<ITextSegment> expected = Lists.newArrayList();
	IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
	while (current != null) {
		expected.addAll(current.getMergedSpaces());
		current = current.getNextHiddenRegion();
	}
	List<ITextSegment> missing = TextRegions.difference(expected, replacements);
	if (!missing.isEmpty()) {
		TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
		for (ITextSegment region : missing)
			toString.add(region, region.getClass().getSimpleName());
		String msg = "The following regions are not formatted:\n" + toString;
		System.err.println(msg);
		Assert.fail(msg);
	}
}
 
Example 2
Source Project: xtext-eclipse   Source File: FormatterTester.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
	List<ITextSegment> expected = Lists.newArrayList();
	IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
	while (current != null) {
		expected.addAll(current.getMergedSpaces());
		current = current.getNextHiddenRegion();
	}
	List<ITextSegment> missing = TextRegions.difference(expected, replacements);
	if (!missing.isEmpty()) {
		TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
		for (ITextSegment region : missing)
			toString.add(region, region.getClass().getSimpleName());
		String msg = "The following regions are not formatted:\n" + toString;
		System.err.println(msg);
		Assert.fail(msg);
	}
}
 
Example 3
protected List<Rewrite> createList() {
	List<Rewrite> sorted = Lists.newArrayList(rewrites);
	Collections.sort(sorted);
	List<Rewrite> result = Lists.newArrayListWithExpectedSize(sorted.size() * 2);
	IHiddenRegion last = original.regionForRootEObject().getPreviousHiddenRegion();
	for (Rewrite rw : sorted) {
		int lastOffset = last.getOffset();
		int rwOffset = rw.originalFirst.getOffset();
		if (rwOffset == lastOffset) {
			result.add(rw);
			last = rw.originalLast;
		} else if (rwOffset > lastOffset) {
			result.add(new Preserve(last, rw.originalFirst));
			result.add(rw);
			last = rw.originalLast;
		} else {
			LOG.error("Error, conflicting document modifications.");
		}
	}
	IHiddenRegion end = original.regionForRootEObject().getNextHiddenRegion();
	if (last.getOffset() < end.getOffset()) {
		result.add(new Preserve(last, end));
	}
	return result;
}
 
Example 4
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 5
Source Project: xtext-core   Source File: TextReplacerMerger.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected ITextReplacer mergeHiddenRegionReplacers(List<? extends ITextReplacer> conflicting) {
	List<IHiddenRegionFormatting> formattings = Lists.newArrayList();
	IHiddenRegion region = null;
	for (ITextReplacer replacer : conflicting) {
		if (replacer instanceof HiddenRegionReplacer) {
			HiddenRegionReplacer hiddenRegionReplacer = (HiddenRegionReplacer) replacer;
			formattings.add(hiddenRegionReplacer.getFormatting());
			if (region == null)
				region = hiddenRegionReplacer.getRegion();
			else if (region != hiddenRegionReplacer.getRegion())
				return null;
		} else
			return null;
	}
	IHiddenRegionFormatting mergedFormatting = merger.merge(formattings);
	if (mergedFormatting != null)
		return formatter.createHiddenRegionReplacer(region, mergedFormatting);
	return null;
}
 
Example 6
Source Project: xtext-core   Source File: WhitespaceReplacer.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected int computeNewLineCount(ITextReplacerContext context) {
	Integer newLineDefault = formatting.getNewLineDefault();
	Integer newLineMin = formatting.getNewLineMin();
	Integer newLineMax = formatting.getNewLineMax();
	if (newLineMin != null || newLineDefault != null || newLineMax != null) {
		if (region instanceof IHiddenRegion && ((IHiddenRegion) region).isUndefined()) {
			if (newLineDefault != null)
				return newLineDefault;
			if (newLineMin != null)
				return newLineMin;
			if (newLineMax != null)
				return newLineMax;
		} else {
			int lineCount = region.getLineCount() - 1;
			if (newLineMin != null && newLineMin > lineCount)
				lineCount = newLineMin;
			if (newLineMax != null && newLineMax < lineCount)
				lineCount = newLineMax;
			return lineCount;
		}
	}
	return 0;
}
 
Example 7
Source Project: xtext-core   Source File: AbstractFormatter2.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isInRequestedRange(EObject obj) {
	Collection<ITextRegion> regions = request.getRegions();
	if (regions.isEmpty())
		return true;
	ITextRegionAccess access = request.getTextRegionAccess();
	IEObjectRegion objRegion = access.regionForEObject(obj);
	if (objRegion == null)
		return false;
	IHiddenRegion previousHidden = objRegion.getPreviousHiddenRegion();
	IHiddenRegion nextHidden = objRegion.getNextHiddenRegion();
	int objOffset = previousHidden != null ? previousHidden.getOffset() : 0;
	int objEnd = nextHidden != null ? nextHidden.getEndOffset() : access.regionForRootEObject().getEndOffset();
	for (ITextRegion region : regions) {
		int regionOffset = region.getOffset();
		int regionEnd = regionOffset + region.getLength();
		if (regionOffset <= objEnd && regionEnd >= objOffset)
			return true;
	}
	return false;
}
 
Example 8
Source Project: xtext-core   Source File: FormatterTestHelper.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
	List<ITextSegment> expected = Lists.newArrayList();
	IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
	while (current != null) {
		expected.addAll(current.getMergedSpaces());
		current = current.getNextHiddenRegion();
	}
	List<ITextSegment> missing = TextRegions.difference(expected, replacements);
	if (!missing.isEmpty()) {
		TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
		for (ITextSegment region : missing)
			toString.add(region, region.getClass().getSimpleName());
		String msg = "The following regions are not formatted:\n" + toString;
		System.err.println(msg);
		Assert.fail(msg);
	}
}
 
Example 9
private void assertToStringDoesNotCrash(final ITextRegionAccess access) {
  IHiddenRegion _previousHiddenRegion = access.regionForRootEObject().getPreviousHiddenRegion();
  ISequentialRegion current = ((ISequentialRegion) _previousHiddenRegion);
  while ((current != null)) {
    {
      Assert.assertNotNull(current.toString());
      boolean _matched = false;
      if (current instanceof IHiddenRegion) {
        _matched=true;
        current = ((IHiddenRegion)current).getNextSemanticRegion();
      }
      if (!_matched) {
        if (current instanceof ISemanticRegion) {
          _matched=true;
          Assert.assertNotNull(((ISemanticRegion)current).getEObjectRegion().toString());
          current = ((ISemanticRegion)current).getNextHiddenRegion();
        }
      }
    }
  }
}
 
Example 10
Source Project: xtext-core   Source File: PartialSerializer.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void serialize(ITextRegionDiffBuilder result) {
	ISemanticSequencer semantic = semanticSequencerProvider.get();
	ISyntacticSequencer syntactic = syntacticSequencerProvider.get();
	IHiddenTokenSequencer hidden = hiddenTokenSequencerProvider.get();
	semantic.init((ISemanticSequenceAcceptor) syntactic, errorAcceptor);
	syntactic.init(context, root, (ISyntacticSequenceAcceptor) hidden, errorAcceptor);
	ISequenceAcceptor acceptor;
	if (insertAt instanceof IHiddenRegion) {
		IHiddenRegion h = (IHiddenRegion) insertAt;
		acceptor = result.replaceSequence(h, h, context, root);
	} else {
		IHiddenRegion originalFirst = insertAt.getPreviousHiddenRegion();
		IHiddenRegion originalLast = insertAt.getNextHiddenRegion();
		acceptor = result.replaceSequence(originalFirst, originalLast, context, root);
	}
	hidden.init(context, root, acceptor, errorAcceptor);
	if (acceptor instanceof TokenStreamSequenceAdapter)
		((TokenStreamSequenceAdapter) acceptor).init(context);
	semantic.createSequence(context, root);
}
 
Example 11
Source Project: xtext-core   Source File: InsertionPointFinder.java    License: Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example 12
protected String toString(IHiddenRegion hiddens) {
	List<IHiddenRegionPart> parts = hiddens.getParts();
	if (parts.isEmpty())
		return HIDDEN;
	List<String> children = Lists.newArrayListWithExpectedSize(parts.size());
	children.add(HIDDEN_PADDED + toString(parts.get(0)));
	for (int i = 1; i < parts.size(); i++)
		children.add(EMPTY_TITLE + toString(parts.get(i)));
	return Joiner.on("\n").join(children);
}
 
Example 13
Source Project: sarl   Source File: BugMultilineCommentIndentation.java    License: Apache License 2.0 5 votes vote down vote up
/** Fixing the bug.
 *
 * @param context the replacement context.
 * @param comment the comment for which the fix must be applied.
 * @return the new context.
 */
@SuppressWarnings("static-method")
public ITextReplacerContext fix(final ITextReplacerContext context, IComment comment) {
	final IHiddenRegion hiddenRegion = comment.getHiddenRegion();
	if (detectBugSituation(hiddenRegion) && fixBug(hiddenRegion)) {
		// Indentation of the first comment line
		final ITextRegionAccess access = comment.getTextRegionAccess();
		final ITextSegment target = access.regionForOffset(comment.getOffset(), 0);
		context.addReplacement(target.replaceWith(context.getIndentationString(1)));
		// Indentation of the comment's lines
		return new FixedReplacementContext(context);
	}
	return context;
}
 
Example 14
@Override
public StringBasedTextRegionAccessDiff create() {
	StringBasedTextRegionAccessDiffAppender appender = createAppender();
	IEObjectRegion root = original.regionForRootEObject();
	appender.copyAndAppend(root.getPreviousHiddenRegion(), PREVIOUS);
	appender.copyAndAppend(root.getPreviousHiddenRegion(), CONTAINER);
	List<Rewrite> rws = createList();
	IHiddenRegion last = null;
	for (Rewrite rw : rws) {
		boolean diff = rw.isDiff();
		if (diff) {
			appender.beginDiff();
		}
		if (rw instanceof Insert) {
			Insert ins = (Insert) rw;
			IHiddenRegion f = ins.getInsertFirst();
			IHiddenRegion l = ins.getInsertLast();
			appender.copyAndAppend(f, NEXT);
			if (f != l) {
				appender.copyAndAppend(f.getNextSemanticRegion(), l.getPreviousSemanticRegion());
			}
			appender.copyAndAppend(l, PREVIOUS);
		}
		if (diff) {
			appender.endDiff();
		}
		if (rw.originalLast != last) {
			appender.copyAndAppend(rw.originalLast, CONTAINER);
		}
		last = rw.originalLast;
	}
	appender.copyAndAppend(root.getNextHiddenRegion(), NEXT);
	StringBasedTextRegionAccessDiff result = appender.finish();
	AbstractEObjectRegion newRoot = result.regionForEObject(root.getSemanticElement());
	result.setRootEObject(newRoot);
	return result;
}
 
Example 15
@Override
public void move(IHiddenRegion insertAt, IHiddenRegion substituteFirst, IHiddenRegion substituteLast) {
	checkOriginal(insertAt);
	checkOriginal(substituteFirst);
	checkOriginal(substituteLast);
	MoveSource source = new MoveSource(substituteFirst, substituteLast);
	MoveTarget target = new MoveTarget(insertAt, source);
	rewrites.add(source);
	rewrites.add(target);
}
 
Example 16
@Override
public void replace(IHiddenRegion originalFirst, IHiddenRegion originalLast, IHiddenRegion modifiedFirst,
		IHiddenRegion modifiedLast) {
	checkOriginal(originalFirst);
	checkOriginal(originalLast);
	rewrites.add(new Replace1(originalFirst, originalLast, modifiedFirst, modifiedLast));
}
 
Example 17
@Override
public void replace(IHiddenRegion originalFirst, IHiddenRegion originalLast, ITextRegionAccess acc) {
	checkOriginal(originalFirst);
	checkOriginal(originalLast);
	IEObjectRegion substituteRoot = acc.regionForRootEObject();
	IHiddenRegion substituteFirst = substituteRoot.getPreviousHiddenRegion();
	IHiddenRegion substituteLast = substituteRoot.getNextHiddenRegion();
	replace(originalFirst, originalLast, substituteFirst, substituteLast);
}
 
Example 18
@Override
public ISequenceAcceptor replaceSequence(IHiddenRegion originalFirst, IHiddenRegion originalLast,
		ISerializationContext ctx, EObject root) {
	checkOriginal(originalFirst);
	checkOriginal(originalLast);
	TextRegionAccessBuildingSequencer sequenceAcceptor = new TextRegionAccessBuildingSequencer();
	rewrites.add(new Replace2(originalFirst, originalLast, sequenceAcceptor));
	return sequenceAcceptor.withRoot(ctx, root);
}
 
Example 19
Source Project: sarl   Source File: BugMultilineCommentIndentation.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean detectBugSituation(IHiddenRegion hiddenRegion) {
	if (hiddenRegion != null) {
		final ISemanticRegion semanticRegion = hiddenRegion.getPreviousSemanticRegion();
		if (semanticRegion != null) {
			final EObject element = semanticRegion.getGrammarElement();
			if (element instanceof Keyword
					&& Strings.equal(((Keyword) element).getValue(), "{")) { //$NON-NLS-1$
				return true;
			}
		}
	}
	return false;
}
 
Example 20
public StringBasedTextRegionAccessDiffAppender(ITextRegionAccess base, Map<ITextSegment, String> textChanges) {
	super();
	this.result = new StringBasedTextRegionAccessDiff(base);
	this.textChanges = textChanges;
	IHiddenRegion region = base.regionForRootEObject().getPreviousHiddenRegion();
	this.diffLastOriginal = region;
	this.diffLastCopy = appendHiddenRegion(region.isUndefined());
}
 
Example 21
protected IHiddenRegion copyAndAppend(IHiddenRegion source) {
	StringHiddenRegion region = appendHiddenRegion(source.isUndefined());
	for (IHiddenRegionPart part : source.getParts()) {
		copyAndAppend(part);
	}
	return region;
}
 
Example 22
public void copyAndAppend(IHiddenRegion region, HiddenRegionPartAssociation association) {
	for (IHiddenRegionPart part : region.getParts()) {
		if (part.getAssociation() == association) {
			copyAndAppend(part);
		}
	}
}
 
Example 23
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 24
Source Project: xtext-core   Source File: AbstractRegionAccess.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IHiddenRegion previousHiddenRegion(EObject owner) {
	AbstractEObjectRegion tokens = regionForEObject(owner);
	if (tokens == null)
		return null;
	return tokens.getLeadingHiddenRegion();
}
 
Example 25
Source Project: xtext-core   Source File: AbstractRegionAccess.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IHiddenRegion nextHiddenRegion(EObject owner) {
	AbstractEObjectRegion tokens = regionForEObject(owner);
	if (tokens == null)
		return null;
	return tokens.getTrailingHiddenRegion();
}
 
Example 26
Source Project: xtext-core   Source File: FormattableDocument.java    License: 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 27
Source Project: xtext-core   Source File: FormattableDocument.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T extends EObject> T append(T owner, Procedure1<? super IHiddenRegionFormatter> after) {
	if (owner != null) {
		IEObjectRegion region = getTextRegionAccess().regionForEObject(owner);
		if (region != null) {
			IHiddenRegion gap = region.getNextHiddenRegion();
			set(gap, after);
		}
	}
	return owner;
}
 
Example 28
Source Project: xtext-core   Source File: FormattableDocument.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T extends EObject> T interior(T object, Procedure1<? super IHiddenRegionFormatter> init) {
	if (object != null) {
		IEObjectRegion objRegion = getTextRegionAccess().regionForEObject(object);
		if (objRegion != null) {
			IHiddenRegion previous = objRegion.getPreviousHiddenRegion();
			IHiddenRegion next = objRegion.getNextHiddenRegion();
			if (previous != null && next != null && previous != next) {
				interior(previous.getNextSemanticRegion(), next.getPreviousSemanticRegion(), init);
			}
		}
	}
	return object;
}
 
Example 29
Source Project: xtext-core   Source File: FormattableDocument.java    License: 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 30
Source Project: xtext-core   Source File: FormattableDocument.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T extends EObject> T prepend(T owner, Procedure1<? super IHiddenRegionFormatter> before) {
	if (owner != null) {
		IEObjectRegion region = getTextRegionAccess().regionForEObject(owner);
		if (region != null) {
			IHiddenRegion gap = region.getPreviousHiddenRegion();
			set(gap, before);
		}
	}
	return owner;
}