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

The following examples show how to use org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion. 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: InsertionPointFinder.java    From xtext-core with 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 #2
Source File: FormatterTester.java    From xtext-eclipse with 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
Source File: StringBasedTextRegionAccessDiffBuilder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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
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 #5
Source File: TextReplacerMerger.java    From xtext-core with 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 File: WhitespaceReplacer.java    From xtext-core with 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 File: FormatterTestHelper.java    From xtext-core with 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 #8
Source File: RegionAccessBuilderTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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 #9
Source File: PartialSerializer.java    From xtext-core with 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 #10
Source File: AbstractFormatter2.java    From xtext-core with 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 #11
Source File: FormatterTester.java    From xtext-extras with 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 #12
Source File: TextRegionAccessToString.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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 File: StringBasedTextRegionAccessDiffBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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 #14
Source File: BugSinglelineCommentIndentation.java    From sarl with 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)));
	}
	return context;
}
 
Example #15
Source File: FormattableDocument.java    From xtext-core with 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;
}
 
Example #16
Source File: StringBasedTextRegionAccessDiffBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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
Source File: StringBasedTextRegionAccessDiffBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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
Source File: StringBasedTextRegionAccessDiffBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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 File: BugMultilineCommentIndentation.java    From sarl with 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
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 #21
Source File: FormattableDocument.java    From xtext-core with 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 #22
Source File: FormattableDocument.java    From xtext-core with 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 #23
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 #24
Source File: StringBasedTextRegionAccessDiffAppender.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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 #25
Source File: StringBasedTextRegionAccessDiffAppender.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected IHiddenRegion copyAndAppend(IHiddenRegion source) {
	StringHiddenRegion region = appendHiddenRegion(source.isUndefined());
	for (IHiddenRegionPart part : source.getParts()) {
		copyAndAppend(part);
	}
	return region;
}
 
Example #26
Source File: StringBasedTextRegionAccessDiffAppender.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public void copyAndAppend(IHiddenRegion region, HiddenRegionPartAssociation association) {
	for (IHiddenRegionPart part : region.getParts()) {
		if (part.getAssociation() == association) {
			copyAndAppend(part);
		}
	}
}
 
Example #27
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 #28
Source File: AbstractRegionAccess.java    From xtext-core with 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 #29
Source File: AbstractRegionAccess.java    From xtext-core with 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 #30
Source File: BugSinglelineCommentIndentation.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.getPreviousSemanticRegion();
	if (semanticRegion != null) {
		final EObject element = semanticRegion.getGrammarElement();
		if (element instanceof Keyword
				&& Strings.equal(((Keyword) element).getValue(), "{")) { //$NON-NLS-1$
			needBugFix = false;
		}
	}
	return needBugFix;
}