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();
}
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()));
}
}
}
}
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);
}
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);
}
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"));
}
Aggregations