From ec5c7b256a827f1597707bf35418e2f2faaff048 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 15 Nov 2011 18:46:29 +0100 Subject: [PATCH] correctly check isIncomplete() for unclosed argument lists (PY-4863) --- .../com/jetbrains/python/formatter/PyBlock.java | 16 +++++----------- .../com/jetbrains/python/psi/PyArgumentList.java | 4 ++++ .../python/psi/impl/PyArgumentListImpl.java | 16 ++++++++++------ .../com/jetbrains/python/PyIndentTest.java | 10 ++++++++++ 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 243d55d945b6..e38fe04eb1b0 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -488,17 +488,6 @@ public class PyBlock implements ASTBlock { } return Indent.getNoneIndent(); - //return null; - -/* - Indent indent; - if (isIncomplete()) { - indent = Indent.getContinuationIndent(); - } else { - indent = Indent.getNoneIndent(); - } - return indent; -*/ } @Nullable @@ -542,6 +531,11 @@ public class PyBlock implements ASTBlock { } } + if (_node.getPsi() instanceof PyArgumentList) { + final PyArgumentList argumentList = (PyArgumentList)_node.getPsi(); + return argumentList.getClosingParen() == null; + } + return false; } diff --git a/python/src/com/jetbrains/python/psi/PyArgumentList.java b/python/src/com/jetbrains/python/psi/PyArgumentList.java index 69296fd0e59e..fad66e5d654b 100644 --- a/python/src/com/jetbrains/python/psi/PyArgumentList.java +++ b/python/src/com/jetbrains/python/psi/PyArgumentList.java @@ -1,5 +1,6 @@ package com.jetbrains.python.psi; +import com.intellij.lang.ASTNode; import com.jetbrains.python.psi.resolve.PyResolveContext; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,4 +33,7 @@ public interface PyArgumentList extends PyElement { */ @NotNull CallArgumentsMapping analyzeCall(PyResolveContext resolveContext); + + @Nullable + ASTNode getClosingParen(); } diff --git a/python/src/com/jetbrains/python/psi/impl/PyArgumentListImpl.java b/python/src/com/jetbrains/python/psi/impl/PyArgumentListImpl.java index 226f64bf7ac4..578b101d85c9 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyArgumentListImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyArgumentListImpl.java @@ -130,9 +130,8 @@ public class PyArgumentListImpl extends PyElementImpl implements PyArgumentList } private void addArgumentLastWithoutComma(PyExpression arg) { - ASTNode node = getNode(); - ASTNode[] pars = node.getChildren(TokenSet.create(PyTokenTypes.RPAR)); - if (pars.length == 0) { + ASTNode par = getClosingParen(); + if (par == null) { // there's no ending paren try { add(arg); @@ -143,10 +142,17 @@ public class PyArgumentListImpl extends PyElementImpl implements PyArgumentList } else { - node.addChild(arg.getNode(), pars[pars.length - 1]); + getNode().addChild(arg.getNode(), par); } } + @Nullable + public ASTNode getClosingParen() { + ASTNode node = getNode(); + final ASTNode[] children = node.getChildren(TokenSet.create(PyTokenTypes.RPAR)); + return children.length == 0 ? null : children[children.length-1]; + } + private void addArgumentNode(PyExpression arg, ASTNode beforeThis, boolean commaFirst) { ASTNode comma = PyElementGenerator.getInstance(getProject()).createComma(); ASTNode node = getNode(); @@ -244,6 +250,4 @@ public class PyArgumentListImpl extends PyElementImpl implements PyArgumentList } return ret; } - - } diff --git a/python/testSrc/com/jetbrains/python/PyIndentTest.java b/python/testSrc/com/jetbrains/python/PyIndentTest.java index d5625faa3af4..4dd8188a760e 100644 --- a/python/testSrc/com/jetbrains/python/PyIndentTest.java +++ b/python/testSrc/com/jetbrains/python/PyIndentTest.java @@ -192,6 +192,16 @@ public class PyIndentTest extends PyTestCase { " params=1)"); } + public void testEnterInNonClosedArgList() { // PY-4863 + doTest("class C:\n" + + " def new_method(self):\n" + + " variable = self._stats.get('outer_key', 'inner_key',", + "class C:\n" + + " def new_method(self):\n" + + " variable = self._stats.get('outer_key', 'inner_key',\n" + + " "); + } + public void testEnterInSet() { // PY-1947 doTest("test_set = {'some_value'}", "test_set = {\n" +