# Using PsiShortNamesCache will not automatically import packages after entering the class name and pressing enter

**URL:** <https://platform.jetbrains.com/t/using-psishortnamescache-will-not-automatically-import-packages-after-entering-the-class-name-and-pressing-enter/3297>\
**Category:** IntelliJ Platform\
**Created:** [December 9, 2025, 3:13am UTC](https://platform.jetbrains.com/t/using-psishortnamescache-will-not-automatically-import-packages-after-entering-the-class-name-and-pressing-enter/3297 "2025-12-09T03:13:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hxl49508](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/hxl49508/32/1117_2.png) [@hxl49508](https://platform.jetbrains.com/u/hxl49508)\
**Post date:** [December 9, 2025, 3:13am UTC](https://platform.jetbrains.com/t/using-psishortnamescache-will-not-automatically-import-packages-after-entering-the-class-name-and-pressing-enter/3297/1 "2025-12-09T03:13:18Z")

</div>

```auto
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import org.jetbrains.annotations.NotNull;
import com.intellij.psi.impl.light.LightClass;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiManager;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.openapi.project.Project;

public class CustomPsiShortNamesCache extends PsiShortNamesCache {

    private static final String MY_CUSTOM_CLASS_NAME = "MyInjectedClass";
    private final Project myProject;

    public CustomPsiShortNamesCache(Project project) {
        this.myProject = project;
    }

    @Override
    public PsiClass @NotNull [] getClassesByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
        if (MY_CUSTOM_CLASS_NAME.equals(name)) {
            PsiClass customClass = createMyCustomPsiClass(name); 
            return new PsiClass[]{customClass};
        }
        return PsiClass.EMPTY_ARRAY;
    }

    @Override
    public String @NotNull [] getAllClassNames() {
        return new String[]{MY_CUSTOM_CLASS_NAME};
    }

    @Override
    public PsiFile @NotNull [] getFilesByName(@NotNull String name) {
        return PsiFile.EMPTY_ARRAY;
    }

    @Override
    public PsiClass @NotNull [] getClassesByFQName(@NotNull String name, @NotNull GlobalSearchScope scope) {
        return PsiClass.EMPTY_ARRAY;
    }

    @Override
    public String @NotNull [] getAllClassNames(@NotNull GlobalSearchScope scope) {
        return new String[]{MY_CUSTOM_CLASS_NAME};
    }

    private PsiClass createMyCustomPsiClass(String name) {
          PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(
                "MyClass" + ".java",
                JavaLanguage.INSTANCE,
                "package com.myclass.test;\n" +
                        "public interface MyClass{}"
        );

        if (psiFile instanceof PsiJavaFile) {
            return ((PsiJavaFile) psiFile).getClasses()[0];
        }
    }
}

```

```auto
<extensions defaultExtensionNs="com.intellij">
    <psi.shortNamesCache implementation="com.your.package.CustomPsiShortNamesCache"/>
</extensions>

```

When I enter MyClass and press enter in the code editor, Idea does not automatically insert import `com.myclass.test.MyClass` at the top of the file, but inserts `com.myclass.test.`before the current class name;

My expectation is to insert an import at the top when importing a class. What should I do

---

<div class="post-metadata">

**Author:** ![yuriy.artamonov](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/yuriy.artamonov/32/48_2.png) [@yuriy.artamonov](https://platform.jetbrains.com/u/yuriy.artamonov)\
**Post date:** [December 9, 2025, 8:46am UTC](https://platform.jetbrains.com/t/using-psishortnamescache-will-not-automatically-import-packages-after-entering-the-class-name-and-pressing-enter/3297/2 "2025-12-09T08:46:25Z")

</div>

Can only say that `PsiShortNamesCache` is not API to implement resolve of PsiClass, it is only used as an actual cache for performance reasons.

The right direction would be

> [@How to register a psi file created in memory into a project module?](https://platform.jetbrains.com/t/how-to-register-a-psi-file-created-in-memory-into-a-project-module/547/8):
>
> Consider providing an implementing of extension point of com.intellij.jvm.elementProvider (com.intellij.lang.jvm.facade.JvmElementProvider interface). This is the entry point for all synthetic declarations visible to Java files. In this case you won’t need to register synthetic files in the project model, and it will be enough to generate instances of PsiClass on the fly (or cache them in a storage of your choice). Can IntelliJ IDEA index a VirtualFile in memory? That’s possible, you can i…
