Set default indent for custom language plugin?

I've created a CustomCodeStyleSetting and both CodeStyleSettingsProvider and LanguageCodeStyleSettingsProvider. The custom CodeStyleSettingsPanel now sets the general indent for my language:

public class MyCodeStyleSettingsPanel extends CodeStyleAbstractPanel {
  public void apply(CodeStyleSettings settings) {
      MyCodeStyleSettings mySettings = settings.getCustomSettings(MyCodeStyleSettings.class);
      
mySettings.XYZ = getIntValue(myXyz);
      // sync indent settings
      
CommonCodeStyleSettings.IndentOptions indentOptions = settings.getCommonSettings(MyLanguage.INSTANCE).getIndentOptions();
      if
(indentOptions != null)
          indentOptions.INDENT_SIZE = indentOptions.CONTINUATION_INDENT_SIZE = mySettings.XYZ;
  }
}


But how (and where) can I set the default indent for my language before the user opens the code style settings dialog for the first time?

0
3 comments

For reference - I think I found the solution.

Instead of LanguageCodeStyleSettings I'm using the FileIndentOptionsProvider extension point, this also handles the indent update of the custom settings:

public class MyFileIndentOptionsProvider extends FileIndentOptionsProvider {
    @Nullable
    @Override
    public
CommonCodeStyleSettings.IndentOptions getIndentOptions(@NotNull CodeStyleSettings codeStyleSettings,
                                                                  @NotNull
PsiFile psiFile) {
        if (psiFile instanceof MyFile) {
            CommonCodeStyleSettings.IndentOptions options = new CommonCodeStyleSettings.IndentOptions();
            
MyCodeStyleSettings customSettings = codeStyleSettings.getCustomSettings(MyCodeStyleSettings.class);
            
options.INDENT_SIZE = options.CONTINUATION_INDENT_SIZE = customSettings != null ? customSettings.XYZ : 69;
            return
options;
        
}
        return null;
    
}
}
0

I ended up using the following for my plugin, which seems to work:

 
langCodeStyleSettingsProvider extension point
 
class extended from LanguageCodeStyleSettingsProvider

 
@Override
public CommonCodeStyleSettings getDefaultCommonSettings() {
   CommonCodeStyleSettings defaultSettings = new CommonCodeStyleSettings( getLanguage() );
   
CommonCodeStyleSettings.IndentOptions indentOptions = defaultSettings.initIndentOptions();
   
indentOptions.INDENT_SIZE = 4;
   
indentOptions.CONTINUATION_INDENT_SIZE = 8;
   
indentOptions.TAB_SIZE = 4;
   
indentOptions.USE_TAB_CHARACTER = true;

   
defaultSettings.LINE_COMMENT_AT_FIRST_COLUMN = false;
   
defaultSettings.KEEP_FIRST_COLUMN_COMMENT = false;

   return
defaultSettings;
}
0

Thanks for the alternative solution!

0

Please sign in to leave a comment.