Search in sources :

Example 1 with AddImportAction

use of com.intellij.codeInsight.daemon.impl.actions.AddImportAction in project intellij-community by JetBrains.

the class OrderEntryFix method importClass.

public static void importClass(@NotNull Module currentModule, @Nullable Editor editor, @Nullable PsiReference reference, @Nullable String className) {
    Project project = currentModule.getProject();
    if (editor != null && reference != null && className != null) {
        DumbService.getInstance(project).withAlternativeResolveEnabled(() -> {
            GlobalSearchScope scope = GlobalSearchScope.moduleWithLibrariesScope(currentModule);
            PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
            if (aClass != null) {
                new AddImportAction(project, reference, editor, aClass).execute();
            }
        });
    }
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) AddImportAction(com.intellij.codeInsight.daemon.impl.actions.AddImportAction)

Example 2 with AddImportAction

use of com.intellij.codeInsight.daemon.impl.actions.AddImportAction in project intellij-community by JetBrains.

the class ImportClassFixBase method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) {
    if (!FileModificationService.getInstance().prepareFileForWrite(file))
        return;
    ApplicationManager.getApplication().runWriteAction(() -> {
        List<PsiClass> classesToImport = getClassesToImport(true);
        PsiClass[] classes = classesToImport.toArray(new PsiClass[classesToImport.size()]);
        if (classes.length == 0)
            return;
        AddImportAction action = createAddImportAction(classes, project, editor);
        action.execute();
    });
}
Also used : AddImportAction(com.intellij.codeInsight.daemon.impl.actions.AddImportAction)

Example 3 with AddImportAction

use of com.intellij.codeInsight.daemon.impl.actions.AddImportAction in project intellij-community by JetBrains.

the class AddModuleDependencyFix method addDependencyOnModule.

private void addDependencyOnModule(Project project, Editor editor, @Nullable Module module) {
    if (module == null)
        return;
    Couple<Module> circularModules = ModuleCompilerUtil.addingDependencyFormsCircularity(myCurrentModule, module);
    if (circularModules == null || showCircularWarning(project, circularModules, module)) {
        WriteAction.run(() -> {
            boolean test = ModuleRootManager.getInstance(myCurrentModule).getFileIndex().isInTestSourceContent(myRefVFile);
            DependencyScope scope = test ? DependencyScope.TEST : DependencyScope.COMPILE;
            JavaProjectModelModificationService.getInstance(project).addDependency(myCurrentModule, module, scope);
            if (editor != null && !myClasses.isEmpty()) {
                PsiClass[] targetClasses = myClasses.stream().filter(c -> ModuleUtilCore.findModuleForPsiElement(c) == module).toArray(PsiClass[]::new);
                if (targetClasses.length > 0 && !DumbService.isDumb(project)) {
                    new AddImportAction(project, myReference, editor, targetClasses).execute();
                }
            }
        });
    }
}
Also used : QuickFixBundle(com.intellij.codeInsight.daemon.QuickFixBundle) JBList(com.intellij.ui.components.JBList) DumbService(com.intellij.openapi.project.DumbService) java.util(java.util) WriteAction(com.intellij.openapi.application.WriteAction) Couple(com.intellij.openapi.util.Couple) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleCompilerUtil(com.intellij.compiler.ModuleCompilerUtil) ContainerUtil(com.intellij.util.containers.ContainerUtil) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) com.intellij.openapi.roots(com.intellij.openapi.roots) Nullable(org.jetbrains.annotations.Nullable) AddImportAction(com.intellij.codeInsight.daemon.impl.actions.AddImportAction) ModuleListCellRenderer(com.intellij.application.options.ModuleListCellRenderer) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) com.intellij.psi(com.intellij.psi) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) AddImportAction(com.intellij.codeInsight.daemon.impl.actions.AddImportAction) Module(com.intellij.openapi.module.Module)

Example 4 with AddImportAction

use of com.intellij.codeInsight.daemon.impl.actions.AddImportAction in project intellij-community by JetBrains.

the class MoveClassToModuleFix method moveClass.

private void moveClass(Project project, Editor editor, PsiFile file, PsiClass aClass) {
    RefactoringActionHandler moveHandler = RefactoringActionHandlerFactory.getInstance().createMoveHandler();
    DataManager dataManager = DataManager.getInstance();
    DataContext dataContext = dataManager.getDataContext();
    final String fqName = aClass.getQualifiedName();
    LOG.assertTrue(fqName != null);
    PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(myCurrentModule, StringUtil.getPackageName(fqName), mySourceRoot, true);
    DataContext context = SimpleDataContext.getSimpleContext(LangDataKeys.TARGET_PSI_ELEMENT.getName(), directory, dataContext);
    moveHandler.invoke(project, new PsiElement[] { aClass }, context);
    PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset());
    PsiClass newClass = JavaPsiFacade.getInstance(project).findClass(fqName, GlobalSearchScope.moduleScope(myCurrentModule));
    if (reference != null && newClass != null) {
        final QuestionAction action = new AddImportAction(project, reference, editor, newClass);
        action.execute();
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) QuestionAction(com.intellij.codeInsight.hint.QuestionAction) AddImportAction(com.intellij.codeInsight.daemon.impl.actions.AddImportAction) DataManager(com.intellij.ide.DataManager) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler)

Aggregations

AddImportAction (com.intellij.codeInsight.daemon.impl.actions.AddImportAction)4 Project (com.intellij.openapi.project.Project)2 ModuleListCellRenderer (com.intellij.application.options.ModuleListCellRenderer)1 QuickFixBundle (com.intellij.codeInsight.daemon.QuickFixBundle)1 QuestionAction (com.intellij.codeInsight.hint.QuestionAction)1 ModuleCompilerUtil (com.intellij.compiler.ModuleCompilerUtil)1 DataManager (com.intellij.ide.DataManager)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)1 WriteAction (com.intellij.openapi.application.WriteAction)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 Module (com.intellij.openapi.module.Module)1 ModuleUtilCore (com.intellij.openapi.module.ModuleUtilCore)1 DumbService (com.intellij.openapi.project.DumbService)1 com.intellij.openapi.roots (com.intellij.openapi.roots)1 Messages (com.intellij.openapi.ui.Messages)1 JBPopup (com.intellij.openapi.ui.popup.JBPopup)1 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)1 Couple (com.intellij.openapi.util.Couple)1