From fadb13158273343381358c9383f91d2f49dbe37d Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 30 Jun 2020 20:46:10 +0300 Subject: [PATCH] EA-232631 Disable f-string-like completion in strings nodes outside of a string literal String nodes are recognized by the lexer and, thus, can still appear even in syntactic contexts where an expression is not expected. In these cases the parser might wrap them in a PsiErrorElement instead of a PyStringLiteralExpression causing the reported CCE. GitOrigin-RevId: 161d88d6307e028ede462f707f01320df2169747 --- .../completion/PyFStringLikeCompletionContributor.java | 3 ++- ...vailableInStringElementsInSyntacticallyIllegalPosition.py | 2 ++ python/testSrc/com/jetbrains/python/Py3CompletionTest.java | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 python/testData/completion/fStringLikeCompletionNotAvailableInStringElementsInSyntacticallyIllegalPosition.py diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java index 4dd0e5097b43..5ec0c4c2bea5 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java @@ -39,7 +39,8 @@ public class PyFStringLikeCompletionContributor extends CompletionContributor { private static final PsiElementPattern.Capture INSIDE_NON_FORMATTED_STRING_ELEMENT = psiElement(PyPlainStringElement.class) - .andNot(psiElement().inside(PyStringFormatCompletionContributor.FORMAT_STRING_CAPTURE)); + .withParent(PyStringLiteralExpression.class) + .andNot(psiElement().inside(PyStringFormatCompletionContributor.FORMAT_STRING_CAPTURE)); public PyFStringLikeCompletionContributor() { extend(CompletionType.BASIC, INSIDE_NON_FORMATTED_STRING_ELEMENT, new CompletionProvider() { diff --git a/python/testData/completion/fStringLikeCompletionNotAvailableInStringElementsInSyntacticallyIllegalPosition.py b/python/testData/completion/fStringLikeCompletionNotAvailableInStringElementsInSyntacticallyIllegalPosition.py new file mode 100644 index 000000000000..adfbf91e56b8 --- /dev/null +++ b/python/testData/completion/fStringLikeCompletionNotAvailableInStringElementsInSyntacticallyIllegalPosition.py @@ -0,0 +1,2 @@ +my_expr = 42 +def '{my_e' \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/Py3CompletionTest.java b/python/testSrc/com/jetbrains/python/Py3CompletionTest.java index f8d06d6f0c17..61d5adbc85e2 100644 --- a/python/testSrc/com/jetbrains/python/Py3CompletionTest.java +++ b/python/testSrc/com/jetbrains/python/Py3CompletionTest.java @@ -472,6 +472,11 @@ public class Py3CompletionTest extends PyTestCase { runWithLanguageLevel(LanguageLevel.getLatest(), this::doNegativeTest); } + // EA-232631 + public void testFStringLikeCompletionNotAvailableInStringElementsInSyntacticallyIllegalPosition() { + runWithLanguageLevel(LanguageLevel.getLatest(), this::doNegativeTest); + } + @Override protected String getTestDataPath() { return super.getTestDataPath() + "/completion";