Eclipse JDT: Call 'correct indentation' programmatically?

Well I think I may have figured out the solution I want. Guess I should have spend more time searching before asking... but for future reference, here's what I did! The good stuff was in the ToolFactory.

Well I think I may have figured out the solution I want. Guess I should have spend more time searching before asking... but for future reference, here's what I did! The good stuff was in the ToolFactory... import org.eclipse.jdt.core.

ToolFactory; import org.eclipse.jdt.core.formatter. CodeFormatter; import org.eclipse.jdt.core. ISourceRange; import org.eclipse.text.edits.

TextEdit; import org.eclipse.jdt.core. ICompilationUnit; ... ICompilationUnit cu = ... ... CodeFormatter formatter = ToolFactory. CreateCodeFormatter(null); ISourceRange range = cu.getSourceRange(); TextEdit indent_edit = formatter.

Format(CodeFormatter. K_COMPILATION_UNIT, cu.getSource(), range.getOffset(), range.getLength(), 0, null); cu. ApplyTextEdit(indent_edit, null); cu.reconcile(); This reformats the entire file.

There are other options if you need to reformat less...

It's probably easier to add the indentation as you process the Java code. Your Eclipse plugin had to read the void foo() { ... } line to know to add the @MyAnnotation, right? Just get the indentation from the Java line, and append your annotation to the indentation.

Thanks Gilbert, but actually I didn't read the line... I get the method location from MethodDeclaration. GetStartPosition() where MethodDeclaration is in the package org.eclipse.jdt.core.dom. Now I could go back and copy what's before the method declaration from the start of the line, but I don't want to copy a bunch of junk if the programmer's using some alternative formatting... – Nels Beckman May 20 '10 at 12:54 Ok, I've not seen these org.eclipse.

Jdt packages before. How are you adding your annotation to the Java source code? – Gilbert Le Blanc May 20 '10 at 13:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions