Search in sources :

Example 91 with Project

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

the class AntTasksProvider method getAntObjects.

private static Map<String, Class> getAntObjects(final GroovyFile groovyFile) {
    final Project project = groovyFile.getProject();
    final Module module = ModuleUtilCore.findModuleForPsiElement(groovyFile);
    Set<VirtualFile> jars = new HashSet<>();
    if (module != null) {
        ContainerUtil.addAll(jars, OrderEnumerator.orderEntries(module).getAllLibrariesAndSdkClassesRoots());
    }
    if (groovyFile.isScript() && GroovyScriptUtil.getScriptType(groovyFile) instanceof GantScriptType) {
        jars.addAll(GantScriptType.additionalScopeFiles(groovyFile));
    }
    final ArrayList<URL> urls = new ArrayList<>();
    for (VirtualFile jar : jars) {
        VirtualFile localFile = PathUtil.getLocalFile(jar);
        if (localFile.getFileSystem() instanceof LocalFileSystem) {
            urls.add(VfsUtilCore.convertToURL(localFile.getUrl()));
        }
    }
    AntClassLoader loader;
    synchronized (ourLock) {
        final Map<List<URL>, AntClassLoader> map = CachedValuesManager.getManager(project).getParameterizedCachedValue(project, KEY, PROVIDER, false, project);
        loader = map.get(urls);
        if (loader == null) {
            map.put(urls, loader = new AntClassLoader(urls));
        }
    }
    return loader.getAntObjects();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) URL(java.net.URL) Project(com.intellij.openapi.project.Project) ReflectedProject(com.intellij.lang.ant.ReflectedProject) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Module(com.intellij.openapi.module.Module)

Example 92 with Project

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

the class ConvertParameterToMapEntryIntention method performRefactoring.

private static void performRefactoring(final PsiElement element, final GrParametersOwner owner, final Collection<PsiElement> occurrences, final boolean createNewFirstParam, @Nullable final String mapParamName, final boolean specifyMapType) {
    final GrParameter param = getAppropriateParameter(element);
    assert param != null;
    final String paramName = param.getName();
    final String mapName = createNewFirstParam ? mapParamName : getFirstParameter(owner).getName();
    final Project project = element.getProject();
    final Runnable runnable = () -> {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
        final GrParameterList list = owner.getParameterList();
        assert list != null;
        final int index = list.getParameterNumber(param);
        if (!createNewFirstParam && index <= 0) {
            // bad undo
            return;
        }
        //final List<GrCall> calls = getCallOccurrences(occurrences);
        try {
            for (PsiElement occurrence : occurrences) {
                GrReferenceExpression refExpr = null;
                GroovyResolveResult resolveResult = null;
                boolean isExplicitGetterCall = false;
                if (occurrence instanceof GrReferenceExpression) {
                    final PsiElement parent = occurrence.getParent();
                    if (parent instanceof GrCall) {
                        refExpr = (GrReferenceExpression) occurrence;
                        resolveResult = refExpr.advancedResolve();
                        final PsiElement resolved = resolveResult.getElement();
                        if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyGetter(((PsiMethod) resolved)) && //check for explicit getter call
                        ((PsiMethod) resolved).getName().equals(refExpr.getReferenceName())) {
                            isExplicitGetterCall = true;
                        }
                    } else if (parent instanceof GrReferenceExpression) {
                        resolveResult = ((GrReferenceExpression) parent).advancedResolve();
                        final PsiElement resolved = resolveResult.getElement();
                        if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
                            refExpr = (GrReferenceExpression) parent;
                        }
                    }
                }
                if (refExpr == null)
                    continue;
                final GrClosureSignature signature = generateSignature(owner, refExpr);
                if (signature == null)
                    continue;
                GrCall call;
                if (isExplicitGetterCall) {
                    PsiElement parent = refExpr.getParent();
                    LOG.assertTrue(parent instanceof GrCall);
                    parent = parent.getParent();
                    if (parent instanceof GrReferenceExpression && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
                        parent = parent.getParent();
                    }
                    if (parent instanceof GrCall) {
                        call = (GrCall) parent;
                    } else {
                        continue;
                    }
                } else {
                    call = (GrCall) refExpr.getParent();
                }
                if (resolveResult.isInvokedOnProperty()) {
                    final PsiElement parent = call.getParent();
                    if (parent instanceof GrCall) {
                        call = (GrCall) parent;
                    } else if (parent instanceof GrReferenceExpression && parent.getParent() instanceof GrCall) {
                        final PsiElement resolved = ((GrReferenceExpression) parent).resolve();
                        if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
                            call = (GrCall) parent.getParent();
                        } else {
                            continue;
                        }
                    }
                }
                final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
                if (argInfos == null)
                    continue;
                final GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = argInfos[index];
                final GrNamedArgument namedArg;
                if (argInfo.isMultiArg) {
                    if (argInfo.args.isEmpty())
                        continue;
                    String arg = "[" + StringUtil.join(ContainerUtil.map(argInfo.args, element1 -> element1.getText()), ", ") + "]";
                    for (PsiElement psiElement : argInfo.args) {
                        psiElement.delete();
                    }
                    namedArg = factory.createNamedArgument(paramName, factory.createExpressionFromText(arg));
                } else {
                    if (argInfo.args.isEmpty())
                        continue;
                    final PsiElement argument = argInfo.args.iterator().next();
                    assert argument instanceof GrExpression;
                    namedArg = factory.createNamedArgument(paramName, (GrExpression) argument);
                    argument.delete();
                }
                call.addNamedArgument(namedArg);
            }
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
        //Replace of occurrences of old parameter in closure/method
        final Collection<PsiReference> references = ReferencesSearch.search(param).findAll();
        for (PsiReference ref : references) {
            final PsiElement elt = ref.getElement();
            if (elt instanceof GrReferenceExpression) {
                GrReferenceExpression expr = (GrReferenceExpression) elt;
                final GrExpression newExpr = factory.createExpressionFromText(mapName + "." + paramName);
                expr.replaceWithExpression(newExpr, true);
            }
        }
        //Add new map parameter to closure/method if it's necessary
        if (createNewFirstParam) {
            try {
                final GrParameter newParam = factory.createParameter(mapName, specifyMapType ? MAP_TYPE_TEXT : "", null);
                list.addAfter(newParam, null);
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }
        //Eliminate obsolete parameter from parameter list
        param.delete();
    };
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(runnable), REFACTORING_NAME, null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) Collection(java.util.Collection) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 93 with Project

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

