Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » formatting a ICompilationUnit in a plugin.
formatting a ICompilationUnit in a plugin. [message #162552] Thu, 03 June 2004 15:33 Go to next message
Eclipse UserFriend
Originally posted by: gbforum.laposte.net

Hi,

I want inside my plugin to automaticaly apply code formating on a
ICompilationUnit. Some thing like :

ICompilationUnit cu;
...

CodeFormatter cformatter = new DefaultCodeFormatter();
TextEdit te = cformatter.format(CodeFormatter.K_COMPILATION_UNIT,
cu.getSource(), cu.getSourceRange().getOffset(),
cu.getSourceRange().getLength(), /* indentation level */0,
BeautifyEntries.EOL );

te.apply(cu); // <- this is what does not exist . how do I re insuflate
the modeification into the ICompilationUnit.

Thanks in advance.
Guillaume
Re: formatting a ICompilationUnit in a plugin. [message #162569 is a reply to message #162552] Thu, 03 June 2004 16:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNO.ca.ibm.comSPAM

Le Thu, 3 Jun 2004 15:33:42 +0000 (UTC), gbforum@laposte.net
(Guillaume) a écrit :
>CodeFormatter cformatter = new DefaultCodeFormatter();
This is not good. This is not API.
The right way to create a code formatter is:
CodeFormatter cFormatter =
org.eclipse.jdt.core.ToolFactory.createCodeFormatter(JavaCor e.getOptions());
// you can set the options you want there

DefaultCodeFormatter is inside an package with "internal" in its name.
You should NEVER call directly such classes. They might be removed,
renamed or changed without any notice since they are not API.

>TextEdit te = cformatter.format(CodeFormatter.K_COMPILATION_UNIT,
>cu.getSource(), cu.getSourceRange().getOffset(),
>cu.getSourceRange().getLength(), /* indentation level */0,
>BeautifyEntries.EOL );

>te.apply(cu); // <- this is what does not exist . how do I re insuflate
>the modeification into the ICompilationUnit.
You need to use a org.eclipse.jface.text.Document. Then you use:
te.apply(myDocument);

Hope this help,
--
Olivier
Re: formatting a ICompilationUnit in a plugin. [message #162684 is a reply to message #162569] Fri, 04 June 2004 06:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gbforum.laposte.net

> The right way to create a code formatter is:
> CodeFormatter cFormatter =
> org.eclipse.jdt.core.ToolFactory.createCodeFormatter(JavaCor e.getOptions());
> // you can set the options you want there
> DefaultCodeFormatter is inside an package with "internal" in its name.
> You should NEVER call directly such classes. They might be removed,
> renamed or changed without any notice since they are not API.
oups. ok Thanks for that correction.


> You need to use a org.eclipse.jface.text.Document. Then you use:
> te.apply(myDocument);

I know that I need to use a IDocument my question is how I made the link
between my ICompilationUnit and my IDocument ?
in fact my ICompilationUnit is a WorkingCopy of a compilation unit. I want
To format the source in it, and then commit the WorkingCopy.
I have the impression that I can copy the content of my workingCopy into a
IDocument but that will be only a copy of String. not a reference that
makes any modification to the IDocument be reported to the
ICompilationUnit. Am I wrong ?

Thanks for the answer,
Guillaume
Re: formatting a ICompilationUnit in a plugin. [message #162752 is a reply to message #162684] Fri, 04 June 2004 13:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNO.ca.ibm.comSPAM

Le Fri, 4 Jun 2004 06:37:33 +0000 (UTC), gbforum@laposte.net
(Guillaume) a écrit :
>I know that I need to use a IDocument my question is how I made the link
>between my ICompilationUnit and my IDocument ?
>in fact my ICompilationUnit is a WorkingCopy of a compilation unit. I want
>To format the source in it, and then commit the WorkingCopy.
>I have the impression that I can copy the content of my workingCopy into a
>IDocument but that will be only a copy of String. not a reference that
>makes any modification to the IDocument be reported to the
>ICompilationUnit. Am I wrong ?
Not sure. If you create the document with the contents of your working
copy and after you applied the edits, you copy back the contents of
the document into the working copy and then you commit it should work
fine.
--
Olivier
Re: formatting a ICompilationUnit in a plugin. [message #162798 is a reply to message #162752] Fri, 04 June 2004 14:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gbforum.laposte.net

> Not sure. If you create the document with the contents of your working
> copy and after you applied the edits, you copy back the contents of
> the document into the working copy and then you commit it should work
> fine.

It should that the magique of copying back the contents of the document
into the compilation unit that I don't know how to do.
Re: formatting a ICompilationUnit in a plugin. [message #162806 is a reply to message #162798] Fri, 04 June 2004 15:09 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNO.ca.ibm.comSPAM

Le Fri, 4 Jun 2004 14:46:29 +0000 (UTC), gbforum@laposte.net
(Guillaume) a écrit :
>It should that the magique of copying back the contents of the document
>into the compilation unit that I don't know how to do.
Did you try this?
compilationUnit.getBuffer().setContents(myDocument.get());
--
Olivier
Re: formatting a ICompilationUnit in a plugin. [message #162991 is a reply to message #162806] Mon, 07 June 2004 07:36 Go to previous message
Eclipse UserFriend
Originally posted by: gbforum.laposte.net

> Did you try this?
> compilationUnit.getBuffer().setContents(myDocument.get());

Thanks so mutch that's what I was missing.
Guillaume
Previous Topic:using existing CVS project
Next Topic:Fwd: Dumb: How to change the font size in the javadoc view?
Goto Forum:
  


Current Time: Thu Apr 18 22:57:43 GMT 2024

Powered by FUDForum. Page generated in 0.02149 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top