public ITextSegment getRegion() { final SeparatorEntry<T, R> prev = this.getLeadingSeparator(); final SeparatorEntry<T, R> trail = this.getTrailingSeparator(); int _xifexpression = (int) 0; if ((prev != null)) { _xifexpression = prev.getSeparator().getEndOffset(); } else { _xifexpression = this.list.getRoot().getOffset(); } final int offset = _xifexpression; int _xifexpression_1 = (int) 0; if ((trail != null)) { _xifexpression_1 = trail.getSeparator().getOffset(); } else { _xifexpression_1 = this.list.getRoot().getEndOffset(); } final int endOffset = _xifexpression_1; ITextRegionAccess _textRegionAccess = this.list.getRoot().getTextRegionAccess(); return new TextSegment(_textRegionAccess, offset, (endOffset - offset)); }
@Override public String toString() { String _xblockexpression = null; { final ITextSegment reg = this.getRegion(); StringConcatenation _builder = new StringConcatenation(); _builder.append("\""); String _text = reg.getText(); _builder.append(_text); _builder.append("\" at offset="); int _offset = reg.getOffset(); _builder.append(_offset); _builder.append(" lenght="); int _length = reg.getLength(); _builder.append(_length); _builder.append(" ("); String _simpleName = this.object.getClass().getSimpleName(); _builder.append(_simpleName); _builder.append(")"); _xblockexpression = _builder.toString(); } return _xblockexpression; }
@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); } }
public void appendDiffs(TextRegionListToString result, TextRegionAccessToString toStr) { if (this.access == null) { return; } int i = 1; for (ITextSegmentDiff diff : this.access.getRegionDifferences()) { ISequentialRegion current = (ISequentialRegion) diff.getOriginalFirstRegion(); ISequentialRegion last = (ISequentialRegion) diff.getOriginalLastRegion(); List<ITextSegment> regions = Lists.newArrayList(); while (current != null) { regions.add(current); if (current == last) { break; } current = current.getNextSequentialRegion(); } result.add("------------ diff " + i + " ------------", false); toStr.appendRegions(result, regions, new DiffColumn(null), true); i++; } }
@Override public String toString() { List<ITextSegment> list = toTokenAndGapList(); if (list.isEmpty()) return "(empty)"; ITextRegionAccess access = list.get(0).getTextRegionAccess(); DiffColumn diff = new DiffColumn(access); TextRegionListToString result = new TextRegionListToString(); if (!hideColumnExplanation) { if (diff.isDiff()) { result.add("Columns: 1:offset 2:length 3:diff 4:kind 5: text 6:grammarElement", false); } else { result.add("Columns: 1:offset 2:length 3:kind 4: text 5:grammarElement", false); } result.add("Kind: H=IHiddenRegion S=ISemanticRegion B/E=IEObjectRegion", false); result.add("", false); } appendRegions(result, list, diff, false); diff.appendDiffs(result, this); return result.toString(); }
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; }
@Override public String toString() { ITextRegionAccess access = getTextRegionAccess(); ITextSegment frame = getFrame(); if (access == null || frame == null) return "(null)"; ITextRegionRewriter rewriter = access.getRewriter(); StringBuilder builder = new StringBuilder(); List<ITextReplacement> replacements = Lists.newArrayList(); for (int i = 0; i < this.items.size(); i++) { Item item = this.items.get(i); ITextSegment region = item.getRegion(); String regionStr = "offset=" + region.getOffset() + " length=" + region.getLength(); String open = i < BRACKETS_OPEN.length ? BRACKETS_OPEN[i] : "[" + i + "["; String close = i < BRACKETS_CLOSE.length ? BRACKETS_CLOSE[i] : "]" + i + "]"; builder.append(open + close + ": " + item.getTitle() + " at " + regionStr + "\n"); replacements.add(rewriter.createReplacement(region.getOffset(), 0, open)); replacements.add(rewriter.createReplacement(region.getEndOffset(), 0, close)); } String vizualized = rewriter.renderToString(frame, replacements); builder.append(box("document snippet", vizualized)); return builder.toString(); }
@Override public void renderToAppendable(ITextSegment input, Iterable<? extends ITextReplacement> rep, Appendable result) throws IOException { int offset = input.getOffset(); String text = input.getText(); List<ITextReplacement> list = new TextReplacementList<ITextReplacement>(rep); Collections.sort(list); int lastOffset = 0; for (ITextReplacement r : list) { result.append(text.subSequence(lastOffset, r.getOffset() - offset)); result.append(r.getReplacementText()); lastOffset = (r.getOffset() - offset) + r.getLength(); } if (lastOffset <= text.length()) result.append(text.subSequence(lastOffset, text.length())); }
@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); } }
protected String applyTextReplacements(Iterable<ITextReplacement> replacements) { ITextSegment region = getRegion(); String input = region.getText(); ArrayList<ITextReplacement> list = Lists.newArrayList(replacements); Collections.sort(list); int startOffset = region.getOffset(); int lastOffset = 0; StringBuilder result = new StringBuilder(); for (ITextReplacement r : list) { int offset = r.getOffset() - startOffset; result.append(input.subSequence(lastOffset, offset)); result.append(r.getReplacementText()); lastOffset = offset + r.getLength(); } result.append(input.subSequence(lastOffset, input.length())); return result.toString(); }
protected String toStringLocal() { List<String> items = Lists.newArrayList(); if (autowrap) items.add("autowrap"); if (canAutowrap != null && canAutowrap >= 0) items.add("canAutowrap"); if (replacer != null) { ITextSegment region = replacer.getRegion(); items.add(format("replacer=[%d-%d-%s|%s]", region.getOffset(), region.getLength(), replacer.getClass().getSimpleName(), replacer.toString())); } if (replacements != null) for (ITextReplacement r : replacements) { String fmt = "replacement=[%d-%d|%s]"; items.add(format(fmt, r.getOffset(), r.getLength(), r.getReplacementText())); } return Joiner.on("; ").join(items); }
@Override public boolean isWrapInRegion() { ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess(); ITextSegment region = getReplacer().getRegion(); int lastOffset = region.getOffset(); for (ITextReplacement rep : this.getLocalReplacements()) { int endOffset = rep.getOffset(); String between = access.textForOffset(lastOffset, endOffset - lastOffset); if (between.contains("\n") || rep.getReplacementText().contains("\n")) { return true; } lastOffset = rep.getEndOffset(); } String rest = access.textForOffset(lastOffset, region.getEndOffset() - lastOffset); if (rest.contains("\n")) { return true; } return false; }
@Override public String toString() { StringConcatenation _builder = new StringConcatenation(); _builder.append("issues="); String _join = IterableExtensions.join(this.issues, ", "); _builder.append(_join); _builder.newLineIfNotEmpty(); _builder.append("set="); final Function1<ITextSegment, String> _function = (ITextSegment it) -> { StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("["); int _offset = it.getOffset(); _builder_1.append(_offset); _builder_1.append(","); int _length = it.getLength(); _builder_1.append(_length); _builder_1.append("]"); return _builder_1.toString(); }; String _replace = IterableExtensions.<ITextSegment, String>map(this.set, _function).toString().replace("\n", ", "); _builder.append(_replace); _builder.newLineIfNotEmpty(); return _builder.toString(); }
protected List<ITextReplacement> mergeReplacements(ITextRegionAccessDiff regions, List<ITextReplacement> rep) { ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter(); List<ITextReplacement> result = Lists.newArrayList(); for (ITextSegmentDiff r : regions.getRegionDifferences()) { int originalStart = r.getOriginalFirstRegion().getOffset(); int originalLength = r.getOriginalLastRegion().getEndOffset() - originalStart; int modifiedStart = r.getModifiedFirstRegion().getOffset(); int modifiedLength = r.getModifiedLastRegion().getEndOffset() - modifiedStart; ITextSegment modifiedRegion = regions.regionForOffset(modifiedStart, modifiedLength); List<ITextReplacement> local = Lists.newArrayList(); for (ITextReplacement re : rep) { if (modifiedRegion.contains(re)) { local.add(re); } } String newText; if (local.isEmpty()) { newText = modifiedRegion.getText(); } else { newText = regions.getRewriter().renderToString(modifiedRegion, local); } ITextReplacement replacement = rewriter.createReplacement(originalStart, originalLength, newText); result.add(replacement); } return result; }
private ITextSegment getRegionForLines(ITextRegionAccess regions, int offset, int lines) { ILineRegion firstLine = regions.regionForLineAtOffset(offset); ILineRegion lastLine = firstLine; for (int i = 1; i < lines; i++) { ILineRegion next = lastLine.getNextLine(); if (next != null) { lastLine = next; } else { break; } } int firstLineOffset = firstLine.getOffset(); ITextSegment region = regions.regionForOffset(firstLineOffset, (lastLine.getEndOffset() - firstLineOffset) + 1); return region; }
public ITextSegment getFrame() { if (this.frame != null) return this.frame; ITextRegionAccess access = getTextRegionAccess(); if (access != null) { ITextSegment impactRegion = TextRegions.merge(this.items); List<ILineRegion> expandToLines = TextRegions.expandToLines(impactRegion, getLeadingLines(), getTrailingLines()); return TextRegions.merge(expandToLines); } return null; }
@Override public String toString() { try { ITextRegionAccess access = getTextRegionAccess(); ITextSegment frame = getFrame(); if (access == null || frame == null) return box(title, "(no changes)"); StringBuilder builder = new StringBuilder(); String vizualized = access.getRewriter().renderToString(frame, items); builder.append(box(title, vizualized)); return builder.toString(); } catch (Exception e) { return box("error", e.getMessage() + "\n" + Throwables.getStackTraceAsString(e)); } }
public String get(ITextSegment seg) { if (this.access == null) { return ""; } String result = this.diffs.get(seg); if (result != null) return result; return empty; }
public ITextSegment getFrame() { if (this.frame != null) return this.frame; ITextRegionAccess access = getTextRegionAccess(); if (access != null) { List<ITextSegment> segments = Lists.newArrayList(); for (Item item : items) segments.add(item.getRegion()); ITextSegment impactRegion = merge(segments); List<ILineRegion> expandToLines = expandToLines(impactRegion, getLeadingLines(), getTrailingLines()); return merge(expandToLines); } return null; }
@Override public String renderToString(ITextSegment input, Iterable<? extends ITextReplacement> replacements) { StringBuilder result = new StringBuilder(); try { renderToAppendable(input, replacements, result); } catch (IOException e) { throw new RuntimeException(e); } return result.toString(); }
@Override public int compareTo(ITextSegment o) { int cmp1 = getOffset() - o.getOffset(); if (cmp1 != 0) return cmp1; int cmp2 = getLength() - o.getLength(); if (cmp2 != 0) return cmp2; return 0; }
@Override public ITextSegment merge(ITextRegion other) { if (contains(other)) return this; if (other instanceof ITextSegment && other.contains(this)) return (ITextSegment) other; int offset = getOffset(); int length = getLength(); int newOffset = Math.min(offset, other.getOffset()); int newLength = Math.max(offset + length, other.getOffset() + other.getLength()) - newOffset; return new TextSegment(getTextRegionAccess(), newOffset, newLength); }
@Override public ITextSegment getIndentation() { String text = getText(); for (int i = 0; i < text.length(); i++) if (!Character.isWhitespace(text.charAt(i))) return new TextSegment(this.getTextRegionAccess(), getOffset(), i); return new TextSegment(this.getTextRegionAccess(), getOffset(), 0); }
protected List<ITextSegment> collectAlternatingSpaceAndComments(boolean includeComments) { List<IHiddenRegionPart> parts = getParts(); if (parts.isEmpty()) { return Collections.<ITextSegment>singletonList(this); } else { ITextSegment lastWhitespace = null; List<ITextSegment> result = Lists.newArrayList(); for (IHiddenRegionPart part : parts) { if (part instanceof IWhitespace) { if (lastWhitespace == null) { result.add(part); lastWhitespace = part; } else { int mergedLength = lastWhitespace.getLength() + part.getLength(); lastWhitespace = new TextSegment(access, lastWhitespace.getOffset(), mergedLength); result.set(result.size() - 1, lastWhitespace); } } else if (part instanceof IComment) { if (lastWhitespace == null) { result.add(new TextSegment(access, part.getOffset(), 0)); } else { lastWhitespace = null; } if (includeComments) { result.add(part); } } } if (lastWhitespace == null) { result.add(new TextSegment(access, getEndOffset(), 0)); } return ImmutableList.copyOf(result); } }
@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; }
protected ITextSegment getFirstSpace() { IComment comment = getComment(); String text = comment.getText(); if (!text.startsWith(prefix)) return null; int start = prefix.length(); for (int i = start; i < text.length(); i++) { char charAt = text.charAt(i); if (!Character.isWhitespace(charAt) || charAt == '\r' || charAt == '\n') return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, i - start); } return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, text.length() - start); }
public TextSegmentSet(Function<? super T, ? extends ITextSegment> region, Function<? super T, String> title, boolean trace) { super(); this.regionGetter = region; this.titleGetter = title; this.traces = trace ? new IdentityHashMap<T, RegionTrace>() : null; }
protected boolean isConflict(ITextSegment region1, ITextSegment region2) { int offset1 = region1.getOffset(); int length1 = region1.getLength(); int offset2 = region2.getOffset(); int length2 = region2.getLength(); if (offset1 < offset2) { return offset1 + length1 > offset2; } else if (offset1 > offset2) { return offset2 + length2 > offset1; } return !(length1 == 0 ^ length2 == 0); }
@Override public void addReplacement(ITextReplacement replacement) { Preconditions.checkNotNull(replacer); ITextSegment replacerRegion = replacer.getRegion(); FormatterRequest request = document.getRequest(); if (!replacerRegion.contains(replacement)) { String frameTitle = replacer.getClass().getSimpleName(); ITextSegment frameRegion = replacer.getRegion(); String replacerTitle = replacement.getReplacementText(); RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, (ITextSegment) replacement)); request.getExceptionHandler().accept(exception); return; } if (!isInRequestedRange(replacement)) { return; } if (!isInUndefinedRegion(replacement)) { if (request.isFormatUndefinedHiddenRegionsOnly()) { return; } if (!request.allowIdentityEdits() && isIdentityEdit(replacement)) { return; } } try { replacements.add(replacement); } catch (ConflictingRegionsException e) { request.getExceptionHandler().accept(e); } }
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 boolean isWrapSincePrevious() { ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess(); ITextSegment region = getRegion(0); ITextSegment previousRegion = getRegion(1); if (previousRegion != null) { int offset = previousRegion.getEndOffset(); String between = access.textForOffset(offset, region.getOffset() - offset); if (between.contains("\n")) { return true; } } return false; }
private String fmt(final ITextSegment region) { StringConcatenation _builder = new StringConcatenation(); _builder.append("["); int _offset = region.getOffset(); _builder.append(_offset); _builder.append(","); int _length = region.getLength(); _builder.append(_length); _builder.append("]"); return _builder.toString(); }
protected Collection<ITextRegion> collectRegionsToFormat(ITextRegionAccessDiff regions) { List<ITextRegion> result = Lists.newArrayList(); for (ITextSegmentDiff diff : regions.getRegionDifferences()) { int offset = diff.getModifiedFirstRegion().getOffset(); int length = diff.getModifiedLastRegion().getEndOffset() - offset; ITextSegment region = regions.regionForOffset(offset, length); result.add(region); } return result; }
/** */ public N4WhitespaceReplacer(ITextSegment whitespace, IHiddenRegionFormatting formatting) { super(whitespace, formatting); }
public SeparatorRegions(final ITextSegment root) { super(); this.root = root; }
@Pure public ITextSegment getRoot() { return this.root; }
public TextRegionsToString add(ITextSegment region, String description) { text.add(region, getReplacement(region, region.getText())); list.add(region, quote(region.getText(), 20) + ": " + description); return this; }