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: Fix QIntValidator to handle '+' sign #20070

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions spyder/plugins/editor/widgets/gotoline.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ def __init__(self, editor):

ok_button = bbox.button(QDialogButtonBox.Ok)
ok_button.setEnabled(False)
# QIntValidator does not handle '+' sign
# See spyder-ide/spyder#20070
self.lineedit.textChanged.connect(
lambda text: ok_button.setEnabled(len(text) > 0))
lambda text: ok_button.setEnabled(len(text) > 0 and text != '+'))
maurerle marked this conversation as resolved.
Show resolved Hide resolved

layout = QHBoxLayout()
layout.addLayout(glayout)
Expand All @@ -75,10 +77,14 @@ def __init__(self, editor):
def text_has_changed(self, text):
"""Line edit's text has changed."""
text = str(text)
if text:

# QIntValidator does not handle '+' sign
# See spyder-ide/spyder#12693
if text and text != '+':
maurerle marked this conversation as resolved.
Show resolved Hide resolved
self.lineno = int(text)
else:
self.lineno = None
maurerle marked this conversation as resolved.
Show resolved Hide resolved
self.lineedit.clear()

def get_line_number(self):
"""Return line number."""
Expand Down
64 changes: 64 additions & 0 deletions spyder/plugins/editor/widgets/tests/test_gotoline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)

"""Tests for gotoline.py"""

# Third party imports
from qtpy.QtWidgets import QDialogButtonBox, QLineEdit

# Local imports
from spyder.plugins.editor.widgets.gotoline import GoToLineDialog

maurerle marked this conversation as resolved.
Show resolved Hide resolved

def test_gotolinedialog_has_cancel_button(codeeditor, qtbot, tmpdir):
"""
Test that GoToLineDialog has a Cancel button.

Test that a GoToLineDialog has a button in a dialog button box and that
this button cancels the dialog window.
"""
editor = codeeditor
dialog = GoToLineDialog(editor)
qtbot.addWidget(dialog)
ok_button = dialog.findChild(QDialogButtonBox).button(QDialogButtonBox.Ok)
cancel_button = dialog.findChild(QDialogButtonBox).button(QDialogButtonBox.Cancel)
assert not ok_button.isEnabled()
with qtbot.waitSignal(dialog.rejected):
cancel_button.click()


def test_gotolinedialog_enter_plus(codeeditor, qtbot):
"""
Regression test for spyder-ide/spyder#12693
"""
editor = codeeditor
dialog = GoToLineDialog(editor)
qtbot.addWidget(dialog)
ok_button = dialog.findChild(QDialogButtonBox).button(QDialogButtonBox.Ok)
cancel_button = dialog.findChild(QDialogButtonBox).button(QDialogButtonBox.Cancel)
lineedit = dialog.findChild(QLineEdit)
lineedit.setText('+')

# Check + sign being cleared and ok button still is disabled
lineedit.setText("+")
assert lineedit.text() == ""
assert not ok_button.isEnabled()

def test_gotolinedialog_check_valid(codeeditor, qtbot):
"""
Check ok button enabled if valid text entered
"""
editor = codeeditor
dialog = GoToLineDialog(editor)
qtbot.addWidget(dialog)
ok_button = dialog.findChild(QDialogButtonBox).button(QDialogButtonBox.Ok)
lineedit = dialog.findChild(QLineEdit)
lineedit.setText("1")
assert lineedit.text() == "1"
assert ok_button.isEnabled()
with qtbot.waitSignal(dialog.accepted):
ok_button.click()
assert dialog.get_line_number() == 1
20 changes: 16 additions & 4 deletions spyder/plugins/variableexplorer/widgets/importwizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ def __init__(self, parent, text):
intvalid = QIntValidator(0, len(to_text_string(text).splitlines()),
self.skiprows_edt)
self.skiprows_edt.setValidator(intvalid)
self.skiprows_edt.textChanged.connect(
lambda text: self.get_skiprows())
other_layout.addWidget(self.skiprows_edt, 0, 1)

other_layout.setColumnMinimumWidth(2, 5)
Expand Down Expand Up @@ -236,7 +238,14 @@ def get_row_sep(self):

def get_skiprows(self):
"""Return number of lines to be skipped"""
return int(to_text_string(self.skiprows_edt.text()))
skip_rows = to_text_string(self.skiprows_edt.text())
# QIntValidator does not handle '+' sign
# See spyder-ide/spyder#20070
if skip_rows and skip_rows != '+':
return int(skip_rows)
else:
self.skiprows_edt.clear()
return 0

def get_comments(self):
"""Return comment string"""
Expand Down Expand Up @@ -364,12 +373,15 @@ def __init__(self, parent):
def _shape_text(self, text, colsep=u"\t", rowsep=u"\n",
transpose=False, skiprows=0, comments='#'):
"""Decode the shape of the given text"""
assert colsep != rowsep
assert colsep != rowsep, 'Column sep should not equal Row sep'
out = []
text_rows = text.split(rowsep)[skiprows:]
text_rows = text.split(rowsep)
assert skiprows < len(text_rows), 'Skip Rows > Line Count'
text_rows = text_rows[skiprows:]
for row in text_rows:
stripped = to_text_string(row).strip()
if len(stripped) == 0 or stripped.startswith(comments):
if len(stripped) == 0 or (comments and
stripped.startswith(comments)):
continue
line = to_text_string(row).split(colsep)
line = [try_to_parse(to_text_string(x)) for x in line]
Expand Down