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 , 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 (
@@ -49,9 +47,9 @@ export class ExampleBrowser implements TreeDataProvider<any> {
49
47
// Refresh the examples when we have it, so we only display certain examples
50
48
this . refresh ( ) ;
51
49
} )
52
- } )
50
+ } )
53
51
}
54
-
52
+
55
53
refresh ( ) {
56
54
this . _onDidChangeTreeData . fire ( ) ;
57
55
}
@@ -60,59 +58,35 @@ export class ExampleBrowser implements TreeDataProvider<any> {
60
58
return element ;
61
59
}
62
60
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
- } )
61
+ async getChildren ( element ?: ExampleGroupItem ) : Promise < SQLExampleItem [ ] > {
62
+ if ( element ) {
63
+ return element . getChildren ( ) ;
70
64
}
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
- )
65
+ else {
66
+ // Unlike the bulk of the examples which are defined in views/examples/index.ts, the services examples are retrieved dynamically
67
+ if ( ! Examples [ ServiceInfoLabel ] ) {
68
+ Examples [ ServiceInfoLabel ] = await getServiceInfo ( ) ;
85
69
}
86
70
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 ;
71
+ if ( this . currentFilter ) {
72
+ // If there is a filter, then show all examples that include this criteria
73
+ const upperFilter = this . currentFilter . toUpperCase ( ) ;
74
+ return Object . values ( Examples )
75
+ . flatMap ( examples => examples . filter ( exampleWorksForOnOS ) )
76
+ . filter ( example => example . name . toUpperCase ( ) . includes ( upperFilter ) || example . content . some ( line => line . toUpperCase ( ) . includes ( upperFilter ) ) )
77
+ . map ( example => new SQLExampleItem ( example ) ) ;
78
+ }
79
+ else {
80
+ return Object . entries ( Examples ) . map ( ( [ name , examples ] ) => new ExampleGroupItem ( name , examples ) ) ;
102
81
}
103
82
}
104
83
}
105
-
106
- getParent ?( element : any ) {
107
- throw new Error ( "Method not implemented." ) ;
108
- }
109
84
}
110
85
111
86
class ExampleGroupItem extends TreeItem {
112
87
constructor ( name : string , private group : SQLExample [ ] ) {
113
88
super ( name , TreeItemCollapsibleState . Collapsed ) ;
114
-
115
- this . iconPath = new ThemeIcon ( `folder` ) ;
89
+ this . iconPath = ThemeIcon . Folder ;
116
90
}
117
91
118
92
getChildren ( ) : SQLExampleItem [ ] {
@@ -125,8 +99,7 @@ class ExampleGroupItem extends TreeItem {
125
99
class SQLExampleItem extends TreeItem {
126
100
constructor ( example : SQLExample ) {
127
101
super ( example . name , TreeItemCollapsibleState . None ) ;
128
-
129
- this . iconPath = new ThemeIcon ( `file` ) ;
102
+ this . iconPath = ThemeIcon . File ;
130
103
131
104
this . tooltip = new MarkdownString ( [ '```sql' , example . content . join ( `\n` ) , '```' ] . join ( `\n` ) ) ;
132
105
@@ -142,11 +115,11 @@ function exampleWorksForOnOS(example: SQLExample): boolean {
142
115
if ( OSData ) {
143
116
const myOsVersion = OSData . version ;
144
117
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
- }
118
+ // If this example has specific system requirements defined
119
+ if ( example . requirements &&
120
+ example . requirements [ myOsVersion ] &&
121
+ OSData . db2Level < example . requirements [ myOsVersion ] ) {
122
+ return false ;
150
123
}
151
124
}
152
125
0 commit comments