Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Formatting source code JDT
Formatting source code JDT [message #1858642] Thu, 13 April 2023 10:38 Go to next message
bretny relly is currently offline bretny rellyFriend
Messages: 14
Registered: March 2023
Junior Member
I'm using JDT to create certain classes. After that, I'd like to format the entire ICompilationUnit, just as if I clicked Ctrl+Shift+F (Source > Format) in an empty Editor.

Here is the code:
private void formatUnitSourceCode(ICompilationUnit targetUnit, IProgressMonitor monitor) throws JavaModelException {
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, targetUnit.getSource(), -1, targetUnit.getSource().length(), 0, null);
    targetUnit.applyTextEdit(formatEdit, monitor);
}


Any suggestions for the JDT API to format the source code programmatically would be greatly appreciated.

I tried it this way, but the code remained unchanged. What exactly am I mssing?
Re: Formatting source code JDT [message #1858807 is a reply to message #1858642] Mon, 24 April 2023 05:29 Go to previous message
Alan Scott is currently offline Alan ScottFriend
Messages: 3
Registered: April 2023
Junior Member
bretny relly wrote on Thu, 13 April 2023 10:38
I'm using JDT to create certain classes. After that, I'd like to format the entire ICompilationUnit, just as if I clicked Ctrl+Shift+F (Source > Format) in an empty Editor.

Here is the code:anti wordle
private void formatUnitSourceCode(ICompilationUnit targetUnit, IProgressMonitor monitor) throws JavaModelException {
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, targetUnit.getSource(), -1, targetUnit.getSource().length(), 0, null);
    targetUnit.applyTextEdit(formatEdit, monitor);
}


Any suggestions for the JDT API to format the source code programmatically would be greatly appreciated.

I tried it this way, but the code remained unchanged. What exactly am I mssing?


Hi there! It looks like you're trying to use JDT to format your Java code programmatically. Your code looks correct, but there might be some issues with the input parameters or the execution context that are preventing it from working as expected.

Here are a few things you could check:

make sure that the compilation unit you're passing in actually contains source code that needs to be formatted. If the source code is already well-formatted, running the formatter on it may not make any changes.

ensure that the monitor you're passing to the applyTextEdit method is not null and is configured correctly to track the progress of the formatting operation. You could try passing a new NullProgressMonitor instance to see if that makes a difference.

check if there are any error messages or exceptions being thrown when you run the code. You could wrap the formatUnitSourceCode method in a try-catch block and log any caught exceptions to the console or a log file.

Lastly, you could try calling the save method on the compilation unit after applying the text edit. It's possible that the changes are being made but not being saved to disk.

I hope this helps! Let me know if you have any other questions.
Previous Topic:Print Predicate or Consumer type with Java proposal or Template
Next Topic:i have a question about the quick picks when you mouse over a error
Goto Forum:
  


Current Time: Tue Apr 23 14:08:00 GMT 2024

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

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

Back to the top