Light files should have PSI only in one project

the following code cause "Light files should have PSI only in one project"

PsiFile file = PsiDocumentManager.getInstance(e.getProject()).getPsiFile(editor.getDocument());

Is there any solution?

 


public class FormatAction extends AnAction {

    public FormatAction() {
        super("Format", "Format", AllIcons.Diff.MagicResolve);
    }


    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getProject();
        if (project == null) {
            return;
        }
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (editor == null) {
            return;
        }
        PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (psiFile != null && !editor.getDocument().getText().isBlank()) {
            WriteCommandAction.runWriteCommandAction(
                    project,
                    () -> {
                        CodeStyleManager.getInstance(project).reformat(psiFile);
                    }
            );
        }
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        super.update(e);
        Presentation presentation = e.getPresentation();
        if (presentation.isEnabled()) {
            Editor editor = e.getData(CommonDataKeys.EDITOR);
            if (editor != null && e.getProject() != null) {
                PsiFile file = PsiDocumentManager.getInstance(e.getProject()).getPsiFile(editor.getDocument());
                if (file.getVirtualFile() != null) {
                    e.getPresentation().setEnabledAndVisible(file.getName().startsWith("Dummy."));
                }
            } else {
                e.getPresentation().setEnabledAndVisible(false);
            }
        }
    }
}

 

 

0

请先登录再写评论。