Skip to content

Commit 12f8797

Browse files
authored
Merge pull request #2441 from Kernel-Hunter/fix-completion-reset-leading-comment
Reset completions for USE and connect statements that start with a comment
2 parents 16a8d94 + c53ca2f commit 12f8797

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Upcoming (TBD)
44
Bug Fixes
55
--------
66
* Refresh completions after `CREATE`, `ALTER`, `DROP` or `RENAME` statements that start with a SQL comment.
7+
* Reset completions after `USE`, `\u`, `\r` or `connect` statements that start with a SQL comment.
78
* Don't flag a single-table `UPDATE` with a `WHERE` clause as destructive when it starts with a `/* ... */` comment.
89

910

‎mycli/packages/utils/sql_utils.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ def need_completion_reset(queries: str) -> bool:
461461
for query in sqlparse.split(queries):
462462
try:
463463
tokens = query.split()
464+
if tokens[0].startswith(("/*", "--", "#")):
465+
# skip leading comments, such as "/* note */ use db"
466+
tokens = sqlparse.format(query, strip_comments=True).split()
464467
first_token = tokens[0]
465468
if first_token.lower() in ("use", "/use", "\\u", "/u"):
466469
return True

‎mycli_test/pytests/test_sql_utils.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ def split(self):
571571
('/connect foo localhost root', True),
572572
('connect', False),
573573
('connect foo localhost root', True),
574+
('/* note */ use foo;', True),
575+
('-- note\nuse foo;', True),
576+
('# note\n\\u foo;', True),
577+
('/* note */ \\r foo', True),
578+
('/* note */ connect', False),
579+
('/* note */ select 1;', False),
574580
],
575581
)
576582
def test_need_completion_reset(queries, expected):

0 commit comments

Comments
 (0)