Skip to content

Commit c182d9d

Browse files
committed
Editor: Don't insert quote if it's written after a escaped backslash
- This eases writing Windows paths. - Add tests for this and also to check that we can correctly insert escaped quotes.
1 parent d1a7167 commit c182d9d

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

spyder/plugins/editor/extensions/closequotes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
# Copyright © Spyder Project Contributors
44
# Licensed under the terms of the MIT License
55
# (see spyder/__init__.py for details)
6+
67
"""This module contains the close quotes editor extension."""
78

9+
import re
10+
811
# Third party imports
912
from qtpy.QtGui import QTextCursor
1013

@@ -27,10 +30,14 @@ def unmatched_quotes_in_line(text):
2730
2831
Distributed under the terms of the BSD License.
2932
"""
33+
# Remove escaped quotes from counting, but only if they are not to the
34+
# right of a backslash because in that case the backslash is the char that
35+
# is escaped.
36+
text = re.sub(r"(?<!\\)\\'", '', text)
37+
text = re.sub(r'(?<!\\)\\"', '', text)
38+
3039
# We check " first, then ', so complex cases with nested quotes will
3140
# get the " to take precedence.
32-
text = text.replace("\\'", "")
33-
text = text.replace('\\"', '')
3441
if text.count('"') % 2:
3542
return '"'
3643
elif text.count("'") % 2:

spyder/plugins/editor/extensions/tests/test_closequotes.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,44 @@
33
# Copyright © Spyder Project Contributors
44
# Licensed under the terms of the MIT License
55
#
6+
67
"""Tests for close quotes."""
78

89
# Third party imports
910
import pytest
1011
from qtpy.QtCore import Qt
11-
from qtpy.QtGui import QTextCursor
12+
from qtpy.QtGui import QFont, QTextCursor
1213

1314
# Local imports
14-
from spyder.utils.qthelpers import qapplication
15+
from spyder.config.base import running_in_ci
1516
from spyder.plugins.editor.widgets.codeeditor import CodeEditor
1617
from spyder.plugins.editor.utils.editor import TextHelper
1718
from spyder.plugins.editor.extensions.closequotes import (
1819
CloseQuotesExtension)
1920

2021

21-
# --- Fixtures
22+
# ---- Fixtures
2223
# -----------------------------------------------------------------------------
2324
@pytest.fixture
24-
def editor_close_quotes():
25+
def editor_close_quotes(qtbot):
2526
"""Set up Editor with close quotes activated."""
26-
app = qapplication()
2727
editor = CodeEditor(parent=None)
28-
kwargs = {}
29-
kwargs['language'] = 'Python'
30-
kwargs['close_quotes'] = True
31-
editor.setup_editor(**kwargs)
32-
return editor
28+
editor.setup_editor(
29+
color_scheme='spyder/dark',
30+
font=QFont("Courier New", 10),
31+
language='Python',
32+
close_quotes=True
33+
)
3334

34-
# --- Tests
35-
# -----------------------------------------------------------------------------
35+
editor.resize(480, 360)
36+
editor.show()
37+
qtbot.addWidget(editor)
38+
39+
return editor
3640

3741

42+
# ---- Tests
43+
# -----------------------------------------------------------------------------
3844
@pytest.mark.parametrize(
3945
'text, expected_text, cursor_column',
4046
[
@@ -48,13 +54,19 @@ def editor_close_quotes():
4854
("''''", "''''''", 3),
4955
('"some_string"', '"some_string"', 13), # Write a string
5056
("'some_string'", "'some_string'", 13),
57+
(r'"\""', r'"\""', 4), # Write escaped quotes
58+
(r"'\''", r"'\''", 4),
59+
(r'"\\"', r'"\\"', 4), # Don't enter escaped quote if the previous
60+
(r"'\\'", r"'\\'", 4), # char is a backslash (for Windows paths)
5161
])
5262
def test_close_quotes(qtbot, editor_close_quotes, text, expected_text,
5363
cursor_column):
5464
"""Test insertion of extra quotes."""
5565
editor = editor_close_quotes
5666

5767
qtbot.keyClicks(editor, text)
68+
if not running_in_ci():
69+
qtbot.wait(1000)
5870
assert editor.toPlainText() == expected_text
5971

6072
assert cursor_column == TextHelper(editor).current_column_nbr()

0 commit comments

Comments
 (0)