From 3b9b10b296b74a13dd1854f997b968b6ae8bd3b7 Mon Sep 17 00:00:00 2001 From: Petr Date: Sat, 10 Feb 2024 16:20:02 +0100 Subject: [PATCH] PY-61639 Move extendWordSelectionHandlers to python.syntax GitOrigin-RevId: d5957878544a0026f249109ca1df0a18e20a8a55 --- python/python-ast/intellij.python.ast.iml | 1 + .../ast/PyAstStringLiteralExpression.java | 55 +++++++++++++++++++ .../python/psi/PyStringLiteralExpression.java | 33 ++--------- .../impl/PyStringLiteralExpressionImpl.java | 24 +------- .../resources/META-INF/PythonSyntax.xml | 6 ++ .../PyBasicWordSelectionFilter.java | 0 .../selectWord/PyCommaSelectionHandler.java | 7 ++- .../selectWord/PyListSelectionHandler.java | 8 +-- .../selectWord/PyLiteralSelectionHandler.java | 4 +- .../PyStatementSelectionHandler.java | 12 ++-- .../selectWord/PyWordSelectionHandler.java | 0 python/src/intellij.python.community.impl.xml | 6 -- 12 files changed, 86 insertions(+), 70 deletions(-) rename python/{ => python-syntax}/src/com/jetbrains/python/editor/selectWord/PyBasicWordSelectionFilter.java (100%) rename python/{ => python-syntax}/src/com/jetbrains/python/editor/selectWord/PyCommaSelectionHandler.java (93%) rename python/{ => python-syntax}/src/com/jetbrains/python/editor/selectWord/PyListSelectionHandler.java (88%) rename python/{ => python-syntax}/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java (92%) rename python/{ => python-syntax}/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java (78%) rename python/{ => python-syntax}/src/com/jetbrains/python/editor/selectWord/PyWordSelectionHandler.java (100%) diff --git a/python/python-ast/intellij.python.ast.iml b/python/python-ast/intellij.python.ast.iml index b78aee9f81a6..5657c7812e74 100644 --- a/python/python-ast/intellij.python.ast.iml +++ b/python/python-ast/intellij.python.ast.iml @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/python/python-ast/src/com/jetbrains/python/ast/PyAstStringLiteralExpression.java b/python/python-ast/src/com/jetbrains/python/ast/PyAstStringLiteralExpression.java index 62ff5564f16c..634aa4153385 100644 --- a/python/python-ast/src/com/jetbrains/python/ast/PyAstStringLiteralExpression.java +++ b/python/python-ast/src/com/jetbrains/python/ast/PyAstStringLiteralExpression.java @@ -15,12 +15,22 @@ */ package com.jetbrains.python.ast; +import com.intellij.lang.ASTNode; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.ElementManipulators; import com.intellij.psi.PsiLanguageInjectionHost; +import com.intellij.psi.tree.TokenSet; +import com.intellij.util.containers.ContainerUtil; +import com.jetbrains.python.PyElementTypes; +import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.StringLiteralExpression; +import one.util.streamex.StreamEx; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; +import java.util.List; + /** * A string literal expression can consist of a number of implicitly concatenated string literals @@ -63,4 +73,49 @@ public interface PyAstStringLiteralExpression extends PyAstLiteralExpression, St default void acceptPyVisitor(PyAstElementVisitor pyVisitor) { pyVisitor.visitPyStringLiteralExpression(this); } + + @NotNull + default List getStringNodes() { + final TokenSet stringNodeTypes = TokenSet.orSet(PyTokenTypes.STRING_NODES, TokenSet.create(PyElementTypes.FSTRING_NODE)); + return Arrays.asList(getNode().getChildren(stringNodeTypes)); + } + + /** + * Returns a list of implicitly concatenated string elements composing this literal expression. + */ + @NotNull + default List getStringElements() { + return StreamEx.of(getStringNodes()) + .map(ASTNode::getPsi) + .select(PyAstStringElement.class) + .toList(); + } + + /** + * Returns value ranges for all nodes that form this string literal expression relative to its start offset. + * Such range doesn't include neither node's prefix like "ur", nor its quotes. + *

+ * For example, for the next "glued" string literal: + *

{@code
+   * u"\u0066\x6F\157" ur'' '''\t'''
+   * }
+ *

+ * this method returns: + *

+ *

+   * [
+   *   [2,16),
+   *   [21,21),
+   *   [26,28),
+   * ]
+   * 
+ */ + @NotNull + default List getStringValueTextRanges() { + final int elementStart = getTextRange().getStartOffset(); + return ContainerUtil.map(getStringElements(), node -> { + final int nodeRelativeOffset = node.getTextRange().getStartOffset() - elementStart; + return node.getContentRange().shiftRight(nodeRelativeOffset); + }); + } } diff --git a/python/python-psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java b/python/python-psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java index bf4a62eb59d4..b58302eecb6e 100644 --- a/python/python-psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java +++ b/python/python-psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java @@ -51,14 +51,15 @@ import java.util.List; * @see PyFormattedStringElement */ public interface PyStringLiteralExpression extends PyAstStringLiteralExpression, PyLiteralExpression, StringLiteralExpression, PsiLanguageInjectionHost { - @NotNull - List getStringNodes(); - /** - * Returns a list of implicitly concatenated string elements composing this literal expression. + * Returns a list of implicitly concatenated string elements composing this literal expression. */ @NotNull - List getStringElements(); + @Override + default List getStringElements() { + //noinspection unchecked + return (List)PyAstStringLiteralExpression.super.getStringElements(); + } int valueOffsetToTextOffset(int valueOffset); @@ -91,28 +92,6 @@ public interface PyStringLiteralExpression extends PyAstStringLiteralExpression, @NotNull List> getDecodedFragments(); - /** - * Returns value ranges for all nodes that form this string literal expression relative to its start offset. - * Such range doesn't include neither node's prefix like "ur", nor its quotes. - *

- * For example, for the next "glued" string literal: - *

{@code
-   * u"\u0066\x6F\157" ur'' '''\t'''
-   * }
- *

- * this method returns: - *

- *

-   * [
-   *   [2,16),
-   *   [21,21),
-   *   [26,28),
-   * ]
-   * 
- */ - @NotNull - List getStringValueTextRanges(); - /** * @return true if this element has single string node and its type is {@link com.jetbrains.python.PyTokenTypes#DOCSTRING} */ diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java index 009f3a8e96be..307602cf1eae 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java @@ -16,6 +16,7 @@ import com.intellij.util.ArrayUtil; import com.intellij.util.containers.ContainerUtil; import com.jetbrains.python.PyElementTypes; import com.jetbrains.python.PyTokenTypes; +import com.jetbrains.python.ast.PyAstStringLiteralExpression; import com.jetbrains.python.lexer.PythonHighlightingLexer; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.types.PyType; @@ -56,12 +57,7 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt public List getStringValueTextRanges() { List result = myValueTextRanges; if (result == null) { - final int elementStart = getTextRange().getStartOffset(); - final List ranges = ContainerUtil.map(getStringElements(), node -> { - final int nodeRelativeOffset = node.getTextRange().getStartOffset() - elementStart; - return node.getContentRange().shiftRight(nodeRelativeOffset); - }); - myValueTextRanges = result = ranges; + myValueTextRanges = result = PyStringLiteralExpression.super.getStringValueTextRanges(); } return result; } @@ -97,22 +93,6 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt .anyMatch(element -> !element.getFragments().isEmpty()); } - @Override - @NotNull - public List getStringNodes() { - final TokenSet stringNodeTypes = TokenSet.orSet(PyTokenTypes.STRING_NODES, TokenSet.create(PyElementTypes.FSTRING_NODE)); - return Arrays.asList(getNode().getChildren(stringNodeTypes)); - } - - @NotNull - @Override - public List getStringElements() { - return StreamEx.of(getStringNodes()) - .map(ASTNode::getPsi) - .select(PyStringElement.class) - .toList(); - } - @NotNull @Override public String getStringValue() { diff --git a/python/python-syntax/resources/META-INF/PythonSyntax.xml b/python/python-syntax/resources/META-INF/PythonSyntax.xml index f3d1704d8851..caa5eff8f6e3 100644 --- a/python/python-syntax/resources/META-INF/PythonSyntax.xml +++ b/python/python-syntax/resources/META-INF/PythonSyntax.xml @@ -6,5 +6,11 @@ + + + + + + \ No newline at end of file diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyBasicWordSelectionFilter.java b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyBasicWordSelectionFilter.java similarity index 100% rename from python/src/com/jetbrains/python/editor/selectWord/PyBasicWordSelectionFilter.java rename to python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyBasicWordSelectionFilter.java diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyCommaSelectionHandler.java b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyCommaSelectionHandler.java similarity index 93% rename from python/src/com/jetbrains/python/editor/selectWord/PyCommaSelectionHandler.java rename to python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyCommaSelectionHandler.java index 8bb90f156e33..ea7cbc6bc7d9 100644 --- a/python/src/com/jetbrains/python/editor/selectWord/PyCommaSelectionHandler.java +++ b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyCommaSelectionHandler.java @@ -23,6 +23,7 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiWhiteSpace; import com.intellij.psi.tree.IElementType; import com.jetbrains.python.PyTokenTypes; +import com.jetbrains.python.ast.*; import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; @@ -37,9 +38,9 @@ import java.util.List; public final class PyCommaSelectionHandler extends ExtendWordSelectionHandlerBase { @Override public boolean canSelect(@NotNull final PsiElement e) { - return e instanceof PyReferenceExpression || e instanceof PyKeyValueExpression || e instanceof PyKeywordArgument - || e instanceof PyNumericLiteralExpression || e instanceof PyStringLiteralExpression || e instanceof PyNamedParameter - || e instanceof PyStarArgument; + return e instanceof PyAstReferenceExpression || e instanceof PyAstKeyValueExpression || e instanceof PyAstKeywordArgument + || e instanceof PyAstNumericLiteralExpression || e instanceof PyAstStringLiteralExpression || e instanceof PyAstNamedParameter + || e instanceof PyAstStarArgument; } @Override diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyListSelectionHandler.java b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyListSelectionHandler.java similarity index 88% rename from python/src/com/jetbrains/python/editor/selectWord/PyListSelectionHandler.java rename to python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyListSelectionHandler.java index 00431688f155..f1d4990cc00f 100644 --- a/python/src/com/jetbrains/python/editor/selectWord/PyListSelectionHandler.java +++ b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyListSelectionHandler.java @@ -21,9 +21,9 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiErrorElement; import com.intellij.psi.PsiWhiteSpace; -import com.jetbrains.python.psi.PyArgumentList; -import com.jetbrains.python.psi.PyListLiteralExpression; -import com.jetbrains.python.psi.PyParameterList; +import com.jetbrains.python.ast.PyAstArgumentList; +import com.jetbrains.python.ast.PyAstListLiteralExpression; +import com.jetbrains.python.ast.PyAstParameterList; import org.jetbrains.annotations.NotNull; import java.util.Collections; @@ -37,7 +37,7 @@ import java.util.List; public final class PyListSelectionHandler extends ExtendWordSelectionHandlerBase { @Override public boolean canSelect(@NotNull PsiElement e) { - return e instanceof PyListLiteralExpression || e instanceof PyParameterList || e instanceof PyArgumentList; + return e instanceof PyAstListLiteralExpression || e instanceof PyAstParameterList || e instanceof PyAstArgumentList; } @Override diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java similarity index 92% rename from python/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java rename to python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java index 34983fe7fb33..9edb0f78a386 100644 --- a/python/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java +++ b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java @@ -9,8 +9,8 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.PyTokenTypes; +import com.jetbrains.python.ast.PyAstStringLiteralExpression; import com.jetbrains.python.lexer.PyStringLiteralLexer; -import com.jetbrains.python.psi.PyStringLiteralExpression; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -27,7 +27,7 @@ public final class PyLiteralSelectionHandler extends ExtendWordSelectionHandlerB @Override public List select(@NotNull PsiElement e, @NotNull CharSequence editorText, int cursorOffset, @NotNull Editor editor) { - final PyStringLiteralExpression literal = PsiTreeUtil.getParentOfType(e, PyStringLiteralExpression.class); + final PyAstStringLiteralExpression literal = PsiTreeUtil.getParentOfType(e, PyAstStringLiteralExpression.class); if (literal != null) { List ranges = literal.getStringValueTextRanges(); List nodes = literal.getStringNodes(); diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java similarity index 78% rename from python/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java rename to python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java index 13c8e3991357..f4b18d8ea20b 100644 --- a/python/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java +++ b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java @@ -6,10 +6,10 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiWhiteSpace; -import com.jetbrains.python.psi.PyCallExpression; -import com.jetbrains.python.psi.PyStatement; -import com.jetbrains.python.psi.PyStatementList; -import com.jetbrains.python.psi.PyStringLiteralExpression; +import com.jetbrains.python.ast.PyAstCallExpression; +import com.jetbrains.python.ast.PyAstStatement; +import com.jetbrains.python.ast.PyAstStatementList; +import com.jetbrains.python.ast.PyAstStringLiteralExpression; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -19,8 +19,8 @@ import java.util.List; public final class PyStatementSelectionHandler extends ExtendWordSelectionHandlerBase { @Override public boolean canSelect(@NotNull final PsiElement e) { - return e instanceof PyStringLiteralExpression || e instanceof PyCallExpression || e instanceof PyStatement || - e instanceof PyStatementList; + return e instanceof PyAstStringLiteralExpression || e instanceof PyAstCallExpression || e instanceof PyAstStatement || + e instanceof PyAstStatementList; } @Override diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyWordSelectionHandler.java b/python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyWordSelectionHandler.java similarity index 100% rename from python/src/com/jetbrains/python/editor/selectWord/PyWordSelectionHandler.java rename to python/python-syntax/src/com/jetbrains/python/editor/selectWord/PyWordSelectionHandler.java diff --git a/python/src/intellij.python.community.impl.xml b/python/src/intellij.python.community.impl.xml index ac453e6646bd..994a76d60f00 100644 --- a/python/src/intellij.python.community.impl.xml +++ b/python/src/intellij.python.community.impl.xml @@ -106,12 +106,6 @@ - - - - - -