Search in sources :

Example 11 with Ref

use of com.intellij.openapi.util.Ref in project intellij-community by JetBrains.

the class GrCreateSubclassAction method createSubclassGroovy.

@Nullable
public static PsiClass createSubclassGroovy(final GrTypeDefinition psiClass, final PsiDirectory targetDirectory, final String className) {
    final Project project = psiClass.getProject();
    final Ref<GrTypeDefinition> targetClass = new Ref<>();
    new WriteCommandAction(project, getTitle(psiClass), getTitle(psiClass)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
            final GrTypeParameterList oldTypeParameterList = psiClass.getTypeParameterList();
            try {
                targetClass.set(CreateClassActionBase.createClassByType(targetDirectory, className, PsiManager.getInstance(project), psiClass, GroovyTemplates.GROOVY_CLASS, true));
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(project, CodeInsightBundle.message("intention.error.cannot.create.class.message", className) + "\n" + e.getLocalizedMessage(), CodeInsightBundle.message("intention.error.cannot.create.class.title")));
                return;
            }
            startTemplate(oldTypeParameterList, project, psiClass, targetClass.get(), false);
        }
    }.execute();
    if (targetClass.get() == null)
        return null;
    if (!ApplicationManager.getApplication().isUnitTestMode() && !psiClass.hasTypeParameters()) {
        final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.get().getContainingFile(), targetClass.get());
        if (editor == null)
            return targetClass.get();
        chooseAndImplement(psiClass, project, targetClass.get(), editor);
    }
    return targetClass.get();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) Ref(com.intellij.openapi.util.Ref) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with Ref

use of com.intellij.openapi.util.Ref in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method findUsagesForLocal.

private static Collection<PsiReference> findUsagesForLocal(GrClosableBlock initializer, final GrVariable var) {
    final Instruction[] flow = ControlFlowUtils.findControlFlowOwner(initializer).getControlFlow();
    final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, var);
    Instruction writeInstr = null;
    final PsiElement parent = initializer.getParent();
    if (parent instanceof GrVariable) {
        writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == var);
    } else if (parent instanceof GrAssignmentExpression) {
        final GrReferenceExpression refExpr = (GrReferenceExpression) ((GrAssignmentExpression) parent).getLValue();
        final Instruction instruction = ContainerUtil.find(flow, instruction1 -> instruction1.getElement() == refExpr);
        LOG.assertTrue(instruction != null);
        final BitSet prev = writes.get(instruction.num());
        if (prev.cardinality() == 1) {
            writeInstr = flow[prev.nextSetBit(0)];
        }
    }
    LOG.assertTrue(writeInstr != null);
    Collection<PsiReference> result = new ArrayList<>();
    for (Instruction instruction : flow) {
        if (!(instruction instanceof ReadWriteVariableInstruction))
            continue;
        if (((ReadWriteVariableInstruction) instruction).isWrite())
            continue;
        final PsiElement element = instruction.getElement();
        if (element instanceof GrVariable && element != var)
            continue;
        if (!(element instanceof GrReferenceExpression))
            continue;
        final GrReferenceExpression ref = (GrReferenceExpression) element;
        if (ref.isQualified() || ref.resolve() != var)
            continue;
        final BitSet prev = writes.get(instruction.num());
        if (prev.cardinality() == 1 && prev.get(writeInstr.num())) {
            result.add(ref);
        }
    }
    return result;
}
Also used : IntroduceParameterRefactoring(com.intellij.refactoring.IntroduceParameterRefactoring) PsiImplUtil(org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Collection(java.util.Collection) Nullable(org.jetbrains.annotations.Nullable) org.jetbrains.plugins.groovy.lang.psi.api.statements(org.jetbrains.plugins.groovy.lang.psi.api.statements) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) List(java.util.List) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) DescriptiveNameUtil(com.intellij.lang.findUsages.DescriptiveNameUtil) ChangedMethodCallInfo(com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ArrayUtil(com.intellij.util.ArrayUtil) PsiUtilBase(com.intellij.psi.util.PsiUtilBase) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) ChangeContextUtil(com.intellij.codeInsight.ChangeContextUtil) UsageViewDescriptorAdapter(com.intellij.refactoring.ui.UsageViewDescriptorAdapter) UsageInfo(com.intellij.usageView.UsageInfo) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) ArrayList(java.util.ArrayList) OldReferencesResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver) GroovyRefactoringUtil(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) Editor(com.intellij.openapi.editor.Editor) UsageViewUtil(com.intellij.usageView.UsageViewUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) ExpressionConverter(com.intellij.psi.impl.ExpressionConverter) TIntProcedure(gnu.trove.TIntProcedure) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions) BitSet(java.util.BitSet) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)

Example 13 with Ref

use of com.intellij.openapi.util.Ref in project intellij-community by JetBrains.

the class CustomCreateProperty method generateCreateComponentsMethod.

