Custom JAVA Code style

Answered

Hello,

I would like to create a plugin that changes the default settings of the code style of Java. I checked the documentation and the only thing I could find was how to customize the code style for a custom language.

Is there any way I can modify the code style of a pre-existing language?

0
1 comment

Hello,

Customizing the defaults for an existing language is not possible from another plugin.

You could create your own code style scheme and automatically set it for your users, perhaps only when they’re using the “Default” scheme. See also this post on how to import a scheme and set it. Instead of importing the scheme, you could also create and modify it manually from code. Here’s a snippet:

// create a scheme containing settings initialized with defaults
final var scheme = new CodeStyleSchemeImpl("My Custom Scheme", false, null);
final var codeStyleSettings = scheme.getCodeStyleSettings();
// modify some settings
codeStyleSettings.getCommonSettings(JavaLanguage.INSTANCE).SPACE_AROUND_ADDITIVE_OPERATORS = false;
// indicate a change was made to the these settings
codeStyleSettings.getModificationTracker().incModificationCount();

// add the scheme and maybe set it as the current scheme
CodeStyleSchemesModel codeStyleSchemesModel = new CodeStyleSchemesModel(project);
final var shouldSwitchToNewScheme = codeStyleSchemesModel.getSelectedScheme().getName().equals(CodeStyleScheme.DEFAULT_SCHEME_NAME);
codeStyleSchemesModel.addScheme(scheme, shouldSwitchToNewScheme);
codeStyleSchemesModel.apply();
// let other parts of the system update
CodeStyleSettingsManager.getInstance(project).notifyCodeStyleSettingsChanged();
0

Please sign in to leave a comment.