Skip to content

Commit

Permalink
advance line
Browse files Browse the repository at this point in the history
Add context modificator
  • Loading branch information
Quentin Peter committed Feb 19, 2023
1 parent 87763e7 commit a710788
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 65 deletions.
9 changes: 9 additions & 0 deletions spyder/plugins/editor/api/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ class CellRun(TypedDict):

# True if the text should be copied over.
copy: bool


class SelectionContextModificator:
ToLine = "up to line"
FromLine = "from line"


class ExtraAction:
Advance = "advance"
26 changes: 15 additions & 11 deletions spyder/plugins/editor/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
from spyder.utils.misc import getcwd_or_home
from spyder.widgets.findreplace import FindReplace
from spyder.plugins.editor.api.run import (
EditorRunConfiguration, FileRun, SelectionRun, CellRun)
EditorRunConfiguration, FileRun, SelectionRun, CellRun,
SelectionContextModificator, ExtraAction)
from spyder.plugins.editor.confpage import EditorConfigPage
from spyder.plugins.editor.utils.autosave import AutosaveForPlugin
from spyder.plugins.editor.utils.switcher import EditorSwitcherManager
Expand Down Expand Up @@ -297,7 +298,7 @@ def __init__(self, parent, ignore_last_opened_files=False):
register_shortcut=True,
add_to_toolbar=True,
add_to_menu=True,
extra_action_name='advance'
extra_action_name=ExtraAction.Advance
)

run.create_run_button(
Expand All @@ -318,7 +319,8 @@ def __init__(self, parent, ignore_last_opened_files=False):
shortcut_context="_",
register_shortcut=True,
add_to_toolbar=True,
add_to_menu=True
add_to_menu=True,
extra_action_name=ExtraAction.Advance,
)

run.create_run_button(
Expand All @@ -329,8 +331,7 @@ def __init__(self, parent, ignore_last_opened_files=False):
register_shortcut=True,
add_to_toolbar=False,
add_to_menu=True,
extra_action_name="to line",
conjunction_or_preposition="up"
context_modificator=SelectionContextModificator.ToLine
)

run.create_run_button(
Expand All @@ -341,8 +342,7 @@ def __init__(self, parent, ignore_last_opened_files=False):
register_shortcut=True,
add_to_toolbar=False,
add_to_menu=True,
extra_action_name="line",
conjunction_or_preposition="from"
context_modificator=SelectionContextModificator.FromLine
)

layout = QVBoxLayout()
Expand Down Expand Up @@ -2936,7 +2936,7 @@ def get_run_configuration(self, metadata_id: str) -> RunConfiguration:
return run_conf

