Ipython Notebook: fixed cell movement for cells with the same text

There is no need to replace the whole document if text in cells were the same. We should explicitly
add synchronize request because document is not modified.
This commit is contained in:
Ekaterina Tuzova
2014-12-24 13:15:56 +03:00
parent e3d7d4e47e
commit cfcf760236
4 changed files with 10 additions and 3 deletions
@@ -24,7 +24,6 @@ public class IpnbMoveCellDownAction extends AnAction {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ipnbFilePanel.moveCell(true);
ipnbFilePanel.saveToFile();
}
});
}
@@ -24,7 +24,6 @@ public class IpnbMoveCellUpAction extends AnAction {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ipnbFilePanel.moveCell(false);
ipnbFilePanel.saveToFile();
}
});
}
@@ -224,6 +224,8 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
addCell(siblingPanel, true);
setSelectedCell(selectedCell);
}
saveToFile();
}
public void deleteSelectedCell() {
@@ -248,6 +250,11 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
public void saveToFile() {
final String oldText = myDocument.getText();
final String newText = IpnbParser.newDocumentText(this);
if (newText == null) return;
if (oldText.equals(newText)) {
new Alarm().addRequest(new MySynchronizeRequest(), 10, ModalityState.stateForComponent(this));
return;
}
try {
final ReplaceInfo replaceInfo = findFragmentToChange(oldText, newText);
if (replaceInfo.getStartOffset() != -1) {
@@ -283,7 +290,7 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
}
}
public static ReplaceInfo findFragmentToChange(final String oldText, final String newText) {
public static ReplaceInfo findFragmentToChange(@NotNull final String oldText, @NotNull final String newText) {
if (oldText.equals(newText)) {
return new ReplaceInfo(-1, -1, null);
}
@@ -9,6 +9,7 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
import org.jetbrains.plugins.ipnb.format.cells.*;
@@ -66,6 +67,7 @@ public class IpnbParser {
writeToFile(ipnbPanel.getIpnbFile().getPath(), json);
}
@Nullable
public static String newDocumentText(@NotNull final IpnbFilePanel ipnbPanel) {
final IpnbFile ipnbFile = ipnbPanel.getIpnbFile();
if (ipnbFile == null) return null;