Search in sources :

Example 1 with DocStringFormat

use of com.jetbrains.python.documentation.docstrings.DocStringFormat in project intellij-community by JetBrains.

the class PyIntegratedToolsProjectConfigurator method checkDocstring.

@NotNull
private static DocStringFormat checkDocstring(@NotNull VirtualFile file, @NotNull Module module) {
    final PsiFile psiFile = PsiManager.getInstance(module.getProject()).findFile(file);
    if (psiFile instanceof PyFile) {
        final DocStringFormat perFileFormat = PyDocumentationSettings.getFormatFromDocformatAttribute(psiFile);
        if (perFileFormat != null) {
            return perFileFormat;
        }
        // Why toplevel docstring owners only
        final PyDocStringOwner[] children = PsiTreeUtil.getChildrenOfType(psiFile, PyDocStringOwner.class);
        if (children != null) {
            for (PyDocStringOwner owner : children) {
                final PyStringLiteralExpression docStringExpression = owner.getDocStringExpression();
                if (docStringExpression != null) {
                    final DocStringFormat guessed = DocStringUtil.guessDocStringFormat(docStringExpression.getStringValue());
                    if (guessed != DocStringFormat.PLAIN) {
                        return guessed;
                    }
                }
            }
        }
    }
    return DocStringFormat.PLAIN;
}
Also used : DocStringFormat(com.jetbrains.python.documentation.docstrings.DocStringFormat) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DocStringFormat

use of com.jetbrains.python.documentation.docstrings.DocStringFormat in project intellij-community by JetBrains.

the class PyTestCase method runWithDocStringFormat.

protected void runWithDocStringFormat(@NotNull DocStringFormat format, @NotNull Runnable runnable) {
    final PyDocumentationSettings settings = PyDocumentationSettings.getInstance(myFixture.getModule());
    final DocStringFormat oldFormat = settings.getFormat();
    settings.setFormat(format);
    try {
        runnable.run();
    } finally {
        settings.setFormat(oldFormat);
    }
}
Also used : DocStringFormat(com.jetbrains.python.documentation.docstrings.DocStringFormat) PyDocumentationSettings(com.jetbrains.python.documentation.PyDocumentationSettings)

Example 3 with DocStringFormat

use of com.jetbrains.python.documentation.docstrings.DocStringFormat in project intellij-community by JetBrains.

the class PyIntegratedToolsProjectConfigurator method updateIntegratedTools.

private static void updateIntegratedTools(final Module module, final int delay) {
    ModalityState modality = ModalityState.current();
    final PyDocumentationSettings docSettings = PyDocumentationSettings.getInstance(module);
    AppExecutorUtil.getAppScheduledExecutorService().schedule(() -> ApplicationManager.getApplication().invokeLater(() -> {
        LOG.debug("Integrated tools configurator has started");
        if (module.isDisposed())
            return;
        @NotNull DocStringFormat docFormat = DocStringFormat.PLAIN;
        //check setup.py
        @NotNull String testRunner = detectTestRunnerFromSetupPy(module);
        if (!testRunner.isEmpty()) {
            LOG.debug("Test runner '" + testRunner + "' was discovered from setup.py in the module '" + module.getModuleFilePath() + "'");
        }
        //try to find test_runner import
        final String extension = PythonFileType.INSTANCE.getDefaultExtension();
        // Module#getModuleScope() and GlobalSearchScope#getModuleScope() search only in source roots
        final GlobalSearchScope searchScope = module.getModuleScope();
        final Collection<VirtualFile> pyFiles = FilenameIndex.getAllFilesByExt(module.getProject(), extension, searchScope);
        for (VirtualFile file : pyFiles) {
            if (file.getName().startsWith("test")) {
                if (testRunner.isEmpty()) {
                    //find test runner import
                    testRunner = checkImports(file, module);
                    if (!testRunner.isEmpty()) {
                        LOG.debug("Test runner '" + testRunner + "' was detected from imports in the file '" + file.getPath() + "'");
                    }
                }
            } else if (docFormat == DocStringFormat.PLAIN) {
                // detect docstring type
                docFormat = checkDocstring(file, module);
                if (docFormat != DocStringFormat.PLAIN) {
                    LOG.debug("Docstring format '" + docFormat + "' was detected from content of the file '" + file.getPath() + "'");
                }
            }
            if (!testRunner.isEmpty() && docFormat != DocStringFormat.PLAIN) {
                break;
            }
        }
        // Check test runners available in the module SDK
        if (testRunner.isEmpty()) {
            //check if installed in sdk
            final Sdk sdk = PythonSdkType.findPythonSdk(module);
            if (sdk != null && sdk.getSdkType() instanceof PythonSdkType) {
                final List<PyPackage> packages = PyPackageUtil.refreshAndGetPackagesModally(sdk);
                if (packages != null) {
                    final boolean nose = PyPackageUtil.findPackage(packages, PyNames.NOSE_TEST) != null;
                    final boolean pytest = PyPackageUtil.findPackage(packages, PyNames.PY_TEST) != null;
                    if (nose)
                        testRunner = PythonTestConfigurationsModel.PYTHONS_NOSETEST_NAME;
                    else if (pytest)
                        testRunner = PythonTestConfigurationsModel.PY_TEST_NAME;
                    if (!testRunner.isEmpty()) {
                        LOG.debug("Test runner '" + testRunner + "' was detected from SDK " + sdk);
                    }
                }
            }
        }
        final TestRunnerService runnerService = TestRunnerService.getInstance(module);
        if (runnerService != null) {
            if (testRunner.isEmpty()) {
                runnerService.setProjectConfiguration(PythonTestConfigurationsModel.PYTHONS_UNITTEST_NAME);
            } else {
                runnerService.setProjectConfiguration(testRunner);
                LOG.info("Test runner '" + testRunner + "' was detected by project configurator");
            }
        }
        // Documentation settings should have meaningful default already
        if (docFormat != DocStringFormat.PLAIN) {
            docSettings.setFormat(docFormat);
            LOG.info("Docstring format '" + docFormat + "' was detected by project configurator");
        }
    }, modality), delay, TimeUnit.MILLISECONDS);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocStringFormat(com.jetbrains.python.documentation.docstrings.DocStringFormat) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ModalityState(com.intellij.openapi.application.ModalityState) Collection(java.util.Collection) List(java.util.List) Sdk(com.intellij.openapi.projectRoots.Sdk) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) PyDocumentationSettings(com.jetbrains.python.documentation.PyDocumentationSettings)

Aggregations

DocStringFormat (com.jetbrains.python.documentation.docstrings.DocStringFormat)3 PyDocumentationSettings (com.jetbrains.python.documentation.PyDocumentationSettings)2 ModalityState (com.intellij.openapi.application.ModalityState)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 PythonSdkType (com.jetbrains.python.sdk.PythonSdkType)1 Collection (java.util.Collection)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1