org.eclipse.xtext.formatting2.ITextReplacerContext Java Examples

The following examples show how to use org.eclipse.xtext.formatting2.ITextReplacerContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: HiddenRegionReplacer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example #3
Source File: ConditionalReplacer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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();
}
 
Example #4
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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;
}
 
Example #5
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: WhitespaceReplacer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #7
Source File: SARLMultilineCommentReplacer.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
	final IComment comment = getComment();
	if (context != null && comment != null) {
		this.formatter.formatMultilineComment(this.bugfix.fix(context, comment), comment);
	}
	return context;
}
 
Example #8
Source File: SARLSinglelineCommentReplacer.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
	final IComment comment = getComment();
	if (context != null && comment != null) {
		this.formatter.formatSinglelineComment(this.bugfix.fix(context, comment), comment);
	}
	return context;
}
 
Example #9
Source File: BugSinglelineCommentIndentation.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Fixing the bug.
 *
 * @param context the replacement context.
 * @param comment the comment for which the fix must be applied.
 * @return the new context.
 */
@SuppressWarnings("static-method")
public ITextReplacerContext fix(final ITextReplacerContext context, IComment comment) {
	final IHiddenRegion hiddenRegion = comment.getHiddenRegion();
	if (detectBugSituation(hiddenRegion)
			&& fixBug(hiddenRegion)) {
		// Indentation of the first comment line
		final ITextRegionAccess access = comment.getTextRegionAccess();
		final ITextSegment target = access.regionForOffset(comment.getOffset(), 0);
		context.addReplacement(target.replaceWith(context.getIndentationString(1)));
	}
	return context;
}
 
Example #10
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example #11
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Fixing the bug.
 *
 * @param context the replacement context.
 * @param comment the comment for which the fix must be applied.
 * @return the new context.
 */
@SuppressWarnings("static-method")
public ITextReplacerContext fix(final ITextReplacerContext context, IComment comment) {
	final IHiddenRegion hiddenRegion = comment.getHiddenRegion();
	if (detectBugSituation(hiddenRegion) && fixBug(hiddenRegion)) {
		// Indentation of the first comment line
		final ITextRegionAccess access = comment.getTextRegionAccess();
		final ITextSegment target = access.regionForOffset(comment.getOffset(), 0);
		context.addReplacement(target.replaceWith(context.getIndentationString(1)));
		// Indentation of the comment's lines
		return new FixedReplacementContext(context);
	}
	return context;
}
 
Example #12
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITextReplacerContext withDocument(IFormattableDocument document) {
	TextReplacerContext context = new TextReplacerContext(document, this, indentation, null);
	if (this.nextReplacerIsChild)
		context.setNextReplacerIsChild();
	return context;
}
 
Example #13
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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();
}
 
Example #14
Source File: WhitespaceReplacer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #15
Source File: WhitespaceReplacer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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
}
 
Example #16
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<ITextReplacement> renderToTextReplacements() {
	ITextReplacerContext first = getFormatter().createTextReplacerContext(this);
	ITextReplacerContext last = createReplacements(first);
	List<ITextReplacement> replacements = last.getReplacementsUntil(first);
	return replacements;
}
 
Example #17
Source File: MaxLineWidthDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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;
}
 
Example #18
Source File: SinglelineDocCommentReplacer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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;
}
 
Example #19
Source File: ArrayBracketsFormattingReplacer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext it) {
	String t = region.getText();
	int offset = region.getOffset();
	for (int i = 0; i < t.length(); i++) {
		if (Character.isWhitespace(t.charAt(i))) {
			it.addReplacement(region.getTextRegionAccess().getRewriter().createReplacement(offset + i, 1, ""));
		}
	}
	return it;
}
 
Example #20
Source File: N4WhitespaceReplacer.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public ITextReplacerContext withIndentation(int indentation) {
	return this.context.withIndentation(indentation);
}
 
Example #22
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public ITextReplacerContext withReplacer(ITextReplacer replacer) {
	return this.context.withReplacer(replacer);
}
 
Example #23
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public ITextReplacerContext withDocument(IFormattableDocument document) {
	return this.context.withDocument(document);
}
 
Example #24
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public List<ITextReplacement> getReplacementsUntil(ITextReplacerContext first) {
	return this.context.getReplacementsUntil(first);
}
 
Example #25
Source File: OffMultilineCommentReplacer.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
	return context;
}
 
Example #26
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public ITextReplacerContext getPreviousContext() {
	return this.context.getPreviousContext();
}
 
Example #27
Source File: DocumentationFormatter.java    From sarl with Apache License 2.0 4 votes vote down vote up
public RegionAccessor(ITextReplacerContext context, IComment comment) {
	super(comment.getText(), null);
	this.context = context;
	this.comment = comment;
	this.access = comment.getTextRegionAccess();
}
 
Example #28
Source File: DocumentationFormatter.java    From sarl with Apache License 2.0 4 votes vote down vote up
public void formatSinglelineComment(ITextReplacerContext context, IComment comment) {
	formatSinglelineComment(context.getIndentationString(), new RegionAccessor(context, comment));
}
 
Example #29
Source File: DocumentationFormatter.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Pure
public void formatMultilineComment(ITextReplacerContext context, IComment comment) {
	formatMultlineComment(context.getIndentationString(), context.getNewLinesString(1), new RegionAccessor(context, comment));
}
 
Example #30
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ITextReplacerContext withIndentation(int indentation) {
	return new TextReplacerContext(document, this, indentation, null);
}