1
+ import vscode from "vscode" ;
2
+ import { DebugJob , getDebugServerJob , getDebugServiceDetails , getDebugServiceJob , isDebugEngineRunning , startServer , startService , stopServer , stopService } from "../api/debug/server" ;
3
+ import { instance } from "../instantiate" ;
4
+ import { t } from "../locale" ;
5
+ import { BrowserItem } from "../typings" ;
6
+
7
+ const title = "IBM i debugger" ;
8
+
9
+ export function initializeDebugBrowser ( context : vscode . ExtensionContext ) {
10
+ const debugBrowser = new DebugBrowser ( ) ;
11
+ const debugTreeViewer = vscode . window . createTreeView (
12
+ `ibmiDebugBrowser` , {
13
+ treeDataProvider : debugBrowser ,
14
+ showCollapseAll : true
15
+ } ) ;
16
+
17
+ const updateDebugBrowser = async ( ) => {
18
+ if ( instance . getConnection ( ) ) {
19
+ debugTreeViewer . title = `${ title } ${ ( await getDebugServiceDetails ( ) ) . version } `
20
+ debugTreeViewer . description = await isDebugEngineRunning ( ) ? t ( "online" ) : t ( "offline" ) ;
21
+ }
22
+ else {
23
+ debugTreeViewer . title = title ;
24
+ debugTreeViewer . description = "" ;
25
+ }
26
+
27
+ debugBrowser . refresh ( ) ;
28
+ }
29
+
30
+ instance . onEvent ( "connected" , updateDebugBrowser ) ;
31
+ instance . onEvent ( "disconnected" , updateDebugBrowser ) ;
32
+
33
+ context . subscriptions . push (
34
+ debugTreeViewer ,
35
+ vscode . commands . registerCommand ( "code-for-ibmi.debug.refresh" , updateDebugBrowser ) ,
36
+ vscode . commands . registerCommand ( "code-for-ibmi.debug.refresh.item" , ( item : DebugItem ) => debugBrowser . refresh ( item ) ) ,
37
+ vscode . commands . registerCommand ( "code-for-ibmi.debug.job.start" , ( item : DebugJobItem ) => item . start ( ) ) ,
38
+ vscode . commands . registerCommand ( "code-for-ibmi.debug.job.stop" , ( item : DebugJobItem ) => item . stop ( ) ) ,
39
+ vscode . commands . registerCommand ( "code-for-ibmi.debug.job.restart" , async ( item : DebugJobItem ) => await item . start ( ) && item . stop ( ) ) ,
40
+ ) ;
41
+ }
42
+
43
+ class DebugBrowser implements vscode . TreeDataProvider < DebugItem > {
44
+ private readonly _emitter : vscode . EventEmitter < DebugItem | undefined | null | void > = new vscode . EventEmitter ( ) ;
45
+ readonly onDidChangeTreeData : vscode . Event < DebugItem | undefined | null | void > = this . _emitter . event ;
46
+
47
+ refresh ( item ?: DebugItem ) {
48
+ this . _emitter . fire ( item ) ;
49
+ }
50
+
51
+ getTreeItem ( element : DebugItem ) {
52
+ return element ;
53
+ }
54
+
55
+ async getChildren ( ) : Promise < DebugItem [ ] > {
56
+ const connection = instance . getConnection ( ) ;
57
+ if ( connection ) {
58
+ return [
59
+ new DebugJobItem ( "server" ,
60
+ t ( "debug.server" ) ,
61
+ startServer ,
62
+ stopServer ,
63
+ await getDebugServerJob ( )
64
+ ) ,
65
+ new DebugJobItem ( "service" ,
66
+ t ( "debug.service" ) ,
67
+ ( ) => startService ( connection ) ,
68
+ ( ) => stopService ( connection ) ,
69
+ await getDebugServiceJob ( )
70
+ )
71
+ ] ;
72
+ }
73
+ else {
74
+ return [ ] ;
75
+ }
76
+ }
77
+ }
78
+
79
+ class DebugItem extends BrowserItem {
80
+ refresh ( ) {
81
+ vscode . commands . executeCommand ( "code-for-ibmi.debug.refresh.item" , this ) ;
82
+ }
83
+ }
84
+
85
+ class DebugJobItem extends DebugItem {
86
+ constructor ( readonly type : "server" | "service" , label : string , readonly startFunction : ( ) => Promise < boolean > , readonly stopFunction : ( ) => Promise < boolean > , readonly debugJob ?: DebugJob ) {
87
+ const running = debugJob !== undefined ;
88
+ super ( label , {
89
+ state : vscode . TreeItemCollapsibleState . None ,
90
+ icon : running ? "pass" : "error" ,
91
+ color : running ? "testing.iconPassed" : "testing.iconFailed"
92
+ } ) ;
93
+ this . contextValue = `debugJob_${ type } _${ running ? "on" : "off" } ` ;
94
+ this . description = running ? debugJob . name : t ( "offline" ) ;
95
+ this . tooltip = `${ t ( `listening.on.port${ debugJob ?. ports . length === 1 ? '' : 's' } ` ) } ${ debugJob ?. ports . join ( ", " ) } ` ;
96
+ }
97
+
98
+ async start ( ) {
99
+ return vscode . window . withProgress ( { title : t ( `start.debug.${ this . type } .task` ) , location : vscode . ProgressLocation . Window } , this . startFunction ) ;
100
+ }
101
+
102
+ async stop ( ) {
103
+ return vscode . window . withProgress ( { title : t ( `stop.debug.${ this . type } .task` ) , location : vscode . ProgressLocation . Window } , this . stopFunction ) ;
104
+ }
105
+ }
0 commit comments