@Override public ITextReplacerContext createReplacements(ITextReplacerContext context) { AbstractFormatter2 formatter = context.getFormatter(); List<IHiddenRegionPart> hiddens = region.getParts(); if (hiddens.isEmpty()) { return formatter.createWhitespaceReplacer(region, formatting).createReplacements(context); } else if ((hiddens.size() == 1 && hiddens.get(0) instanceof IWhitespace)) { return formatter.createWhitespaceReplacer(hiddens.get(0), formatting).createReplacements(context); } else { List<ITextReplacer> replacers = createReplacers(formatter); applyHiddenRegionFormatting(replacers); ITextReplacerContext current = context; current.setNextReplacerIsChild(); for (ITextReplacer replacer : replacers) current = replacer.createReplacements(current.withReplacer(replacer)); return current; } }
protected ITextReplacer mergeCompositeReplacers(List<? extends ITextReplacer> conflicting) { ICompositeTextReplacer composite = null; for (ITextReplacer replacer : conflicting) if (replacer instanceof ICompositeTextReplacer) { if (composite == null) composite = ((ICompositeTextReplacer) replacer); else return null; } if (composite == null) return null; for (ITextReplacer r : conflicting) if (r != composite) composite.addReplacer(r); return composite; }
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; }
@Override public ITextReplacerContext createReplacements(ITextReplacerContext context) { context.setNextReplacerIsChild(); for (ISubFormatter formatter : subFormatters) { try { ITextSegment region = getRegion(); SubDocument subDocument = new SubDocument(region, getDocument()); for (ITextReplacer replacer : replacers) subDocument.addReplacer(replacer); formatter.format(subDocument); ITextReplacerContext first = context.withReplacer(subDocument); ITextReplacerContext last = subDocument.createReplacements(first); return last; } catch (FormattingNotApplicableException e) { // no need to do anything. // Try the next SubFormatter until one doens't throw a FormattingNotApplicableException } } throw new FormattingNotApplicableException(); }
@Override public void addReplacer(ITextReplacer replacer) { if (!this.getRegion().contains(replacer.getRegion())) { String frameTitle = getClass().getSimpleName(); ITextSegment frameRegion = getRegion(); String replacerTitle = replacer.getClass().getSimpleName(); ITextSegment replacerRegion = replacer.getRegion(); RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, replacerRegion)); getRequest().getExceptionHandler().accept(exception); return; } try { getReplacers().add(replacer, getFormatter().createTextReplacerMerger()); } catch (ConflictingRegionsException e) { getRequest().getExceptionHandler().accept(e); } }
@Override public ITextReplacerContext withReplacer(ITextReplacer replacer) { ITextReplacerContext current = this; while (current != null) { ITextReplacer lastReplacer = current.getReplacer(); if (lastReplacer != null) { if (nextReplacerIsChild) { Preconditions.checkArgument(lastReplacer.getRegion().contains(replacer.getRegion())); } else { Preconditions .checkArgument(lastReplacer.getRegion().getEndOffset() <= replacer.getRegion().getOffset()); } break; } current = current.getPreviousContext(); } return new TextReplacerContext(document, this, indentation, replacer); }
protected WhitespaceReplacer findWhitespaceThatSeparatesSemanticRegions(List<ITextReplacer> replacers) { boolean hasSeenWrap = false; for (ITextReplacer replacer : replacers) { if (replacer instanceof WhitespaceReplacer) { WhitespaceReplacer whitespaceReplacer = (WhitespaceReplacer) replacer; hasSeenWrap |= whitespaceReplacer.getRegion().isMultiline(); if (hasSeenWrap) return whitespaceReplacer; } } return (WhitespaceReplacer) replacers.get(replacers.size() - 1); }
@Override public ITextReplacer merge(List<? extends ITextReplacer> conflicting) { ITextReplacer result = mergeHiddenRegionReplacers(conflicting); if (result == null) result = mergeCompositeReplacers(conflicting); return result; }
protected TextSegmentSet<ITextReplacer> createTextReplacerSet() { return new ArrayListTextSegmentSet<ITextReplacer>(ITextReplacer.GET_REGION, new Function<ITextReplacer, String>() { @Override public String apply(ITextReplacer input) { if (input instanceof HiddenRegionReplacer) return new HiddenRegionFormattingToString() .apply(((HiddenRegionReplacer) input).getFormatting()); return input.getClass().getSimpleName(); } }, getRequest().isEnableDebugTracing()); }
@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; }
protected TextReplacerContext(IFormattableDocument document, ITextReplacerContext previous, int indentation, ITextReplacer replacer) { super(); this.document = document; this.indentation = indentation; this.previous = previous; this.replacer = replacer; this.replacements = createTextReplacementsSet(); }
protected ITextSegment getRegion(int index) { ITextReplacerContext current = this; while (current != null) { ITextReplacer replacer2 = current.getReplacer(); if (replacer2 != null) { if (index == 0) { return replacer2.getRegion(); } else index--; } current = current.getPreviousContext(); } return null; }
public FilteredSubDocument(ITextSegment region, IFormattableDocument parent, Predicate<? super ITextReplacer> filter) { super(region, parent); this.filter = filter; }
@Override public void addReplacer(ITextReplacer replacer) { if (filter.apply(replacer)) super.addReplacer(replacer); }
@Override public void addReplacer(ITextReplacer replacer) { replacers.add(replacer); }
@Override public void addReplacer(ITextReplacer replacer) { validate(replacer); super.addReplacer(replacer); }
protected void validate(ITextReplacer replacer) throws FormattingNotApplicableException { if (replacer instanceof HiddenRegionReplacer) validate((HiddenRegionReplacer) replacer); }
protected TextSegmentSet<ITextReplacer> getReplacers() { if (replacers == null) replacers = createTextReplacerSet(); return replacers; }
protected ITextReplacerContext createReplacements(ITextReplacerContext previous) { Integer maxLineWidth = getRequest().getPreferences().getPreference(FormatterPreferenceKeys.maxLineWidth); ITextReplacerContext context = previous.withDocument(this); ITextReplacerContext wrappable = null; Set<ITextReplacer> wrapped = Sets.newHashSet(); Iterator<ITextReplacer> replacers = getReplacers().iterator(); while (replacers.hasNext()) { ITextReplacer replacer = replacers.next(); context = context.withReplacer(replacer); if (wrappable != null && context.isWrapSincePrevious()) { wrappable = null; } if (wrappable != null && needsAutowrap(wrappable, context, maxLineWidth)) { // TODO: raise report if replacer claims it can do autowrap but // then doesn't while (context != wrappable) { context = context.getPreviousContext(); } replacer = context.getReplacer(); replacers = getReplacers().iteratorAfter(replacer); context.setAutowrap(true); wrappable = null; } ITextReplacerContext nextContext = replacer.createReplacements(context); if (wrappable != null && context.isWrapInRegion()) { wrappable = null; } else { Integer canAutowrap = context.canAutowrap(); if (canAutowrap != null && canAutowrap >= 0 && !context.isAutowrap() && !wrapped.contains(replacer)) { boolean can = true; if (wrappable != null) { int lastEndOffset = wrappable.canAutowrap() + wrappable.getReplacer().getRegion().getEndOffset(); int thisEndOffset = canAutowrap + context.getReplacer().getRegion().getEndOffset(); can = lastEndOffset < thisEndOffset; } if (can) { wrappable = context; wrapped.add(replacer); } } } context = nextContext; } return context.withDocument(previous.getDocument()); }
@Override public IFormattableSubDocument withReplacerFilter(Predicate<? super ITextReplacer> filter) { return new FilteredSubDocument(getRegion(), this, filter); }
@Override public ITextReplacer getReplacer() { return replacer; }
public void addReplacer(ITextReplacer replacer);