Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Improve cells support in the Editor #20509

Merged
merged 22 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions spyder/plugins/editor/panels/codefolding.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from spyder.api.panel import Panel
from spyder.plugins.editor.utils.editor import (TextHelper, DelayJobRunner,
drift_color)
from spyder.plugins.outlineexplorer.api import (OutlineExplorerData as OED,
dalthviz marked this conversation as resolved.
Show resolved Hide resolved
is_cell_header)
from spyder.utils.icon_manager import ima
from spyder.utils.palette import QStylePalette

Expand Down Expand Up @@ -239,6 +241,14 @@ def paintEvent(self, event):
# on the folding panel.
super(FoldingPanel, self).paintEvent(event)
painter = QPainter(self)
cell_line_color = QColor(Qt.darkGray)
dalthviz marked this conversation as resolved.
Show resolved Hide resolved
pen = painter.pen()
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
pen.setStyle(Qt.SolidLine)
pen.setBrush(cell_line_color)
painter.setPen(pen)
for top_position, line_number, block in self.editor.visible_blocks:
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
if is_cell_header(block):
painter.drawLine(0, top_position, self.width(), top_position)

if not self._display_folding and not self._key_pressed:
if any(self.folding_status.values()):
Expand All @@ -247,13 +257,20 @@ def paintEvent(self, event):
self._draw_collapsed_indicator(
line_number, top_position, block,
painter, mouse_hover=True)
if is_cell_header(block):
painter.drawLine(0, top_position, self.width(),
top_position)
ccordoba12 marked this conversation as resolved.
Show resolved Hide resolved
return

# Draw background over the selected non collapsed fold region
if self._mouse_over_line is not None:
block = self.editor.document().findBlockByNumber(
self._mouse_over_line)
try:
self._draw_fold_region_background(block, painter)
if is_cell_header(block):
painter.drawLine(0, top_position, self.width(),
top_position)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary either.

except (ValueError, KeyError):
# Catching the KeyError above is necessary to avoid
# issue spyder-ide/spyder#10918.
Expand All @@ -263,6 +280,8 @@ def paintEvent(self, event):
pass
# Draw fold triggers
for top_position, line_number, block in self.editor.visible_blocks:
if is_cell_header(block):
painter.drawLine(0, top_position, self.width(), top_position)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nor this.

self._draw_collapsed_indicator(
line_number, top_position, block, painter, mouse_hover=False)

Expand Down
12 changes: 11 additions & 1 deletion spyder/plugins/editor/panels/linenumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from spyder.utils.icon_manager import ima
from spyder.api.panel import Panel
from spyder.plugins.completion.api import DiagnosticSeverity
from spyder.plugins.outlineexplorer.api import (OutlineExplorerData as OED,
dalthviz marked this conversation as resolved.
Show resolved Hide resolved
is_cell_header)


class LineNumberArea(Panel):
Expand Down Expand Up @@ -69,7 +71,7 @@ def paintEvent(self, event):

