com.jetbrains.python.psi.PyFile Java Examples
The following examples show how to use
com.jetbrains.python.psi.PyFile.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PyTestContextProvider.java From intellij with Apache License 2.0 | 6 votes |
@Nullable private static TestLocation testLocation(PsiElement element) { PsiFile file = element.getContainingFile(); if (!(file instanceof PyFile) || !PyTestUtils.isTestFile((PyFile) file)) { return null; } PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class, false); if (pyClass == null || !PyTestUtils.isTestClass(pyClass)) { return new TestLocation((PyFile) file, null, null); } PyFunction pyFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class, false); if (pyFunction != null && PyTestUtils.isTestFunction(pyFunction)) { return new TestLocation((PyFile) file, pyClass, pyFunction); } return new TestLocation((PyFile) file, pyClass, null); }
Example #2
Source File: PyBinaryContextProvider.java From intellij with Apache License 2.0 | 6 votes |
@Nullable @Override public BinaryRunContext getRunContext(ConfigurationContext context) { Location<?> location = context.getLocation(); if (location == null) { return null; } PsiElement element = location.getPsiElement(); PsiFile file = element.getContainingFile(); if (!(file instanceof PyFile)) { return null; } TargetInfo binaryTarget = getTargetLabel(file); if (binaryTarget == null) { return null; } return BinaryRunContext.create(file, binaryTarget); }
Example #3
Source File: PantsPythonTestRunConfigurationProducer.java From intellij-pants-plugin with Apache License 2.0 | 6 votes |
private boolean isOrContainsPyTests(PsiElement element) { if (new PyTestFinder().isTest(element)) { return true; } if (element instanceof PyFile) { PyFile pyFile = (PyFile) element; for (PyClass pyClass : pyFile.getTopLevelClasses()) { if (PythonUnitTestUtil.isTestClass(pyClass, ThreeState.YES, null)) { return true; } } } else if (element instanceof PsiDirectory) { PsiDirectory directory = (PsiDirectory) element; for (PsiFile file : directory.getFiles()) { if (isOrContainsPyTests(file)) { return true; } } } return false; }
Example #4
Source File: PantsTargetIndex.java From intellij-pants-plugin with Apache License 2.0 | 6 votes |
public static List<PsiElement> resolveTargetByName(@Nls String name, @NotNull Project project, GlobalSearchScope scope) { final PsiManager psiManager = PsiManager.getInstance(project); final ArrayList<PsiElement> result = new ArrayList<>(); final Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope); for (VirtualFile virtualFile : containingFiles) { final PsiFile psiFile = psiManager.findFile(virtualFile); if (psiFile instanceof PyFile) { final PyReferenceExpression referenceExpression = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile).get(name); final PsiPolyVariantReference reference = referenceExpression != null ? referenceExpression.getReference() : null; final PsiElement definition = reference != null ? reference.resolve() : null; if (definition != null) { result.add(definition); } else if (referenceExpression != null) { // at least something result.add(referenceExpression); } } } return result; }
Example #5
Source File: PantsTargetIndex.java From intellij-pants-plugin with Apache License 2.0 | 6 votes |
@Override @NotNull public Map<String, Void> map(@NotNull final FileContent inputData) { final PsiFile psiFile = inputData.getPsiFile(); if (psiFile instanceof PyFile) { final Map<String, PyReferenceExpression> targetDefinitions = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile); return ContainerUtil.newMapFromKeys( targetDefinitions.keySet().iterator(), new Convertor<String, Void>() { @Override public Void convert(String o) { return null; } } ); } return Collections.emptyMap(); }
Example #6
Source File: PythonEnvironmentVariablesUsagesProvider.java From idea-php-dotenv-plugin with MIT License | 5 votes |
@NotNull @Override public Collection<KeyUsagePsiElement> getUsages(PsiFile psiFile) { if(psiFile instanceof PyFile) { PythonEnvironmentCallsVisitor visitor = new PythonEnvironmentCallsVisitor(); psiFile.acceptChildren(visitor); return visitor.getCollectedItems(); } return Collections.emptyList(); }
Example #7
Source File: BlazePyBuiltinReferenceResolveProvider.java From intellij with Apache License 2.0 | 5 votes |
@Override public List<RatedResolveResult> resolveName( PyQualifiedExpression element, TypeEvalContext context) { if (!Blaze.isBlazeProject(element.getProject())) { return ImmutableList.of(); } PyBuiltinCache cache = getBuiltInCache(element); if (cache == null) { return ImmutableList.of(); } String referencedName = element.getReferencedName(); if (referencedName == null) { return ImmutableList.of(); } List<RatedResolveResult> result = Lists.newArrayList(); PyFile bfile = cache.getBuiltinsFile(); if (bfile != null && !PyUtil.isClassPrivateName(referencedName)) { PsiElement resultElement = bfile.getElementNamed(referencedName); if (resultElement == null && "__builtins__".equals(referencedName)) { resultElement = bfile; // resolve __builtins__ reference } if (resultElement != null) { result.add( new ImportedResolveResult( resultElement, PyReferenceImpl.getRate(resultElement, context), null)); } } return result; }
Example #8
Source File: PySyncStatusContributor.java From intellij with Apache License 2.0 | 5 votes |
@Nullable @Override public PsiFileAndName toPsiFileAndName(BlazeProjectData projectData, ProjectViewNode<?> node) { if (!(node instanceof PsiFileNode)) { return null; } PsiFile psiFile = ((PsiFileNode) node).getValue(); if (!(psiFile instanceof PyFile)) { return null; } return new PsiFileAndName(psiFile, psiFile.getName()); }
Example #9
Source File: BazelPyImportResolverStrategyTest.java From intellij with Apache License 2.0 | 5 votes |
@Test public void testResolveWorkspaceImport() { PsiFile bar = workspace.createPsiFile(WorkspacePath.createIfValid("foo/bar.py")); PsiFile source = workspace.createPsiFile( WorkspacePath.createIfValid("lib/source.py"), "from foo import bar"); List<PyFromImportStatement> imports = ((PyFile) source).getFromImports(); assertThat(imports).hasSize(1); assertThat(imports.get(0).getImportElements()[0].resolve()).isEqualTo(bar); }
Example #10
Source File: BazelPyImportResolverStrategyTest.java From intellij with Apache License 2.0 | 5 votes |
@Test public void testResolveInitPy() { PsiFile initPy = workspace.createPsiFile(WorkspacePath.createIfValid("pyglib/flags/__init__.py")); PsiFile source = workspace.createPsiFile( WorkspacePath.createIfValid("lib/source.py"), "from pyglib import flags"); List<PyFromImportStatement> imports = ((PyFile) source).getFromImports(); assertThat(imports).hasSize(1); assertThat(imports.get(0).getImportElements()[0].resolve()).isEqualTo(initPy); }
Example #11
Source File: BazelPyGenfilesImportResolverStrategyTest.java From intellij with Apache License 2.0 | 5 votes |
@Test public void testResolveGenfiles() { BlazeInfo roots = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData().getBlazeInfo(); PsiFile genfile = fileSystem.createPsiFile(new File(roots.getGenfilesDirectory(), "foo/bar.py").getPath()); PsiFile source = workspace.createPsiFile( WorkspacePath.createIfValid("lib/source.py"), "from foo import bar"); List<PyFromImportStatement> imports = ((PyFile) source).getFromImports(); assertThat(imports).hasSize(1); assertThat(imports.get(0).getImportElements()[0].resolve()).isEqualTo(genfile); }
Example #12
Source File: PantsCompletionContributor.java From intellij-pants-plugin with Apache License 2.0 | 5 votes |
private boolean isTopLevelExpression(@NotNull CompletionParameters parameters) { PsiElement position = parameters.getPosition(); PsiElement expression = position.getParent(); if (!(expression instanceof PyExpression)) return false; PsiElement statement = expression.getParent(); if (!(statement instanceof PyStatement)) return false; PsiElement file = statement.getParent(); return file instanceof PyFile; }
Example #13
Source File: PantsPsiUtil.java From intellij-pants-plugin with Apache License 2.0 | 5 votes |
@NotNull public static Map<String, PyReferenceExpression> findTargetDefinitions(@NotNull PyFile pyFile) { final PyFunction buildFileAliases = pyFile.findTopLevelFunction("build_file_aliases"); final PyStatement[] statements = buildFileAliases != null ? buildFileAliases.getStatementList().getStatements() : PyStatement.EMPTY_ARRAY; final Map<String, PyReferenceExpression> result = new HashMap<>(); for (PyStatement statement : statements) { if (!(statement instanceof PyReturnStatement)) { continue; } final PyExpression returnExpression = ((PyReturnStatement)statement).getExpression(); if (!(returnExpression instanceof PyCallExpression)) { continue; } final PyArgumentList argumentList = ((PyCallExpression)returnExpression).getArgumentList(); final Collection<PyKeywordArgument> targetDefinitions = PsiTreeUtil.findChildrenOfType(argumentList, PyKeywordArgument.class); for (PyKeywordArgument targets : targetDefinitions) { final PyExpression targetsExpression = targets != null ? targets.getValueExpression() : null; if (targetsExpression instanceof PyDictLiteralExpression) { for (PyKeyValueExpression keyValueExpression : ((PyDictLiteralExpression)targetsExpression).getElements()) { final PyExpression keyExpression = keyValueExpression.getKey(); final PyExpression valueExpression = keyValueExpression.getValue(); if (keyExpression instanceof PyStringLiteralExpression) { result.put( ((PyStringLiteralExpression)keyExpression).getStringValue(), valueExpression instanceof PyReferenceExpression ? (PyReferenceExpression)valueExpression : null ); } } } } } return result; }
Example #14
Source File: PantsPsiUtilTest.java From intellij-pants-plugin with Apache License 2.0 | 5 votes |
public void testAliases() { final PsiFile psiFile = myFixture.configureByFile("aliases.py"); assertTrue(psiFile instanceof PyFile); final Map<String, PyReferenceExpression> definitions = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile); assertContainsElements( definitions.keySet(), Arrays.asList("annotation_processor", "jar_library", "scala_library", "scalac_plugin", "rglobs") ); }
Example #15
Source File: BlazePyUseScopeEnlarger.java From intellij with Apache License 2.0 | 4 votes |
private static boolean isPyFileOutsideProject(PsiElement element) { PsiFile file = element.getContainingFile(); return file instanceof PyFile && !inProjectScope(file.getProject(), file.getViewProvider().getVirtualFile()); }
Example #16
Source File: PyTestContextProvider.java From intellij with Apache License 2.0 | 4 votes |
private TestLocation( PyFile testFile, @Nullable PyClass testClass, @Nullable PyFunction testFunction) { this.testFile = testFile; this.testClass = testClass; this.testFunction = testFunction; }