def get_run_configuration_per_context(
self, context, action_name,
self, context, extra_action_name, context_modificator,
re_run=False
) -> Optional[RunConfiguration]:
editorstack = self.get_current_editorstack()
Expand All @@ -2950,17 +2950,21 @@ def get_run_configuration_per_context(
context_name = None

if context == RunContext.Selection:
if action_name == 'to line':
if context_modificator == SelectionContextModificator.ToLine:
ret = editorstack.get_to_current_line()
if ret is not None:
text, offsets, line_cols, enc = ret
else:
return
elif action_name == 'line':
elif (
context_modificator == SelectionContextModificator.FromLine
):
text, offsets, line_cols, enc = (
editorstack.get_from_current_line())
else:
text, offsets, line_cols, enc = editorstack.get_selection()
if extra_action_name == ExtraAction.Advance:
editorstack.advance_line()
context_name = 'Selection'
run_input = SelectionRun(
path=fname, selection=text, encoding=enc,
Expand All @@ -2978,7 +2982,7 @@ def get_run_configuration_per_context(
line_col_bounds=line_cols, character_bounds=offsets,
copy=copy_cell)

if action_name == 'advance':
if extra_action_name == ExtraAction.Advance:
editorstack.advance_cell()

metadata: RunConfigurationMetadata = {
Expand Down
25 changes: 16 additions & 9 deletions spyder/plugins/editor/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2786,7 +2786,7 @@ def format_document_or_selection(self, index=None):

# ------ Run
def _get_lines_cursor(self, direction):
""" Select and run all lines from cursor in given direction"""
""" Select and return all lines from cursor in given direction"""
editor = self.get_current_editor()
finfo = self.get_current_finfo()
enc = finfo.encoding
Expand All @@ -2808,21 +2808,21 @@ def _get_lines_cursor(self, direction):

def get_to_current_line(self):
"""
Run all lines from the beginning up to, but not including, current
Get all lines from the beginning up to, but not including, current
line.
"""
return self._get_lines_cursor(direction='up')

def get_from_current_line(self):
"""
Run all lines from and including the current line to the end of
Get all lines from and including the current line to the end of
the document.
"""
return self._get_lines_cursor(direction='down')

def get_selection(self):
"""
Run selected text or current line in console.
Get selected text or current line in console.
If some text is selected, then execute that text in console.
Expand All @@ -2844,17 +2844,24 @@ def get_selection(self):
line = editor.get_current_line()
text = line.lstrip()

if editor.is_cursor_on_last_line() and text:
editor.append(editor.get_line_separator())

editor.move_cursor_to_next('line', 'down')

return (
text, (line_off_from, line_off_to),
(line_col_from, line_col_to),
encoding
)

def advance_line(self):
"""Advance to the next line."""
editor = self.get_current_editor()
if (
editor.is_cursor_on_last_line()
and editor.get_current_line().strip()
):
editor.append(editor.get_line_separator())

editor.move_cursor_to_next('line', 'down')


def get_current_cell(self):
"""Get current cell attributes."""
text, block, off_pos, line_col_pos = (
Expand Down
5 changes: 5 additions & 0 deletions spyder/plugins/editor/widgets/tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def test_run_top_line(editor_bot, qtbot):
editor.go_to_line(1) # line number is one based
editor.move_cursor(3)
text, _, _, _ = editor_stack.get_selection()
editor_stack.advance_line()
assert text == 'a = 1'

# check cursor moves to start of next line; note line number is zero based
Expand All @@ -300,6 +301,7 @@ def test_run_last_nonempty_line(editor_bot, qtbot):
editor_stack, editor = editor_bot
editor.go_to_line(4)
text, _, _, _ = editor_stack.get_selection()
editor_stack.advance_line()
assert text == 'x = 2'
assert editor.get_cursor_line_column() == (4, 0) # check cursor moves down

Expand All @@ -308,12 +310,14 @@ def test_run_empty_line_in_middle(editor_bot, qtbot):
editor_stack, editor = editor_bot
editor.go_to_line(3)
_, _, _, _ = editor_stack.get_selection()
editor_stack.advance_line()
assert editor.get_cursor_line_column() == (3, 0) # check cursor moves down


def test_run_last_line_when_empty(editor_bot, qtbot):
editor_stack, editor = editor_bot
_, _, _, _ = editor_stack.get_selection()
editor_stack.advance_line()
# check cursor doesn't move
assert editor.get_cursor_line_column() == (4, 0)

Expand All @@ -323,6 +327,7 @@ def test_run_last_line_when_nonempty(editor_bot, qtbot):
editor.stdkey_backspace() # delete empty line at end
old_text = editor.toPlainText()
text, _, _, _ = editor_stack.get_selection()
editor_stack.advance_line()
assert text == 'x = 2'
expected_new_text = old_text + editor.get_line_separator()
# check blank line got added
Expand Down
8 changes: 6 additions & 2 deletions spyder/plugins/run/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ def get_run_configuration(self, uuid: str) -> RunConfiguration:
def get_run_configuration_per_context(
self,
context: str,
action_name: Optional[str] = None,
extra_action_name: Optional[str] = None,
context_modificator: Optional[str] = None,
re_run: bool = False
) -> Optional[RunConfiguration]:
"""
Expand All @@ -323,10 +324,13 @@ def get_run_configuration_per_context(
context: str
The context identifier for which the run configuration
is requested.
action_name: Optional[str]
extra_action_name: Optional[str]
If not None, the name of the action that the provider should take
after gathering the run configuration input. Else, no action needs
to take place.
context_modificator: Optional[str]
str describing how to alter the context.
e.g. run selection <from line>
re_run: bool
If True, then the requested configuration should correspond to the
last executed configuration for the given context.
Expand Down
Loading

0 comments on commit a710788

Please sign in to comment.