From ea0f6d295ef27cc853f13583d3f4990b23e2c492 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 4 May 2017 14:17:27 +0300 Subject: [PATCH] PY-14224 show line numbers in code cell --- .../actions/IpnbToggleLineNumbersAction.java | 27 +++++++++++++++++++ .../ipnb/editor/panels/IpnbFilePanel.java | 10 ++++--- .../panels/code/IpnbCodeSourcePanel.java | 4 +-- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbToggleLineNumbersAction.java diff --git a/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbToggleLineNumbersAction.java b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbToggleLineNumbersAction.java new file mode 100644 index 000000000000..2872a6feafdb --- /dev/null +++ b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbToggleLineNumbersAction.java @@ -0,0 +1,27 @@ +package org.jetbrains.plugins.ipnb.editor.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorSettings; +import org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel; + +public class IpnbToggleLineNumbersAction extends AnAction { + private final IpnbCodePanel myPanel; + + public IpnbToggleLineNumbersAction(IpnbCodePanel panel) { + super("Toggle line numbers"); + myPanel = panel; + } + + @Override + public void actionPerformed(AnActionEvent e) { + toggleLineNumbers(myPanel); + } + + public static void toggleLineNumbers(IpnbCodePanel panel) { + final Editor editor = panel.getEditor(); + final EditorSettings editorSettings = editor.getSettings(); + editorSettings.setLineNumbersShown(!editorSettings.isLineNumbersShown()); + } +} diff --git a/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/IpnbFilePanel.java b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/IpnbFilePanel.java index b509dac9f03e..3956644cee10 100644 --- a/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/IpnbFilePanel.java +++ b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/IpnbFilePanel.java @@ -33,6 +33,7 @@ import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor; import org.jetbrains.plugins.ipnb.editor.actions.IpnbCutCellAction; import org.jetbrains.plugins.ipnb.editor.actions.IpnbDeleteCellAction; import org.jetbrains.plugins.ipnb.editor.actions.IpnbPasteCellAction; +import org.jetbrains.plugins.ipnb.editor.actions.IpnbToggleLineNumbersAction; import org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel; import org.jetbrains.plugins.ipnb.format.IpnbFile; import org.jetbrains.plugins.ipnb.format.IpnbParser; @@ -196,8 +197,7 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D IpnbEditablePanel panel; if (cell instanceof IpnbCodeCell) { panel = new IpnbCodePanel(myProject, myParent, (IpnbCodeCell)cell); - add(panel); - myIpnbPanels.add(panel); + addComponent(panel); } else if (cell instanceof IpnbMarkdownCell) { panel = new IpnbMarkdownPanel((IpnbMarkdownCell)cell, this); @@ -567,7 +567,6 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D mySelectedCellPanel.switchToEditing(); repaint(); } - if (e.getKeyCode() == KeyEvent.VK_UP) { selectPrev(mySelectedCellPanel); } @@ -579,6 +578,11 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D IpnbDeleteCellAction.deleteCell(this); } } + else if (e.getKeyCode() == KeyEvent.VK_L) { + if (mySelectedCellPanel instanceof IpnbCodePanel && !mySelectedCellPanel.isEditing()) { + IpnbToggleLineNumbersAction.toggleLineNumbers((IpnbCodePanel)mySelectedCellPanel); + } + } else if (e.getModifiers() == Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) { if (e.getKeyCode() == KeyEvent.VK_X) { if (!mySelectedCellPanel.isEditing()) { diff --git a/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/code/IpnbCodeSourcePanel.java b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/code/IpnbCodeSourcePanel.java index e9d73afddc85..055f12039fe2 100644 --- a/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/code/IpnbCodeSourcePanel.java +++ b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/code/IpnbCodeSourcePanel.java @@ -53,13 +53,11 @@ public class IpnbCodeSourcePanel extends IpnbPanel imp private Editor myEditor; @NotNull private final Project myProject; @NotNull private final IpnbCodePanel myParent; - @NotNull private final String mySource; public IpnbCodeSourcePanel(@NotNull final Project project, @NotNull final IpnbCodePanel parent, @NotNull final IpnbCodeCell cell) { super(cell, new HorizontalLayout(5)); myProject = project; myParent = parent; - mySource = cell.getSourceAsString(); final JComponent panel = createViewPanel(); add(panel); addRightClickMenu(); @@ -210,7 +208,7 @@ public class IpnbCodeSourcePanel extends IpnbPanel imp final MouseEvent mouseEvent = e.getMouseEvent(); if (SwingUtilities.isRightMouseButton(mouseEvent) && mouseEvent.getClickCount() == 1) { final ListPopup menu = createPopupMenu(new DefaultActionGroup(new IpnbMergeCellAboveAction(), new IpnbMergeCellBelowAction(), - new IpnbSplitCellAction())); + new IpnbSplitCellAction(), new IpnbToggleLineNumbersAction(myParent))); menu.show(RelativePoint.fromScreen(e.getMouseEvent().getLocationOnScreen())); } }