Setting code style scheme from plugin

I want to read/set code style scheme from plugin.

Tried to use CodeStyleSchemes.getInstance().getCurrentScheme() and CodeStyleSchemes.getInstance().setCurrentScheme() but it seemed that what was returned by getCurrentScheme was not the same as in the settings dialog.

Why is that? Am I using wrong API?

5 comments

Could you please clarify "was not the same as in the settings dialog"?

0

The API is correct but more actions may be needed depending on what you are trying to achieve eventually.

0

What I saw was that if CodeStyleSchemes.getInstance().getCurrentScheme().getName() returned e.g. 'CompanyStyle' and then I went into normal IDEA settings dialog it showed 'Default' for code style scheme.

So it was like there was a mismatch between the API and what was shown in IDEA settings dialog.

0

com.intellij.psi.codeStyle.CodeStyleSettingsManager#getSettings(Project)

0

It also depends on the currently selected project. You can choose a different IDE scheme for every project. Assuming that it's a global (IDE) scheme, not "Project", you can find it using the following code:

CodeStyleSchemes.getInstance().findPreferredScheme(getProjectSettings().PREFERRED_PROJECT_CODE_STYLE

Or simply use CodeStyleSchemesModel as follows:

CodeStyleSchemesModel codeStyleSchemesModel = new CodeStyleSchemesModel(project);
CodeStyleScheme scheme = codeStyleSchemesModel.getSelectedScheme();

You can also add/remove/select schemes via CodeStyleSchemesMdoel and the use apply() at the end.

0

Please sign in to leave a comment.