diff --git a/python/src/META-INF/python-plugin-common.xml b/python/src/META-INF/python-plugin-common.xml index 357c5c060491..f8d3597b9b9d 100644 --- a/python/src/META-INF/python-plugin-common.xml +++ b/python/src/META-INF/python-plugin-common.xml @@ -24,6 +24,7 @@ + diff --git a/python/src/com/jetbrains/python/editor/selectWord/PySliceSelectHandler.java b/python/src/com/jetbrains/python/editor/selectWord/PySliceSelectHandler.java new file mode 100644 index 000000000000..a8dabf9c1eae --- /dev/null +++ b/python/src/com/jetbrains/python/editor/selectWord/PySliceSelectHandler.java @@ -0,0 +1,41 @@ +package com.jetbrains.python.editor.selectWord; + +import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; +import com.jetbrains.python.psi.PyExpression; +import com.jetbrains.python.psi.PySliceExpression; + +import java.util.Collections; +import java.util.List; + +/** + * @author yole + */ +public class PySliceSelectHandler extends ExtendWordSelectionHandlerBase { + @Override + public boolean canSelect(PsiElement e) { + return e instanceof PySliceExpression; + } + + @Override + public List select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { + PySliceExpression slice = (PySliceExpression) e; + int sliceStart = slice.getLowerBound().getTextOffset(); + + int sliceEnd; + final PyExpression stride = slice.getStride(); + if (stride != null) { + sliceEnd = stride.getTextRange().getEndOffset(); + } + else { + sliceEnd = slice.getUpperBound().getTextRange().getEndOffset(); + } + + if (cursorOffset >= sliceStart && cursorOffset < sliceEnd) { + return Collections.singletonList(new TextRange(sliceStart, sliceEnd)); + } + return Collections.emptyList(); + } +} diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java b/python/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java index a40553466cbb..168f84a1670b 100644 --- a/python/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java +++ b/python/src/com/jetbrains/python/editor/selectWord/PyStatementSelectionHandler.java @@ -16,7 +16,7 @@ import java.util.List; public class PyStatementSelectionHandler extends ExtendWordSelectionHandlerBase { public boolean canSelect(final PsiElement e) { return e instanceof PyStringLiteralExpression || e instanceof PyCallExpression || e instanceof PyStatement || - e instanceof PyStatementList || e instanceof PyFunction || e instanceof PyClass; + e instanceof PyStatementList; } public List select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) { diff --git a/python/src/com/jetbrains/python/psi/PySliceExpression.java b/python/src/com/jetbrains/python/psi/PySliceExpression.java index e5c324f5dc16..87bfa14e9c03 100644 --- a/python/src/com/jetbrains/python/psi/PySliceExpression.java +++ b/python/src/com/jetbrains/python/psi/PySliceExpression.java @@ -1,28 +1,15 @@ -/* - * Copyright 2005 Pythonid Project - * - * 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; +import org.jetbrains.annotations.Nullable; + /** - * Created by IntelliJ IDEA. - * User: yole - * Date: 29.05.2005 - * Time: 14:26:17 - * To change this template use File | Settings | File Templates. + * @author yole */ public interface PySliceExpression extends PyExpression { PyExpression getOperand(); + PyExpression getLowerBound(); + PyExpression getUpperBound(); + + @Nullable + PyExpression getStride(); } diff --git a/python/src/com/jetbrains/python/psi/impl/PySliceExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PySliceExpressionImpl.java index 7d213db26ddf..c60a155d940f 100644 --- a/python/src/com/jetbrains/python/psi/impl/PySliceExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PySliceExpressionImpl.java @@ -1,19 +1,3 @@ -/* - * Copyright 2005 Pythonid Project - * - * 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.impl; import com.intellij.lang.ASTNode; @@ -21,18 +5,15 @@ import com.jetbrains.python.PyElementTypes; import com.jetbrains.python.psi.PyExpression; import com.jetbrains.python.psi.PySliceExpression; import com.jetbrains.python.psi.types.PyType; +import org.jetbrains.annotations.Nullable; /** - * Created by IntelliJ IDEA. - * User: yole - * Date: 29.05.2005 - * Time: 14:26:31 - * To change this template use File | Settings | File Templates. + * @author yole */ public class PySliceExpressionImpl extends PyElementImpl implements PySliceExpression { - public PySliceExpressionImpl(ASTNode astNode) { - super(astNode); - } + public PySliceExpressionImpl(ASTNode astNode) { + super(astNode); + } public PyType getType() { return getOperand().getType(); @@ -41,4 +22,17 @@ public class PySliceExpressionImpl extends PyElementImpl implements PySliceExpre public PyExpression getOperand() { return childToPsiNotNull(PyElementTypes.EXPRESSIONS, 0); } + + public PyExpression getLowerBound() { + return childToPsiNotNull(PyElementTypes.EXPRESSIONS, 1); + } + + public PyExpression getUpperBound() { + return childToPsi(PyElementTypes.EXPRESSIONS, 2); + } + + @Nullable + public PyExpression getStride() { + return childToPsi(PyElementTypes.EXPRESSIONS, 3); + } } diff --git a/python/testData/selectWord/slice/after1.py b/python/testData/selectWord/slice/after1.py new file mode 100644 index 000000000000..504e3dfab440 --- /dev/null +++ b/python/testData/selectWord/slice/after1.py @@ -0,0 +1 @@ +"hello world again"[10:12] \ No newline at end of file diff --git a/python/testData/selectWord/slice/after2.py b/python/testData/selectWord/slice/after2.py new file mode 100644 index 000000000000..3c0cc481345f --- /dev/null +++ b/python/testData/selectWord/slice/after2.py @@ -0,0 +1 @@ +"hello world again"[10:12] \ No newline at end of file diff --git a/python/testData/selectWord/slice/before.py b/python/testData/selectWord/slice/before.py new file mode 100644 index 000000000000..a107fe6e84b4 --- /dev/null +++ b/python/testData/selectWord/slice/before.py @@ -0,0 +1 @@ +"hello world again"[10:12] \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PySelectWordTest.java b/python/testSrc/com/jetbrains/python/PySelectWordTest.java index 8a2723eef3b3..b60ef56e7b9d 100644 --- a/python/testSrc/com/jetbrains/python/PySelectWordTest.java +++ b/python/testSrc/com/jetbrains/python/PySelectWordTest.java @@ -15,6 +15,10 @@ public class PySelectWordTest extends PyLightFixtureTestCase { doTest(); } + public void testSlice() throws Exception { // PY-288 + doTest(); + } + private void doTest() throws Exception { myFixture.copyDirectoryToProject("", ""); @NonNls final String path = getTestName(true);