Search in sources :

Example 86 with PsiPackage

use of com.intellij.psi.PsiPackage in project markdown-doclet by Abnaxos.

the class MarkdownDocumentationProvider method generateDoc.

@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    boolean process = false;
    for (Class supported : SUPPORTED_ELEMENT_TYPES) {
        if (supported.isInstance(element)) {
            process = true;
            break;
        }
    }
    if (!process) {
        return null;
    }
    PsiFile file = null;
    if (element instanceof PsiDirectory) {
        // let's see whether we can map the directory to a package; if so, change the
        // element to the package and continue
        PsiPackage pkg = JavaDirectoryService.getInstance().getPackage((PsiDirectory) element);
        if (pkg != null) {
            element = pkg;
        } else {
            return null;
        }
    }
    if (element instanceof PsiPackage) {
        for (PsiDirectory dir : ((PsiPackage) element).getDirectories()) {
            PsiFile info = dir.findFile(PsiPackage.PACKAGE_INFO_FILE);
            if (info != null) {
                ASTNode node = info.getNode();
                if (node != null) {
                    ASTNode docCommentNode = node.findChildByType(JavaDocElementType.DOC_COMMENT);
                    if (docCommentNode != null) {
                        // the default implementation will now use this file
                        // we're going to take over below, if Markdown is enabled in
                        // the corresponding module
                        // see JavaDocInfoGenerator.generatePackageJavaDoc()
                        file = info;
                        break;
                    }
                }
            }
            if (dir.findFile("package.html") != null) {
                // leave that to the default
                return null;
            }
        }
    } else {
        if (JavaLanguage.INSTANCE.equals(element.getLanguage())) {
            element = element.getNavigationElement();
            if (element.getContainingFile() != null) {
                file = element.getContainingFile();
            }
        }
    }
    if (file != null) {
        DocCommentProcessor processor = new DocCommentProcessor(file);
        if (processor.isEnabled()) {
            String docHtml;
            if (element instanceof PsiMethod) {
                docHtml = super.generateDoc(PsiProxy.forMethod((PsiMethod) element), originalElement);
            } else if (element instanceof PsiParameter) {
                docHtml = super.generateDoc(PsiProxy.forParameter((PsiParameter) element), originalElement);
            } else {
                MarkdownJavaDocInfoGenerator javaDocInfoGenerator = new MarkdownJavaDocInfoGenerator(element.getProject(), element, processor);
                List<String> docURLs = getExternalJavaDocUrl(element);
                String text = javaDocInfoGenerator.generateDocInfo(docURLs);
                Plugin.print("Intermediate HTML output", text);
                docHtml = JavaDocExternalFilter.filterInternalDocInfo(text);
            }
            docHtml = extendCss(docHtml);
            Plugin.print("Final HTML output", docHtml);
            return docHtml;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) PsiDirectory(com.intellij.psi.PsiDirectory) ASTNode(com.intellij.lang.ASTNode) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) PsiPackage(com.intellij.psi.PsiPackage) List(java.util.List) Nullable(org.jetbrains.annotations.Nullable)

Example 87 with PsiPackage

use of com.intellij.psi.PsiPackage in project azure-tools-for-java by Microsoft.

the class CreateFunctionAction method invokeDialog.

