@@ -4,6 +4,7 @@ import { Config } from "../../config";
44import { QueryHistoryItem } from "../../Storage" ;
55
66const openSqlDocumentCommand = `vscode-db2i.openSqlDocument` ;
7+ const openHistoryItemCommand = `vscode-db2i.queryHistory.openItem` ;
78
89export class queryHistory implements TreeDataProvider < any > {
910 private _onDidChangeTreeData : EventEmitter < TreeItem | undefined | null | void > = new EventEmitter < TreeItem | undefined | null | void > ( ) ;
@@ -19,24 +20,50 @@ export class queryHistory implements TreeDataProvider<any> {
1920 window . showTextDocument ( doc ) ;
2021 } ) ;
2122 } ) ,
23+
24+ commands . registerCommand ( openHistoryItemCommand , ( item ?: QueryHistoryItem ) => {
25+ if ( ! item ) {
26+ return ;
27+ }
28+
29+ let content = item . query + `;` ;
30+
31+ if ( item . substatements && item . substatements . length > 0 ) {
32+ content += `\n\n-- Substatements: ${ item . substatements . length } \n` ;
33+ content += item . substatements . map ( sub => sub + `;` ) . join ( `\n` ) ;
34+ }
35+
36+ workspace . openTextDocument ( {
37+ language : `sql` ,
38+ content
39+ } ) . then ( doc => {
40+ window . showTextDocument ( doc ) ;
41+ } ) ;
42+ } ) ,
43+
2244 commands . registerCommand ( `vscode-db2i.queryHistory.find` , async ( ) => {
2345 commands . executeCommand ( 'queryHistory.focus' ) ;
2446 commands . executeCommand ( 'list.find' ) ;
2547 } ) ,
2648
27- commands . registerCommand ( `vscode-db2i.queryHistory.prepend` , async ( newQuery ?: string ) => {
49+ commands . registerCommand ( `vscode-db2i.queryHistory.prepend` , async ( newQuery ?: string , substatement ?: string ) => {
2850 if ( newQuery && Config . ready ) {
2951 let currentList = Config . getPastQueries ( ) ;
3052 const existingQueryi = currentList . findIndex ( queryItem => queryItem . query . trim ( ) === newQuery . trim ( ) ) ;
31- const existingQuery = currentList [ existingQueryi ] ;
32-
33- const newQueryItem : QueryHistoryItem = {
53+ const existingQuery = currentList [ existingQueryi ] || {
3454 query : newQuery ,
3555 unix : Math . floor ( Date . now ( ) / 1000 ) ,
3656 } ;
3757
38- if ( existingQuery ) {
39- newQueryItem . starred = existingQuery . starred ; // Preserve starred status
58+ if ( substatement ) {
59+ if ( ! existingQuery . substatements ) {
60+ existingQuery . substatements = [ ] ;
61+ }
62+
63+ // If the substatement already exists, don't add it again
64+ if ( ! existingQuery . substatements . includes ( substatement ) ) {
65+ existingQuery . substatements . push ( substatement ) ;
66+ }
4067 }
4168
4269 // If it exists, remove it
@@ -46,7 +73,7 @@ export class queryHistory implements TreeDataProvider<any> {
4673
4774 // If it's at the top, don't add it, it's already at the top
4875 if ( existingQueryi !== 0 ) {
49- currentList . splice ( 0 , 0 , newQueryItem ) ;
76+ currentList . splice ( 0 , 0 , existingQuery ) ;
5077 }
5178
5279 await Config . setPastQueries ( currentList ) ;
@@ -193,11 +220,17 @@ class PastQueryNode extends TreeItem {
193220
194221 this . contextValue = `query` ;
195222
196- this . tooltip = new MarkdownString ( [ '```sql' , item . query , '```' ] . join ( `\n` ) ) ;
223+ let markdownLines = [ '```sql' , item . query ] ;
224+
225+ if ( item . substatements && item . substatements . length > 0 ) {
226+ markdownLines . push ( `` , `-- substatements: ${ item . substatements . length } ` ) ;
227+ }
228+
229+ this . tooltip = new MarkdownString ( markdownLines . join ( `\n` ) ) ;
197230
198231 this . command = {
199- command : openSqlDocumentCommand ,
200- arguments : [ item . query ] ,
232+ command : openHistoryItemCommand ,
233+ arguments : [ item ] ,
201234 title : `Open into new document`
202235 } ;
203236
0 commit comments