Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jdt-core-dev] Strange formatting when using JDT core DOM infrastructure

Thanks Olivier!

Changing my code to:

        declaration.modifiers().add(0, annotation);

indeed moved the annotation before the public declaration.

Now for your second advise (to format the code after I get it from the document). I tried to follow this great forum discussion:

http://www.eclipse.org/forums/index.php/m/829341/

However, if I create my TextEdit this way:

        CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
        TextEdit edit = formatter.format(0, document.get(), 0, document.get().length(), 0, null);
        edit.apply(document);

Then I lose all the modifications that I did in the lines before and get the initial class (now formatted):

public class JavaClass {
}

I tried to call unit.rewrite(document, null); before formatter.format, but without luck.

Could someone tell me how do you format my document?

Thanks again!
Ivan

P.S. I saw also that there is this package org.eclipse.jface.text.formatter, which has some ContentFormatter classes. While I am using org.eclipse.jdt.core.formatter.CodeFormatter. What is the correct way?

On Tue, Mar 27, 2012 at 4:37 AM, Olivier Thomann <Olivier_Thomann@xxxxxxxxxx> wrote:
Hi,

You can add the annotation in the first position inside the modifiers. You can format the code at the end once you get the source from the document.

Olivier


From: "Ivan St. Ivanov" <ivan.st.ivanov@xxxxxxxxx>
To: jdt-core-dev@xxxxxxxxxxx
Date: 2012-03-26 18:27
Subject: [jdt-core-dev] Strange formatting when using JDT core DOM        infrastructure
Sent by: jdt-core-dev-bounces@xxxxxxxxxxx





Hi folks!

I'm working on the JBoss Forge tool (https://docs.jboss.org/author/display/FORGE/Home). For some of its features it uses JDT core tooling to generate or modify Java classes. For example, it may create JPA entities. However, the final format is quite weird:

package com.example.test;

import javax.persistence.Entity;public @Entity class Employee {}

I would expect something like that:

package com.example.test;

import javax.persistence.Entity;

@Entity
public class Employee {}

You noticed how in the generated input the import, the annotation and the class declaration are on a single line? And @Entity is placed between the public and class words?

I have attached here a sample class, which demonstrates how we use the JDT parser API. Could you please tell me what I am doing wrong? And is it possible to have more beautiful formatting?

Thanks and regards,
Ivan[attachment "JdtFormatterTest.java" deleted by Olivier Thomann/Ottawa/IBM] _______________________________________________
jdt-core-dev mailing list
jdt-core-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jdt-core-dev



_______________________________________________
jdt-core-dev mailing list
jdt-core-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jdt-core-dev



Back to the top