mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Optimize Imports shouldn't delete unused imports (PY-2201)
This commit is contained in:
@@ -23,6 +23,7 @@ import com.jetbrains.python.psi.patterns.Matcher;
|
||||
import com.jetbrains.python.psi.patterns.ParentMatcher;
|
||||
import com.jetbrains.python.psi.patterns.SyntaxMatchers;
|
||||
import com.jetbrains.python.psi.resolve.ImportedResolveResult;
|
||||
import com.jetbrains.python.psi.resolve.ResolveImportUtil;
|
||||
import com.jetbrains.python.psi.types.*;
|
||||
import com.jetbrains.python.validation.PythonReferenceImporter;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
@@ -349,6 +350,17 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
|
||||
if (PsiTreeUtil.getParentOfType(importStatement, PyTryExceptStatement.class) != null) {
|
||||
continue;
|
||||
}
|
||||
if (unusedImport instanceof PyImportElement) {
|
||||
if (ResolveImportUtil.resolveImportElement((PyImportElement)unusedImport) == null) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
assert importStatement instanceof PyFromImportStatement;
|
||||
if (ResolveImportUtil.resolveFromImportStatementSource((PyFromImportStatement)importStatement) == null) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (unusedImport instanceof PyStarImportElement || areAllImportsUnused(importStatement, unusedImports)) {
|
||||
unusedStatements.add(importStatement);
|
||||
result.add(importStatement);
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
import sys, os
|
||||
import sys, re
|
||||
print sys.argv
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
import xyzzy_shazam
|
||||
@@ -0,0 +1 @@
|
||||
import xyzzy_shazam
|
||||
@@ -8,31 +8,35 @@ import com.jetbrains.python.fixtures.PyLightFixtureTestCase;
|
||||
* @author yole
|
||||
*/
|
||||
public class PyOptimizeImportsTest extends PyLightFixtureTestCase {
|
||||
public void testSimple() throws Exception {
|
||||
public void testSimple() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testOneOfMultiple() throws Exception {
|
||||
public void testOneOfMultiple() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testImportStar() throws Exception {
|
||||
public void testImportStar() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testImportStarOneOfMultiple() throws Exception {
|
||||
public void testImportStarOneOfMultiple() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testTryExcept() throws Exception {
|
||||
public void testTryExcept() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFromFuture() throws Exception {
|
||||
public void testFromFuture() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
public void testUnresolved() { // PY-2201
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile("optimizeImports/" + getTestName(true) + ".py");
|
||||
OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));
|
||||
myFixture.checkResultByFile("optimizeImports/" + getTestName(true) + ".after.py");
|
||||
|
||||
Reference in New Issue
Block a user