|
|
Re: How to load a formatter profile to a workspace configuration [message #253639 is a reply to message #253583] |
Fri, 30 May 2008 17:50 |
john Messages: 10 Registered: July 2009 |
Junior Member |
|
|
Thank you, but I think I was not clear enough.
In the plugin I'm working on, I have a hook that runs a job during
eclipse's startup. I'm trying to find a way to review the current
formatter profile configuration. After reviewing the current profile, I'd
like this plugin to create a new generate a new profile from an xml file
bundled with the plugin. I'd like to load the profile into eclipse's
preferences at run time, and apply the new profile once that's been
accomplished.
I tried using JavaCore.getOptions() and JavaCore.getDefaultOptions to
review the current profile loaded into eclipse. Then I tried using
JavaCore.setOptions(Hashmap options) to load the desired options into the
environment. When I've done this, JavaCore believes that the options have
been loaded, but the profile is not visible through the standard
preferences dialogue (preferences>Java>Code Style>formatter). So this is
not sufficient, I actually need to create a new profile and apply it
automatically at startup.
I tried grabbing the workspace preferences using
getInstancePreferences("org.eclipse.jdt.ui"). Note, I also try and grab
the another preference sheet.
Now, this second tactic is effective in retrieving the values of a custom
profile loaded into eclipse. However, trying to use Preferences
myNode.put(myNewPrefName, myNewPrefValue) does not appear successful.
That is to say, the "put" values are not actually loaded into the
preference store.
It may be that I am just being very foolish about this, and there's
something very easy to accomplish the task of having a plugin load
formatter preferences at eclipse startup. I hope I am, and that someone
will tell me about it.
thank you
|
|
|
Re: How to load a formatter profile to a workspace configuration [message #253673 is a reply to message #253639] |
Mon, 02 June 2008 13:22 |
john Messages: 10 Registered: July 2009 |
Junior Member |
|
|
Ok, for the benefit of others:
To load a custom code style formatter into an eclipse workspace from a
plugin:
-create a new object referencing the eclipse workspace preferences, create
an instance of org.eclipse.core.runtime.preferences.InstanceScope
-use the getNode(String preferenceName) method to retrieve an instance of
Preferences representing that preference file.
//"org.eclipse.jdt.ui" is a useful one, for example.
//The actual preference files can be found in the workspace.
//They're located at:
//yourWorkspace/.metadata/.plugins/org.eclipse.core.runtime/ .settings
-load values into the Preferences object using put.
-load the values into memory using the "flush" function.
Now, you can tweak all the formatting rules by altering
"org.eclipse.jdt.core"
However, its necessary to then specify the name of this preference scheme
in "org.eclipse.jdt.core", including a name. To actually have the this
awesome new profile show up in eclipse preference, you need load a value
into the org.eclipse.jdt.ui.pref Preferences. You need to load the name
of the your new profile as "org.eclipse.jdt.ui.formatterprofiles".
Except, you need to load a String that's actually "_"+(String)yourName
Yeah, so, if you flush that, you can bundle and configure your own
"default" code style formatter into eclipse with your plugin. Yay.
|
|
|
|
Re: How to load a formatter profile to a workspace configuration [message #802108 is a reply to message #800050] |
Sun, 19 February 2012 12:15 |
|
Done, with Helios version !
1) Define your Code styles profiles (C and Java for me), for example: myCodeStyle_C and myCodeStyle_Java
2) Export Preferences in file: File/Export/General/Preferences, then Checkbox "Export all" + name your file + finish
3) Edit this last file,
- for "C" Code style:
search "/instance/org.eclipse.cdt.ui/org.eclipse.cdt.ui.formatterprofiles=" [RefA] is rest of line
search /instance/org.eclipse.cdt.ui/formatter_profile=" [RefB] is the rest of line
NB: [RefB] starts with "_"
Copy/Paste [RefA] into an editor, then replace "\=" by "=", '"' by '\"', "\r\n" by nothing
prefix with cs_c_xml=", sufix with "; , then you should get something like this:
cs_c_xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><profiles version=\"1\"><profile kind=\"CodeFormatterProfile\" name=\"myCodeStyle_C\" version=\"1\"><setting id=\"org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration\" value=\"do not insert\"/> ... </profile></profiles>"
in the plugin code:
IEclipsePreferences rootCS = Platform.getPreferencesService().getRootNode();
String qualifierCS = "";
qualifierCS = "org.eclipse.cdt.ui";
cs_c_xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><profiles version=\"1\"><profile kind=\"CodeFormatterProfile\" name=\"myCodeStyle_C\" version=\"1\"><setting id=\"org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration\" value=\"do not insert\"/> ... </profile></profiles>";
rootCS.node(InstanceScope.SCOPE).node(qualifierCS).put("org.eclipse.cdt.ui.formatterprofiles", cs_c_xml);
// /instance/org.eclipse.cdt.ui/formatter_profile=_myCodeStyle_C
rootCS.node(InstanceScope.SCOPE).node(qualifierCS).put("formatter_profile", "_myCodeStyle_C");
qualifierCS = "org.eclipse.cdt.core";
// for each item of cs_c_xml starting with setting id=[refC].[refD]=
// example : <setting id=\"org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration\" value=\"do not insert\"/>
rootCS.node(InstanceScope.SCOPE).node(qualifierCS).put("org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration", "do not insert");
...
- for "Java" Code style, do the same as above with :
search "/instance/org.eclipse.jdt.ui/org.eclipse.jdt.ui.formatterprofiles=" [RefA] is rest of line
search /instance/org.eclipse.jdt.ui/formatter_profile=" [RefB] is the rest of line
qualifierCS = "org.eclipse.jdt.ui";
qualifierCS = "org.eclipse.jdt.core";
Hope it can help, Thanks John
Regards, Jerome
[Updated on: Mon, 20 February 2012 09:13] Report message to a moderator
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05010 seconds