Java 类org.eclipse.xtext.formatting2.ITextReplacerContext 实例源码

项目:xtext-core    文件:HiddenRegionReplacer.java   
@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;
    }
}
项目:xtext-core    文件:ConditionalReplacer.java   
@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();
}
项目:xtext-core    文件:FormattableDocument.java   
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;
}
项目:xtext-core    文件:WhitespaceReplacer.java   
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;
}
项目:xtext-core    文件:TextReplacerContext.java   
@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;
}
项目:xtext-core    文件:TextReplacerContext.java   
@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);
}
项目:n4js    文件:N4WhitespaceReplacer.java   
@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);
}
项目:xtext-extras    文件:ArrayBracketsFormattingReplacer.java   
@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;
}
项目:xtext-core    文件:SinglelineDocCommentReplacer.java   
@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;
}
项目:xtext-core    文件:MaxLineWidthDocument.java   
@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;
}
项目:xtext-core    文件:FormattableDocument.java   
@Override
public List<ITextReplacement> renderToTextReplacements() {
    ITextReplacerContext first = getFormatter().createTextReplacerContext(this);
    ITextReplacerContext last = createReplacements(first);
    List<ITextReplacement> replacements = last.getReplacementsUntil(first);
    return replacements;
}
项目:xtext-core    文件:WhitespaceReplacer.java   
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
}
项目:xtext-core    文件:WhitespaceReplacer.java   
@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);
}
项目:xtext-core    文件:TextReplacerContext.java   
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();
}
项目:xtext-core    文件:TextReplacerContext.java   
@Override
public ITextReplacerContext withDocument(IFormattableDocument document) {
    TextReplacerContext context = new TextReplacerContext(document, this, indentation, null);
    if (this.nextReplacerIsChild)
        context.setNextReplacerIsChild();
    return context;
}
项目:xtext-core    文件:TextReplacerContext.java   
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;
}
项目:n4js    文件:OffMultilineCommentReplacer.java   
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    return context;
}
项目:n4js    文件:N4WhitespaceReplacer.java   
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    return super.createReplacements(context);
}
项目:xtext-core    文件:SubDocument.java   
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext previous) {
    ITextReplacerContext context = ((TextReplacerContext) previous).withDocument(this);
    context.setNextReplacerIsChild();
    return ((TextReplacerContext) super.createReplacements(context)).withDocument(previous.getDocument());
}
项目:xtext-core    文件:FormattableDocument.java   
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());
}
项目:xtext-core    文件:SinglelineCodeCommentReplacer.java   
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    return context;
}
项目:xtext-core    文件:TextReplacerContext.java   
@Override
public ITextReplacerContext getPreviousContext() {
    return previous;
}
项目:xtext-core    文件:TextReplacerContext.java   
@Override
public ITextReplacerContext withIndentation(int indentation) {
    return new TextReplacerContext(document, this, indentation, null);
}