1+
2+ 'use strict' ;
3+
4+ // This file implements the TreeView for XMake configuration in VS Code.
5+ // It defines the data structure, item types, and data provider for the configuration view.
6+
7+ import * as vscode from 'vscode' ;
8+ import { Status } from './status' ;
9+
10+
11+ // Enum for item types in the configuration view: folder or entry
12+ enum XMakeConfigureViewItemType {
13+ FOLDER ,
14+ ENTRY
15+ }
16+
17+
18+ // Folder item info type
19+ type XMakeConfigureViewFolderInfo = {
20+ type : XMakeConfigureViewItemType . FOLDER ;
21+ name : string ;
22+ }
23+
24+
25+ // Entry item info type (leaf node)
26+ type XMakeConfigureViewEntryInfo = {
27+ type : XMakeConfigureViewItemType . ENTRY ;
28+ name : string ;
29+ value : string ;
30+ }
31+
32+
33+ // Map entry names to their corresponding command and title
34+ const XMakeCommandMap : Record < string , { command : string , title : string } > = {
35+ "Name" : { command : "setProjectRoot" , title : "Change Project Name" } ,
36+ "Platform" : { command : "setTargetPlat" , title : "Change Platform" } ,
37+ "Architecture" : { command : "setTargetArch" , title : "Change Architecture" } ,
38+ "Toolchain" : { command : "setTargetToolchain" , title : "Change Toolchain" } ,
39+ "Mode" : { command : "setBuildMode" , title : "Change Mode" } ,
40+ "Target" : { command : "setDefaultTarget" , title : "Change Target" }
41+ } ;
42+
43+
44+ // TreeItem for the XMake configuration view
45+ class XMakeConfigureViewItem extends vscode . TreeItem {
46+ info : XMakeConfigureViewFolderInfo | XMakeConfigureViewEntryInfo ;
47+
48+ /**
49+ * Construct a TreeItem for either a folder or an entry.
50+ * @param info Folder or entry info
51+ */
52+ constructor ( info : XMakeConfigureViewFolderInfo | XMakeConfigureViewEntryInfo ) {
53+ let label : string ;
54+ let collapsibleState : vscode . TreeItemCollapsibleState ;
55+ let description : string | undefined = undefined ;
56+
57+ if ( info . type === XMakeConfigureViewItemType . FOLDER ) {
58+ label = info . name ;
59+ collapsibleState = vscode . TreeItemCollapsibleState . Expanded ;
60+ } else {
61+ label = info . name ;
62+ description = info . value ;
63+ collapsibleState = vscode . TreeItemCollapsibleState . None ;
64+ }
65+ super ( label , collapsibleState ) ;
66+ this . info = info ;
67+ // Assign command for entry items so they are clickable
68+ if ( info . type === XMakeConfigureViewItemType . ENTRY ) {
69+ this . command = {
70+ command : `xmake.${ XMakeCommandMap [ info . name ] . command } ` ,
71+ title : XMakeCommandMap [ info . name ] . title ,
72+ }
73+ // For debug: log command registration
74+ console . log ( `Command registered: ${ this . command . command } with title: ${ this . command . title } ` ) ;
75+ }
76+ if ( description ) {
77+ this . description = description ;
78+ }
79+ }
80+ }
81+
82+
83+ // The root structure of the configuration view: folders and their children
84+ const ROOT_STRUCTURE = [
85+ { type : XMakeConfigureViewItemType . FOLDER , name : "Project" , children : [ "Name" ] } ,
86+ { type : XMakeConfigureViewItemType . FOLDER , name : "Configure" , children : [ "Platform" , "Architecture" , "Toolchain" , "Mode" ] } ,
87+ { type : XMakeConfigureViewItemType . FOLDER , name : "Build" , children : [ "Target" ] }
88+ ] ;
89+
90+
91+ // Map entry names to Status property keys
92+ const XMakeStatusMap : Record < string , string > = {
93+ "Name" : "project" ,
94+ "Platform" : "plat" ,
95+ "Architecture" : "arch" ,
96+ "Toolchain" : "toolchain" ,
97+ "Mode" : "mode" ,
98+ "Target" : "target"
99+ } ;
100+
101+
102+ // Data provider for the XMake configuration TreeView
103+ class XMakeConfigureViewDataProvider implements vscode . TreeDataProvider < XMakeConfigureViewItem > {
104+ private status : Status ;
105+ private _onDidChangeTreeData :
106+ vscode . EventEmitter < XMakeConfigureViewItem | undefined | void > =
107+ new vscode . EventEmitter ( ) ;
108+ readonly onDidChangeTreeData :
109+ vscode . Event < XMakeConfigureViewItem | undefined | void > =
110+ this . _onDidChangeTreeData . event ;
111+
112+ /**
113+ * Refresh the tree view by firing the change event.
114+ */
115+ refresh ( ) : void {
116+ this . _onDidChangeTreeData . fire ( ) ;
117+ }
118+
119+ /**
120+ * @param status The Status instance for current configuration
121+ */
122+ constructor ( status ?: Status ) {
123+ this . status = status ;
124+ }
125+
126+ /**
127+ * Get the TreeItem for a given element.
128+ */
129+ getTreeItem ( element : XMakeConfigureViewItem ) : XMakeConfigureViewItem {
130+ return element ;
131+ }
132+
133+ /**
134+ * Get the children for a given element (or root folders if no element).
135+ */
136+ getChildren ( element ?: XMakeConfigureViewItem ) : vscode . ProviderResult < XMakeConfigureViewItem [ ] > {
137+ if ( ! element ) {
138+ // Return root folders
139+ return ROOT_STRUCTURE . map ( folder =>
140+ new XMakeConfigureViewItem ( {
141+ type : XMakeConfigureViewItemType . FOLDER ,
142+ name : folder . name
143+ } )
144+ ) ;
145+ }
146+ if ( element . info . type === XMakeConfigureViewItemType . FOLDER ) {
147+ // Return entries under the folder
148+ const folder = ROOT_STRUCTURE . find ( f => f . name === element . info . name ) ;
149+ if ( ! folder ) return [ ] ;
150+ return folder . children . map ( childName => {
151+ let value = "unknown" ;
152+ if ( this . status && typeof this . status === 'object' ) {
153+ const key = XMakeStatusMap [ childName ] ;
154+ value = this . status [ key ] ?? "unknown" ;
155+ }
156+ return new XMakeConfigureViewItem ( {
157+ type : XMakeConfigureViewItemType . ENTRY ,
158+ name : childName ,
159+ value : value
160+ } ) ;
161+ } ) ;
162+ }
163+ return [ ] ;
164+ }
165+ }
166+
167+
168+ /**
169+ * Controller class for the XMake configuration TreeView.
170+ * Handles data provider, refresh, and disposal.
171+ */
172+ export class XMakeConfigureView implements vscode . Disposable {
173+ private _dataProvider : XMakeConfigureViewDataProvider ;
174+ private _treeView : vscode . TreeView < vscode . TreeItem > ;
175+
176+ /**
177+ * @param status The Status instance for current configuration
178+ */
179+ constructor ( status : Status ) {
180+ this . _dataProvider = new XMakeConfigureViewDataProvider ( status ) ;
181+ this . _treeView = vscode . window . createTreeView (
182+ "xmakeConfigureView" ,
183+ { treeDataProvider : this . _dataProvider }
184+ )
185+ }
186+
187+ /**
188+ * Refresh the configuration view.
189+ */
190+ refresh ( ) : void {
191+ this . _dataProvider . refresh ( ) ;
192+ }
193+
194+ /**
195+ * Dispose the tree view and its resources.
196+ */
197+ dispose ( ) {
198+ this . _treeView . dispose ( ) ;
199+ }
200+ }
0 commit comments