mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-15 02:59:33 +07:00
PY-22422 Consider new code style settings in actions that might create/update "from" imports
In particular, in "Import ..." quickfix, smart completion for class names and in a few refactorings.
This commit is contained in:
@@ -26,6 +26,7 @@ import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.QualifiedName;
|
||||
@@ -473,6 +474,8 @@ public class AddImportHelper {
|
||||
final PyElementGenerator generator = PyElementGenerator.getInstance(file.getProject());
|
||||
final PyImportElement importElement = generator.createImportElement(LanguageLevel.forElement(file), name, asName);
|
||||
existingImport.add(importElement);
|
||||
// May need to add parentheses, trailing comma, etc.
|
||||
CodeStyleManager.getInstance(file.getProject()).reformat(existingImport);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileSystemItem;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.util.QualifiedName;
|
||||
import com.intellij.ui.SimpleColoredComponent;
|
||||
import com.intellij.ui.SimpleTextAttributes;
|
||||
@@ -203,6 +204,7 @@ public class ImportFromExistingAction implements QuestionAction {
|
||||
// add another import element right after the one we got
|
||||
PsiElement newImportElement = gen.createImportElement(LanguageLevel.getDefault(), myName, null);
|
||||
parent.add(newImportElement);
|
||||
CodeStyleManager.getInstance(myTarget.getProject()).reformat(parent);
|
||||
}
|
||||
else { // just 'import'
|
||||
// all we need is to qualify our target
|
||||
|
||||
@@ -1 +1 @@
|
||||
from urllib import urlopen, urlencode, unquote_plus # this is a comment
|
||||
from urllib import urlopen, urlencode, unquote_plus # this is a comment
|
||||
|
||||
@@ -1 +1 @@
|
||||
from urllib import (urlopen, urlencode, unquote_plus)
|
||||
from urllib import (urlopen, urlencode, unquote_plus)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
|
||||
class Bar:
|
||||
pass
|
||||
@@ -0,0 +1,6 @@
|
||||
from module import (
|
||||
Foo,
|
||||
Bar,
|
||||
)
|
||||
|
||||
print(Foo(), Bar)
|
||||
@@ -0,0 +1,3 @@
|
||||
from module import Foo
|
||||
|
||||
print(Foo(), Ba<caret>)
|
||||
@@ -0,0 +1,2 @@
|
||||
from module import foo
|
||||
print(foo, <error descr="Unresolved reference 'bar'">b<caret>ar</error>)
|
||||
@@ -0,0 +1,6 @@
|
||||
from module import (
|
||||
foo,
|
||||
bar,
|
||||
)
|
||||
|
||||
print(foo, bar)
|
||||
@@ -0,0 +1,2 @@
|
||||
foo = 1
|
||||
bar = 2
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
from lib import (
|
||||
Class1,
|
||||
Class2,
|
||||
)
|
||||
|
||||
print(Class1())
|
||||
|
||||
|
||||
def func():
|
||||
print(Class2())
|
||||
@@ -0,0 +1,6 @@
|
||||
class Class1:
|
||||
pass
|
||||
|
||||
|
||||
class Class2:
|
||||
pass
|
||||
@@ -0,0 +1,5 @@
|
||||
from lib import Class2
|
||||
|
||||
|
||||
def func():
|
||||
print(Class2())
|
||||
@@ -0,0 +1,3 @@
|
||||
from lib import Class1
|
||||
|
||||
print(Class1())
|
||||
@@ -0,0 +1,6 @@
|
||||
class Class1:
|
||||
pass
|
||||
|
||||
|
||||
class Class2:
|
||||
pass
|
||||
@@ -17,6 +17,7 @@ package com.jetbrains.python;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionType;
|
||||
import com.intellij.codeInsight.lookup.Lookup;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.jetbrains.python.codeInsight.PyCodeInsightSettings;
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
|
||||
@@ -77,6 +78,16 @@ public class PyClassNameCompletionTest extends PyTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-22422
|
||||
public void testReformatUpdatedFromImport() {
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_WRAPPING = CommonCodeStyleSettings.WRAP_ALWAYS;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_BEFORE_RIGHT_PARENTHESIS = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_AFTER_LEFT_PARENTHESIS = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE = true;
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
final String path = "/completion/className/" + getTestName(true);
|
||||
myFixture.copyDirectoryToProject(path, "");
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.jetbrains.python.quickFixes;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.ex.QuickFixWrapper;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.jetbrains.python.PyQuickFixTestCase;
|
||||
@@ -56,6 +57,16 @@ public class AddImportQuickFixTest extends PyQuickFixTestCase {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// PY-22422
|
||||
public void testAddParenthesesAndTrailingCommaToUpdatedFromImport() {
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_WRAPPING = CommonCodeStyleSettings.WRAP_ALWAYS;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_BEFORE_RIGHT_PARENTHESIS = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_AFTER_LEFT_PARENTHESIS = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE = true;
|
||||
doMultiFileAutoImportTest("Import");
|
||||
}
|
||||
|
||||
// PY-21563
|
||||
public void testCombineFromImportsForReferencesInTypeComment() {
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.search.ProjectScope;
|
||||
import com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesProcessor;
|
||||
import com.intellij.testFramework.PlatformTestUtil;
|
||||
@@ -381,6 +382,17 @@ public class PyMoveTest extends PyTestCase {
|
||||
doMoveSymbolTest("fnToMove", "toFile.py");
|
||||
}
|
||||
|
||||
// PY-22422
|
||||
public void testReformatFromImports() {
|
||||
getPythonCodeStyleSettings().OPTIMIZE_IMPORTS_JOIN_FROM_IMPORTS_WITH_SAME_SOURCE = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_WRAPPING = CommonCodeStyleSettings.WRAP_ALWAYS;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_AFTER_LEFT_PARENTHESIS = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_BEFORE_RIGHT_PARENTHESIS = true;
|
||||
getPythonCodeStyleSettings().FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE = true;
|
||||
doMoveSymbolTest("func", "b.py");
|
||||
}
|
||||
|
||||
private void doMoveFileTest(String fileName, String toDirName) {
|
||||
Project project = myFixture.getProject();
|
||||
PsiManager manager = PsiManager.getInstance(project);
|
||||
|
||||
Reference in New Issue
Block a user