@@ -4,6 +4,7 @@ import { Config } from "../../config";
4
4
import { QueryHistoryItem } from "../../Storage" ;
5
5
6
6
const openSqlDocumentCommand = `vscode-db2i.openSqlDocument` ;
7
+ const openHistoryItemCommand = `vscode-db2i.queryHistory.openItem` ;
7
8
8
9
export class queryHistory implements TreeDataProvider < any > {
9
10
private _onDidChangeTreeData : EventEmitter < TreeItem | undefined | null | void > = new EventEmitter < TreeItem | undefined | null | void > ( ) ;
@@ -19,24 +20,50 @@ export class queryHistory implements TreeDataProvider<any> {
19
20
window . showTextDocument ( doc ) ;
20
21
} ) ;
21
22
} ) ,
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
+
22
44
commands . registerCommand ( `vscode-db2i.queryHistory.find` , async ( ) => {
23
45
commands . executeCommand ( 'queryHistory.focus' ) ;
24
46
commands . executeCommand ( 'list.find' ) ;
25
47
} ) ,
26
48
27
- commands . registerCommand ( `vscode-db2i.queryHistory.prepend` , async ( newQuery ?: string ) => {
49
+ commands . registerCommand ( `vscode-db2i.queryHistory.prepend` , async ( newQuery ?: string , substatement ?: string ) => {
28
50
if ( newQuery && Config . ready ) {
29
51
let currentList = Config . getPastQueries ( ) ;
30
52
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 ] || {
34
54
query : newQuery ,
35
55
unix : Math . floor ( Date . now ( ) / 1000 ) ,
36
56
} ;
37
57
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
+ }
40
67
}
41
68
42
69
// If it exists, remove it
@@ -46,7 +73,7 @@ export class queryHistory implements TreeDataProvider<any> {
46
73
47
74
// If it's at the top, don't add it, it's already at the top
48
75
if ( existingQueryi !== 0 ) {
49
- currentList . splice ( 0 , 0 , newQueryItem ) ;
76
+ currentList . splice ( 0 , 0 , existingQuery ) ;
50
77
}
51
78
52
79
await Config . setPastQueries ( currentList ) ;
@@ -193,11 +220,17 @@ class PastQueryNode extends TreeItem {
193
220
194
221
this . contextValue = `query` ;
195
222
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` ) ) ;
197
230
198
231
this . command = {
199
- command : openSqlDocumentCommand ,
200
- arguments : [ item . query ] ,
232
+ command : openHistoryItemCommand ,
233
+ arguments : [ item ] ,
201
234
title : `Open into new document`
202
235
} ;
203
236
0 commit comments