Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Using the CDT code formatter from another plugin(Using the CDT code formatter from another plugin)
Using the CDT code formatter from another plugin [message #807618] Sun, 26 February 2012 19:42 Go to next message
Stephan Korsholm is currently offline Stephan KorsholmFriend
Messages: 3
Registered: February 2012
Junior Member
Hi,

I'm working on a plugin that as one of it's outcomes produces some C code that it writes to a file on the file system.

I would really like to format this C code using the CDT code formatter (I guess the org.eclipse.cdt.core.formatter.CodeFormatter class would be relevant here).

Is it possible to set up and call the CDT code formatter from some Java code in another plugin? Any input on how I might go about this is greatly appreciated.

Kind regards,

- Stephan
Re: Using the CDT code formatter from another plugin [message #807679 is a reply to message #807618] Sun, 26 February 2012 21:54 Go to previous messageGo to next message
Stephan Korsholm is currently offline Stephan KorsholmFriend
Messages: 3
Registered: February 2012
Junior Member
I found the answer myself Smile It's like finally deciding to go to the doctor, and then you get well....

Here is my current solution,

package icecaptools.compiler;

import org.eclipse.cdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

public class EclipseCCodeFormatter implements IcecapCodeFormatter {

private CodeFormatter codeFormatter;

public EclipseCCodeFormatter()
{
codeFormatter = org.eclipse.cdt.core.ToolFactory.createDefaultCodeFormatter(null);
}

public String format(String source) {

// int kind, String source, int offset, int length, int indentationLevel, String lineSeparator

TextEdit edit = codeFormatter.format(0, source, 0, source.length(), 0, null);

IDocument document= new Document(source);
try {
edit.apply(document);
} catch (MalformedTreeException e) {
;
} catch (BadLocationException e) {
;
}
String formattedSource = document.get();
return formattedSource;
}
}

It is absolutely fantastic what you can do with just a couple of lines of code in the open source world Smile

Thanks to whoever made this stuff available!

- Stephan
Re: Using the CDT code formatter from another plugin [message #826679 is a reply to message #807679] Thu, 22 March 2012 11:23 Go to previous messageGo to next message
Pankaj Missing name is currently offline Pankaj Missing nameFriend
Messages: 4
Registered: December 2011
Junior Member
Hi Stephan,
Thanks for putting up the solution.

I tried the writing similar code as yours to create a CodeFormatter instance using ToolFactory.createDefaultCodeFormatter. code got compiled but gave me runtime error

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/cdt/core/ToolFactory
at FormatterTest.main(FormatterTest.java:19)
Caused by: java.lang.ClassNotFoundException: org.eclipse.cdt.core.ToolFactory
...
...

I tried peeking into the jar files under <path>/eclipse/plugins folder to see which jar file provides createDefaultCodeFormatter for eclipse distribution I am using. Somehow I found that none of jar files contain this method. Could you help me fix this problem.

Thanks,
Pankaj
Re: Using the CDT code formatter from another plugin [message #826982 is a reply to message #826679] Thu, 22 March 2012 18:51 Go to previous messageGo to next message
Stephan Korsholm is currently offline Stephan KorsholmFriend
Messages: 3
Registered: February 2012
Junior Member
To get the example above working I added the following dependencies to the plugin manifest,

org.eclipse.cdt.core
org.eclipse.text

These should be available if you have installed CDT.

- Stephan
Re: Using the CDT code formatter from another plugin [message #829341 is a reply to message #826982] Mon, 26 March 2012 06:56 Go to previous messageGo to next message
Pankaj Missing name is currently offline Pankaj Missing nameFriend
Messages: 4
Registered: December 2011
Junior Member
Thanks for the reply Stephan. I am a C programmer and pretty novice in Java programming. Could you help me how to add the dependencies in the manifest. Yeah I have installed CDT (Eclipse Indigo distribution). Appreciate your suggestion.
Re: Using the CDT code formatter from another plugin [message #830891 is a reply to message #829341] Wed, 28 March 2012 07:26 Go to previous messageGo to next message
Pankaj Missing name is currently offline Pankaj Missing nameFriend
Messages: 4
Registered: December 2011
Junior Member
I figured out the trouble and fixed it. Basically I had to add all the jar files provided by eclipse in the classpath. Very Happy
Re: Using the CDT code formatter from another plugin [message #905858 is a reply to message #826982] Fri, 31 August 2012 08:02 Go to previous message
Brian Joyce is currently offline Brian JoyceFriend
Messages: 1
Registered: July 2009
Junior Member
Thanks Stephan, this was just what I was looking for - so thank you for sharing your solution.

Cheers
Brian
Previous Topic:subdir.mk not generated properly
Next Topic:Bug - Confirm?
Goto Forum:
  


Current Time: Thu Apr 18 03:49:09 GMT 2024

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

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

Back to the top