Getting indent settings from CodeStyleSettings does not work.

已回答

When using CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(project).getMainProjectCodeStyle(); with my style settings changed from the default (Tab Size: 2, Indent: 2), I still get a response of 4 (the default size) when calling any of the following:

  • settings.getTabSize(JavaFileType)
  • settings.getIndentOptions().TAB_SIZE
  • settings.getIndentOptions().INDENT_SIZE

even though they should all give a response of 2 due to the setting changes.

Has anyone experienced this before, or am I just using this incorrectly? 

 

0

Hi,

Please provide more information.

Where do you change the settings? Please paste screenshot.

What is the full context of the code? Please share the whole method or class.

0

Hi,

Settings are changed in the settings menu, have attached screenshot below. For the record, I have changed default, demo, and project settings all with the same results.

This is the method attempting to get the tab size, as mentioned I have tried many different methods to get the tab size.

public class SettingUtils {
  private SettingUtils() {
  }

  public static String getDefaultTabCharacter(Project project) {
    var settings = CodeStyleSettingsManager.getInstance(project).getMainProjectCodeStyle();

    assert settings != null;
    var useSoftTabs = !settings.getIndentOptions(FileUtils.getJavaFileType()).USE_TAB_CHARACTER;

    if (useSoftTabs) {
      var tabSize = settings.getIndentOptions().TAB_SIZE;
      return " ".repeat(tabSize);
    } else {
      return "\t";
    }
  }
}

And  to get the project, I'm using the following line in my action:
this.project = e.getRequiredData(CommonDataKeys.PROJECT);

Let me know if you'd like any more context.

Thanks.

0

Please try one of com.intellij.application.options.CodeStyle's static methods, e.g., getSettings() or getLanguageSettings().

0

Great that worked. Thank you Kaorl

0

请先登录再写评论。