From d4567533d6dfe0266d731ed5a1f893faaf814d03 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 9 Jul 2025 16:00:30 +0200 Subject: [PATCH] codestyle cleanup: notnull GitOrigin-RevId: 525875990920936a8dfd6a9eb1ef47c896194899 --- .../application/options/CodeStyle.java | 26 +++++------ .../psi/codeStyle/CodeStyleSettings.java | 45 +++++++++---------- .../codeStyle/CommonCodeStyleSettings.java | 2 +- .../FileTypeIndentOptionsFactory.java | 3 ++ .../cache/CodeStyleCachingServiceImpl.java | 16 ++----- .../com/intellij/util/ui/PresentableEnum.java | 2 + .../FileTypeIndentOptionsProvider.java | 3 ++ .../codeStyle/WrappingAndBracesPanel.java | 2 +- .../python/formatter/PyCodeStyleSettings.java | 2 +- 9 files changed, 50 insertions(+), 51 deletions(-) diff --git a/platform/code-style-api/src/com/intellij/application/options/CodeStyle.java b/platform/code-style-api/src/com/intellij/application/options/CodeStyle.java index 9a1c5718b4db..0a5c2e507c94 100644 --- a/platform/code-style-api/src/com/intellij/application/options/CodeStyle.java +++ b/platform/code-style-api/src/com/intellij/application/options/CodeStyle.java @@ -87,21 +87,21 @@ public final class CodeStyle { public static @NotNull CodeStyleSettings getSettings(@NotNull Project project, @NotNull VirtualFile file) { CodeStyleSettings localOrTempSettings = getLocalOrTemporarySettings(project); if (localOrTempSettings != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("localOrTemp settings for " + file.getName()); + if (LOG.isTraceEnabled()) { + LOG.trace("localOrTemp settings for " + file.getName()); } return localOrTempSettings; } return getCachedOrProjectSettings(project, file); } - public static CodeStyleSettings getSettings(@NotNull PsiFile file) { + public static @NotNull CodeStyleSettings getSettings(@NotNull PsiFile file) { final Project project = file.getProject(); CodeStyleSettings localOrTempSettings = getLocalOrTemporarySettings(project); if (localOrTempSettings != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("localOrTemp settings for " + file.getName()); + if (LOG.isTraceEnabled()) { + LOG.trace("localOrTemp settings for " + file.getName()); } return localOrTempSettings; } @@ -117,7 +117,7 @@ public final class CodeStyle { return getCachedOrProjectSettings(project, virtualFile); } - private static CodeStyleSettings getCachedOrProjectSettings(@NotNull Project project, @NotNull VirtualFile file) { + private static @NotNull CodeStyleSettings getCachedOrProjectSettings(@NotNull Project project, @NotNull VirtualFile file) { CodeStyleSettings cachedSettings = CodeStyleCachingService.getInstance(project).tryGetSettings(file); if (LOG.isDebugEnabled()) { LOG.debug((cachedSettings != null ? "cached" : "project") + " settings for " + file.getName()); @@ -167,12 +167,12 @@ public final class CodeStyle { return virtualFile != null && virtualFile.isInLocalFileSystem(); } - public static CodeStyleSettings getSettings(@NotNull Project project, @NotNull Document document) { + public static @NotNull CodeStyleSettings getSettings(@NotNull Project project, @NotNull Document document) { PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document); return file != null ? getSettings(file) : getSettings(project); } - public static CodeStyleSettings getSettings(@NotNull Editor editor) { + public static @NotNull CodeStyleSettings getSettings(@NotNull Editor editor) { Project project = editor.getProject(); VirtualFile file = editor.getVirtualFile(); if (file != null && project != null) { @@ -206,7 +206,7 @@ public final class CodeStyle { * @param Settings class type. * @return The current custom settings associated with the PSI file. */ - public static @NotNull T getCustomSettings(@NotNull PsiFile file, Class customSettingsClass) { + public static @NotNull T getCustomSettings(@NotNull PsiFile file, @NotNull Class customSettingsClass) { CodeStyleSettings rootSettings = getSettings(file); return rootSettings.getCustomSettings(customSettingsClass); } @@ -250,7 +250,7 @@ public final class CodeStyle { * @return The resulting indent options. * @see FileIndentOptionsProvider */ - public static CommonCodeStyleSettings.IndentOptions getIndentOptions(@NotNull Project project, @NotNull VirtualFile virtualFile) { + public static @NotNull CommonCodeStyleSettings.IndentOptions getIndentOptions(@NotNull Project project, @NotNull VirtualFile virtualFile) { CodeStyleSettings rootSetting = getSettings(project, virtualFile); return rootSetting.getIndentOptionsByFile(project, virtualFile, null); } @@ -469,7 +469,7 @@ public final class CodeStyle { * @return Test code style settings. */ @TestOnly - public static CodeStyleSettings createTestSettings(@Nullable CodeStyleSettings baseSettings) { + public static @NotNull CodeStyleSettings createTestSettings(@Nullable CodeStyleSettings baseSettings) { return CodeStyleSettingsManager.createTestSettings(baseSettings); } @@ -540,7 +540,7 @@ public final class CodeStyle { * @return Test code style settings. */ @TestOnly - public static CodeStyleSettings createTestSettings() { + public static @NotNull CodeStyleSettings createTestSettings() { return CodeStyleSettingsManager.createTestSettings(null); } @@ -559,7 +559,7 @@ public final class CodeStyle { * @param editor The current editor. * @param offset The offset to find the language at. */ - public static CommonCodeStyleSettings getLocalLanguageSettings(Editor editor, int offset) { + public static @NotNull CommonCodeStyleSettings getLocalLanguageSettings(@NotNull Editor editor, int offset) { PsiFile psiFile = PsiEditorUtil.getPsiFile(editor); Language language = PsiUtilCore.getLanguageAtOffset(psiFile, offset); return getLanguageSettings(psiFile, language); diff --git a/platform/code-style-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java b/platform/code-style-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java index de47d561a32b..d9ca9e399624 100644 --- a/platform/code-style-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java +++ b/platform/code-style-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java @@ -72,7 +72,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea /** * Produces the default configurable id for the configurables that didn't override it, produced by {@link CodeStyleSettingsProvider}. */ - public static String generateConfigurableIdByLanguage(@NotNull Language language) { + public static @NotNull String generateConfigurableIdByLanguage(@NotNull Language language) { return "preferences.sourceCode." + language.getID(); } @@ -176,7 +176,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea myRepeatAnnotations.addAll(from.myRepeatAnnotations); } - public void copyFrom(CodeStyleSettings from) { + public void copyFrom(@NotNull CodeStyleSettings from) { CommonCodeStyleSettings.copyPublicFields(from, this); OTHER_INDENT_OPTIONS.copyFrom(from.OTHER_INDENT_OPTIONS); mySoftMargins.setValues(from.getDefaultSoftMargins()); @@ -273,13 +273,13 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea /** @deprecated Use {@link com.intellij.psi.codeStyle.JavaCodeStyleSettings#getRepeatAnnotations()} */ @Deprecated(forRemoval = true) - public List getRepeatAnnotations() { + public @NotNull List getRepeatAnnotations() { return myRepeatAnnotations; } /** @deprecated Use {@link com.intellij.psi.codeStyle.JavaCodeStyleSettings#setRepeatAnnotations(List)} */ @Deprecated(forRemoval = true) - public void setRepeatAnnotations(List repeatAnnotations) { + public void setRepeatAnnotations(@NotNull List repeatAnnotations) { myRepeatAnnotations.clear(); myRepeatAnnotations.addAll(repeatAnnotations); } @@ -412,14 +412,14 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea /** @deprecated Use {@link com.intellij.psi.codeStyle.JavaCodeStyleSettings#getImportLayoutTable()} */ @Deprecated @Override - public PackageEntryTable getImportLayoutTable() { + public @NotNull PackageEntryTable getImportLayoutTable() { return IMPORT_LAYOUT_TABLE; } /** @deprecated Use {@link com.intellij.psi.codeStyle.JavaCodeStyleSettings#getPackagesToUseImportOnDemand()} */ @Deprecated @Override - public PackageEntryTable getPackagesToUseImportOnDemand() { + public @NotNull PackageEntryTable getPackagesToUseImportOnDemand() { return PACKAGES_TO_USE_IMPORT_ON_DEMAND; } @@ -550,7 +550,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea } @Override - public void readExternal(Element element) throws InvalidDataException { + public void readExternal(@NotNull Element element) throws InvalidDataException { myVersion = getVersion(element); myCustomCodeStyleSettingsManager.notifySettingsBeforeLoading(); myStoredOptions.processOptions(element); @@ -609,7 +609,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea } @Override - public void writeExternal(Element element) throws WriteExternalException { + public void writeExternal(@NotNull Element element) throws WriteExternalException { setVersion(element, myVersion); CodeStyleSettings defaultSettings = new CodeStyleSettings(true, false); DefaultJDOMExternalizer.write(this, element, myStoredOptions.createDiffFilter(this, defaultSettings)); @@ -641,7 +641,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea } } - private static IndentOptions getDefaultIndentOptions(FileType fileType) { + private static @NotNull IndentOptions getDefaultIndentOptions(@NotNull FileType fileType) { for (final FileTypeIndentOptionsFactory factory : CodeStyleSettingsService.getInstance().getFileTypeIndentOptionsFactories()) { if (factory.getFileType().equals(fileType)) { return getFileTypeIndentOptions(factory); @@ -858,7 +858,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea } } - private static IndentOptions getFileTypeIndentOptions(@NotNull FileTypeIndentOptionsFactory factory) { + private static @NotNull IndentOptions getFileTypeIndentOptions(@NotNull FileTypeIndentOptionsFactory factory) { try { return factory.createIndentOptions(); } @@ -931,7 +931,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea * @return Language-specific code style settings or shared settings if not found. * @see CommonCodeStyleSettingsManager#getCommonSettings */ - public CommonCodeStyleSettings getCommonSettings(String langName) { + public @NotNull CommonCodeStyleSettings getCommonSettings(@NotNull String langName) { return myCommonSettingsManager.getCommonSettings(langName); } @@ -1008,16 +1008,15 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea } public enum WrapStyle implements PresentableEnum { - DO_NOT_WRAP(CommonCodeStyleSettings.DO_NOT_WRAP, CodeStyleBundle.messagePointer("wrapping.do.not.wrap")), WRAP_AS_NEEDED(CommonCodeStyleSettings.WRAP_AS_NEEDED, CodeStyleBundle.messagePointer("wrapping.wrap.if.long")), WRAP_ON_EVERY_ITEM(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM, CodeStyleBundle.messagePointer("wrapping.chop.down.if.long")), WRAP_ALWAYS(CommonCodeStyleSettings.WRAP_ALWAYS, CodeStyleBundle.messagePointer("wrapping.wrap.always")); private final int myId; - private final Supplier<@Label String> myDescription; + private final @NotNull Supplier<@Label String> myDescription; - WrapStyle(int id, @NotNull Supplier<@Label String> description) { + WrapStyle(int id, @NotNull Supplier<@Label @NotNull String> description) { myId = id; myDescription = description; } @@ -1026,7 +1025,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea return myId; } - @Override public @Label String getPresentableText() { + @Override public @Label @NotNull String getPresentableText() { return myDescription.get(); } @@ -1052,7 +1051,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea private final String myValue; private final Supplier<@Label String> myDescription; - HtmlTagNewLineStyle(@NotNull String value, @NotNull Supplier<@Label String> description) { + HtmlTagNewLineStyle(@NotNull String value, @NotNull Supplier<@Label @NotNull String> description) { myValue = value; myDescription = description; } @@ -1062,7 +1061,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea return myValue; } - @Override public @Label String getPresentableText() { + @Override public @Label @NotNull String getPresentableText() { return myDescription.get(); } } @@ -1075,12 +1074,12 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea public final String quote; private final Supplier<@Label String> myDescription; - QuoteStyle(@NotNull String quote, @NotNull Supplier<@Label String> description) { + QuoteStyle(@NotNull String quote, @NotNull Supplier<@Label @NotNull String> description) { this.quote = quote; myDescription = description; } - @Override public @Label String getPresentableText() { + @Override public @Label @NotNull String getPresentableText() { return myDescription.get(); } } @@ -1152,7 +1151,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea * @param language The language to set soft margins for. * @param softMargins The soft margins to set. */ - public void setSoftMargins(@NotNull Language language, List softMargins) { + public void setSoftMargins(@NotNull Language language, @NotNull List softMargins) { CommonCodeStyleSettings languageSettings = myCommonSettingsManager.getCommonSettings(language); assert languageSettings != null : "Settings for language " + language.getDisplayName() + " do not exist"; languageSettings.setSoftMargins(softMargins); @@ -1169,7 +1168,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea * Sets the default soft margins used for languages not defining them explicitly. * @param softMargins The default soft margins. */ - public void setDefaultSoftMargins(List softMargins) { + public void setDefaultSoftMargins(@NotNull List softMargins) { mySoftMargins.setValues(softMargins); } @@ -1177,7 +1176,7 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea return myExcludedFiles; } - public SimpleModificationTracker getModificationTracker() { + public @NotNull SimpleModificationTracker getModificationTracker() { return myModificationTracker; } @@ -1201,8 +1200,8 @@ public class CodeStyleSettings extends LegacyCodeStyleSettings implements Clonea myCustomCodeStyleSettingsManager.registerCustomSettings(this, factory); } + @NotNull CustomCodeStyleSettingsManager getCustomCodeStyleSettingsManager() { return myCustomCodeStyleSettingsManager; } - } diff --git a/platform/code-style-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java b/platform/code-style-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java index 71467fe807a3..01de279326a7 100644 --- a/platform/code-style-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java +++ b/platform/code-style-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java @@ -1207,7 +1207,7 @@ public class CommonCodeStyleSettings implements CommentStyleSettings { return mySoftMargins.getValues(); } - public void setSoftMargins(List values) { + public void setSoftMargins(@NotNull List values) { mySoftMargins.setValues(values); } } diff --git a/platform/code-style-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsFactory.java b/platform/code-style-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsFactory.java index 13c0e253a512..522a0aa66562 100644 --- a/platform/code-style-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsFactory.java +++ b/platform/code-style-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsFactory.java @@ -3,10 +3,13 @@ package com.intellij.psi.codeStyle; import com.intellij.openapi.fileTypes.FileType; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; @ApiStatus.Internal public interface FileTypeIndentOptionsFactory { + @NotNull CommonCodeStyleSettings.IndentOptions createIndentOptions(); + @NotNull FileType getFileType(); } \ No newline at end of file diff --git a/platform/code-style-impl/src/com/intellij/application/options/codeStyle/cache/CodeStyleCachingServiceImpl.java b/platform/code-style-impl/src/com/intellij/application/options/codeStyle/cache/CodeStyleCachingServiceImpl.java index 51bbba44336a..4541be15496d 100644 --- a/platform/code-style-impl/src/com/intellij/application/options/codeStyle/cache/CodeStyleCachingServiceImpl.java +++ b/platform/code-style-impl/src/com/intellij/application/options/codeStyle/cache/CodeStyleCachingServiceImpl.java @@ -115,24 +115,16 @@ public final class CodeStyleCachingServiceImpl implements CodeStyleCachingServic } } - private static class VirtualFileGetter implements Supplier { - private final VirtualFile virtualFile; - - private VirtualFileGetter(VirtualFile file) { virtualFile = file; } - + private record VirtualFileGetter(@NotNull VirtualFile virtualFile) implements Supplier { @Override - public VirtualFile get() { + public @NotNull VirtualFile get() { return virtualFile; } } - private static class LightVirtualFileCopyGetter implements Supplier { - private final LightVirtualFile virtualFile; - - private LightVirtualFileCopyGetter(LightVirtualFile file) { virtualFile = file; } - + private record LightVirtualFileCopyGetter(@NotNull LightVirtualFile virtualFile) implements Supplier { @Override - public VirtualFile get() { + public @NotNull VirtualFile get() { return getCopy(virtualFile); } } diff --git a/platform/core-api/src/com/intellij/util/ui/PresentableEnum.java b/platform/core-api/src/com/intellij/util/ui/PresentableEnum.java index 09c4254f3125..de29782473ee 100644 --- a/platform/core-api/src/com/intellij/util/ui/PresentableEnum.java +++ b/platform/core-api/src/com/intellij/util/ui/PresentableEnum.java @@ -2,7 +2,9 @@ package com.intellij.util.ui; import com.intellij.openapi.util.NlsContexts; +import org.jetbrains.annotations.NotNull; public interface PresentableEnum { + @NotNull @NlsContexts.Label String getPresentableText(); } diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsProvider.java b/platform/lang-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsProvider.java index b3c13b74315f..d64ea88fe596 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsProvider.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/FileTypeIndentOptionsProvider.java @@ -20,6 +20,7 @@ import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.fileTypes.FileType; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; /** * Allows to specify indent options for specific file types as opposed to languages. For a language it is highly recommended to use @@ -31,9 +32,11 @@ public interface FileTypeIndentOptionsProvider extends FileTypeIndentOptionsFact ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.fileTypeIndentOptionsProvider"); @Override + @NotNull CommonCodeStyleSettings.IndentOptions createIndentOptions(); @Override + @NotNull FileType getFileType(); @NonNls diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/WrappingAndBracesPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/WrappingAndBracesPanel.java index a9d96705f1a8..20348c1e32eb 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/WrappingAndBracesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/WrappingAndBracesPanel.java @@ -195,7 +195,7 @@ public class WrappingAndBracesPanel extends OptionTableWithPreviewPanel { } } - private static List castToIntList(@Nullable Object value) { + private static @NotNull List castToIntList(@Nullable Object value) { if (value instanceof List && !((List)value).isEmpty() && ((List)value).get(0) instanceof Integer) { //noinspection unchecked return (List)value; diff --git a/python/python-syntax-core/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java b/python/python-syntax-core/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java index 33176b6180ba..e3ed2d8b4058 100644 --- a/python/python-syntax-core/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java +++ b/python/python-syntax-core/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java @@ -30,7 +30,7 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings { } @Override - public String getPresentableText() { + public @NotNull String getPresentableText() { return PySyntaxCoreBundle.message(key); }