From 7b9bf36bd747597628637066a54ebeb3529b35e9 Mon Sep 17 00:00:00 2001 From: Morgan Bartholomew Date: Tue, 14 Apr 2026 17:47:25 +1000 Subject: [PATCH] refactor python: j2k augmented assignment statements GitOrigin-RevId: 7bc0031faa4027e20bee67e8e0b06cc094d2f16f --- .../python/ast/PyAstAugAssignmentStatement.kt | 42 ++++++++----------- .../python/psi/PyAssignmentExpression.kt | 36 +++++++--------- .../python/psi/PyAugAssignmentStatement.kt | 23 ++++------ .../python/psi/PyQualifiedExpression.kt | 27 +++--------- .../psi/impl/PyAugAssignmentStatementImpl.kt | 20 ++++----- 5 files changed, 52 insertions(+), 96 deletions(-) diff --git a/python/python-ast/src/com/jetbrains/python/ast/PyAstAugAssignmentStatement.kt b/python/python-ast/src/com/jetbrains/python/ast/PyAstAugAssignmentStatement.kt index 4cd8fc8cdcbb..a0783510fcb2 100644 --- a/python/python-ast/src/com/jetbrains/python/ast/PyAstAugAssignmentStatement.kt +++ b/python/python-ast/src/com/jetbrains/python/ast/PyAstAugAssignmentStatement.kt @@ -1,35 +1,27 @@ // Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.jetbrains.python.ast; - -import com.intellij.psi.PsiElement; -import com.jetbrains.python.PyTokenTypes; -import com.jetbrains.python.PythonDialectsTokenSetProvider; -import com.jetbrains.python.ast.impl.PyPsiUtilsCore; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +package com.jetbrains.python.ast +import com.intellij.psi.PsiElement +import com.jetbrains.python.PyTokenTypes +import com.jetbrains.python.PythonDialectsTokenSetProvider +import com.jetbrains.python.ast.impl.PyPsiUtilsCore +import org.jetbrains.annotations.ApiStatus @ApiStatus.Experimental -public interface PyAstAugAssignmentStatement extends PyAstStatement { - default @NotNull PyAstExpression getTarget() { - final PyAstExpression target = childToPsi(PythonDialectsTokenSetProvider.getInstance().getExpressionTokens(), 0); - if (target == null) { - throw new RuntimeException("Target missing in augmented assignment statement"); +interface PyAstAugAssignmentStatement : PyAstStatement { + val target: PyAstExpression + get() { + return childToPsi(PythonDialectsTokenSetProvider.getInstance().expressionTokens, 0) + ?: throw RuntimeException("Target missing in augmented assignment statement") } - return target; - } - default @Nullable PyAstExpression getValue() { - return childToPsi(PythonDialectsTokenSetProvider.getInstance().getExpressionTokens(), 1); - } + val value: PyAstExpression? + get() = childToPsi(PythonDialectsTokenSetProvider.getInstance().expressionTokens, 1) - default @Nullable PsiElement getOperation() { - return PyPsiUtilsCore.getChildByFilter(this, PyTokenTypes.AUG_ASSIGN_OPERATIONS, 0); - } + val operation: PsiElement? + get() = PyPsiUtilsCore.getChildByFilter(this, PyTokenTypes.AUG_ASSIGN_OPERATIONS, 0) - @Override - default void acceptPyVisitor(PyAstElementVisitor pyVisitor) { - pyVisitor.visitPyAugAssignmentStatement(this); + override fun acceptPyVisitor(pyVisitor: PyAstElementVisitor) { + pyVisitor.visitPyAugAssignmentStatement(this) } } diff --git a/python/python-psi-api/src/com/jetbrains/python/psi/PyAssignmentExpression.kt b/python/python-psi-api/src/com/jetbrains/python/psi/PyAssignmentExpression.kt index 816af441a043..df1280190e09 100644 --- a/python/python-psi-api/src/com/jetbrains/python/psi/PyAssignmentExpression.kt +++ b/python/python-psi-api/src/com/jetbrains/python/psi/PyAssignmentExpression.kt @@ -1,29 +1,23 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.jetbrains.python.psi; +// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.jetbrains.python.psi -import com.jetbrains.python.ast.PyAstAssignmentExpression; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; +import com.jetbrains.python.ast.PyAstAssignmentExpression +import org.jetbrains.annotations.ApiStatus /** * Represents assignment expressions introduced in Python 3.8 (PEP 572). */ @ApiStatus.NonExtendable -public interface PyAssignmentExpression extends PyAstAssignmentExpression, PyExpression { +interface PyAssignmentExpression : PyAstAssignmentExpression, PyExpression { + override val target: PyTargetExpression? + /** + * @return LHS of an expression (before :=), null if underlying target is not an identifier. + */ + get() = super.target as PyTargetExpression? - /** - * @return LHS of an expression (before :=), null if underlying target is not an identifier. - */ - @Override - default @Nullable PyTargetExpression getTarget() { - return (PyTargetExpression)PyAstAssignmentExpression.super.getTarget(); - } - - /** - * @return RHS of an expression (after :=), null if assigned value is omitted or not an expression. - */ - @Override - default @Nullable PyExpression getAssignedValue() { - return (PyExpression)PyAstAssignmentExpression.super.getAssignedValue(); - } + override val assignedValue: PyExpression? + /** + * @return RHS of an expression (after :=), null if assigned value is omitted or not an expression. + */ + get() = super.assignedValue as PyExpression? } diff --git a/python/python-psi-api/src/com/jetbrains/python/psi/PyAugAssignmentStatement.kt b/python/python-psi-api/src/com/jetbrains/python/psi/PyAugAssignmentStatement.kt index 4efc49fe7643..5ea4aa16f87e 100644 --- a/python/python-psi-api/src/com/jetbrains/python/psi/PyAugAssignmentStatement.kt +++ b/python/python-psi-api/src/com/jetbrains/python/psi/PyAugAssignmentStatement.kt @@ -1,19 +1,12 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.jetbrains.python.psi; +// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.jetbrains.python.psi -import com.jetbrains.python.ast.PyAstAugAssignmentStatement; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import com.jetbrains.python.ast.PyAstAugAssignmentStatement +interface PyAugAssignmentStatement : PyAstAugAssignmentStatement, PyStatement { + override val target: PyExpression + get() = super.value as PyExpression -public interface PyAugAssignmentStatement extends PyAstAugAssignmentStatement, PyStatement { - @Override - default @NotNull PyExpression getTarget() { - return (PyExpression)PyAstAugAssignmentStatement.super.getTarget(); - } - - @Override - default @Nullable PyExpression getValue() { - return (PyExpression)PyAstAugAssignmentStatement.super.getValue(); - } + override val value: PyExpression? + get() = super.value as PyExpression? } diff --git a/python/python-psi-api/src/com/jetbrains/python/psi/PyQualifiedExpression.kt b/python/python-psi-api/src/com/jetbrains/python/psi/PyQualifiedExpression.kt index 01e9b8118f70..4c29d3bf0572 100644 --- a/python/python-psi-api/src/com/jetbrains/python/psi/PyQualifiedExpression.kt +++ b/python/python-psi-api/src/com/jetbrains/python/psi/PyQualifiedExpression.kt @@ -1,28 +1,11 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.jetbrains.python.psi; +// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.jetbrains.python.psi -import com.jetbrains.python.ast.PyAstQualifiedExpression; -import org.jetbrains.annotations.Nullable; +import com.jetbrains.python.ast.PyAstQualifiedExpression /** * Represents a qualified expression, that is, of "a.b.c..." sort. */ -public interface PyQualifiedExpression extends PyAstQualifiedExpression, PyExpression { - @Override - @Nullable - PyExpression getQualifier(); +interface PyQualifiedExpression : PyAstQualifiedExpression, PyExpression { + override fun getQualifier(): PyExpression? } diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyAugAssignmentStatementImpl.kt b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyAugAssignmentStatementImpl.kt index e21fb9df2e77..4d11650542ab 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyAugAssignmentStatementImpl.kt +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyAugAssignmentStatementImpl.kt @@ -1,18 +1,12 @@ // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.jetbrains.python.psi.impl; +package com.jetbrains.python.psi.impl -import com.intellij.lang.ASTNode; -import com.jetbrains.python.psi.PyAugAssignmentStatement; -import com.jetbrains.python.psi.PyElementVisitor; +import com.intellij.lang.ASTNode +import com.jetbrains.python.psi.PyAugAssignmentStatement +import com.jetbrains.python.psi.PyElementVisitor - -public class PyAugAssignmentStatementImpl extends PyElementImpl implements PyAugAssignmentStatement { - public PyAugAssignmentStatementImpl(ASTNode astNode) { - super(astNode); - } - - @Override - protected void acceptPyVisitor(PyElementVisitor pyVisitor) { - pyVisitor.visitPyAugAssignmentStatement(this); +class PyAugAssignmentStatementImpl(astNode: ASTNode?) : PyElementImpl(astNode), PyAugAssignmentStatement { + override fun acceptPyVisitor(pyVisitor: PyElementVisitor) { + pyVisitor.visitPyAugAssignmentStatement(this) } }