Skip to content

Commit c8a81a8

Browse files
author
Marthin Lachira
committed
🚀 feat(query-history): append history queries without replacing
1 parent 9f9b362 commit c8a81a8

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,28 @@ def _apply_history_query(self: QueryMixinHost, query: str) -> None:
745745
# Focus query input - this triggers on_descendant_focus which updates footer bindings
746746
self.query_input.focus()
747747

748+
def _append_history_query(self: QueryMixinHost, query: str) -> None:
749+
"""Append a query to the end of the editor without replacing text."""
750+
if query is None:
751+
return
752+
753+
current_text = self.query_input.text
754+
if current_text:
755+
trimmed_current = current_text.rstrip("\n")
756+
separator = "\n" if trimmed_current else ""
757+
new_text = f"{trimmed_current}{separator}{query}"
758+
else:
759+
new_text = query
760+
761+
self._push_undo_state()
762+
self.query_input.text = new_text
763+
764+
lines = new_text.split("\n")
765+
last_line = len(lines) - 1
766+
last_col = len(lines[-1]) if lines else 0
767+
self.query_input.cursor_location = (last_line, last_col)
768+
self.query_input.focus()
769+
748770
def action_show_history(self: QueryMixinHost) -> None:
749771
"""Show query history for the current connection."""
750772
if not self.current_config:
@@ -772,6 +794,8 @@ def _handle_history_result(self: QueryMixinHost, result: Any) -> None:
772794
action, data = result
773795
if action == "select":
774796
self._apply_history_query(data)
797+
elif action == "append":
798+
self._append_history_query(data)
775799
elif action == "delete":
776800
self._delete_history_entry(data)
777801
self.action_show_history()
@@ -843,6 +867,10 @@ def _handle_telescope_result(self: QueryMixinHost, result: Any) -> None:
843867
query = data.get("query", "")
844868
connection_name = data.get("connection_name", "")
845869
self._run_telescope_query(connection_name, query)
870+
elif action == "append":
871+
query = data.get("query", "")
872+
if query:
873+
self._append_history_query(query)
846874
elif action == "delete":
847875
timestamp = data.get("timestamp", "")
848876
connection_name = data.get("connection_name", "")

sqlit/domains/query/ui/screens/query_history.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class QueryHistoryScreen(ModalScreen):
2626
Binding("escape", "cancel", "Cancel", priority=True),
2727
Binding("q", "cancel", "Cancel"),
2828
Binding("enter", "select", "Select"),
29+
Binding("a", "append", "Append"),
2930
Binding("d", "delete", "Delete"),
3031
Binding("asterisk", "toggle_star", "Star"),
3132
Binding("slash", "open_filter", "Filter"),
@@ -177,7 +178,12 @@ def compose(self) -> ComposeResult:
177178
else:
178179
title = f"Query History - {self.connection_name}"
179180
empty_message = "No query history for this connection"
180-
shortcuts = [("Select", "<enter>"), ("Star", "*"), ("Delete", "D")]
181+
shortcuts = [
182+
("Select", "<enter>"),
183+
("Append", "a"),
184+
("Star", "*"),
185+
("Delete", "D"),
186+
]
181187

182188
self._merged_entries = self._merge_entries()
183189

@@ -240,6 +246,22 @@ def action_select(self) -> None:
240246
except Exception:
241247
self.dismiss(None)
242248

249+
def action_append(self) -> None:
250+
entries = self._get_display_entries()
251+
if not entries:
252+
self.dismiss(None)
253+
return
254+
255+
try:
256+
option_list = self.query_one("#history-list", OptionList)
257+
idx = option_list.highlighted
258+
if idx is not None and idx < len(entries):
259+
self.dismiss(self._build_action_result("append", entries[idx]))
260+
else:
261+
self.dismiss(None)
262+
except Exception:
263+
self.dismiss(None)
264+
243265
def on_option_list_option_selected(self, event: OptionList.OptionSelected) -> None:
244266
if event.option_list.id == "history-list":
245267
idx = event.option_list.highlighted

0 commit comments

Comments
 (0)