Skip to content

Commit 7055e72

Browse files
MaxteabagLVG77
andauthored
feat: add toggle comment selection (gcs) to query editor (#112)øf
Co-authored-by: Lyubomir Georgiev <lyubomir.georgiev@gmail.com>
1 parent e9e4634 commit 7055e72

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

sqlit/core/keymap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ def _build_leader_commands(self) -> list[LeaderCommandDef]:
281281
LeaderCommandDef("j", "down", "Comment line down", "Comment", menu="gc"),
282282
LeaderCommandDef("k", "up", "Comment line up", "Comment", menu="gc"),
283283
LeaderCommandDef("G", "to_end", "Comment to end", "Comment", menu="gc"),
284+
LeaderCommandDef("s", "selection", "Toggle selection", "Comment", menu="gc"),
284285
# ry results yank menu
285286
LeaderCommandDef("c", "cell", "Copy cell", "Copy", menu="ry"),
286287
LeaderCommandDef("y", "row", "Copy row", "Copy", menu="ry"),

sqlit/domains/query/ui/mixins/query_editing_comments.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,28 @@ def action_gc_to_end(self: QueryMixinHost) -> None:
6565
new_text, new_col = toggle_comment_lines(text, row, end_row)
6666
self.query_input.text = new_text
6767
self.query_input.cursor_location = (row, new_col)
68+
69+
def action_gc_selection(self: QueryMixinHost) -> None:
70+
"""Toggle comment on currently selected text (gcs)."""
71+
self._clear_leader_pending()
72+
73+
if not self._has_selection():
74+
return
75+
76+
self._push_undo_state()
77+
from sqlit.domains.query.editing.comments import toggle_comment_lines
78+
79+
selection = self.query_input.selection
80+
start, end = self._ordered_selection(selection)
81+
82+
start_row = start[0]
83+
end_row = end[0]
84+
85+
# If selection ends at the start of a line, don't include that line
86+
if end[1] == 0 and end_row > start_row:
87+
end_row -= 1
88+
89+
text = self.query_input.text
90+
new_text, new_col = toggle_comment_lines(text, start_row, end_row)
91+
self.query_input.text = new_text
92+
self.query_input.cursor_location = (start_row, new_col)

tests/unit/test_comments.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,46 @@ def test_tabs_preserved(self):
129129
new_text, col = toggle_comment_lines(text, 0, 0)
130130
assert new_text == "\t-- SELECT * FROM users"
131131
assert col == 4 # 1 tab + "-- "
132+
133+
@pytest.mark.parametrize(
134+
"text, start_row, end_row, expected_text, expected_col",
135+
[
136+
# Toggle on: simple multi-line
137+
(
138+
"SELECT *\nFROM users",
139+
0,
140+
1,
141+
"-- SELECT *\n-- FROM users",
142+
3,
143+
),
144+
# Toggle off: simple multi-line
145+
(
146+
"-- SELECT *\n-- FROM users",
147+
0,
148+
1,
149+
"SELECT *\nFROM users",
150+
0,
151+
),
152+
# Toggle on: mixed content (first line uncommented -> comment all)
153+
(
154+
"SELECT *\n-- FROM users",
155+
0,
156+
1,
157+
"-- SELECT *\n-- -- FROM users",
158+
3,
159+
),
160+
# Toggle off: mixed content (first line commented -> uncomment all)
161+
(
162+
"-- SELECT *\nFROM users",
163+
0,
164+
1,
165+
"SELECT *\nFROM users",
166+
0,
167+
),
168+
],
169+
)
170+
def test_toggle_scenarios_parametrized(self, text, start_row, end_row, expected_text, expected_col):
171+
"""Test various toggle scenarios mimicking selection behavior."""
172+
new_text, col = toggle_comment_lines(text, start_row, end_row)
173+
assert new_text == expected_text
174+
assert col == expected_col

0 commit comments

Comments
 (0)