Skip to content

Commit e616763

Browse files
Marthin LachiraMrthnx
authored andcommitted
🚀 feat(query-history): append history queries without replacing
1 parent a132802 commit e616763

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
@@ -754,6 +754,28 @@ def _apply_history_query(self: QueryMixinHost, query: str) -> None:
754754
# Focus query input - this triggers on_descendant_focus which updates footer bindings
755755
self.query_input.focus()
756756

757+
def _append_history_query(self: QueryMixinHost, query: str) -> None:
758+
"""Append a query to the end of the editor without replacing text."""
759+
if query is None:
760+
return
761+
762+
current_text = self.query_input.text
763+
if current_text:
764+
trimmed_current = current_text.rstrip("\n")
765+
separator = "\n" if trimmed_current else ""
766+
new_text = f"{trimmed_current}{separator}{query}"
767+
else:
768+
new_text = query
769+
770+
self._push_undo_state()
771+
self.query_input.text = new_text
772+
773+
lines = new_text.split("\n")
774+
last_line = len(lines) - 1
775+
last_col = len(lines[-1]) if lines else 0
776+
self.query_input.cursor_location = (last_line, last_col)
777+
self.query_input.focus()
778+
757779
def action_show_history(self: QueryMixinHost) -> None:
758780
"""Show query history for the current connection."""
759781
if not self.current_config:
@@ -781,6 +803,8 @@ def _handle_history_result(self: QueryMixinHost, result: Any) -> None:
781803
action, data = result
782804
if action == "select":
783805
self._apply_history_query(data)
806+
elif action == "append":
807+
self._append_history_query(data)
784808
elif action == "delete":
785809
self._delete_history_entry(data)
786810
self.action_show_history()
@@ -853,6 +877,10 @@ def _handle_telescope_result(self: QueryMixinHost, result: Any) -> None:
853877
connection_name = data.get("connection_name", "")
854878
database = data.get("database", "")
855879
self._run_telescope_query(connection_name, query, database=database)
880+
elif action == "append":
881+
query = data.get("query", "")
882+
if query:
883+
self._append_history_query(query)
856884
elif action == "delete":
857885
timestamp = data.get("timestamp", "")
858886
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)