1
- import { EventEmitter , MarkdownString , workspace } from "vscode" ;
2
- import { window } from "vscode" ;
3
- import { CancellationToken , Event , ExtensionContext , ProviderResult , ThemeIcon , TreeDataProvider , TreeItem , TreeItemCollapsibleState , commands } from "vscode" ;
4
- import { SQLExample , Examples , ServiceInfoLabel } from "." ;
5
- import { OSData , fetchSystemInfo } from "../../config" ;
1
+ import { Event , EventEmitter , ExtensionContext , MarkdownString , ThemeIcon , TreeDataProvider , TreeItem , TreeItemCollapsibleState , Uri , commands , window , workspace } from "vscode" ;
2
+ import { Examples , SQLExample , ServiceInfoLabel } from "." ;
6
3
import { getInstance } from "../../base" ;
4
+ import { OSData , fetchSystemInfo } from "../../config" ;
7
5
import { getServiceInfo } from "../../database/serviceInfo" ;
8
6
9
7
const openExampleCommand = `vscode-db2i.examples.open` ;
10
8
11
9
export class ExampleBrowser implements TreeDataProvider < any > {
12
10
private _onDidChangeTreeData : EventEmitter < TreeItem | undefined | null | void > = new EventEmitter < TreeItem | undefined | null | void > ( ) ;
13
11
readonly onDidChangeTreeData : Event < TreeItem | undefined | null | void > = this . _onDidChangeTreeData . event ;
14
-
15
- private currentFilter : string | undefined ;
12
+
13
+ private currentFilter : string | undefined ;
16
14
17
15
constructor ( context : ExtensionContext ) {
18
16
context . subscriptions . push (
@@ -40,6 +38,11 @@ export class ExampleBrowser implements TreeDataProvider<any> {
40
38
commands . registerCommand ( `vscode-db2i.examples.clearFilter` , async ( ) => {
41
39
this . currentFilter = undefined ;
42
40
this . refresh ( ) ;
41
+ } ) ,
42
+
43
+ commands . registerCommand ( "vscode-db2i.examples.reload" , ( ) => {
44
+ delete Examples [ ServiceInfoLabel ] ;
45
+ this . refresh ( ) ;
43
46
} )
44
47
) ;
45
48
@@ -49,9 +52,9 @@ export class ExampleBrowser implements TreeDataProvider<any> {
49
52
// Refresh the examples when we have it, so we only display certain examples
50
53
this . refresh ( ) ;
51
54
} )
52
- } )
55
+ } )
53
56
}
54
-
57
+
55
58
refresh ( ) {
56
59
this . _onDidChangeTreeData . fire ( ) ;
57
60
}
@@ -60,74 +63,53 @@ export class ExampleBrowser implements TreeDataProvider<any> {
60
63
return element ;
61
64
}
62
65
63
- async getChildren ( element ?: ExampleGroupItem ) : Promise < any [ ] > {
64
- // Unlike the bulk of the examples which are defined in views/examples/index.ts, the services examples are retrieved dynamically
65
- if ( ! Examples [ ServiceInfoLabel ] ) {
66
- getServiceInfo ( ) . then ( serviceExamples => {
67
- Examples [ ServiceInfoLabel ] = serviceExamples ;
68
- this . refresh ( ) ;
69
- } )
66
+ async getChildren ( element ?: ExampleGroupItem ) : Promise < SQLExampleItem [ ] > {
67
+ if ( element ) {
68
+ return element . getChildren ( ) ;
70
69
}
71
-
72
- if ( this . currentFilter ) {
73
- // If there is a filter, then show all examples that include this criteria
74
- let items : SQLExampleItem [ ] = [ ] ;
75
-
76
- const upperFilter = this . currentFilter . toUpperCase ( ) ;
77
-
78
- for ( const exampleName in Examples ) {
79
- items . push (
80
- ...Examples [ exampleName ]
81
- . filter ( example => exampleWorksForOnOS ( example ) )
82
- . filter ( example => example . name . toUpperCase ( ) . includes ( upperFilter ) || example . content . some ( line => line . toUpperCase ( ) . includes ( upperFilter ) ) )
83
- . map ( example => new SQLExampleItem ( example ) )
84
- )
70
+ else {
71
+ // Unlike the bulk of the examples which are defined in views/examples/index.ts, the services examples are retrieved dynamically
72
+ if ( ! Examples [ ServiceInfoLabel ] ) {
73
+ Examples [ ServiceInfoLabel ] = await getServiceInfo ( ) ;
85
74
}
86
75
87
- return items ;
88
-
89
- } else {
90
- if ( element ) {
91
- return element . getChildren ( ) ;
92
- } else {
93
- let items : ExampleGroupItem [ ] = [ ] ;
94
-
95
- for ( const exampleName in Examples ) {
96
- items . push (
97
- new ExampleGroupItem ( exampleName , Examples [ exampleName ] )
98
- )
99
- }
100
-
101
- return items ;
76
+ if ( this . currentFilter ) {
77
+ // If there is a filter, then show all examples that include this criteria
78
+ const upperFilter = this . currentFilter . toUpperCase ( ) ;
79
+ return Object . values ( Examples )
80
+ . flatMap ( examples => examples . filter ( exampleWorksForOnOS ) )
81
+ . filter ( example => example . name . toUpperCase ( ) . includes ( upperFilter ) || example . content . some ( line => line . toUpperCase ( ) . includes ( upperFilter ) ) )
82
+ . sort ( sort )
83
+ . map ( example => new SQLExampleItem ( example ) ) ;
84
+ }
85
+ else {
86
+ return Object . entries ( Examples )
87
+ . sort ( ( [ name1 ] , [ name2 ] ) => sort ( name1 , name2 ) )
88
+ . map ( ( [ name , examples ] ) => new ExampleGroupItem ( name , examples ) ) ;
102
89
}
103
90
}
104
91
}
105
-
106
- getParent ?( element : any ) {
107
- throw new Error ( "Method not implemented." ) ;
108
- }
109
92
}
110
93
111
94
class ExampleGroupItem extends TreeItem {
112
95
constructor ( name : string , private group : SQLExample [ ] ) {
113
96
super ( name , TreeItemCollapsibleState . Collapsed ) ;
114
-
115
- this . iconPath = new ThemeIcon ( `folder` ) ;
97
+ this . iconPath = ThemeIcon . Folder ;
116
98
}
117
99
118
100
getChildren ( ) : SQLExampleItem [ ] {
119
101
return this . group
120
102
. filter ( example => exampleWorksForOnOS ( example ) )
103
+ . sort ( sort )
121
104
. map ( example => new SQLExampleItem ( example ) ) ;
122
105
}
123
106
}
124
107
125
108
class SQLExampleItem extends TreeItem {
126
109
constructor ( example : SQLExample ) {
127
110
super ( example . name , TreeItemCollapsibleState . None ) ;
128
-
129
- this . iconPath = new ThemeIcon ( `file` ) ;
130
-
111
+ this . iconPath = ThemeIcon . File ;
112
+ this . resourceUri = Uri . parse ( '_.sql' ) ;
131
113
this . tooltip = new MarkdownString ( [ '```sql' , example . content . join ( `\n` ) , '```' ] . join ( `\n` ) ) ;
132
114
133
115
this . command = {
@@ -142,13 +124,19 @@ function exampleWorksForOnOS(example: SQLExample): boolean {
142
124
if ( OSData ) {
143
125
const myOsVersion = OSData . version ;
144
126
145
- // If this example has specific system requirements defined..
146
- if ( example . requirements && example . requirements [ myOsVersion ] ) {
147
- if ( OSData . db2Level < example . requirements [ myOsVersion ] ) {
148
- return false ;
149
- }
127
+ // If this example has specific system requirements defined
128
+ if ( example . requirements &&
129
+ example . requirements [ myOsVersion ] &&
130
+ OSData . db2Level < example . requirements [ myOsVersion ] ) {
131
+ return false ;
150
132
}
151
133
}
152
134
153
135
return true ;
136
+ }
137
+
138
+ function sort ( string1 : string | SQLExample , string2 : string | SQLExample ) {
139
+ string1 = typeof string1 === "string" ? string1 : string1 . name ;
140
+ string2 = typeof string2 === "string" ? string2 : string2 . name ;
141
+ return string1 . localeCompare ( string2 ) ;
154
142
}
0 commit comments