Auto setting code style scheme from plugin

Answered

I want to auto-import and select my own code style scheme on IDE level from plugin.

I found the API in these posts:  1  2

My code like this:

final VirtualFile selectedXml = VfsUtil.findFileByIoFile(mySchemeXmlFile, true);
CodeStyleSchemesUIConfiguration.Util.setRecentImportFile(selectedXml);
final CodeStyleScheme schemeImported = new CodeStyleSchemeImpl("GoogleStyle", false, null);

final SchemeImporter<CodeStyleScheme> importer = SchemeImporterEP.getImporter("IntelliJ IDEA code style XML", CodeStyleScheme.class);
assert importer != null;

importer.importScheme(project, selectedXml, schemeImported, new SchemeFactory<CodeStyleScheme>() {
           @Override
           public @NotNull CodeStyleScheme createNewScheme(@Nullable String name) {
               return schemeImported;
           }
});

CodeStyleSchemesModel codeStyleSchemesModel = new CodeStyleSchemesModel(project);
codeStyleSchemesModel.addScheme(schemeImported, true);
codeStyleSchemesModel.apply();

After run the codes, I found two problems:

1. Use SchemeImporter API, the "Import Scheme" dialog will open, is there any API can import schemes quietly?


2. When I quit IDE and restarted,  reopen Settings-editor-code style panel, the imported scheme was lost.  Is there something wrong?

 

Thanks.

0
3 comments

Waiting for answer from responsible developer.

0

OK…

For 1, I have no idea… ignored.

For 2, finally I save the scheme like this:

CodeStyleSchemesConfigurable codeStyleSchemesConfigurable = new CodeStyleSchemesConfigurable(project);
codeStyleSchemesConfigurable.apply();
0

Hello,

1. To get around the dialog, you can use CodeStyleSettingsLoader, which the XML importer uses behind the scenes:

CodeStyleSettings codeStyleSettings = (new CodeStyleSettingsLoader().loadSettings(selectedXml));
codeStyleSettings.resetDeprecatedFields();
schemeImported.setCodeStyleSettings(codeStyleSettings);
codeStyleSettings.getModificationTracker().incModificationCount();

2. CodeStyleSettingsManager#notifyCodeStyleSettingsChanged should be called after making these changes, so that other parts of the system can update properly. codeStyleSchemesConfigurable.apply(); calls it, among other things. (Note that this code should run on the EDT.)

CodeStyleSchemesModel codeStyleSchemesModel = new CodeStyleSchemesModel(project);
codeStyleSchemesModel.addScheme(schemeImported, true);
codeStyleSchemesModel.apply();
CodeStyleSettingsManager.getInstance(anActionEvent.getProject()).notifyCodeStyleSettingsChanged();
0

Please sign in to leave a comment.