mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Merge branch 'python-fixes'
This commit is contained in:
@@ -2434,7 +2434,7 @@ if __name__ == "__main__":
|
||||
names.remove('__main__') # we don't want ourselves processed
|
||||
ok = True
|
||||
for name in names:
|
||||
ok = ok and processOne(name, None, True)
|
||||
ok = processOne(name, None, True) and ok
|
||||
if not ok:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.containers.HashSet;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache;
|
||||
import com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction;
|
||||
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.psi.impl.PyBinaryExpressionNavigator;
|
||||
@@ -76,6 +77,9 @@ public class PyCodeFragmentUtil {
|
||||
if (next instanceof ConditionalInstruction){
|
||||
continue;
|
||||
}
|
||||
if (next instanceof ReadWriteInstruction && ((ReadWriteInstruction)next).getAccess().isAssertTypeAccess()) {
|
||||
continue;
|
||||
}
|
||||
final PsiElement nextElement = next.getElement();
|
||||
// Ignore binary operations control flow
|
||||
if (nextElement != null && PyBinaryExpressionNavigator.getBinaryExpressionByOperand(nextElement) != null){
|
||||
|
||||
+4
-6
@@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
* @author oleg
|
||||
*/
|
||||
public class PyExtractMethodHandler implements RefactoringActionHandler {
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext) {
|
||||
editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
|
||||
// select editor text fragment
|
||||
@@ -35,12 +35,11 @@ public class PyExtractMethodHandler implements RefactoringActionHandler {
|
||||
invokeOnEditor(project, editor, file);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) {
|
||||
// ignore this
|
||||
}
|
||||
|
||||
private void invokeOnEditor(final Project project, final Editor editor, final PsiFile file) {
|
||||
private static void invokeOnEditor(final Project project, final Editor editor, final PsiFile file) {
|
||||
CommonRefactoringUtil.checkReadOnlyStatus(project, file);
|
||||
PsiElement element1 = null;
|
||||
PsiElement element2 = null;
|
||||
@@ -112,7 +111,7 @@ public class PyExtractMethodHandler implements RefactoringActionHandler {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private PsiElement[] getStatementsRange(final PsiElement element1, final PsiElement element2) {
|
||||
private static PsiElement[] getStatementsRange(final PsiElement element1, final PsiElement element2) {
|
||||
final PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
|
||||
if (parent == null) {
|
||||
return null;
|
||||
@@ -136,5 +135,4 @@ public class PyExtractMethodHandler implements RefactoringActionHandler {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-12
@@ -2,6 +2,7 @@ package com.jetbrains.python.refactoring.extractmethod;
|
||||
|
||||
import com.intellij.codeInsight.codeFragment.CodeFragment;
|
||||
import com.intellij.lang.LanguageNamesValidation;
|
||||
import com.intellij.lang.refactoring.NamesValidator;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -45,7 +46,6 @@ import java.util.Map;
|
||||
* @author oleg
|
||||
*/
|
||||
public class PyExtractMethodUtil {
|
||||
|
||||
public static final String NAME = "extract.method.name";
|
||||
|
||||
private PyExtractMethodUtil() {
|
||||
@@ -108,16 +108,17 @@ public class PyExtractMethodUtil {
|
||||
builder.append("(").append(createCallArgsString(variableData)).append(")");
|
||||
PsiElement callElement = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyCallExpression.class, builder.toString());
|
||||
|
||||
//# replace statements with call
|
||||
// Replace statements with call
|
||||
callElement = replaceElements(elementsRange, callElement);
|
||||
|
||||
// # Set editor
|
||||
// Set editor
|
||||
setSelectionAndCaret(editor, callElement);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, "Extract method", null);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
|
||||
public void run() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@@ -210,8 +211,10 @@ public class PyExtractMethodUtil {
|
||||
|
||||
if (fragment.getOutputVariables().isEmpty()) {
|
||||
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Generate method
|
||||
PyFunction generatedMethod = generateMethodFromExpression(project, methodName, variableData, expression, flags);
|
||||
@@ -234,7 +237,9 @@ public class PyExtractMethodUtil {
|
||||
PsiElement callElement = fragment.isReturnInstructionInside() ? returnStatement : returnStatement.getExpression();
|
||||
|
||||
// replace statements with call
|
||||
callElement = PyPsiUtils.replaceExpression(expression, callElement);
|
||||
if (callElement != null) {
|
||||
callElement = PyPsiUtils.replaceExpression(expression, callElement);
|
||||
}
|
||||
|
||||
// Set editor
|
||||
setSelectionAndCaret(editor, callElement);
|
||||
@@ -372,15 +377,18 @@ public class PyExtractMethodUtil {
|
||||
addFakeParameters(builder, variableData);
|
||||
final PyFunction method = builder.buildFunction(project, LanguageLevel.getDefault());
|
||||
final PyStatementList statementList = method.getStatementList();
|
||||
|
||||
assert statementList != null;
|
||||
for (PsiElement element : elementsRange) {
|
||||
if (element instanceof PsiWhiteSpace){
|
||||
if (element instanceof PsiWhiteSpace) {
|
||||
continue;
|
||||
}
|
||||
statementList.add(element);
|
||||
}
|
||||
// remove last instruction
|
||||
statementList.getFirstChild().delete();
|
||||
final PsiElement child = statementList.getFirstChild();
|
||||
if (child != null) {
|
||||
child.delete();
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
@@ -406,15 +414,15 @@ public class PyExtractMethodUtil {
|
||||
final PsiElement element,
|
||||
final boolean isClassMethod,
|
||||
final boolean isStaticMethod) {
|
||||
final ExtractMethodValidator validator = new PyExtractMethodValidator(element, project);
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()){
|
||||
final ExtractMethodValidator validator = new PyExtractMethodValidator(element, project);
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
String name = System.getProperty(NAME);
|
||||
if (name == null){
|
||||
name = "foo";
|
||||
}
|
||||
final String error = validator.check(name);
|
||||
if (error != null){
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()){
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
throw new CommonRefactoringUtil.RefactoringErrorHintException(error);
|
||||
}
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
@@ -517,7 +525,9 @@ public class PyExtractMethodUtil {
|
||||
}
|
||||
|
||||
public boolean isValidName(final String name) {
|
||||
return LanguageNamesValidation.INSTANCE.forLanguage(PythonLanguage.getInstance()).isIdentifier(name, myProject);
|
||||
final NamesValidator validator = LanguageNamesValidation.INSTANCE.forLanguage(PythonLanguage.getInstance());
|
||||
assert validator != null;
|
||||
return validator.isIdentifier(name, myProject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class IronPythonSdkFlavor extends PythonSdkFlavor {
|
||||
|
||||
@Override
|
||||
public String getVersionString(String sdkHome) {
|
||||
return "IronPython " + getVersionFromOutput(sdkHome, "-V", "\\w+ ([0-9\\.]+).*", true);
|
||||
return "IronPython " + getVersionFromOutput(sdkHome, "-V", "\\w+ ([0-9\\.]+).*");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -36,7 +36,7 @@ public class JythonSdkFlavor extends PythonSdkFlavor {
|
||||
|
||||
@Override
|
||||
public String getVersionString(String sdkHome) {
|
||||
return getVersionFromOutput(sdkHome, "--version", "(Jython \\S+)( on .*)?", false);
|
||||
return getVersionFromOutput(sdkHome, "--version", "(Jython \\S+)( on .*)?");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,6 +21,6 @@ public class PyPySdkFlavor extends PythonSdkFlavor {
|
||||
|
||||
@Override
|
||||
public String getVersionString(String sdkHome) {
|
||||
return getVersionFromOutput(sdkHome, "--version", "\\[(PyPy .+)\\]", true);
|
||||
return getVersionFromOutput(sdkHome, "--version", "\\[(PyPy [^ ]+).*\\]");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,11 +80,11 @@ public abstract class PythonSdkFlavor {
|
||||
|
||||
@Nullable
|
||||
public String getVersionString(String sdkHome) {
|
||||
return getVersionFromOutput(sdkHome, "-V", "(Python \\S+).*", false);
|
||||
return getVersionFromOutput(sdkHome, "-V", "(Python \\S+).*");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected static String getVersionFromOutput(String sdkHome, String version_opt, String version_regexp, boolean stdout) {
|
||||
protected static String getVersionFromOutput(String sdkHome, String version_opt, String version_regexp) {
|
||||
Pattern pattern = Pattern.compile(version_regexp);
|
||||
String run_dir = new File(sdkHome).getParent();
|
||||
final ProcessOutput process_output = SdkUtil.getProcessOutput(run_dir, new String[]{sdkHome, version_opt});
|
||||
@@ -97,8 +97,11 @@ public abstract class PythonSdkFlavor {
|
||||
);
|
||||
return null;
|
||||
}
|
||||
final List<String> lines = stdout ? process_output.getStdoutLines() : process_output.getStderrLines();
|
||||
return SdkUtil.getFirstMatch(lines, pattern);
|
||||
final String result = SdkUtil.getFirstMatch(process_output.getStderrLines(), pattern);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
return SdkUtil.getFirstMatch(process_output.getStdoutLines(), pattern);
|
||||
}
|
||||
|
||||
public Collection<String> getExtraDebugOptions() {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class C:
|
||||
def baz(self, arg_new):
|
||||
self.bar(arg_new)
|
||||
|
||||
def foo(self, option, arg):
|
||||
if option:
|
||||
self.baz(arg)
|
||||
|
||||
def bar(self, arg):
|
||||
pass
|
||||
@@ -0,0 +1,7 @@
|
||||
class C:
|
||||
def foo(self, option, arg):
|
||||
if option:
|
||||
<selection>self.bar(arg)</selection>
|
||||
|
||||
def bar(self, arg):
|
||||
pass
|
||||
@@ -147,4 +147,9 @@ public class PyExtractMethodTest extends LightMarkedTestCase {
|
||||
public void testStaticMethod() {
|
||||
doTest("staticmethod.before.py", "baz", "staticmethod.after.py");
|
||||
}
|
||||
|
||||
// PY-5123
|
||||
public void testMethodInIf() {
|
||||
doTest("methodInIf.before.py", "baz", "methodInIf.after.py");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user