Skip to content

Commit 084f1c6

Browse files
committed
core/views: Add support for copying code snippets.
Fixes #1123. Introduces support for copying code snippets in the message information popup. Makes use of the CodeSnippetButton class and its methods. Tests added.
1 parent 309bd26 commit 084f1c6

File tree

3 files changed

+147
-3
lines changed

3 files changed

+147
-3
lines changed

tests/ui_tools/test_popups.py

+65-2
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ def mock_external_classes(self, mocker: MockerFixture, msg_box: MessageBox) -> N
467467
message=self.message,
468468
topic_links=OrderedDict(),
469469
message_links=OrderedDict(),
470+
code_snippets=list(),
470471
time_mentions=list(),
471472
title="Full Rendered Message",
472473
)
@@ -519,6 +520,7 @@ def test_keypress_show_msg_info(
519520
msg=self.message,
520521
topic_links=OrderedDict(),
521522
message_links=OrderedDict(),
523+
code_snippets=list(),
522524
time_mentions=list(),
523525
)
524526

@@ -543,6 +545,7 @@ def mock_external_classes(self, mocker: MockerFixture, msg_box: MessageBox) -> N
543545
message=self.message,
544546
topic_links=OrderedDict(),
545547
message_links=OrderedDict(),
548+
code_snippets=list(),
546549
time_mentions=list(),
547550
title="Full Raw Message",
548551
)
@@ -595,6 +598,7 @@ def test_keypress_show_msg_info(
595598
msg=self.message,
596599
topic_links=OrderedDict(),
597600
message_links=OrderedDict(),
601+
code_snippets=list(),
598602
time_mentions=list(),
599603
)
600604

@@ -618,6 +622,7 @@ def mock_external_classes(self, mocker: MockerFixture) -> None:
618622
message=self.message,
619623
topic_links=OrderedDict(),
620624
message_links=OrderedDict(),
625+
code_snippets=list(),
621626
time_mentions=list(),
622627
title="Edit History",
623628
)
@@ -666,6 +671,7 @@ def test_keypress_show_msg_info(
666671
msg=self.message,
667672
topic_links=OrderedDict(),
668673
message_links=OrderedDict(),
674+
code_snippets=list(),
669675
time_mentions=list(),
670676
)
671677

@@ -944,6 +950,7 @@ def mock_external_classes(
944950
OrderedDict(),
945951
OrderedDict(),
946952
list(),
953+
list(),
947954
)
948955

949956
def test_init(self, message_fixture: Message) -> None:
@@ -961,6 +968,7 @@ def test_pop_up_info_order(self, message_fixture: Message) -> None:
961968
title="Message Information",
962969
topic_links=topic_links,
963970
message_links=message_links,
971+
code_snippets=list(),
964972
time_mentions=list(),
965973
)
966974
msg_links = msg_info_view.button_widgets
@@ -1009,6 +1017,7 @@ def test_keypress_edit_history(
10091017
title="Message Information",
10101018
topic_links=OrderedDict(),
10111019
message_links=OrderedDict(),
1020+
code_snippets=list(),
10121021
time_mentions=list(),
10131022
)
10141023
size = widget_size(msg_info_view)
@@ -1020,6 +1029,7 @@ def test_keypress_edit_history(
10201029
message=message_fixture,
10211030
topic_links=OrderedDict(),
10221031
message_links=OrderedDict(),
1032+
code_snippets=list(),
10231033
time_mentions=list(),
10241034
)
10251035
else:
@@ -1038,6 +1048,7 @@ def test_keypress_full_rendered_message(
10381048
title="Message Information",
10391049
topic_links=OrderedDict(),
10401050
message_links=OrderedDict(),
1051+
code_snippets=list(),
10411052
time_mentions=list(),
10421053
)
10431054
size = widget_size(msg_info_view)
@@ -1048,6 +1059,7 @@ def test_keypress_full_rendered_message(
10481059
message=message_fixture,
10491060
topic_links=OrderedDict(),
10501061
message_links=OrderedDict(),
1062+
code_snippets=list(),
10511063
time_mentions=list(),
10521064
)
10531065

@@ -1064,6 +1076,7 @@ def test_keypress_full_raw_message(
10641076
title="Message Information",
10651077
topic_links=OrderedDict(),
10661078
message_links=OrderedDict(),
1079+
code_snippets=list(),
10671080
time_mentions=list(),
10681081
)
10691082
size = widget_size(msg_info_view)
@@ -1074,6 +1087,7 @@ def test_keypress_full_raw_message(
10741087
message=message_fixture,
10751088
topic_links=OrderedDict(),
10761089
message_links=OrderedDict(),
1090+
code_snippets=list(),
10771091
time_mentions=list(),
10781092
)
10791093

