Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(DD) MergeBlocksWithJumpRefactoring - MalformedTreeException #291

Open
cal101 opened this issue Aug 3, 2017 · 1 comment
Open

(DD) MergeBlocksWithJumpRefactoring - MalformedTreeException #291

cal101 opened this issue Aug 3, 2017 · 1 comment

Comments

@cal101
Copy link
Collaborator

cal101 commented Aug 3, 2017

MalformedTreeException, Range of child edit lies outside of parent edit

With help of improved delta debugging #286 can be further analyzed.

The minimal available test case is 97 lines and depends on other eclipse classes.

It's a reduced form of
http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/TextEditsBuilder.java

package org.eclipse.jdt.internal.formatter;
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_LINE;
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_BLOCK;
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_JAVADOC;
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameNotAToken;
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameStringLiteral;
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameWHITESPACE;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
import org.eclipse.jdt.internal.formatter.Token.WrapMode;
import org.eclipse.jdt.internal.formatter.Token.WrapPolicy;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
public class TextEditsBuilder extends TokenTraverser {
	private final String source;
	private TokenManager tm;
	private final DefaultCodeFormatterOptions options;
	private final StringBuilder buffer;
	private final List<TextEdit> edits = new ArrayList<TextEdit>();
	private final List<IRegion> regions;
	private final TextEditsBuilder parent;
	public TextEditsBuilder(String source, List<IRegion> regions, TokenManager tokenManager,
			DefaultCodeFormatterOptions options) {
		this.source = source;
		this.options = options;
		this.regions = adaptRegions(regions);
		this.parent = null;
		this.buffer = new StringBuilder();
	}
	private List<IRegion> adaptRegions(List<IRegion> givenRegions) {
		ArrayList<IRegion> result = new ArrayList<IRegion>();
		for (IRegion region : givenRegions) {
			int start = region.getOffset();
			int end = start + region.getLength() - 1;
			int sourceStart = this.tm.get(0).originalStart;
			if (end > start && end > sourceStart) {
				Token token = this.tm.get(this.tm.findIndex(end, -1, false));
				if ((token.tokenType == TokenNameCOMMENT_BLOCK || token.tokenType == TokenNameCOMMENT_JAVADOC)
						&& end < token.originalEnd) {
				}
			}
			IRegion adapted = new Region(start, end - start + 1);
		}
		return result;
	}
	protected boolean token(Token token, int index) {
		if (token.tokenType == TokenNameStringLiteral)
		if (getNext() == null) {
		}
		return true;
	}
	private void bufferLineSeparator(Token token, boolean emptyLine) {
		if (token != null && token.tokenType == TokenNameNotAToken)
			return; // this is an unformatted block comment, don't force asterisk
		if (getNext() == null && !emptyLine)
			return; // this is the last token of block comment, asterisk is included
		int spaces = 0;
		if (this.options.use_tabs_only_for_leading_indentations
				&& this.options.tab_char != DefaultCodeFormatterOptions.SPACE) {
			WrapPolicy wrapPolicy = token.getWrapPolicy();
			while (wrapPolicy != null) {
				Token parentLineStart = this.tm.get(this.tm.findFirstTokenInLine(wrapPolicy.wrapParentIndex));
				if (wrapPolicy.wrapMode != WrapMode.BLOCK_INDENT)
					spaces += token.getIndent() - parentLineStart.getIndent();
			}
		}
	}
	public static void appendIndentationString(StringBuilder target, int tabChar, int tabSize, int indent,
			int additionalSpaces) {
		int spacesCount = additionalSpaces;
		int tabsCount = 0;
		char[] indentChars = new char[tabsCount + spacesCount];
		Arrays.fill(indentChars, 0, tabsCount, '\t');
	}
	private ReplaceEdit getReplaceEdit(int editStart, int editEnd, String text, IRegion region) {
		return new ReplaceEdit(editStart, editEnd - editStart, text);
	}
	private boolean isOnlyWhitespace(String text) {
		for (int i = 0; i < text.length(); i++)
			if (!ScannerHelper.isWhitespace(text.charAt(i)))
				return false;
		return true;
	}
	private void handleSingleLineComment(Token lineComment, int index) {
		List<Token> structure = lineComment.getInternalStructure();
		if (structure.get(0).tokenType == TokenNameWHITESPACE) {
		}
	}
	public void processComment(Token commentToken) {
		if (commentToken.tokenType == TokenNameCOMMENT_LINE) {
		}
	}
}

that result in


org.autorefactor.util.IllegalStateException: TextEditsBuilder.java:1:1:Illegal changes have been created by org.autorefactor.refactoring.rules.UseDiamondOperatorRefactoring, org.autorefactor.refactoring.rules.AddBracketsToControlStatementRefactoring, org.autorefactor.refactoring.rules.RemoveFieldsDefaultValuesRefactoring, org.autorefactor.refactoring.rules.RemoveEmptyLinesRefactoring, org.autorefactor.refactoring.rules.CommentsRefactoring, org.autorefactor.refactoring.rules.MergeBlocksWithJumpRefactoring for file TextEditsBuilder.java. (Range of child edit lies outside of parent edit) Parent text edit: {DeleteEdit} [6867,70]
  {CopySourceEdit} [6871,54], child text edit: {CopySourceEdit} [6930,69]
@cal101 cal101 changed the title (DD) MergeBlocksWithJumpRefactoring - MalformedTreeException, Range of child edit lies outside of parent edit (DD) MergeBlocksWithJumpRefactoring - MalformedTreeException Aug 3, 2017
@Fabrice-TIERCELIN
Copy link
Collaborator

This is due to the inline comment at the end of the return. JDT is lost for the location of the end of the statement. This is closely related to #181.

We will look for a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants