@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; } }
@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(); }
protected boolean needsAutowrap(ITextReplacerContext wrappable, ITextReplacerContext context, int maxLineWidth) { if (context.getLeadingCharsInLineCount() > maxLineWidth) return true; int offset = wrappable.getReplacer().getRegion().getOffset(); int length = context.getReplacer().getRegion().getEndOffset() - offset; if (length > wrappable.canAutowrap()) return false; // for (ITextReplacement rep : context.getReplacementsUntil(wrappable)) // if (rep.getReplacementText().contains("\n")) // return true; // TextSegment region = new TextSegment(getTextRegionAccess(), offset, // length); // String text = TextReplacements.apply(region, ); // if (text.contains("\n")) // return true; return false; }
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; }
@Override public List<ITextReplacement> getReplacementsUntil(ITextReplacerContext first) { ITextReplacerContext current = this; List<Iterable<ITextReplacement>> reversedReplacements = Lists.newArrayList(); while (current != null) { Iterable<ITextReplacement> localReplacements = current.getLocalReplacements(); if (!Iterables.isEmpty(localReplacements)) reversedReplacements.add(localReplacements); if (current == first) break; current = current.getPreviousContext(); } Collections.reverse(reversedReplacements); List<ITextReplacement> flattenedReplacements = new TextReplacementList<ITextReplacement>(); for (Iterable<ITextReplacement> chunk : reversedReplacements) Iterables.addAll(flattenedReplacements, chunk); return flattenedReplacements; }
@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); }
@Override protected int computeNewLineCount(ITextReplacerContext context) { // In case no information is configured, we do not want to swallow any lines (super give 0 here): IHiddenRegionFormatting formatting = getFormatting(); if (formatting.getNewLineDefault() == null && formatting.getNewLineMin() == null && formatting.getNewLineMax() == null) { // return the actual newlines: return getRegion().getLineCount() - 1; } // all other cases are handled as always: return super.computeNewLineCount(context); }
@Override public ITextReplacerContext createReplacements(final ITextReplacerContext it) { final String t = this.region.getText(); final int offset = this.region.getOffset(); for (int i = 0; (i < t.length()); i++) { boolean _isWhitespace = Character.isWhitespace(t.charAt(i)); if (_isWhitespace) { it.addReplacement(this.region.getTextRegionAccess().getRewriter().createReplacement((offset + i), 1, "")); } } return it; }
@Override public ITextReplacerContext createReplacements(ITextReplacerContext context) { ITextSegment firstSpace = getFirstSpace(); if (firstSpace != null) { if (hasEmptyBody()) context.addReplacement(firstSpace.replaceWith("")); else context.addReplacement(firstSpace.replaceWith(" ")); } return context; }
@Override public ITextReplacerContext createReplacements(ITextReplacerContext context) { ITextReplacerContext last = super.createReplacements(context); List<ITextReplacement> replacements = last.getReplacementsUntil(context); String string = applyTextReplacements(replacements); if (string.contains("\n")) throw new FormattingNotApplicableException(); int leadingCharCount = context.getLeadingCharsInLineCount(); int formattedLength = string.length(); int lineLength = leadingCharCount + formattedLength; if (lineLength > maxLineWidth) throw new FormattingNotApplicableException(); return last; }
@Override public List<ITextReplacement> renderToTextReplacements() { ITextReplacerContext first = getFormatter().createTextReplacerContext(this); ITextReplacerContext last = createReplacements(first); List<ITextReplacement> replacements = last.getReplacementsUntil(first); return replacements; }
protected int computeNewIndentation(ITextReplacerContext context) { Integer indentationIncrease = formatting.getIndentationIncrease(); Integer indentationDecrease = formatting.getIndentationDecrease(); int indenation = context.getIndentation(); if (indentationIncrease != null) indenation += indentationIncrease; if (indentationDecrease != null) indenation -= indentationDecrease; if (indenation >= 0) return indenation; return 0; // TODO: handle indentation underflow }
@Override public ITextReplacerContext createReplacements(ITextReplacerContext context) { if (formatting.getAutowrap() != null && formatting.getAutowrap() >= 0) context.setCanAutowrap(formatting.getAutowrap()); String space = formatting.getSpace(); int trailingNewLinesOfPreviousRegion = trailingNewLinesOfPreviousRegion(); int computedNewLineCount = computeNewLineCount(context); int newLineCount = Math.max(computedNewLineCount - trailingNewLinesOfPreviousRegion, 0); if (newLineCount == 0 && context.isAutowrap()) { IAutowrapFormatter onAutowrap = formatting.getOnAutowrap(); if (onAutowrap != null) { onAutowrap.format(region, formatting, context.getDocument()); } newLineCount = 1; } int indentationCount = computeNewIndentation(context); if (newLineCount == 0 && trailingNewLinesOfPreviousRegion == 0) { if (space != null) context.addReplacement(region.replaceWith(space)); } else { boolean noIndentation = formatting.getNoIndentation() == Boolean.TRUE; String newLines = context.getNewLinesString(newLineCount); String indentation = noIndentation ? "" : context.getIndentationString(indentationCount); context.addReplacement(region.replaceWith(newLines + indentation)); } return context.withIndentation(indentationCount); }
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(); }
@Override public ITextReplacerContext withDocument(IFormattableDocument document) { TextReplacerContext context = new TextReplacerContext(document, this, indentation, null); if (this.nextReplacerIsChild) context.setNextReplacerIsChild(); return context; }
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; }
@Override public ITextReplacerContext createReplacements(ITextReplacerContext context) { return context; }
@Override public ITextReplacerContext createReplacements(ITextReplacerContext context) { return super.createReplacements(context); }
@Override public ITextReplacerContext createReplacements(ITextReplacerContext previous) { ITextReplacerContext context = ((TextReplacerContext) previous).withDocument(this); context.setNextReplacerIsChild(); return ((TextReplacerContext) super.createReplacements(context)).withDocument(previous.getDocument()); }
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 ITextReplacerContext getPreviousContext() { return previous; }
@Override public ITextReplacerContext withIndentation(int indentation) { return new TextReplacerContext(document, this, indentation, null); }