@@ -1103,7 +1117,7 @@ def test_keypress_view_in_browser(
11031117
assert self.controller.open_in_browser.called
11041118

11051119
def test_height_noreactions(self) -> None:
1106-
expected_height = 8
1120+
expected_height = 9
11071121
# 6 = 1 (date & time) +1 (sender's name) +1 (sender's email)
11081122
# +1 (display group header)
11091123
# +1 (whitespace column)
@@ -1176,10 +1190,11 @@ def test_height_reactions(
11761190
OrderedDict(),
11771191
OrderedDict(),
11781192
list(),
1193+
list(),
11791194
)
11801195
# 12 = 7 labels + 2 blank lines + 1 'Reactions' (category)
11811196
# + 4 reactions (excluding 'Message Links').
1182-
expected_height = 14
1197+
expected_height = 15
11831198
assert self.msg_info_view.height == expected_height
11841199

11851200
@pytest.mark.parametrize(
@@ -1229,6 +1244,54 @@ def test_create_link_buttons(
12291244
assert link_w._wrapped_widget.attr_map == expected_attr_map
12301245
assert link_width == expected_link_width
12311246

1247+
@pytest.mark.parametrize(
1248+
[
1249+
"initial_code_snippet",
1250+
"expected_code",
1251+
"expected_attr_map",
1252+
"expected_focus_map",
1253+
],
1254+
[
1255+
(
1256+
[
1257+
(
1258+
"Python",
1259+
[
1260+
("pygments:k", "def"),
1261+
("pygments:w", " "),
1262+
("pygments:nf", "main"),
1263+
("pygments:p", "()"),
1264+
("pygments:w", "\n "),
1265+
("pygments:nb", "print"),
1266+
("pygments:p", "("),
1267+
("pygments:s2", '"Hello"'),
1268+
("pygments:p", ")"),
1269+
("pygments:w", "\n"),
1270+
],
1271+
)
1272+
],
1273+
'1: Python\ndef main()\n print("Hello")...',
1274+
{None: "popup_contrast"},
1275+
{None: "selected"},
1276+
)
1277+
],
1278+
ids=["with_code_snippet"],
1279+
)
1280+
def test_create_code_snippet_buttons(
1281+
self,
1282+
initial_code_snippet: List[Tuple[str, List[Tuple[str, str]]]],
1283+
expected_code: str,
1284+
expected_attr_map: Dict[None, str],
1285+
expected_focus_map: Dict[None, str],
1286+
) -> None:
1287+
[code_w], _ = self.msg_info_view.create_code_snippet_buttons(
1288+
self.controller, initial_code_snippet
1289+
)
1290+
1291+
assert code_w._wrapped_widget.original_widget.text == expected_code
1292+
assert code_w._wrapped_widget.focus_map == expected_focus_map
1293+
assert code_w._wrapped_widget.attr_map == expected_attr_map
1294+
12321295

12331296
class TestStreamInfoView:
12341297
@pytest.fixture(autouse=True)

zulipterminal/core.py

+8
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def show_msg_info(
261261
msg: Message,
262262
topic_links: Dict[str, Tuple[str, int, bool]],
263263
message_links: Dict[str, Tuple[str, int, bool]],
264+
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
264265
time_mentions: List[Tuple[str, str]],
265266
) -> None:
266267
msg_info_view = MsgInfoView(
@@ -269,6 +270,7 @@ def show_msg_info(
269270
"Message Information (up/down scrolls)",
270271
topic_links,
271272
message_links,
273+
code_snippets,
272274
time_mentions,
273275
)
274276
self.show_pop_up(msg_info_view, "area:msg")
@@ -336,6 +338,7 @@ def show_full_rendered_message(
336338
message: Message,
337339
topic_links: Dict[str, Tuple[str, int, bool]],
338340
message_links: Dict[str, Tuple[str, int, bool]],
341+
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
339342
time_mentions: List[Tuple[str, str]],
340343
) -> None:
341344
self.show_pop_up(
@@ -344,6 +347,7 @@ def show_full_rendered_message(
344347
message,
345348
topic_links,
346349
message_links,
350+
code_snippets,
347351
time_mentions,
348352
"Full rendered message (up/down scrolls)",
349353
),
@@ -355,6 +359,7 @@ def show_full_raw_message(
355359
message: Message,
356360
topic_links: Dict[str, Tuple[str, int, bool]],
357361
message_links: Dict[str, Tuple[str, int, bool]],
362+
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
358363
time_mentions: List[Tuple[str, str]],
359364
) -> None:
360365
self.show_pop_up(
@@ -363,6 +368,7 @@ def show_full_raw_message(
363368
message,
364369
topic_links,
365370
message_links,
371+
code_snippets,
366372
time_mentions,
367373
"Full raw message (up/down scrolls)",
368374
),
@@ -374,6 +380,7 @@ def show_edit_history(
374380
message: Message,
375381
topic_links: Dict[str, Tuple[str, int, bool]],
376382
message_links: Dict[str, Tuple[str, int, bool]],
383+
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
377384
time_mentions: List[Tuple[str, str]],
378385
) -> None:
379386
self.show_pop_up(
@@ -382,6 +389,7 @@ def show_edit_history(
382389
message,
383390
topic_links,
384391
message_links,
392+
code_snippets,
385393
time_mentions,
386394
"Edit History (up/down scrolls)",
387395
),

0 commit comments

Comments
 (0)