@Override public void format(final ITextSegment region, final IHiddenRegionFormatting wrapped, @Extension final IFormattableDocument document) { if ((!this.hasWrapped)) { IHiddenRegion _switchResult = null; boolean _matched = false; if (region instanceof IHiddenRegion) { _matched=true; _switchResult = ((IHiddenRegion)region); } if (!_matched) { if (region instanceof IHiddenRegionPart) { _matched=true; _switchResult = ((IHiddenRegionPart)region).getHiddenRegion(); } } final IHiddenRegion hiddenRegion = _switchResult; final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> { it.indent(); }; document.set(hiddenRegion, this.last, _function); this.hasWrapped = true; } }
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); } }
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; }
protected ISequentialRegion copyAndAppend(ISequentialRegion first, ISequentialRegion last) { ISequentialRegion current = first; ISequentialRegion result = null; while (true) { if (current instanceof IHiddenRegion) { copyAndAppend((IHiddenRegion) current); } else if (current instanceof ISemanticRegion) { copyAndAppend((ISemanticRegion) current, current.getText()); } if (result == null) { result = this.last; } if (current == last) { break; } current = current.getNextSequentialRegion(); if (current == null) { throw new IllegalStateException("last didn't match"); } } return result; }
@Override public List<IHiddenRegionPart> merge(ITextRegionAccess b, IHiddenRegion first, IHiddenRegion last, boolean del) { List<IHiddenRegionPart> parts1 = first.getParts(); List<IHiddenRegionPart> parts2 = last.getParts(); boolean firstInFile1 = first.getPreviousSemanticRegion() == null; boolean firstInFile2 = last.getPreviousSemanticRegion() == null; int index1 = firstInFile1 ? getIndexFirstInFile(parts1) : getIndex(parts1); int index2 = firstInFile2 ? getIndexFirstInFile(parts2) : getIndex(parts2); List<IHiddenRegionPart> result = Lists.newArrayList(); if (del && !first.isMultiline()) { result.addAll(parts1); } else { for (int i = 0; i < index1; i++) { result.add(parts1.get(i)); } } if (del && !last.isMultiline()) { result.addAll(parts2); } else { for (int i = index2; i < parts2.size(); i++) { result.add(parts2.get(i)); } } return result; }
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; }
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; }
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; }
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(); } } } } }
@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); }
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; }
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); }
@Override public SequentialRegionDiff apply(StringBasedTextRegionAccessDiffAppender appender) { StringBasedRegionAccess textRegionAccess = sequencer.getRegionAccess(); IEObjectRegion substituteRoot = textRegionAccess.regionForRootEObject(); IHiddenRegion substituteFirst = substituteRoot.getPreviousHiddenRegion(); IHiddenRegion substituteLast = substituteRoot.getNextHiddenRegion(); SequentialRegionDiff result = appender.copyAndAppend((IHiddenRegion) originalFirst, (IHiddenRegion) originalLast, substituteFirst, substituteLast); return result; }
protected void addRemoveAction(IHiddenRegion first, IHiddenRegion last, boolean del) { for (RewriteAction rw : rewrites) { if (rw.originalFirst == last) { rw.originalFirst = first; return; } if (rw.originalLast == first) { rw.originalLast = last; return; } } rewrites.add(new RemoveRewriteAction(first, last, del)); }
@Override public void replace(IHiddenRegion originalFirst, IHiddenRegion originalLast, IHiddenRegion modifiedFirst, IHiddenRegion modifiedLast) { checkOriginal(originalFirst); checkOriginal(originalLast); addInsert(new ReplaceRewriteAction(originalFirst, originalLast, modifiedFirst, modifiedLast)); }
@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); }
@Override public ISequenceAcceptor replaceSequence(IHiddenRegion originalFirst, IHiddenRegion originalLast, ISerializationContext ctx, EObject root) { checkOriginal(originalFirst); checkOriginal(originalLast); TextRegionAccessBuildingSequencer sequenceAcceptor = new TextRegionAccessBuildingSequencer(); addInsert(new SequenceRewriteAction(originalFirst, originalLast, sequenceAcceptor)); return sequenceAcceptor.withRoot(ctx, root); }
protected IHiddenRegion copyAndAppend(IHiddenRegion source) { ISequentialRegion last2 = this.last; StringHiddenRegion region = appendHiddenRegion(source.isUndefined()); List<IHiddenRegionPart> parts; if (region == last2) { parts = hiddenRegionMerger.merge(access.getOriginalTextRegionAccess(), region, source, false); } else { parts = source.getParts(); } copyHiddenRegionParts(region, parts); return region; }
protected IHiddenRegion copyAndAppend(IHiddenRegion region1, IHiddenRegion region2) { StringHiddenRegion region = appendHiddenRegion(region1.isUndefined() || region2.isUndefined()); ITextRegionAccess original = access.getOriginalTextRegionAccess(); List<IHiddenRegionPart> merged = hiddenRegionMerger.merge(original, region1, region2, false); copyHiddenRegionParts(region, merged); return region; }
public SequentialRegionDiff copyAndAppend(IHiddenRegion originalFirst, IHiddenRegion originalLast, IHiddenRegion substituteFirst, IHiddenRegion substituteLast) { if (substituteFirst == substituteLast) { return copySurroundingHidden(originalFirst, originalLast, true); } IHiddenRegion first = copyAndAppend(originalFirst, substituteFirst); ISemanticRegion firstSem = substituteFirst.getNextSemanticRegion(); ISemanticRegion lastSem = substituteLast.getPreviousSemanticRegion(); copyAndAppend(firstSem, lastSem); IHiddenRegion last = copyAndAppend(substituteLast, originalLast); return new SequentialRegionDiff(originalFirst, originalLast, first, last); }
public SequentialRegionDiff copySurroundingHidden(IHiddenRegion originalFirst, IHiddenRegion originalLast, boolean delete) { boolean firstHidden = originalFirst instanceof IHiddenRegion; boolean lastHidden = originalLast instanceof IHiddenRegion; if (firstHidden && lastHidden) { IHiddenRegion hiddenFirst = (IHiddenRegion) originalFirst; IHiddenRegion hiddenLast = (IHiddenRegion) originalLast; StringHiddenRegion merged = appendHiddenRegion(hiddenFirst.isUndefined() || hiddenLast.isUndefined()); ITextRegionAccess original = access.getOriginalTextRegionAccess(); List<IHiddenRegionPart> parts = hiddenRegionMerger.merge(original, hiddenFirst, hiddenLast, delete); copyHiddenRegionParts(merged, parts); return new SequentialRegionDiff(originalFirst, originalLast, merged, merged); } IHiddenRegion inserted = null; if (originalFirst == null) { if (!lastHidden) { inserted = appendHiddenRegion(true); } } else if (originalLast == null) { if (!firstHidden) { inserted = appendHiddenRegion(true); } } else if (!firstHidden && !lastHidden) { inserted = appendHiddenRegion(true); } return new SequentialRegionDiff(originalFirst, originalLast, inserted, inserted); }
@Override public IHiddenRegion previousHiddenRegion(EObject owner) { AbstractEObjectRegion tokens = regionForEObject(owner); if (tokens == null) return null; return tokens.getLeadingHiddenRegion(); }
@Override public IHiddenRegion nextHiddenRegion(EObject owner) { AbstractEObjectRegion tokens = regionForEObject(owner); if (tokens == null) return null; return tokens.getTrailingHiddenRegion(); }
@Override public ISemanticRegion append(ISemanticRegion token, Procedure1<? super IHiddenRegionFormatter> after) { if (token != null) { IHiddenRegion gap = token.getNextHiddenRegion(); set(gap, after); } return token; }
@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; }
@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; }
@Override public ISemanticRegion prepend(ISemanticRegion token, Procedure1<? super IHiddenRegionFormatter> before) { if (token != null) { IHiddenRegion gap = token.getPreviousHiddenRegion(); set(gap, before); } return token; }
@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; }
@Override public Pair<IHiddenRegion, IHiddenRegion> set(IHiddenRegion first, IHiddenRegion second, Procedure1<? super IHiddenRegionFormatter> init) { if (first != null && second != null) { AbstractFormatter2 formatter = getFormatter(); IHiddenRegionFormatting f1 = formatter.createHiddenRegionFormatting(); IHiddenRegionFormatting f2 = formatter.createHiddenRegionFormatting(); init.apply(formatter.createHiddenRegionFormatter(f1, f2)); ITextReplacer replacer1 = formatter.createHiddenRegionReplacer(first, f1); ITextReplacer replacer2 = formatter.createHiddenRegionReplacer(second, f2); addReplacer(replacer1); addReplacer(replacer2); } return Pair.of(first, second); }
@Override public IHiddenRegion set(IHiddenRegion hiddenRegion, Procedure1<? super IHiddenRegionFormatter> init) { if (hiddenRegion != null) { AbstractFormatter2 formatter = getFormatter(); IHiddenRegionFormatting formatting = formatter.createHiddenRegionFormatting(); init.apply(formatter.createHiddenRegionFormatter(formatting)); ITextReplacer replacer = formatter.createHiddenRegionReplacer(hiddenRegion, formatting); addReplacer(replacer); } return hiddenRegion; }
@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; }
@Override public <T extends EObject> T surround(T owner, Procedure1<? super IHiddenRegionFormatter> beforeAndAfter) { if (owner != null && !owner.eIsProxy()) { IEObjectRegion region = getTextRegionAccess().regionForEObject(owner); if (region == null) return owner; IHiddenRegion previous = region.getPreviousHiddenRegion(); IHiddenRegion next = region.getNextHiddenRegion(); set(previous, next, beforeAndAfter); } return owner; }
public IndentOnceAutowrapFormatter(final IHiddenRegion last) { super(); this.last = last; }
Pair<IHiddenRegion, IHiddenRegion> set(IHiddenRegion first, IHiddenRegion second, Procedure1<? super IHiddenRegionFormatter> init);
public RemoveRewriteAction(IHiddenRegion originalFirst, IHiddenRegion originalLast, boolean delete) { super(originalFirst, originalLast); this.delete = delete; }
@Override public SequentialRegionDiff apply(StringBasedTextRegionAccessDiffAppender appender) { return appender.copySurroundingHidden((IHiddenRegion) originalFirst, (IHiddenRegion) originalLast, delete); }
public ReplaceRewriteAction(IHiddenRegion originalFirst, IHiddenRegion originalLast, IHiddenRegion modifiedFirst, IHiddenRegion modifiedLast) { super(originalFirst, originalLast); this.modifiedFirst = modifiedFirst; this.modifiedLast = modifiedLast; }
@Override public SequentialRegionDiff apply(StringBasedTextRegionAccessDiffAppender appender) { SequentialRegionDiff result = appender.copyAndAppend((IHiddenRegion) originalFirst, (IHiddenRegion) originalLast, modifiedFirst, modifiedLast); return result; }
public SequenceRewriteAction(IHiddenRegion originalFirst, IHiddenRegion originalLast, TextRegionAccessBuildingSequencer sequencer) { super(originalFirst, originalLast); this.sequencer = sequencer; }