fixed PY-14503 IPython Notebook, command-shift-up/down moves cell correctly, but scrolls to another location in the notebook

This commit is contained in:
Ekaterina Tuzova
2014-12-25 17:38:20 +03:00
parent cfcf760236
commit ea308e9c12
4 changed files with 23 additions and 45 deletions
@@ -19,7 +19,6 @@ import org.jetbrains.plugins.ipnb.IpnbFileType;
public class IpnbEditorProvider implements FileEditorProvider, DumbAware {
@NonNls private static final String SELECTED_CELL = "selected";
@NonNls private static final String ID = "id";
@NonNls private static final String TOP = "top";
@Override
public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
@@ -40,10 +39,9 @@ public class IpnbEditorProvider implements FileEditorProvider, DumbAware {
@NotNull
@Override
public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) {
final IpnbEditorState state = new IpnbEditorState(-1, 0, 0);
final IpnbEditorState state = new IpnbEditorState(-1, 0);
final Element child = sourceElement.getChild(SELECTED_CELL);
state.setSelectedIndex(child == null ? 0 : Integer.parseInt(child.getAttributeValue(ID)));
state.setSelectedTop(child == null ? 0 : Integer.parseInt(child.getAttributeValue(TOP)));
return state;
}
@@ -51,10 +49,8 @@ public class IpnbEditorProvider implements FileEditorProvider, DumbAware {
public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) {
IpnbEditorState editorState = (IpnbEditorState)state;
final int id = editorState.getSelectedIndex();
final int location = editorState.getSelectedTop();
final Element element = new Element(SELECTED_CELL);
element.setAttribute(ID, String.valueOf(id));
element.setAttribute(TOP, String.valueOf(location));
targetElement.addContent(element);
}
@@ -21,12 +21,10 @@ import com.intellij.openapi.fileEditor.FileEditorStateLevel;
final class IpnbEditorState implements FileEditorState{
private final transient long myDocumentModificationStamp; // should not be serialized
private int mySelectedIndex = 0;
private int mySelectedTop;
public IpnbEditorState(final long modificationStamp, int selectedComponentIndex, int top) {
public IpnbEditorState(final long modificationStamp, int selectedComponentIndex) {
myDocumentModificationStamp = modificationStamp;
mySelectedIndex = selectedComponentIndex;
mySelectedTop = top;
}
public boolean equals(final Object o) {
@@ -38,14 +36,6 @@ final class IpnbEditorState implements FileEditorState{
return myDocumentModificationStamp == state.myDocumentModificationStamp;
}
public int getSelectedTop() {
return mySelectedTop;
}
public void setSelectedTop(int selectedTop) {
mySelectedTop = selectedTop;
}
public void setSelectedIndex(int selectedIndex) {
mySelectedIndex = selectedIndex;
}
@@ -317,10 +317,23 @@ public class IpnbFileEditor extends UserDataHolderBase implements FileEditor {
public void selectionChanged(@NotNull IpnbPanel ipnbPanel) {
if (myCellTypeCombo == null) return;
updateCellTypeCombo(ipnbPanel);
updateScrollPosition(ipnbPanel);
}
});
}
private void updateScrollPosition(@NotNull final IpnbPanel ipnbPanel) {
final Rectangle rect = myIpnbFilePanel.getVisibleRect();
final Rectangle cellBounds = ipnbPanel.getBounds();
if (cellBounds.getY() <= rect.getY()) {
myScrollPane.getVerticalScrollBar().setValue(cellBounds.y);
}
if (cellBounds.getY() + cellBounds.getHeight() > rect.getY() + rect.getHeight()) {
myScrollPane.getVerticalScrollBar().setValue(cellBounds.y - rect.height + cellBounds.height);
}
}
private void updateCellTypeCombo(@NotNull final IpnbPanel ipnbPanel) {
if (ipnbPanel instanceof IpnbHeadingPanel) {
final IpnbHeadingCell cell = ((IpnbHeadingPanel)ipnbPanel).getCell();
@@ -360,18 +373,15 @@ public class IpnbFileEditor extends UserDataHolderBase implements FileEditor {
@Override
public FileEditorState getState(@NotNull FileEditorStateLevel level) {
final int index = getIpnbFilePanel().getSelectedIndex();
final IpnbEditablePanel cell = getIpnbFilePanel().getSelectedCell();
final int top = cell != null ? cell.getTop() : 0;
final Document document = FileDocumentManager.getInstance().getCachedDocument(myFile);
long modificationStamp = document != null ? document.getModificationStamp() : myFile.getModificationStamp();
return new IpnbEditorState(modificationStamp, index, top);
return new IpnbEditorState(modificationStamp, index);
}
@Override
public void setState(@NotNull FileEditorState state) {
final int index = ((IpnbEditorState)state).getSelectedIndex();
final int position = ((IpnbEditorState)state).getSelectedTop();
myIpnbFilePanel.setInitialPosition(index, position);
myIpnbFilePanel.setInitialPosition(index);
}
@Override
@@ -58,9 +58,7 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
@Nullable private IpnbEditablePanel mySelectedCell;
boolean switchToEditing = false;
private IpnbEditablePanel myBufferPanel;
private int myIncrement = 10;
private int myInitialSelection = 0;
private int myInitialPosition = 0;
public IpnbFilePanel(@NotNull final Project project, @NotNull final IpnbFileEditor parent, @NotNull final VirtualFile vFile,
@NotNull final IpnbFileEditor.CellSelectionListener listener) {
@@ -147,7 +145,6 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
toSelect.switchToEditing();
switchToEditing = false;
}
myParent.getScrollPane().getViewport().setViewPosition(new Point(0, myInitialPosition));
}
add(createEmptyPanel());
}
@@ -216,6 +213,7 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
final IpnbEditableCell cell = selectedCell.getCell();
myIpnbFile.addCell(cell, index + 1);
myIpnbPanels.add(index + 1, selectedCell);
selectPrev(selectedCell);
setSelectedCell(selectedCell);
}
else {
@@ -412,29 +410,12 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
mySelectedCell.switchToEditing();
repaint();
}
int index = myIpnbPanels.indexOf(mySelectedCell);
final Rectangle rect = getVisibleRect();
if (e.getKeyCode() == KeyEvent.VK_UP) {
selectPrev(mySelectedCell);
if (index > 0) {
final Rectangle cellBounds = mySelectedCell.getBounds();
if (cellBounds.getY() <= rect.getY()) {
myIncrement = rect.y - cellBounds.y;
getParent().dispatchEvent(e);
}
}
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
selectNext(mySelectedCell);
if (index < myIpnbPanels.size() - 1) {
final Rectangle cellBounds = mySelectedCell.getBounds();
if (cellBounds.getY() + cellBounds.getHeight() > rect.getY() + rect.getHeight()) {
myIncrement = cellBounds.y + cellBounds.height - rect.y - rect.height;
getParent().dispatchEvent(e);
}
}
}
else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
if (!mySelectedCell.isEditing()) {
@@ -514,9 +495,8 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
}
}
public void setInitialPosition(int index, int position) {
public void setInitialPosition(int index) {
myInitialSelection = index;
myInitialPosition = position;
}
public void setSelectedCell(@NotNull final IpnbEditablePanel ipnbPanel) {
@@ -527,7 +507,9 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
revalidate();
UIUtil.requestFocus(this);
repaint();
myListener.selectionChanged(ipnbPanel);
if (ipnbPanel.getBounds().getHeight() != 0) {
myListener.selectionChanged(ipnbPanel);
}
}
@Nullable
@@ -561,7 +543,7 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return myIncrement;
return 10;
}