I'd like to develop a plugin (tool bar buttons) for Eclipse CDT where users can easily switch between 8 and 4 spaces tabs and turn on/off soft tabs. (Why bother you asked? Thanks to the coding guideline in my org for tabbing difference between C/C++ legacy and new codes)
I managed to create toolbar buttons but I couldn't find information to modify Editor Preferences (The ones you normally find in Workspace preferences General->Editors->Text Editors).
The question 4587572 seems to cover a bit but I'm still very new to Plug-in dev so I don't really understand.
I guess I want to modify EDITOR_TAB_WIDTH and EDITOR_SPACES_FOR_TABS properties of org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants for the running text Editor.
Not only modifying, I couldn't even read the properties with following code. Just returns me default value:30 I provided.
int width = Platform.getPreferencesService().getInt(
"org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants",
"EDITOR_TAB_WIDTH", 30, null);
My question in summary is: How do I modify tab settings of a running Editor from my plugin?
Much appreciate for your help.
You can use code similar to the following to get and set preferences in any plugin.
IPreferenceStore s = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.ui");
ss.setValue("SHOW_MEMORY_MONITOR", true);
You should try installing and using the AnyEdit Tools which does the job -- one of the most popular eclipse plugins.
Thanks @nonty for suggestion. It works well. For benefits of others, here's my full code to change tab settings in CDT editor.
public void run(IAction action) {
if(action.isChecked())
{
IPreferenceStore ps = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.cdt.core");
ps.setValue("org.eclipse.cdt.core.formatter.tabulation.size", 8);
ps.setValue("org.eclipse.cdt.core.formatter.indentation.size", 8);
ps.setValue("org.eclipse.cdt.core.formatter.use_tabs_only_for_leading_indentations", true);
ps.setValue("org.eclipse.cdt.core.formatter.tabulation.char", "tab"); //=mixed/space/tab
// To check if the value
// int tabWidth = ps.getInt("org.eclipse.cdt.core.formatter.tabulation.size");
// String tabFormat = ps.getString("org.eclipse.cdt.core.formatter.tabulation.char");
// MessageDialog.openInformation(null, "CDT Tab Width", "CDT tab width: " + tabWidth + " format: " + tabFormat);
}
}
Now all I need to do is make sure each Editor tab remembers it's Tab settings and automatically switch back when tab changes. Where do I start... doh!
来源:https://stackoverflow.com/questions/6343288/writing-eclipse-plugin-to-modify-editor-preferences