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

项目: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    文件:TextReplacerMerger.java   
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;
}
项目:xtext-core    文件:TextReplacerMerger.java   
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;
}
项目: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   
@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);
    }
}
项目: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);
}
项目:xtext-core    文件:HiddenRegionReplacer.java   
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);
}
项目:xtext-core    文件:TextReplacerMerger.java   
@Override
public ITextReplacer merge(List<? extends ITextReplacer> conflicting) {
    ITextReplacer result = mergeHiddenRegionReplacers(conflicting);
    if (result == null)
        result = mergeCompositeReplacers(conflicting);
    return result;
}
项目:xtext-core    文件:FormattableDocument.java   
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());
}
项目:xtext-core    文件:FormattableDocument.java   
@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);
}
项目:xtext-core    文件:FormattableDocument.java   
@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;
}
项目: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   
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;
}
项目:xtext-core    文件:FilteredSubDocument.java   
public FilteredSubDocument(ITextSegment region, IFormattableDocument parent, Predicate<? super ITextReplacer> filter) {
    super(region, parent);
    this.filter = filter;
}
项目:xtext-core    文件:FilteredSubDocument.java   
@Override
public void addReplacer(ITextReplacer replacer) {
    if (filter.apply(replacer))
        super.addReplacer(replacer);
}
项目:xtext-core    文件:ConditionalReplacer.java   
@Override
public void addReplacer(ITextReplacer replacer) {
    replacers.add(replacer);
}
项目:xtext-core    文件:MaxLineWidthDocument.java   
@Override
public void addReplacer(ITextReplacer replacer) {
    validate(replacer);
    super.addReplacer(replacer);
}
项目:xtext-core    文件:MaxLineWidthDocument.java   
protected void validate(ITextReplacer replacer) throws FormattingNotApplicableException {
    if (replacer instanceof HiddenRegionReplacer)
        validate((HiddenRegionReplacer) replacer);
}
项目:xtext-core    文件:FormattableDocument.java   
protected TextSegmentSet<ITextReplacer> getReplacers() {
    if (replacers == null)
        replacers = createTextReplacerSet();
    return replacers;
}
项目: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    文件:FormattableDocument.java   
@Override
public IFormattableSubDocument withReplacerFilter(Predicate<? super ITextReplacer> filter) {
    return new FilteredSubDocument(getRegion(), this, filter);
}
项目:xtext-core    文件:TextReplacerContext.java   
@Override
public ITextReplacer getReplacer() {
    return replacer;
}
项目:xtext-core    文件:ICompositeTextReplacer.java   
public void addReplacer(ITextReplacer replacer);