diff --git a/platform/lang-api/src/com/intellij/codeInsight/template/TemplateContextType.java b/platform/lang-api/src/com/intellij/codeInsight/template/TemplateContextType.java index fb600f753973..2398aadcb939 100644 --- a/platform/lang-api/src/com/intellij/codeInsight/template/TemplateContextType.java +++ b/platform/lang-api/src/com/intellij/codeInsight/template/TemplateContextType.java @@ -21,6 +21,8 @@ import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.fileTypes.SyntaxHighlighter; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.NullableLazyValue; +import com.intellij.openapi.util.VolatileNullableLazyValue; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -36,7 +38,7 @@ public abstract class TemplateContextType { private final String myContextId; @NotNull private final String myPresentableName; - private final Class myBaseContextType; + private final NullableLazyValue myBaseContextType; protected TemplateContextType(@NotNull @NonNls String id, @NotNull String presentableName) { this(id, presentableName, EverywhereContextType.class); @@ -47,7 +49,7 @@ public abstract class TemplateContextType { @Nullable Class baseContextType) { myContextId = id; myPresentableName = presentableName; - myBaseContextType = baseContextType; + myBaseContextType = VolatileNullableLazyValue.createValue(() -> baseContextType == null ? null : EP_NAME.findExtension(baseContextType)); } @NotNull @@ -77,7 +79,7 @@ public abstract class TemplateContextType { @Nullable public TemplateContextType getBaseContextType() { - return myBaseContextType != null ? EP_NAME.findExtension(myBaseContextType) : null; + return myBaseContextType.getValue(); } public Document createDocument(CharSequence text, Project project) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java index 224330fe0d0d..b211f6a21bfe 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java @@ -49,6 +49,7 @@ import org.jetbrains.annotations.TestOnly; import java.util.*; public class TemplateManagerImpl extends TemplateManager implements Disposable { + private static final TemplateContextType[] ourContextTypes = Extensions.getExtensions(TemplateContextType.EP_NAME); private final Project myProject; private boolean myTemplateTesting; @@ -512,7 +513,7 @@ public class TemplateManagerImpl extends TemplateManager implements Disposable { } public static TemplateContextType[] getAllContextTypes() { - return Extensions.getExtensions(TemplateContextType.EP_NAME); + return ourContextTypes; } @Override diff --git a/platform/util/src/com/intellij/openapi/util/VolatileNullableLazyValue.java b/platform/util/src/com/intellij/openapi/util/VolatileNullableLazyValue.java index 907626eb6fd2..513d03bd1e46 100644 --- a/platform/util/src/com/intellij/openapi/util/VolatileNullableLazyValue.java +++ b/platform/util/src/com/intellij/openapi/util/VolatileNullableLazyValue.java @@ -15,6 +15,7 @@ */ package com.intellij.openapi.util; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -42,4 +43,18 @@ public abstract class VolatileNullableLazyValue extends NullableLazyValue } return value; } + + @SuppressWarnings("MethodOverridesStaticMethodOfSuperclass") + @NotNull + public static VolatileNullableLazyValue createValue(@NotNull final Factory value) { + return new VolatileNullableLazyValue() { + + @Nullable + @Override + protected T compute() { + return value.create(); + } + }; + } + }