Merge remote-tracking branch 'origin/master'

This commit is contained in:
Dmitry Trofimov
2012-08-28 13:45:05 +02:00
27 changed files with 240 additions and 99 deletions
+1 -1
View File
@@ -2280,7 +2280,7 @@ def is_mac_skipped_module(path, f):
return 0
def is_skipped_module(path, f):
return is_mac_skipped_module(path, f) or is_posix_skipped_module(path, f[:f.rindex('.')])
return is_mac_skipped_module(path, f) or is_posix_skipped_module(path, f[:f.rindex('.')]) or 'pynestkernel' in path
def is_module(d, root):
@@ -88,6 +88,8 @@
<declarationRangeHandler key="com.jetbrains.python.psi.PyFunction"
implementationClass="com.jetbrains.python.codeInsight.PyDeclarationRangeHandler"/>
<targetElementEvaluator language="Python" implementationClass="com.jetbrains.python.codeInsight.PyTargetElementEvaluator"/>
<!-- Run/Debug -->
<applicationService serviceInterface="com.jetbrains.python.run.PyRunConfigurationFactory"
serviceImplementation="com.jetbrains.python.run.PyRunConfigurationFactoryImpl"/>
@@ -0,0 +1,42 @@
package com.jetbrains.python.codeInsight;
import com.intellij.codeInsight.TargetElementEvaluator;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.psi.PyReferenceExpression;
import com.jetbrains.python.psi.resolve.PyResolveContext;
import com.jetbrains.python.psi.resolve.QualifiedResolveResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author yole
*/
public class PyTargetElementEvaluator implements TargetElementEvaluator {
@Override
public boolean includeSelfInGotoImplementation(@NotNull PsiElement element) {
return false;
}
@Nullable
@Override
public PsiElement getElementByReference(PsiReference ref, int flags) {
if ((flags & TargetElementUtilBase.ELEMENT_NAME_ACCEPTED) == 0){
return null;
}
final PsiElement element = ref.getElement();
PsiElement result = ref.resolve();
if (result instanceof PyReferenceExpression &&
PsiTreeUtil.getParentOfType(element, ScopeOwner.class) == PsiTreeUtil.getParentOfType(result, ScopeOwner.class)) {
QualifiedResolveResult resolveResult = ((PyReferenceExpression)result).followAssignmentsChain(PyResolveContext.noImplicits());
PsiElement finalResult = resolveResult.getElement();
if (PsiTreeUtil.getParentOfType(element, ScopeOwner.class) == PsiTreeUtil.getParentOfType(finalResult, ScopeOwner.class)) {
return finalResult;
}
}
return result;
}
}
@@ -236,6 +236,13 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
.inside(PyWithStatement.class)
);
private static final PsiElementPattern.Capture<PsiElement> IN_EXCEPT_AFTER_REF =
psiElement().afterLeaf(psiElement()
.withElementType(PyTokenTypes.IDENTIFIER)
.inside(PyReferenceExpression.class)
.inside(PyExceptPart.class)
);
private static final PsiElementPattern.Capture<PsiElement> IN_COND_STMT =
psiElement().inside(psiElement(PyStatementList.class).inside(psiElement(PyConditionalStatementPart.class)));
@@ -526,7 +533,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
extend(
CompletionType.BASIC, psiElement()
.withLanguage(PythonLanguage.getInstance())
.andOr(IN_IMPORT_AFTER_REF, IN_WITH_AFTER_REF)
.andOr(IN_IMPORT_AFTER_REF, IN_WITH_AFTER_REF, IN_EXCEPT_AFTER_REF)
.andNot(AFTER_QUALIFIER)
,
new PyKeywordCompletionProvider("as")
@@ -44,7 +44,7 @@ public class TypeAssertionIntention implements IntentionAction {
PyExpression problemElement =
PsiTreeUtil.getTopmostParentOfType(file.findElementAt(editor.getCaretModel().getOffset()-1), PyQualifiedExpression.class);
if (problemElement == null) return false;
if (problemElement instanceof PyQualifiedExpression && ((PyQualifiedExpression)problemElement).getReferencedName() == null) {
if (problemElement instanceof PyQualifiedExpression) {
final PyExpression qualifier = ((PyQualifiedExpression)problemElement).getQualifier();
if (qualifier != null && !qualifier.getText().equals(PyNames.CANONICAL_SELF)) {
problemElement = qualifier;
@@ -54,6 +54,7 @@ public class TypeAssertionIntention implements IntentionAction {
if (problemElement.getParent() instanceof PyCallExpression ||
PsiTreeUtil.getParentOfType(problemElement, PyListCompExpression.class) != null ||
PsiTreeUtil.getParentOfType(problemElement, PyLambdaExpression.class) != null ||
PsiTreeUtil.getParentOfType(problemElement, PyGeneratorExpression.class) != null ||
(reference != null && reference.resolve() == null)) {
return false;
}
@@ -83,7 +83,7 @@ public class PyMethodParametersInspection extends PyInspection {
if (flags != null) {
PyParameterList plist = node.getParameterList();
PyParameter[] params = plist.getParameters();
final String method_name = node.getName();
final String methodName = node.getName();
final String CLS = "cls"; // TODO: move to style settings
final String MCS = "mcs"; // as per pylint inspection C0203
if (params.length == 0) { // fix: add
@@ -138,27 +138,30 @@ public class PyMethodParametersInspection extends PyInspection {
return;
}
if (flags.isMetaclassMethod()) {
String expected_name;
if (flags.isStaticMethod() && !PyNames.NEW.equals(methodName)) {
return;
}
String expectedName;
String alternativeName = null;
if (PyNames.NEW.equals(method_name) || flags.isClassMethod()) {
expected_name = MCS;
if (PyNames.NEW.equals(methodName) || flags.isClassMethod()) {
expectedName = MCS;
}
else if (flags.isSpecialMetaclassMethod()) {
expected_name = CLS;
expectedName = CLS;
}
else {
expected_name = PyNames.CANONICAL_SELF;
expectedName = PyNames.CANONICAL_SELF;
alternativeName = CLS;
}
if (!expected_name.equals(pname) && (alternativeName == null || !alternativeName.equals(pname))) {
if (!expectedName.equals(pname) && (alternativeName == null || !alternativeName.equals(pname))) {
registerProblem(
PyUtil.sure(params[0].getNode()).getPsi(),
PyBundle.message("INSP.usually.named.$0", expected_name),
new RenameParameterQuickFix(expected_name)
PyBundle.message("INSP.usually.named.$0", expectedName),
new RenameParameterQuickFix(expectedName)
);
}
}
else if (flags.isClassMethod() || PyNames.NEW.equals(method_name)) {
else if (flags.isClassMethod() || PyNames.NEW.equals(methodName)) {
if (!CLS.equals(pname)) {
registerProblem(
PyUtil.sure(params[0].getNode()).getPsi(),
@@ -76,7 +76,7 @@ public class PyNonAsciiCharInspection extends PyInspection {
@Override
public void visitPyStringLiteralExpression(PyStringLiteralExpression node) {
checkString(node, node.getStringValue());
checkString(node, node.getText());
}
}
@@ -498,7 +498,7 @@ public class PyPackageManagerImpl extends PyPackageManager {
if (binaryFile != null) {
final ProjectJdkImpl tmpSdk = new ProjectJdkImpl("", PythonSdkType.getInstance());
tmpSdk.setHomePath(path);
final PyPackageManagerImpl manager = (PyPackageManagerImpl)PyPackageManagers.getInstance().forSdk(tmpSdk);
final PyPackageManagerImpl manager = new PyPackageManagerImpl(tmpSdk);
manager.installManagement(DISTRIBUTE);
manager.installManagement(PIP);
}
@@ -181,7 +181,6 @@ public class PyPackagesPanel extends JPanel {
final int[] rows = myPackagesTable.getSelectedRows();
final Sdk selectedSdk = mySelectedSdk;
if (selectedSdk != null) {
final List<PyRequirement> requirements = new ArrayList<PyRequirement>();
for (int row : rows) {
final Object pyPackage = myPackagesTableModel.getValueAt(row, 0);
if (pyPackage instanceof PyPackage) {
@@ -193,10 +192,10 @@ public class PyPackagesPanel extends JPanel {
public void handleResult(Object result, URL url, String method) {
final List<String> releases = (List<String>)result;
PyPIPackageUtil.INSTANCE.addPackageReleases(packageName, releases);
if (releases.isEmpty() ||
PyRequirement.VERSION_COMPARATOR.compare((String)currentVersion, releases.get(0)) < 0) {
requirements.add(new PyRequirement(packageName));
}
if (!releases.isEmpty() &&
PyRequirement.VERSION_COMPARATOR.compare((String)currentVersion, releases.get(0)) >= 0)
return;
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
@@ -225,7 +224,7 @@ public class PyPackagesPanel extends JPanel {
}
}
});
ui.install(requirements, Collections.singletonList("-U"));
ui.install(Collections.singletonList(new PyRequirement(packageName)), Collections.singletonList("-U"));
myUpgradeButton.setEnabled(false);
}
}, ModalityState.any());
@@ -648,6 +648,9 @@ public class PyUtil {
if (turnDirIntoInit(directory) != null) {
return true;
}
if (LanguageLevel.forFile(directory.getVirtualFile()).isAtLeast(LanguageLevel.PYTHON33)) {
return true;
}
return hasNamespacePackageFile(directory);
}
@@ -215,7 +215,10 @@ public class PyReferenceImpl implements PsiReferenceEx, PsiPolyVariantReference
final ScopeOwner owner = ScopeUtil.getScopeOwner(uexpr);
if (owner != null) {
final Scope scope = ControlFlowCache.getScope(owner);
if (owner == originalOwner && !scope.isGlobal(referencedName)) {
if (uexpr == originalOwner && originalOwner instanceof PyClass) {
uexpr = null;
}
else if (owner == originalOwner && !scope.isGlobal(referencedName)) {
final ResolveResultList latest = resolveToLatestDefs(owner, myElement, referencedName);
if (!latest.isEmpty()) {
return latest;
@@ -112,19 +112,7 @@ public class VirtualEnvSdkFlavor extends CPythonSdkFlavor {
@Override
public boolean isValidSdkPath(@NotNull File file) {
if (!super.isValidSdkPath(file)) return false;
File bin = file.getParentFile();
if (bin != null) {
File[] children = bin.listFiles();
if (children != null) {
for (File f : children) {
//is it good enough to determine virtual env?
if (f.getName().equals("activate_this.py")) {
return true;
}
}
}
}
return false;
return PythonSdkType.getVirtualEnvRoot(file.getPath()) != null;
}
@Override
@@ -4,6 +4,7 @@ import com.intellij.lang.annotation.Annotation;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayUtil;
import com.jetbrains.cython.psi.CythonClass;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.highlighting.PyHighlighter;
import com.jetbrains.python.psi.*;
@@ -48,6 +49,9 @@ public class HighlightingAnnotator extends PyAnnotator {
if (cls instanceof CythonClass && ((CythonClass)cls).isCppClass()) {
return false;
}
if (PyNames.NEW.equals(function.getName())) {
return true;
}
final PyFunction.Modifier modifier = function.getModifier();
if (modifier != PyFunction.Modifier.CLASSMETHOD && modifier != PyFunction.Modifier.STATICMETHOD) {
return true;
@@ -1,58 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>first_arg.py</file>
<line>13</line>
<description>Usually first parameter of a method is named 'self'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>16</line>
<description>Method must have a first parameter, usually called 'self'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>19</line>
<description>First parameter of a non-static method must not be a tuple</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>25</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>29</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>34</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>39</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>62</line>
<description>Usually first parameter of such methods is named 'self'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>65</line>
<description>Usually first parameter of such methods is named 'mcs'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>68</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>75</line>
<description>Usually first parameter of such methods is named 'mcs'</description>
</problem>
</problems>
@@ -0,0 +1,80 @@
def foo(bar):
pass # nothing
def __call__(foo):
pass # nothing, out of class
def innocent(f):
"innocent deco"
return f
class Foo(object):
def __init__(<weak_warning descr="Usually first parameter of a method is named 'self'">a</weak_warning>, b):
pass # rename a
def foo<error descr="Method must have a first parameter, usually called 'self'">()</error>:
pass # propose self
def loo<weak_warning descr="First parameter of a non-static method must not be a tuple">((l, g), *rest)</weak_warning>:
pass # complain at tuple
def zoo(*animals):
pass # nothing
def __new__(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@classmethod
def boo(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@classmethod
@innocent
def boo(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@innocent
@classmethod
def boo(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@staticmethod
def moo(a):
pass # nothing
@staticmethod
def qoo((x, y, z), t):
pass # nothing
@staticmethod
@innocent
def qoo((x, y, z), t):
pass # nothing
@innocent
@staticmethod
def qoo((x, y, z), t):
pass # nothing
class Meta(type):
def foo(<weak_warning descr="Usually first parameter of such methods is named 'self'">first</weak_warning>): # rename to "self"
pass
def __new__(<weak_warning descr="Usually first parameter of such methods is named 'mcs'">self</weak_warning>, *rest): # rename to "mcs"
pass
def __call__(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>): # rename to "cls"
pass
def bar(cls): # <- rename to "self"
return "foobar"
@classmethod
def baz(<weak_warning descr="Usually first parameter of such methods is named 'mcs'">moo</weak_warning>): # <- rename to "mcs"
return "foobar"
@staticmethod
def bazz(param1):
return "foobar"
@@ -0,0 +1,4 @@
class C:
print(<error descr="Unresolved reference 'C'">C</error>) #fail
def f(self):
print(C) #pass
@@ -0,0 +1,3 @@
def cover(lab=1):
lab += 1
return l<caret>ab
@@ -0,0 +1,3 @@
def cover(bar=1):
bar += 1
return bar
@@ -0,0 +1,4 @@
from p1.m1 import foo
foo()
#<ref>
@@ -0,0 +1,2 @@
def foo():
pass
@@ -0,0 +1,4 @@
import p1
p1
#<ref>
@@ -0,0 +1,2 @@
def foo():
pass
@@ -404,6 +404,29 @@ public class PyMultiFileResolveTest extends PyResolveTestCase {
assertResolvesTo(PyFunction.class, "foo");
}
// PY-7156
public void testPython33NamespacePackage() {
setLanguageLevel(LanguageLevel.PYTHON33);
try {
final PsiElement element = doResolve();
assertInstanceOf(element, PyImportedModule.class);
final PyImportedModule module = (PyImportedModule)element;
assertEquals("p1", module.getImportedPrefix().toString());
} finally {
setLanguageLevel(null);
}
}
// PY-7156
public void testFromPython33NamespacePackageImport() {
setLanguageLevel(LanguageLevel.PYTHON33);
try {
assertResolvesTo(PyFunction.class, "foo");
} finally {
setLanguageLevel(null);
}
}
private void prepareTestDirectory() {
final String testName = getTestName(true);
myFixture.copyDirectoryToProject(testName, "");
@@ -523,12 +523,14 @@ public class PythonCompletionTest extends PyTestCase {
}
public void testTypeMembers() { // PY-5311
myFixture.configureByText(PythonFileType.INSTANCE, "a = 'string'\n" +
"a.<caret>");
assertFalse(doTestByText("a = 'string'\n" +
"a.<caret>").contains("mro"));
}
private List<String> doTestByText(String text) {
myFixture.configureByText(PythonFileType.INSTANCE, text);
myFixture.completeBasic();
final List<String> strings = myFixture.getLookupElementStrings();
assertFalse(strings.contains("mro"));
return myFixture.getLookupElementStrings();
}
public void testDunderAllReference() { // PY-5502
@@ -588,4 +590,16 @@ public class PythonCompletionTest extends PyTestCase {
public void testUnknownNewReturnType() { // PY-6671
doTest();
}
public void testAsInWith() { // PY-3701
setLanguageLevel(LanguageLevel.PYTHON27);
assertTrue(doTestByText("with foo <caret>").contains("as"));
}
public void testAsInExcept() { // PY-1846
setLanguageLevel(LanguageLevel.PYTHON27);
assertTrue(doTestByText("try:\n" +
" pass\n" +
"except IOError <caret>").contains("as"));
}
}
@@ -49,8 +49,7 @@ public class PythonInspectionsTest extends PyTestCase {
}
public void testPyMethodParametersInspection() {
LocalInspectionTool inspection = new PyMethodParametersInspection();
doTest(getTestName(false), inspection);
doHighlightingTest(PyMethodParametersInspection.class);
}
public void testPyNestedDecoratorsInspection() {
@@ -180,6 +180,11 @@ public class PyUnresolvedReferencesInspectionTest extends PyTestCase {
doTest();
}
// PY-5995
public void testClassInClassBody() {
doTest();
}
private void doTest() {
myFixture.configureByFile(TEST_DIRECTORY + getTestName(true) + ".py");
myFixture.enableInspections(PyUnresolvedReferencesInspection.class);
@@ -87,6 +87,10 @@ public class PyRenameTest extends PyTestCase {
doTest("bar");
}
public void testRenameAugAssigned() { // PY-3698
doTest("bar");
}
public void testRenameProperty() { // PY-5948
setLanguageLevel(LanguageLevel.PYTHON26);
try {