smart handling for slices in select word (PY-288)

This commit is contained in:
Dmitry Jemerov
2010-01-19 21:17:09 +03:00
parent a9cdeae01f
commit df885d659c
9 changed files with 76 additions and 46 deletions
@@ -24,6 +24,7 @@
<codeInsight.parameterInfo language="Python" implementationClass="com.jetbrains.python.PyParameterInfoHandler"/>
<colorSettingsPage implementation="com.jetbrains.python.PythonColorsPage"/>
<extendWordSelectionHandler implementation="com.jetbrains.python.editor.selectWord.PyWordSelectionHandler"/>
<extendWordSelectionHandler implementation="com.jetbrains.python.editor.selectWord.PySliceSelectHandler"/>
<extendWordSelectionHandler implementation="com.jetbrains.python.editor.selectWord.PyStatementSelectionHandler"/>
<completion.contributor language="Python" implementationClass="com.jetbrains.python.codeInsight.PyKeywordCompletionContributor"/>
<completion.contributor language="Python" implementationClass="com.jetbrains.python.codeInsight.PySpecialMethodNamesCompletionContributor"/>
@@ -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<TextRange> 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();
}
}
@@ -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<TextRange> select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) {
@@ -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();
}
@@ -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);
}
}
@@ -0,0 +1 @@
"hello world again"[<selection>10</selection>:12]
@@ -0,0 +1 @@
"hello world again"[<selection>10:12</selection>]
@@ -0,0 +1 @@
"hello world again"[1<caret>0:12]
@@ -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);