@@ -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" , "" )
0 commit comments