Resolve module attributes in Python stub files (PY-16402)

This commit is contained in:
Andrey Vlasovskikh
2015-07-13 14:19:05 +03:00
parent db896df79f
commit 60fdbc241d
6 changed files with 78 additions and 0 deletions
+1
View File
@@ -630,6 +630,7 @@
<!-- typing -->
<typeProvider implementation="com.jetbrains.python.codeInsight.PyTypingTypeProvider"/>
<typeProvider implementation="com.jetbrains.python.pyi.PyiTypeProvider"/>
<pyModuleMembersProvider implementation="com.jetbrains.python.pyi.PyiModuleMembersProvider"/>
<typeProvider implementation="com.jetbrains.python.debugger.PyCallSignatureTypeProvider"/>
<pyReferenceResolveProvider implementation="com.jetbrains.python.psi.resolve.PythonBuiltinReferenceResolveProvider"/>
@@ -0,0 +1,66 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.pyi;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.codeInsight.PyCustomMember;
import com.jetbrains.python.psi.NameDefiner;
import com.jetbrains.python.psi.PyElement;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.resolve.PointInImport;
import com.jetbrains.python.psi.types.PyModuleMembersProvider;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* @author vlan
*/
public class PyiModuleMembersProvider extends PyModuleMembersProvider {
@Nullable
@Override
public PsiElement resolveMember(PyFile module, String name) {
final PsiElement pythonStub = PyiUtil.getPythonStub(module);
if (pythonStub instanceof NameDefiner) {
return ((NameDefiner)pythonStub).getElementNamed(name);
}
return null;
}
@Override
public Collection<PyCustomMember> getMembers(PyFile module, PointInImport point) {
final PsiElement pythonStub = PyiUtil.getPythonStub(module);
if (pythonStub instanceof NameDefiner) {
final List<PyCustomMember> results = new ArrayList<PyCustomMember>();
for (PyElement element : ((NameDefiner)pythonStub).iterateNames()) {
final String name = element.getName();
if (name != null) {
results.add(new PyCustomMember(name, element));
}
}
return results;
}
return Collections.emptyList();
}
@Override
protected Collection<PyCustomMember> getMembersByQName(PyFile module, String qName) {
return null;
}
}
@@ -0,0 +1,5 @@
import m1
print(m1.foo)
# <ref>
@@ -0,0 +1 @@
foo = ... # int
@@ -18,6 +18,7 @@ package com.jetbrains.python.pyi;
import com.jetbrains.python.PythonTestUtil;
import com.jetbrains.python.fixtures.PyMultiFileResolveTestCase;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyTargetExpression;
/**
* @author vlan
@@ -39,4 +40,8 @@ public class PyiResolveTest extends PyMultiFileResolveTestCase {
public void testFromPyiToClassInPy() {
assertResolvesTo(PyClass.class, "C");
}
public void testModuleAttribute() {
assertResolvesTo(PyTargetExpression.class, "foo");
}
}