Reputation: 1107
I'm writing a plugin for eclipse(Kepler) CDT on windows 8.1. My plugin extends eclipse and enables the user to create a project with a specific configurations. I want all my plugin projects to be with the same c code format. So in my plugin code, when creating the project files and configuration, I added the following code in order to add the wanted format:
ProjectScope scope = new ProjectScope(project);
IEclipsePreferences ref = scope.getNode("org.eclipse.cdt.core");
ref.put("org.eclipse.cdt.core.formatter.lineSplit", "100");
ref.put("org.eclipse.cdt.core.formatter.alignment_for_parameters_in_method_declaration", "18");
ref.put("org.eclipse.cdt.core.formatter.brace_position_for_block", "next_line");
ref.put("org.eclipse.cdt.core.formatter.brace_position_for_method_declaration", "next_line");
ref.put("org.eclipse.cdt.core.formatter.tabulation.char", "space");
ref.put("org.eclipse.cdt.core.formatter.alignment_for_arguments_in_method_invocation", "18");
ref.put("org.eclipse.cdt.core.formatter.alignment_for_constructor_initializer_list", "16");
ref.flush();
This code really does its job and configures the format as I want, but the generated format exists only in the project properties, and not being applied yet. If I want to apply the format and cause the files to show with this format, I have to click on apply button, or to press CTRL+SHIFT+F.
Do you know about any way to apply the format programmability, though each project will be generated with its auto-generated files that are formatted already?
Upvotes: 1
Views: 305
Reputation: 1107
I found the solution to my problem. thanks @Bananewizen for helping!
I found that the command id behind the ctrl+shift+f is: ICEditorActionDefinitionIds.FORMAT
and I succeeded to run the command programmability, but I had to handle the file loading in order to format its code. so I implemented IPartListener, and in partOpened function, I wrote:
@Override
public void partOpened(IWorkbenchPart part) {
try {
IFile file = (IFile) part.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
final String cmdName = ICEditorActionDefinitionIds.FORMAT;
IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
try {
handlerService.executeCommand(cmdName, null);
} catch (Exception e) {}
} catch (Exception e) {}
}
Now the handle is found and it works well!
Upvotes: 1
Reputation: 22070
You should be able to find the command ID using either the Eclipse menu spy (Alt-Shift-F2) or by looking up the key binding for the formatter in the key preferences or by importing the cdt.ui plugin as source plugin into your workbench.
When you have the command ID, then you can execute it programmatically using the command service.
Upvotes: 1