Search in sources :

Example 36 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyTreeStructureProvider method getTopLevelElement.

@Override
public PsiElement getTopLevelElement(PsiElement element) {
    PyPsiUtils.assertValid(element);
    final Ref<PsiFile> containingFileRef = Ref.create();
    ApplicationManager.getApplication().runReadAction(() -> containingFileRef.set(element.getContainingFile()));
    final PsiFile containingFile = containingFileRef.get();
    if (!(containingFile instanceof PyFile)) {
        return null;
    }
    List<PsiElement> parents = new ArrayList<>();
    PyDocStringOwner container = PsiTreeUtil.getParentOfType(element, PyDocStringOwner.class);
    while (container != null) {
        if (container instanceof PyFile) {
            break;
        }
        parents.add(0, container);
        container = PsiTreeUtil.getParentOfType(container, PyDocStringOwner.class);
    }
    for (PsiElement parent : parents) {
        if (parent instanceof PyFunction) {
            // we don't display any nodes under functions
            return parent;
        }
    }
    if (parents.size() > 0) {
        return parents.get(parents.size() - 1);
    }
    return element.getContainingFile();
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) ArrayList(java.util.ArrayList) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement)

Example 37 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyExtractSuperclassPresenterImpl method validateView.

@Override
protected void validateView() throws BadDataException {
    super.validateView();
    final Project project = myClassUnderRefactoring.getProject();
    if (!myNamesValidator.isIdentifier(myView.getSuperClassName(), project)) {
        throw new BadDataException(PyBundle.message("refactoring.extract.super.name.0.must.be.ident", myView.getSuperClassName()));
    }
    boolean rootFound = false;
    final File moduleFile = new File(myView.getModuleFile());
    try {
        final String targetDir = FileUtil.toSystemIndependentName(moduleFile.getCanonicalPath());
        for (final VirtualFile file : ProjectRootManager.getInstance(project).getContentRoots()) {
            if (StringUtil.startsWithIgnoreCase(targetDir, file.getPath())) {
                rootFound = true;
                break;
            }
        }
    } catch (final IOException ignore) {
    }
    if (!rootFound) {
        throw new BadDataException(PyBundle.message("refactoring.extract.super.target.path.outside.roots"));
    }
    // TODO: Cover with test. It can't be done for now, because testFixture reports root path incorrectly
    // PY-12173
    myView.getModuleFile();
    final VirtualFile moduleVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(moduleFile);
    if (moduleVirtualFile != null) {
        final PsiFile psiFile = PsiManager.getInstance(project).findFile(moduleVirtualFile);
        if (psiFile instanceof PyFile) {
            if (((PyFile) psiFile).findTopLevelClass(myView.getSuperClassName()) != null) {
                throw new BadDataException(PyBundle.message("refactoring.extract.super.target.class.already.exists", myView.getSuperClassName()));
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) BadDataException(com.jetbrains.python.refactoring.classes.membersManager.vp.BadDataException) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) PyFile(com.jetbrains.python.psi.PyFile) PyFile(com.jetbrains.python.psi.PyFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 38 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyIronPythonTest method testImportBuiltInSystem.

/**
   * Tests skeletons for built-in classes. We can't compare files (CLR class may be changed from version to version),
   * but we are sure there should be class System.Web.AspNetHostingPermissionLevel which is part of public API
   */
@Test
public void testImportBuiltInSystem() throws Exception {
    final SkeletonTestTask task = new SkeletonTestTask(null, "System.Web", "import_system.py", null) {

        @Override
        public void runTestOn(@NotNull final String sdkHome) throws IOException, InvalidSdkException {
            super.runTestOn(sdkHome);
            ApplicationManager.getApplication().runReadAction(() -> {
                final PyFile skeleton = (PyFile) myFixture.getFile();
                Assert.assertNotNull("System.Web does not contain class AspNetHostingPermissionLevel. Error generating stub? It has classes  " + skeleton.getTopLevelClasses(), skeleton.findTopLevelClass("AspNetHostingPermissionLevel"));
            });
        }
    };
    runPythonTest(task);
}
Also used : PyFile(com.jetbrains.python.psi.PyFile) NotNull(org.jetbrains.annotations.NotNull) Test(org.junit.Test)

Example 39 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyDeprecationTest method testFileStub.

public void testFileStub() {
    myFixture.configureByFile("deprecation/deprecatedModule.py");
    PyFile file = (PyFile) myFixture.getFile();
    assertEquals("the deprecated module is deprecated; use a non-deprecated module instead", file.getDeprecationMessage());
    PlatformTestUtil.tryGcSoftlyReachableObjects();
    assertNotParsed(file);
    assertEquals("the deprecated module is deprecated; use a non-deprecated module instead", file.getDeprecationMessage());
    assertNotParsed(file);
}
Also used : PyFile(com.jetbrains.python.psi.PyFile)

Example 40 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyBlockEvaluatorTest method testImport.

/**
   * Ensures module has any vars imported from external modules
   */
public void testImport() {
    myFixture.copyDirectoryToProject("blockEvaluator", "");
    final PyFile file = PyUtil.as(myFixture.configureByFile("my_module.py"), PyFile.class);
    assert file != null : "Failed to read file";
    final PyBlockEvaluator sut = new PyBlockEvaluator();
    sut.evaluate(file);
    Assert.assertEquals("Failed to read var from package module", "foo", sut.getValueAsString("VARIABLE_IN_PACKAGE_MODULE"));
    Assert.assertEquals("Failed to read var from package", "foo", sut.getValueAsString("VARIABLE_IN_PACKAGE"));
    Assert.assertEquals("Failed to read list from another module", Arrays.asList("a", "b", "c", "d"), sut.getValueAsList("SOME_LIST"));
    Assert.assertEquals("Failed to read var from another module", "42", sut.getValueAsString("SOME_VARIABLE"));
    Assert.assertEquals("Failed to read var from another module with alias", "foo", sut.getValueAsString("MY_RENAMED_VAR"));
}
Also used : PyBlockEvaluator(com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator) PyFile(com.jetbrains.python.psi.PyFile)

Aggregations

PyFile (com.jetbrains.python.psi.PyFile)42 PsiElement (com.intellij.psi.PsiElement)15 PsiFile (com.intellij.psi.PsiFile)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 Project (com.intellij.openapi.project.Project)9 PyFunction (com.jetbrains.python.psi.PyFunction)9 PyClass (com.jetbrains.python.psi.PyClass)8 NotNull (org.jetbrains.annotations.NotNull)8 ArrayList (java.util.ArrayList)7 PsiDirectory (com.intellij.psi.PsiDirectory)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PyStatement (com.jetbrains.python.psi.PyStatement)3 PyBlockEvaluator (com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator)3 File (java.io.File)3 Nullable (org.jetbrains.annotations.Nullable)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 Editor (com.intellij.openapi.editor.Editor)2 Module (com.intellij.openapi.module.Module)2 PsiNamedElement (com.intellij.psi.PsiNamedElement)2