PY-14176 Diagnostic for optimize imports is more noticeable in logs

and no longer clashes with log entries for AddImportHelper
This commit is contained in:
Mikhail Golubev
2017-10-23 18:58:30 +03:00
parent 8769595543
commit e9ebf97e2b
2 changed files with 51 additions and 35 deletions
@@ -122,14 +122,24 @@ public class AddImportHelper {
PROJECT
}
private static class ImportPriorityChoice {
final ImportPriority myPriority;
final String myDescription;
static class ImportPriorityChoice {
private final ImportPriority myPriority;
private final String myDescription;
public ImportPriorityChoice(@NotNull ImportPriority priority, @NotNull String description) {
myPriority = priority;
myDescription = description;
}
@NotNull
public ImportPriority getPriority() {
return myPriority;
}
@NotNull
public String getDescription() {
return myDescription;
}
}
private static final ImportPriority UNRESOLVED_SYMBOL_PRIORITY = ImportPriority.THIRD_PARTY;
@@ -261,17 +271,32 @@ public class AddImportHelper {
return getSameGroupImportsComparator(existingImport.getProject()).compare(newImport, existingImport) < 0;
}
@NotNull
public static ImportPriority getImportPriority(@NotNull PsiElement importLocation, @NotNull PsiFileSystemItem toImport) {
final ImportPriorityChoice choice = getImportPriorityWithReason(importLocation, toImport);
LOG.debug(String.format("Import group for %s at %s is %s: %s", toImport, importLocation.getContainingFile(),
choice.myPriority, choice.myDescription));
return choice.myPriority;
}
@NotNull
public static ImportPriority getImportPriority(@NotNull PyImportStatementBase importStatement) {
final ImportPriorityChoice choice = getImportPriorityWithReason(importStatement);
LOG.debug(String.format("Import group for '%s' is %s: %s", importStatement.getText(), choice.myPriority, choice.myDescription));
return choice.myPriority;
}
@NotNull
static ImportPriorityChoice getImportPriorityWithReason(@NotNull PyImportStatementBase importStatement) {
final PsiElement resolved;
final PsiElement resolveAnchor;
if (importStatement instanceof PyFromImportStatement) {
final PyFromImportStatement fromImportStatement = (PyFromImportStatement)importStatement;
if (fromImportStatement.isFromFuture()) {
return logImportPriorityChoice(ImportPriority.FUTURE, importStatement, "import from __future__");
return new ImportPriorityChoice(ImportPriority.FUTURE, "import from __future__");
}
if (fromImportStatement.getRelativeLevel() > 0) {
return logImportPriorityChoice(ImportPriority.PROJECT, importStatement, "explicit relative import");
return new ImportPriorityChoice(ImportPriority.PROJECT, "explicit relative import");
}
resolveAnchor = ((PyFromImportStatement)importStatement).getImportSource();
resolved = fromImportStatement.resolveImportSource();
@@ -279,14 +304,14 @@ public class AddImportHelper {
else {
final PyImportElement firstImportElement = ArrayUtil.getFirstElement(importStatement.getImportElements());
if (firstImportElement == null) {
return logImportPriorityChoice(UNRESOLVED_SYMBOL_PRIORITY, importStatement, "incomplete import statement");
return new ImportPriorityChoice(UNRESOLVED_SYMBOL_PRIORITY, "incomplete import statement");
}
resolveAnchor = firstImportElement;
resolved = firstImportElement.resolve();
}
if (resolved == null) {
return logImportPriorityChoice(UNRESOLVED_SYMBOL_PRIORITY, importStatement,
resolveAnchor == null ? "incomplete import statement" : resolveAnchor.getText() + " is unresolved");
return new ImportPriorityChoice(UNRESOLVED_SYMBOL_PRIORITY,
resolveAnchor == null ? "incomplete import statement" : resolveAnchor.getText() + " is unresolved");
}
PsiFileSystemItem resolvedFileOrDir;
@@ -302,34 +327,18 @@ public class AddImportHelper {
}
if (resolvedFileOrDir instanceof PyiFile) {
resolvedFileOrDir = as(PyiUtil.getOriginalElement((PyiFile)resolvedFileOrDir), PsiFileSystemItem.class);
resolvedFileOrDir = as(PyiUtil.getOriginalElement((PyiFile)resolvedFileOrDir), PsiFileSystemItem.class);
}
if (resolvedFileOrDir == null) {
return logImportPriorityChoice(UNRESOLVED_SYMBOL_PRIORITY, importStatement, resolved + " is not a file or directory");
return new ImportPriorityChoice(UNRESOLVED_SYMBOL_PRIORITY, resolved + " is not a file or directory");
}
final ImportPriorityChoice choice = getImportPriorityWithReason(importStatement, resolvedFileOrDir);
logImportPriorityChoice(choice.myPriority, importStatement, choice.myDescription);
return choice.myPriority;
return getImportPriorityWithReason(importStatement, resolvedFileOrDir);
}
@NotNull
private static ImportPriority logImportPriorityChoice(@NotNull ImportPriority priority, @NotNull PyImportStatementBase importStatement,
@NotNull String description) {
LOG.debug(String.format("Import group for '%s' is %s: %s", importStatement.getText(), priority, description));
return priority;
}
@NotNull
public static ImportPriority getImportPriority(@NotNull PsiElement importLocation, @NotNull PsiFileSystemItem toImport) {
final ImportPriorityChoice choice = getImportPriorityWithReason(importLocation, toImport);
LOG.debug(String.format("Import group for %s at %s is %s: %s", toImport, importLocation.getContainingFile(), choice.myPriority, choice.myDescription));
return choice.myPriority;
}
@NotNull
public static ImportPriorityChoice getImportPriorityWithReason(@NotNull PsiElement importLocation, @NotNull PsiFileSystemItem toImport) {
static ImportPriorityChoice getImportPriorityWithReason(@NotNull PsiElement importLocation, @NotNull PsiFileSystemItem toImport) {
final VirtualFile vFile = toImport.getVirtualFile();
if (vFile == null) {
return new ImportPriorityChoice(UNRESOLVED_SYMBOL_PRIORITY, toImport + " doesn't have an associated virtual file");
@@ -343,12 +352,13 @@ public class AddImportHelper {
final Sdk pythonSdk = module != null ? PythonSdkType.findPythonSdk(module) : projectRootManager.getProjectSdk();
if (PythonSdkType.isStdLib(vFile, pythonSdk)) {
return new ImportPriorityChoice(ImportPriority.BUILTIN, vFile + " is either somewhere in lib but not under site-packages," +
return new ImportPriorityChoice(ImportPriority.BUILTIN, vFile + " is either in lib but not under site-packages," +
" or belongs to the root of skeletons," +
" or is a .pyi stub definition for stdlib module");
}
else {
return new ImportPriorityChoice(ImportPriority.THIRD_PARTY, "Fall back value for " + vFile);
return new ImportPriorityChoice(ImportPriority.THIRD_PARTY, pythonSdk == null ? "SDK for " + vFile + " isn't found"
: "Fall back value for " + vFile);
}
}
@@ -18,6 +18,7 @@ package com.jetbrains.python.codeInsight.imports;
import com.google.common.collect.Ordering;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.lang.ImportOptimizer;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Couple;
@@ -45,6 +46,7 @@ import static com.jetbrains.python.psi.PyUtil.as;
* @author yole
*/
public class PyImportOptimizer implements ImportOptimizer {
private static final Logger LOG = Logger.getInstance(PyImportOptimizer.class);
private boolean mySortImports = true;
@@ -75,10 +77,12 @@ public class PyImportOptimizer implements ImportOptimizer {
}
});
return () -> {
LOG.debug(String.format("----------------- OPTIMIZE IMPORTS STARTED (%s) -----------------", file.getVirtualFile()));
visitor.optimizeImports();
if (mySortImports && file instanceof PyFile) {
new ImportSorter((PyFile)file).run();
}
LOG.debug("----------------- OPTIMIZE IMPORTS FINISHED -----------------");
};
}
@@ -95,11 +99,11 @@ public class PyImportOptimizer implements ImportOptimizer {
private final PyCodeStyleSettings myPySettings;
private final List<PyImportStatementBase> myImportBlock;
private final Map<ImportPriority, List<PyImportStatementBase>> myGroups;
private final MultiMap<PyImportStatementBase, PsiComment> myOldImportToLineComments = MultiMap.create();
private final MultiMap<PyImportStatementBase, PsiComment> myOldImportToInnerComments = MultiMap.create();
private final MultiMap<String, PyFromImportStatement> myOldFromImportBySources = MultiMap.create();
private final MultiMap<PyImportStatementBase, PsiComment> myNewImportToLineComments = MultiMap.create();
// Contains trailing and nested comments of modified (split and joined) imports
private final MultiMap<PyImportStatementBase, PsiComment> myNewImportToInnerComments = MultiMap.create();
@@ -123,8 +127,10 @@ public class PyImportOptimizer implements ImportOptimizer {
analyzeImports(myImportBlock);
for (PyImportStatementBase importStatement : myImportBlock) {
final ImportPriority priority = AddImportHelper.getImportPriority(importStatement);
myGroups.get(priority).add(importStatement);
final AddImportHelper.ImportPriorityChoice choice = AddImportHelper.getImportPriorityWithReason(importStatement);
LOG.debug(String.format("Import group for '%s' is %s: %s",
importStatement.getText(), choice.getPriority(), choice.getDescription()));
myGroups.get(choice.getPriority()).add(importStatement);
}
boolean hasTransformedImports = false;