the class AfterNewClassInsertHandler method generateAnonymousBody.

@Nullable
private static Runnable generateAnonymousBody(final Editor editor, final PsiFile file) {
    final Project project = file.getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    int offset = editor.getCaretModel().getOffset();
    PsiElement element = file.findElementAt(offset);
    if (element == null)
        return null;
    PsiElement parent = element.getParent().getParent();
    if (!(parent instanceof PsiAnonymousClass))
        return null;
    return ConstructorInsertHandler.genAnonymousBodyFor((PsiAnonymousClass) parent, editor, file, project);
}
Also used : Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 94 with Project

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

the class GroovyMethodSignatureInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    if (!(item.getObject() instanceof PsiMethod)) {
        return;
    }
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
    final Editor editor = context.getEditor();
    final PsiMethod method = (PsiMethod) item.getObject();
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    final StringBuilder buffer = new StringBuilder();
    final CharSequence chars = editor.getDocument().getCharsSequence();
    int endOffset = editor.getCaretModel().getOffset();
    final Project project = context.getProject();
    int afterSharp = CharArrayUtil.shiftBackwardUntil(chars, endOffset - 1, "#") + 1;
    int signatureOffset = afterSharp;
    PsiElement element = context.getFile().findElementAt(signatureOffset - 1);
    final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(element.getProject());
    PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class);
    if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
        final PsiDocTagValue value = tag.getValueElement();
        endOffset = value.getTextRange().getEndOffset();
    }
    editor.getDocument().deleteString(afterSharp, endOffset);
    editor.getCaretModel().moveToOffset(signatureOffset);
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    editor.getSelectionModel().removeSelection();
    buffer.append(method.getName()).append("(");
    final int afterParenth = afterSharp + buffer.length();
    for (int i = 0; i < parameters.length; i++) {
        final PsiType type = TypeConversionUtil.erasure(parameters[i].getType());
        buffer.append(type.getCanonicalText());
        if (i < parameters.length - 1) {
            buffer.append(",");
            if (styleSettings.SPACE_AFTER_COMMA)
                buffer.append(" ");
        }
    }
    buffer.append(")");
    if (!(tag instanceof PsiInlineDocTag)) {
        buffer.append(" ");
    } else {
        final int currentOffset = editor.getCaretModel().getOffset();
        if (chars.charAt(currentOffset) == '}') {
            afterSharp++;
        } else {
            buffer.append("} ");
        }
    }
    String insertString = buffer.toString();
    EditorModificationUtil.insertStringAtCaret(editor, insertString);
    editor.getCaretModel().moveToOffset(afterSharp + buffer.length());
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    shortenReferences(project, editor, context, afterParenth);
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PsiInlineDocTag(com.intellij.psi.javadoc.PsiInlineDocTag) Editor(com.intellij.openapi.editor.Editor)

Example 95 with Project

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

the class GantRunner method ensureRunnerConfigured.

@Override
public void ensureRunnerConfigured(@NotNull GroovyScriptRunConfiguration configuration) throws RuntimeConfigurationException {
    Project project = configuration.getProject();
    if (GantUtils.getSDKInstallPath(configuration.getModule(), project).isEmpty()) {
        RuntimeConfigurationException e = new RuntimeConfigurationException("Gant is not configured");
        e.setQuickFix(() -> ShowSettingsUtil.getInstance().editConfigurable(project, new GantConfigurable(project)));
        throw e;
    }
}
Also used : Project(com.intellij.openapi.project.Project) RuntimeConfigurationException(com.intellij.execution.configurations.RuntimeConfigurationException)

Aggregations

Project (com.intellij.openapi.project.Project)3623 VirtualFile (com.intellij.openapi.vfs.VirtualFile)874 NotNull (org.jetbrains.annotations.NotNull)580 Nullable (org.jetbrains.annotations.Nullable)478 Module (com.intellij.openapi.module.Module)368 PsiFile (com.intellij.psi.PsiFile)334 Editor (com.intellij.openapi.editor.Editor)301 PsiElement (com.intellij.psi.PsiElement)292 ArrayList (java.util.ArrayList)214 File (java.io.File)212 Document (com.intellij.openapi.editor.Document)180 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)172 List (java.util.List)158 IOException (java.io.IOException)107 TextRange (com.intellij.openapi.util.TextRange)99 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)96 IncorrectOperationException (com.intellij.util.IncorrectOperationException)95 Presentation (com.intellij.openapi.actionSystem.Presentation)94 DataContext (com.intellij.openapi.actionSystem.DataContext)92 PsiDirectory (com.intellij.psi.PsiDirectory)90