Bug 93631 - End lines are missing uring the Rewrite
Summary: End lines are missing uring the Rewrite
Status: RESOLVED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2 M6   Edit
Assignee: Martin Aeschlimann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-05-04 06:59 EDT by julien CLA
Modified: 2006-03-27 06:51 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description julien CLA 2005-05-04 06:59:30 EDT
In a Javadoc, we I modify the Text content in a javadoc. The end-lines are 
missing in the output. For example, I have the orginale code:

/**
 * @author eric
 * 
 * Line1
 * Line2
 * @param arg1
 */

After the modification or clone, when I rewite the code via
	Document document = new Document(targetSource);
	TextEdit mods = compilationUnit.rewrite(document, null);
	mods.apply(document);

I get the following results: 
/**
 * @author  eric Line1 Line2
 * @param arg1
 */

There are two issue:
   1. one more space before the "eric"(rf: 88126)
   2. end-lines are missing

But in debug when I select the TagElement, it is displayed correctly. What I 
found is the following codes in ASTRewriteFlattener:
	public boolean visit(TagElement node) {
		Object tagName= getAttribute(node, 
TagElement.TAG_NAME_PROPERTY);
		if (tagName != null) {
			this.result.append((String) tagName);
		}
		List list= getChildList(node, TagElement.FRAGMENTS_PROPERTY);
		for (int i= 0; i < list.size(); i++) {
			if (i > 0 || tagName != null) {
				this.result.append(' ');
			}
			ASTNode curr= (ASTNode) list.get(i);
			if (curr instanceof TagElement) {
				this.result.append('{');
				curr.accept(this);
				this.result.append('}');
			} else {
				curr.accept(this);
			}
		}
		return false;
	}

I guess it should be:

	public boolean visit(TagElement node) {
		Object tagName= getAttribute(node, 
TagElement.TAG_NAME_PROPERTY);
		if (tagName != null) {
			this.result.append((String) tagName);
		}
		List list= getChildList(node, TagElement.FRAGMENTS_PROPERTY);
		for (int i= 0; i < list.size(); i++) {
                        if (i == 0)
                        {
			    if (tagName != null)
			         this.result.append(' ');
                        }
                        else
			    this.result.append("\n * ")

			ASTNode curr= (ASTNode) list.get(i);
			if (curr instanceof TagElement) {
				this.result.append('{');
				curr.accept(this);
				this.result.append('}');
			} else {
				curr.accept(this);
			}
		}
		return false;
	}
Comment 1 Martin Aeschlimann CLA 2005-05-04 09:18:23 EDT
It seems that cloning a node looses all format information. The cloned node is
similar to a node that you constructed yourself (with ast.newXY).

The modifying rewrite approach doesn't give you the full possibilities, so I
suggest you to use the describing approach -> ASTRewrite

use createCopyTarget to copy a node (including all the modifications inside) or
createMoveTarget to move.

I'll discuss with David if cloning should do a copy

Comment 2 julien CLA 2005-05-04 10:12:05 EDT
It seems we cannot use the ASTRewrite to copy a node into another 
CompilationUnit since the AST are different. I get an exception during the add:
Caused by: java.lang.IllegalArgumentException
	at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1852)
	at org.eclipse.jdt.core.dom.ASTNode.preReplaceChild(ASTNode.java:1918)

the exception is raised in the following codes:
	static void checkNewChild(ASTNode node, ASTNode newChild,
			boolean cycleCheck, Class nodeType) {
		if (newChild.ast != node.ast) {
			// new child is from a different AST
			throw new IllegalArgumentException();
		}		
Comment 3 Martin Aeschlimann CLA 2005-05-04 10:21:46 EDT
That's correct, you can't copy to a different AST.
In that case it's best to copy it in a string placeholder
(ASTRewrite#createStringPlaceholder).
That's the only way to not loose any special formatting.

If you don't care about that, thing should work as soon as I fix the AST
rewriter to correctly format Javadoc comments.
Comment 4 julien CLA 2005-05-04 11:32:50 EDT
    ASTRewrite rewrite = ASTRewrite.create(target.getAST());
    value = (Javadoc) rewrite.createStringPlaceholder(value.toString(), 
ASTNode.JAVADOC);
    target.setJavadoc(value);

Finally, i get an empty Javadoc:
/**
 */
Comment 5 Martin Aeschlimann CLA 2005-05-04 12:37:17 EDT
value.toString() doesn't work here, you have to get it from the source of the
compilation unit (The AST doesn't know its source anymore).

I know it's nasty, but after getting the comment from the source you have to
remove the indents of the Javadoc comment as the string placeholder requires an
unindented string.


Comment 6 julien CLA 2005-05-04 12:48:08 EDT
In debug, I check the toString() content. I get a correct value. I have the 
same problem with a constant string:
      String content = "/** \n * test\n */"; 
      value = (Javadoc) rewrite.createStringPlaceholder(content, 
ASTNode.JAVADOC);
Comment 7 Martin Aeschlimann CLA 2005-05-24 11:13:13 EDT
what's the state here?

as said, if you want to copy nodes to a different AST, it has to be done on a
string basis.

Please reopen if I missed a problem.
Comment 8 Philipe Mulet CLA 2006-03-27 06:49:33 EST
reopening to close properly as no further action is planned there.
Comment 9 Philipe Mulet CLA 2006-03-27 06:51:06 EST
no further action planned, assuming it is fine as reporter didn't comment in 10 months.