@Override
protected PsiElement[] invokeDialog(Project project, PsiDirectory psiDirectory) {
    final Operation operation = TelemetryManager.createOperation(TelemetryConstants.FUNCTION, TelemetryConstants.CREATE_FUNCTION_TRIGGER);
    try {
        operation.start();
        PsiPackage pkg = JavaDirectoryService.getInstance().getPackage(psiDirectory);
        // get existing package from current directory
        String hintPackageName = pkg == null ? "" : pkg.getQualifiedName();
        CreateFunctionForm form = new CreateFunctionForm(project, hintPackageName);
        List<PsiElement> psiElements = new ArrayList<>();
        if (form.showAndGet()) {
            final FunctionTemplate bindingTemplate;
            try {
                Map<String, String> parameters = form.getTemplateParameters();
                final String connectionName = parameters.get("connection");
                String triggerType = form.getTriggerType();
                String packageName = parameters.get("packageName");
                String className = parameters.get("className");
                PsiDirectory directory = ClassUtil.sourceRoot(psiDirectory);
                String newName = packageName.replace('.', '/');
                bindingTemplate = AzureFunctionsUtils.getFunctionTemplate(triggerType);
                operation.trackProperty(TelemetryConstants.TRIGGER_TYPE, triggerType);
                if (StringUtils.equalsIgnoreCase(triggerType, CreateFunctionForm.EVENT_HUB_TRIGGER)) {
                    if (StringUtils.isBlank(connectionName)) {
                        throw new AzureExecutionException(message("function.createFunction.error.connectionMissed"));
                    }
                    parameters.putIfAbsent("eventHubName", "myeventhub");
                    parameters.putIfAbsent("consumerGroup", "$Default");
                }
                final String functionClassContent = AzureFunctionsUtils.substituteParametersInTemplate(bindingTemplate, parameters);
                if (StringUtils.isNotEmpty(functionClassContent)) {
                    AzureTaskManager.getInstance().write(() -> {
                        CreateFileAction.MkDirs mkDirs = ApplicationManager.getApplication().runWriteAction((Computable<CreateFileAction.MkDirs>) () -> new CreateFileAction.MkDirs(newName + '/' + className, directory));
                        PsiFileFactory factory = PsiFileFactory.getInstance(project);
                        try {
                            mkDirs.directory.checkCreateFile(className + ".java");
                        } catch (final IncorrectOperationException e) {
                            final String dir = mkDirs.directory.getName();
                            final String error = String.format("failed to create function class[%s] in directory[%s]", className, dir);
                            throw new AzureToolkitRuntimeException(error, e);
                        }
                        CommandProcessor.getInstance().executeCommand(project, () -> {
                            PsiFile psiFile = factory.createFileFromText(className + ".java", JavaFileType.INSTANCE, functionClassContent);
                            psiElements.add(mkDirs.directory.add(psiFile));
                        }, null, null);
                        if (StringUtils.equalsIgnoreCase(triggerType, CreateFunctionForm.EVENT_HUB_TRIGGER)) {
                            try {
                                String connectionString = form.getEventHubNamespace() == null ? DEFAULT_EVENT_HUB_CONNECTION_STRING : getEventHubNamespaceConnectionString(form.getEventHubNamespace());
                                AzureFunctionsUtils.applyKeyValueToLocalSettingFile(new File(project.getBasePath(), "local.settings.json"), parameters.get("connection"), connectionString);
                            } catch (IOException e) {
                                EventUtil.logError(operation, ErrorType.systemError, e, null, null);
                                final String error = "failed to get connection string and save to local settings";
                                throw new AzureToolkitRuntimeException(error, e);
                            }
                        }
                    });
                }
            } catch (AzureExecutionException e) {
                AzureMessager.getMessager().error(e);
                EventUtil.logError(operation, ErrorType.systemError, e, null, null);
            }
        }
        if (!psiElements.isEmpty()) {
            FileEditorManager.getInstance(project).openFile(psiElements.get(0).getContainingFile().getVirtualFile(), false);
        }
        return psiElements.toArray(new PsiElement[0]);
    } finally {
        operation.complete();
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) ArrayList(java.util.ArrayList) PsiPackage(com.intellij.psi.PsiPackage) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) IOException(java.io.IOException) FunctionTemplate(com.microsoft.azure.toolkit.lib.legacy.function.template.FunctionTemplate) PsiDirectory(com.intellij.psi.PsiDirectory) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) CreateFunctionForm(com.microsoft.azure.toolkit.intellij.function.CreateFunctionForm) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiFile(com.intellij.psi.PsiFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) CreateFileAction(com.intellij.ide.actions.CreateFileAction)

Example 88 with PsiPackage