public static void generateCreateComponentsMethod(final PsiClass aClass) {
    final PsiFile psiFile = aClass.getContainingFile();
    if (psiFile == null)
        return;
    final VirtualFile vFile = psiFile.getVirtualFile();
    if (vFile == null)
        return;
    if (!FileModificationService.getInstance().prepareFileForWrite(psiFile))
        return;
    final Ref<SmartPsiElementPointer> refMethod = new Ref<>();
    CommandProcessor.getInstance().executeCommand(aClass.getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
        PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
        try {
            PsiMethod method = factory.createMethodFromText("private void " + AsmCodeGenerator.CREATE_COMPONENTS_METHOD_NAME + "() { \n // TODO: place custom component creation code here \n }", aClass);
            final PsiMethod psiMethod = (PsiMethod) aClass.add(method);
            refMethod.set(SmartPointerManager.getInstance(aClass.getProject()).createSmartPsiElementPointer(psiMethod));
            CodeStyleManager.getInstance(aClass.getProject()).reformat(psiMethod);
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }), null, null);
    if (!refMethod.isNull()) {
        SwingUtilities.invokeLater(() -> {
            final PsiMethod element = (PsiMethod) refMethod.get().getElement();
            if (element != null) {
                final PsiCodeBlock body = element.getBody();
                assert body != null;
                final PsiComment comment = PsiTreeUtil.getChildOfType(body, PsiComment.class);
                if (comment != null) {
                    new OpenFileDescriptor(comment.getProject(), vFile, comment.getTextOffset()).navigate(true);
                }
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Ref(com.intellij.openapi.util.Ref) IncorrectOperationException(com.intellij.util.IncorrectOperationException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 14 with Ref

use of com.intellij.openapi.util.Ref in project intellij-community by JetBrains.

the class CreateClassToBindFix method createBoundFields.

private void createBoundFields(final PsiClass formClass) throws IncorrectOperationException {
    final Module module = myEditor.getRootContainer().getModule();
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
    final PsiManager psiManager = PsiManager.getInstance(myEditor.getProject());
    final Ref<IncorrectOperationException> exception = new Ref<>();
    FormEditingUtil.iterate(myEditor.getRootContainer(), new FormEditingUtil.ComponentVisitor() {

        public boolean visit(final IComponent component) {
            if (component.getBinding() != null) {
                final PsiClass fieldClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope);
                if (fieldClass != null) {
                    PsiType fieldType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(fieldClass);
                    try {
                        PsiField field = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createField(component.getBinding(), fieldType);
                        formClass.add(field);
                    } catch (IncorrectOperationException e) {
                        exception.set(e);
                        return false;
                    }
                }
            }
            return true;
        }
    });
    if (!exception.isNull()) {
        throw exception.get();
    }
}
Also used : Ref(com.intellij.openapi.util.Ref) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IComponent(com.intellij.uiDesigner.lw.IComponent) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) FormEditingUtil(com.intellij.uiDesigner.FormEditingUtil)

Example 15 with Ref

use of com.intellij.openapi.util.Ref in project intellij-community by JetBrains.

the class PyAbstractTestProcessRunner method rerunFailedTests.

/**
   * Rerun current tests. Make sure there is at least one failed test.
   * <strong>Run in AWT thread only!</strong>
   */
public void rerunFailedTests() {
    assert getFailedTestsCount() > 0 : "No failed tests. What you want to rerun?";
    assert myLastProcessDescriptor != null : "No last run descriptor. First run tests at least one time";
    final List<ProgramRunner<?>> run = getAvailableRunnersForLastRun();
    Assert.assertFalse("No runners to rerun", run.isEmpty());
    final ProgramRunner<?> runner = run.get(0);
    final ExecutionEnvironment restartAction = RerunFailedActionsTestTools.findRestartAction(myLastProcessDescriptor);
    Assert.assertNotNull("No restart action", restartAction);
    final Ref<ProcessHandler> handlerRef = new Ref<>();
    try {
        runner.execute(restartAction, descriptor -> handlerRef.set(descriptor.getProcessHandler()));
    } catch (final ExecutionException e) {
        throw new AssertionError("ExecutionException can't be thrown in tests. Probably, API changed. Got: " + e);
    }
    final ProcessHandler handler = handlerRef.get();
    if (handler == null) {
        return;
    }
    handler.waitFor();
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) Ref(com.intellij.openapi.util.Ref) ProcessHandler(com.intellij.execution.process.ProcessHandler) ProgramRunner(com.intellij.execution.runners.ProgramRunner) ExecutionException(com.intellij.execution.ExecutionException)

Aggregations

Ref (com.intellij.openapi.util.Ref)253 Nullable (org.jetbrains.annotations.Nullable)82 NotNull (org.jetbrains.annotations.NotNull)70 VirtualFile (com.intellij.openapi.vfs.VirtualFile)48 Project (com.intellij.openapi.project.Project)38 List (java.util.List)30 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)26 PsiElement (com.intellij.psi.PsiElement)26 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)22 PsiFile (com.intellij.psi.PsiFile)20 Module (com.intellij.openapi.module.Module)18 Editor (com.intellij.openapi.editor.Editor)16 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)16 File (java.io.File)16 Task (com.intellij.openapi.progress.Task)15 Pair (com.intellij.openapi.util.Pair)14 VcsException (com.intellij.openapi.vcs.VcsException)14 IncorrectOperationException (com.intellij.util.IncorrectOperationException)14 Document (com.intellij.openapi.editor.Document)13