Painting line number area
"""
painter = QPainter(self)
painter = QPainter(self)
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
painter.fillRect(event.rect(), self.editor.sideareas_color)
font_height = self.editor.fontMetrics().height()

Expand Down Expand Up @@ -98,6 +100,14 @@ def draw_pixmap(xleft, ytop, pixmap):
for top, line_number, block in self.editor.visible_blocks:
data = block.userData()
if self._markers_margin and data:
if is_cell_header(block):
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
cell_line_color = QColor(Qt.darkGray)
dalthviz marked this conversation as resolved.
Show resolved Hide resolved
pen = painter.pen()
pen.setStyle(Qt.SolidLine)
pen.setBrush(cell_line_color)
painter.setPen(pen)
painter.drawLine(0, top, self.width(), top)

if data.code_analysis:
errors = 0
warnings = 0
Expand Down
23 changes: 19 additions & 4 deletions spyder/plugins/editor/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,12 @@ def get_plugin_actions(self):

re_run_action = create_action(self, _("Re-run &last script"),
icon=ima.icon('run_again'),
tip=_("Run again last file"),
triggered=self.re_run_file)
tip=_("Run again last file"),
triggered=self.re_run_file)
self.register_shortcut(re_run_action, context="_",
name="Re-run last script",
add_shortcut_to_tip=True)

jsbautista marked this conversation as resolved.
Show resolved Hide resolved
run_selected_action = create_action(self, _("Run &selection or "
"current line"),
icon=ima.icon('run_selection'),
Expand Down Expand Up @@ -774,6 +774,14 @@ def get_plugin_actions(self):
add_shortcut_to_tip=True)

# --- Edit Toolbar ---
create_new_cell = create_action(self, _("Create new Cell at "
"current line"),
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
icon=ima.icon('run_selection'),
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
tip=_("Create new Cell"),
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
triggered=self.create_cell,
context=Qt.WidgetShortcut)
self.register_shortcut(create_new_cell, context="Editor",
name="create_new_cell")
self.toggle_comment_action = create_action(self,
_("Comment")+"/"+_("Uncomment"), icon=ima.icon('comment'),
tip=_("Comment current line or selection"),
Expand Down Expand Up @@ -1074,7 +1082,8 @@ def get_plugin_actions(self):
self.main.run_menu_actions = (
run_menu_actions + self.main.run_menu_actions)
run_toolbar_actions = [run_action, run_cell_action,
run_cell_advance_action, run_selected_action]
run_cell_advance_action, run_selected_action,
create_new_cell]
dalthviz marked this conversation as resolved.
Show resolved Hide resolved
self.main.run_toolbar_actions += run_toolbar_actions

# ---- Source menu/toolbar construction ----
Expand Down Expand Up @@ -2895,6 +2904,12 @@ def run_selection(self, prefix=None):
editorstack = self.get_current_editorstack()
editorstack.run_selection(prefix)

@Slot()
def create_cell(self, prefix=None):
editor = self.get_current_editor()
if editor is not None:
editor.create_new_cell()

@Slot()
def run_to_line(self):
"""Run all lines from beginning up to current line"""
Expand Down
25 changes: 24 additions & 1 deletion spyder/plugins/editor/widgets/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ def create_shortcuts(self):
('editor', 'go to definition', self.go_to_definition_from_cursor),
('editor', 'toggle comment', self.toggle_comment),
('editor', 'blockcomment', self.blockcomment),
('editor', 'create_new_cell', self.create_new_cell),
('editor', 'unblockcomment', self.unblockcomment),
('editor', 'transform to uppercase', self.transform_to_uppercase),
('editor', 'transform to lowercase', self.transform_to_lowercase),
Expand Down Expand Up @@ -4212,6 +4213,28 @@ def __in_block_comment(cursor):
cursor3.endEditBlock()
return True

def create_new_cell(self):
firstline = '#%% Start Cell \r\n'
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
endline = '\r\n'
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
cursor = self.textCursor()
if self.has_selected_text():
self.extend_selection_to_complete_lines()
start_pos, end_pos = cursor.selectionStart(), cursor.selectionEnd()
endline = '\r\n#%% End Cell'
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
else:
start_pos = end_pos = cursor.position()
cursor.beginEditBlock()
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
cursor.setPosition(start_pos)
cursor.movePosition(QTextCursor.StartOfBlock)
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
cursor.setPosition(end_pos)
cursor.movePosition(QTextCursor.EndOfBlock)
cursor.insertText(endline)
cursor.setPosition(start_pos)
cursor.movePosition(QTextCursor.StartOfBlock)
cursor.insertText(firstline)
cursor.endEditBlock()

return None
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
# ---- Kill ring handlers
# Taken from Jupyter's QtConsole
# Copyright (c) 2001-2015, IPython Development Team
Expand Down Expand Up @@ -5540,7 +5563,7 @@ def _draw_editor_cell_divider(self):

for top, line_number, block in self.visible_blocks:
if is_cell_header(block):
painter.drawLine(4, top, self.width(), top)
painter.drawLine(0, top, self.width(), top)

@property
def visible_blocks(self):
Expand Down