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: Correctly hide calltip when matching closing parenthesis #3984

Merged
merged 9 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions spyder/app/tests/edit_calltip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = [1,2,3]
(max(a) )
32 changes: 31 additions & 1 deletion spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from spyder.app.cli_options import get_options
from spyder.app.mainwindow import initialize, run_spyder

from spyder.utils.test import close_save_message_box

#==============================================================================
# Constants
Expand Down Expand Up @@ -81,6 +81,36 @@ def close_widget():
#==============================================================================
# Tests
#==============================================================================
@flaky(max_runs=10)
def test_calltip(main_window, qtbot):
"""Hide the calltip in the editor when a matching ')' is found."""
# Load test file
text = 'a = [1,2,3]\n(max'
test_file = osp.join(LOCATION, 'edit_calltip.py')
Copy link
Member

Choose a reason for hiding this comment

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

@dalthviz, can't we get rid of this file and use instead an empty file?

main_window.editor.load(test_file)
code_editor = main_window.editor.get_focus_widget()

# Set text to start
code_editor.set_text(text)
code_editor.go_to_line(2)
code_editor.move_cursor(5)
calltip = code_editor.calltip_widget
assert not calltip.isVisible()

qtbot.keyPress(code_editor, Qt.Key_ParenLeft, delay=3000)
qtbot.keyPress(code_editor, Qt.Key_A, delay=1000)
qtbot.waitUntil(lambda: calltip.isVisible(), timeout=1000)

qtbot.keyPress(code_editor, Qt.Key_ParenRight, delay=1000)
qtbot.keyPress(code_editor, Qt.Key_Space)
assert not calltip.isVisible()
qtbot.keyPress(code_editor, Qt.Key_ParenRight, delay=1000)
qtbot.keyPress(code_editor, Qt.Key_Enter, delay=1000)

QTimer.singleShot(1000, lambda: close_save_message_box(qtbot))
main_window.editor.close_file()


@flaky(max_runs=10)
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
def test_open_notebooks_from_project_explorer(main_window, qtbot):
Expand Down
19 changes: 19 additions & 0 deletions spyder/utils/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

"""Tests utilities."""

# Standard library imports
import os

# Third party imports
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QMessageBox, QApplication
Expand All @@ -22,3 +25,19 @@ def close_message_box(qtbot):
for w in top_level_widgets:
if isinstance(w, QMessageBox):
qtbot.keyClick(w, Qt.Key_Enter)

def close_save_message_box(qtbot):
"""
Closes QMessageBox's for save that can appear when testing.

You can use this with QTimer to close a QMessageBox for save functions.
Before calling anything that may show a QMessageBox for save call:
QTimer.singleShot(1000, lambda: close_save_message_box(qtbot))
"""
top_level_widgets = QApplication.topLevelWidgets()
for w in top_level_widgets:
if isinstance(w, QMessageBox):
if os.name == 'nt':
qtbot.keyClick(w, Qt.Key_Enter)
else:
qtbot.keyClick(w, Qt.Key_N, modifier=Qt.ShiftModifier)
5 changes: 5 additions & 0 deletions spyder/widgets/calltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ def eventFilter(self, obj, event):

if etype == QEvent.KeyPress:
key = event.key()
cursor = self._text_edit.textCursor()
prev_char = self._text_edit.get_character(cursor.position(),
offset=-1)
if key in (Qt.Key_Enter, Qt.Key_Return,
Qt.Key_Down, Qt.Key_Up):
self.hide()
elif key == Qt.Key_Escape:
self.hide()
return True
elif prev_char == ')':
self.hide()

elif etype == QEvent.FocusOut:
self.hide()
Expand Down
6 changes: 3 additions & 3 deletions spyder/widgets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ def get_text(self, position_from, position_to):
text = text[:-1]
return text

def get_character(self, position):
"""Return character at *position*"""
position = self.get_position(position)
def get_character(self, position, offset=0):
"""Return character at *position* with the given offset."""
position = self.get_position(position) + offset
cursor = self.textCursor()
cursor.movePosition(QTextCursor.End)
if position < cursor.position():
Expand Down