use of com.intellij.psi.PsiPackage in project azure-tools-for-java by Microsoft.

the class CreateFunctionAction method doCheckPackageExists.

private static boolean doCheckPackageExists(PsiDirectory directory) {
    PsiPackage pkg = JavaDirectoryService.getInstance().getPackage(directory);
    if (pkg == null) {
        return false;
    }
    String name = pkg.getQualifiedName();
    return StringUtil.isEmpty(name) || PsiNameHelper.getInstance(directory.getProject()).isQualifiedName(name);
}
Also used : PsiPackage(com.intellij.psi.PsiPackage)

Example 89 with PsiPackage

use of com.intellij.psi.PsiPackage in project moe-ide-integration by multi-os-engine.

the class MOEJUnitUtil method getModelPackagesWithName.

public static Vector<PsiPackage> getModelPackagesWithName(Module myModule, final String packageRootName) {
    final Vector<PsiPackage> packages = new Vector<PsiPackage>();
    final PsiManager psiManager = PsiManager.getInstance(myModule.getProject());
    final FileIndex fileIndex = ModuleRootManager.getInstance(myModule).getFileIndex();
    fileIndex.iterateContent(new ContentIterator() {

        public boolean processFile(VirtualFile fileOrDir) {
            if (fileOrDir.isDirectory() && fileIndex.isUnderSourceRootOfType(fileOrDir, JavaModuleSourceRootTypes.SOURCES)) {
                final PsiDirectory psiDirectory = psiManager.findDirectory(fileOrDir);
                if (psiDirectory != null) {
                    PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
                    if (aPackage != null) {
                        String actName = aPackage.getQualifiedName();
                        if (actName.equals(packageRootName) || actName.startsWith(packageRootName)) {
                            packages.add(aPackage);
                        }
                    }
                }
            }
            return true;
        }
    });
    return packages;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileIndex(com.intellij.openapi.roots.FileIndex) ContentIterator(com.intellij.openapi.roots.ContentIterator) PsiDirectory(com.intellij.psi.PsiDirectory) PsiManager(com.intellij.psi.PsiManager) PsiPackage(com.intellij.psi.PsiPackage) Vector(java.util.Vector)

Example 90 with PsiPackage

use of com.intellij.psi.PsiPackage in project moe-ide-integration by multi-os-engine.

the class MOEJUnitUtil method getTestClasses.

public static Vector<PsiClass> getTestClasses(Module myModule, final String packageRootName) {
    Vector<PsiPackage> packages = getModelPackagesWithName(myModule, packageRootName);
    Vector<PsiClass> testClasses = new Vector<PsiClass>();
    Iterator<PsiPackage> it = packages.iterator();
    while (it.hasNext()) {
        PsiPackage p = it.next();
        PsiClass[] packageClasses = p.getClasses();
        if (packageClasses != null && packageClasses.length > 0) {
            for (PsiClass c : packageClasses) {
                if (JUnitUtil.isTestClass(c)) {
                    testClasses.add(c);
                }
            }
        }
    }
    return testClasses;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage) Vector(java.util.Vector)

Aggregations

PsiPackage (com.intellij.psi.PsiPackage)93 PsiDirectory (com.intellij.psi.PsiDirectory)24 PsiClass (com.intellij.psi.PsiClass)22 Module (com.intellij.openapi.module.Module)19 Project (com.intellij.openapi.project.Project)14 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)14 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 JavaAnalysisScope (com.intellij.analysis.JavaAnalysisScope)12 PsiFile (com.intellij.psi.PsiFile)12 PsiElement (com.intellij.psi.PsiElement)11 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)10 NotNull (org.jetbrains.annotations.NotNull)10 CyclicDependenciesBuilder (com.intellij.cyclicDependencies.CyclicDependenciesBuilder)9 Nullable (org.jetbrains.annotations.Nullable)9 AnalysisScope (com.intellij.analysis.AnalysisScope)8 HashSet (java.util.HashSet)6 List (java.util.List)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)4 ArrayList (java.util.ArrayList)4