From 2402957c9d2238f355db03c8c6ea7940c258172e Mon Sep 17 00:00:00 2001 From: Petr Date: Fri, 4 Jul 2025 17:27:56 +0200 Subject: [PATCH] PY-71549 Move async-related check from PyKeywordHighlightingAnnotator to PyAsyncAwaitAnnotatorVisitor It also caused double highlighting on the frontend as PyKeywordHighlightingAnnotator is currently registered in both frontend and backend. GitOrigin-RevId: cdf111acfdb8a979613007e12abcf6c7dac9c9dd --- .../resources/messages/PyPsiBundle.properties | 1 + .../validation/PyAsyncAwaitAnnotatorVisitor.java | 15 +++++++++++++++ .../messages/PySyntaxCoreBundle.properties | 3 --- .../PyKeywordHighlightingAnnotator.java | 8 -------- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/python/python-psi-impl/resources/messages/PyPsiBundle.properties b/python/python-psi-impl/resources/messages/PyPsiBundle.properties index e5c442dd9427..c038302e8d1d 100644 --- a/python/python-psi-impl/resources/messages/PyPsiBundle.properties +++ b/python/python-psi-impl/resources/messages/PyPsiBundle.properties @@ -170,6 +170,7 @@ ANN.continue.break.or.return.in.star.except='break', 'continue' and 'return' can ANN.await.outside.async.function='await' outside async function ANN.async.with.outside.function='async with' outside async function ANN.async.for.outside.function='async for' outside async function +ANN.function.cannot.be.async=function \"{0}\" cannot be async QFIX.convert.into.async.function=Convert to async function ### quick doc generator diff --git a/python/python-psi-impl/src/com/jetbrains/python/validation/PyAsyncAwaitAnnotatorVisitor.java b/python/python-psi-impl/src/com/jetbrains/python/validation/PyAsyncAwaitAnnotatorVisitor.java index 5f3a495f36d5..c3e1f6ff5cfd 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/validation/PyAsyncAwaitAnnotatorVisitor.java +++ b/python/python-psi-impl/src/com/jetbrains/python/validation/PyAsyncAwaitAnnotatorVisitor.java @@ -8,12 +8,15 @@ import com.intellij.modcommand.ModPsiUpdater; import com.intellij.modcommand.PsiUpdateModCommandAction; import com.intellij.psi.PsiElement; import com.jetbrains.python.PyPsiBundle; +import com.jetbrains.python.PySyntaxCoreBundle; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil; import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; +import java.util.Optional; + public final class PyAsyncAwaitAnnotatorVisitor extends PyElementVisitor { private final @NotNull PyAnnotationHolder myHolder; @@ -102,6 +105,18 @@ public final class PyAsyncAwaitAnnotatorVisitor extends PyElementVisitor { checkComprehension(node); } + @Override + public void visitPyFunction(@NotNull PyFunction node) { + if (!node.isAsyncAllowed()) { + Optional + .ofNullable(node.getNode()) + .map(astNode -> astNode.findChildByType(PyTokenTypes.ASYNC_KEYWORD)) + .ifPresent(asyncNode -> myHolder.newAnnotation(HighlightSeverity.ERROR, + PyPsiBundle.message("ANN.function.cannot.be.async", node.getName())) + .range(asyncNode).create()); + } + } + private static class ConvertIntoAsyncFunctionFix extends PsiUpdateModCommandAction { protected ConvertIntoAsyncFunctionFix(@NotNull PyFunction element) { super(element); diff --git a/python/python-syntax-core/resources/messages/PySyntaxCoreBundle.properties b/python/python-syntax-core/resources/messages/PySyntaxCoreBundle.properties index b36a9d9484c7..57622a95fdb9 100644 --- a/python/python-syntax-core/resources/messages/PySyntaxCoreBundle.properties +++ b/python/python-syntax-core/resources/messages/PySyntaxCoreBundle.properties @@ -1,6 +1,3 @@ -### Annotators ### -ANN.function.cannot.be.async=function \"{0}\" cannot be async - ### Formatter formatter.panel.dict.alignment.do.not.align=Do not align formatter.panel.dict.alignment.align.on.colon=Align on colon diff --git a/python/python-syntax-core/src/com/jetbrains/python/validation/PyKeywordHighlightingAnnotator.java b/python/python-syntax-core/src/com/jetbrains/python/validation/PyKeywordHighlightingAnnotator.java index 24af6caa05ed..4965cec0aac9 100644 --- a/python/python-syntax-core/src/com/jetbrains/python/validation/PyKeywordHighlightingAnnotator.java +++ b/python/python-syntax-core/src/com/jetbrains/python/validation/PyKeywordHighlightingAnnotator.java @@ -55,14 +55,6 @@ public final class PyKeywordHighlightingAnnotator extends PyAnnotatorBase implem if (node.isAsyncAllowed()) { highlightKeyword(node, PyTokenTypes.ASYNC_KEYWORD); } - else { - Optional - .ofNullable(node.getNode()) - .map(astNode -> astNode.findChildByType(PyTokenTypes.ASYNC_KEYWORD)) - .ifPresent(asyncNode -> myHolder.newAnnotation(HighlightSeverity.ERROR, - PySyntaxCoreBundle.message("ANN.function.cannot.be.async", node.getName())) - .range(asyncNode).create()); - } } @Override