我在Eclipse中生成一个插件。我的插件在一个开放的编辑器中生成源JSP代码,所以我想格式化整个代码,就像按下Ctrl + Shift + F一样。
我尝试用Eclipse JDT做到这一点:
Properties prefs = new Properties();
prefs.setProperty(JavaCore.COMPILER_SOURCE, CompilerOptions.VERSION_1_8);
prefs.setProperty(JavaCore.COMPILER_COMPLIANCE, CompilerOptions.VERSION_1_8);
prefs.setProperty(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, CompilerOptions.VERSION_1_8);
CodeFormatter formatter = ToolFactory.createCodeFormatter(prefs);
String texto2 = texto;
IDocument dc = new Document(texto2);
//ICompilationUnit cu = (ICompilationUnit)JavaCore.create(texto_editor);
TextEdit edit_formatear =
formatter.format(CodeFormatter.K_COMPILATION_UNIT,
texto2, 0, texto2.length(), 0, null);
if(edit_formatear!=null){
edit_formatear.apply(dc);
System.out.println("Edit: "+dc.get());
}
} catch (BadLocationException e) {
e.printStackTrace();
}
但我认为这段代码只适用于java代码......
有什么想法吗?