Skip to content

Commit

Permalink
fix #493: strip whitespace from current_query and selection (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer authored Mar 29, 2024
1 parent 96e3d21 commit bf28964
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Bug Fixes

- If the cursor is after the final semicolon in the query editor, and there is only whitespace after the semicolon, Harlequin will now execute the last query before the semicolon, instead of doing nothing when clicking Run Query or pressing <kbd>ctrl+j</kbd>.

## [1.16.1] - 2024-03-27

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion src/harlequin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ def _validate_selection(self) -> str:
"""
if self.editor is None:
return ""
selection = self.editor.selected_text
selection = self.editor.selected_text.strip()
if self.connection is None:
return selection
if selection:
Expand Down
4 changes: 2 additions & 2 deletions src/harlequin/components/code_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def current_query(self) -> str:
after = (lno, len(self.text_input.document.get_line(lno)))
return self.text_input.get_text_range(
start=(before[0], before[1]), end=(after[0], after[1])
)
).strip()

@property
def previous_query(self) -> str:
Expand All @@ -82,7 +82,7 @@ def previous_query(self) -> str:

return self.text_input.get_text_range(
start=(first[0], first[1]), end=(second[0], second[1])
)
).strip()

def on_mount(self) -> None:
self.post_message(EditorCollection.EditorSwitched(active_editor=self))
Expand Down
51 changes: 51 additions & 0 deletions tests/functional_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,57 @@ async def test_multiple_queries(
assert all(snap_results)


@pytest.mark.asyncio
async def test_single_query_terminated_with_semicolon(
app_all_adapters: Harlequin,
) -> None:
app = app_all_adapters
messages: list[Message] = []
async with app.run_test(message_hook=messages.append) as pilot:
await app.workers.wait_for_complete()
while app.editor is None:
await pilot.pause()
q = "select 1; \n\t\n"
app.editor.text = q
await pilot.press("ctrl+j")

# should only run current query
await app.workers.wait_for_complete()
await pilot.pause()
[query_submitted_message] = [
m for m in messages if isinstance(m, QuerySubmitted)
]
assert query_submitted_message.query_text == "select 1;"
assert app.results_viewer.tab_count == 1

app.editor.focus()
await pilot.press("ctrl+a")
await pilot.press("ctrl+j")

# should not run whitespace query, even though included
# in selection.
await app.workers.wait_for_complete()
await pilot.pause()
[_, query_submitted_message] = [
m for m in messages if isinstance(m, QuerySubmitted)
]
assert query_submitted_message.query_text == "select 1;"
assert app.results_viewer.tab_count == 1

app.editor.focus()
await pilot.press("ctrl+end")
await pilot.press("ctrl+j")
# should run previous query
assert not app.editor.current_query
await app.workers.wait_for_complete()
await pilot.pause()
[*_, query_submitted_message] = [
m for m in messages if isinstance(m, QuerySubmitted)
]
assert query_submitted_message.query_text == "select 1;"
assert app.results_viewer.tab_count == 1


@pytest.mark.asyncio
@pytest.mark.parametrize(
"bad_query",
Expand Down

0 comments on commit bf28964

Please sign in to comment.