fixed PY-8567 Unwrap/Remove action is not avalable with caret/selection at the very end of the statement

This commit is contained in:
Ekaterina Tuzova
2013-01-24 15:57:16 +04:00
parent a08116e53f
commit 1f9bbfb4d8
4 changed files with 26 additions and 0 deletions
@@ -2,6 +2,12 @@ package com.jetbrains.python.refactoring.unwrap;
import com.intellij.codeInsight.unwrap.UnwrapDescriptorBase;
import com.intellij.codeInsight.unwrap.Unwrapper;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.python.psi.PyUtil;
import org.jetbrains.annotations.Nullable;
/**
* user : ktisha
@@ -21,4 +27,19 @@ public class PyUnwrapDescriptor extends UnwrapDescriptorBase{
new PyWithUnwrapper()
};
}
@Override
@Nullable
protected PsiElement findTargetElement(Editor editor, PsiFile file) {
int offset = editor.getCaretModel().getOffset();
PsiElement endElement = PyUtil.findNonWhitespaceAtOffset(file, offset);
SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) {
PsiElement startElement = PyUtil.findNonWhitespaceAtOffset(file, selectionModel.getSelectionStart());
if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) {
return startElement;
}
}
return endElement;
}
}
@@ -0,0 +1 @@
x = 1
@@ -0,0 +1,2 @@
while True:
x = 1<caret>
@@ -41,6 +41,8 @@ public class PyUnwrapperTest extends PyTestCase {
public void testWithUnwrap() throws Throwable {doTest(LanguageLevel.PYTHON32);}
public void testEndOfStatementUnwrap() throws Throwable {doTest();}
private void doTest() {
doTest(0);
}