@@ -119,6 +119,7 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
119119
120120 static properties = {
121121 _workspaceItems : { state : true } ,
122+ _fileSeparator : { state : false } ,
122123 _workspaceTreeNames : { state : true } ,
123124 _workspaceActions : { state : true } ,
124125 _filteredActions : { state : true } ,
@@ -134,6 +135,7 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
134135 super ( ) ;
135136 this . md = new MarkdownIt ( ) ;
136137 this . _workspaceItems = null ;
138+ this . _fileSeparator = '/' ;
137139 this . _workspaceTreeNames = null ;
138140 this . _workspaceActions = [ ] ;
139141 this . _filteredActions = this . _workspaceActions ;
@@ -192,8 +194,9 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
192194 directoryTree . selectFile ( this . _selectedWorkspaceItem . name ) ;
193195 this . _selectWorkspaceItem ( this . _selectedWorkspaceItem ) ;
194196 } else {
195- directoryTree . selectFile ( [ ...this . _workspaceItems . values ( ) ] [ 0 ] . name ) ;
196- this . _selectWorkspaceItem ( [ ...this . _workspaceItems . values ( ) ] [ 0 ] ) ;
197+ const selectedItem = QwcWorkspace . getFirstJavaFile ( [ ...this . _workspaceItems . values ( ) ] ) ;
198+ directoryTree . selectFile ( selectedItem . name ) ;
199+ this . _selectWorkspaceItem ( selectedItem ) ;
197200 }
198201 }
199202 }
@@ -217,6 +220,7 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
217220 _renderWorkspaceTree ( ) {
218221 return html `< qui-directory-tree id ="directoryTree " class ="files "
219222 .directory ="${ this . _workspaceTreeNames } "
223+ .fileSeparator ="${ this . _fileSeparator } "
220224 header ="Source Code "
221225 @file-select ="${ this . _onFileSelect } "
222226 > </ qui-directory-tree > ` ;
@@ -252,7 +256,7 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
252256 </ div >
253257
254258 < div class ="mainMenuBarTitle " @dblclick ="${ this . _toggleSplit } ">
255- ${ this . _selectedWorkspaceItem ?. name ?. split ( '/' ) . pop ( ) }
259+ ${ this . _selectedWorkspaceItem ?. name ?. split ( this . _fileSeparator ) . pop ( ) }
256260 </ div >
257261
258262 < div class ="mainMenuBarActions ">
@@ -688,8 +692,10 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
688692
689693 _loadWorkspaceItems ( ) {
690694 this . jsonRpc . getWorkspaceItems ( ) . then ( jsonRpcResponse => {
691- if ( Array . isArray ( jsonRpcResponse . result ) ) {
692- this . _workspaceItems = new Map ( jsonRpcResponse . result . map ( obj => [ obj . name , obj ] ) ) ;
695+ const items = jsonRpcResponse . result ?. items ;
696+ if ( Array . isArray ( items ) ) {
697+ this . _fileSeparator = jsonRpcResponse . result . fileSeparator ;
698+ this . _workspaceItems = new Map ( items . map ( obj => [ obj . name , obj ] ) ) ;
693699 } else {
694700 console . error ( "Expected an array but got:" , jsonRpcResponse . result ) ;
695701 }
@@ -747,7 +753,7 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
747753 _convertDirectoryStructureToTree ( ) {
748754 const root = [ ] ;
749755 this . _workspaceItems . forEach ( ( value , key ) => {
750- const parts = value . name . split ( '/' ) ;
756+ const parts = value . name . split ( this . _fileSeparator ) ;
751757 let currentLevel = root ;
752758
753759 parts . forEach ( ( part , index ) => {
@@ -770,7 +776,21 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
770776 } ) ;
771777 } ) ;
772778
779+ // folder goes first so that we have same deterministic behavior on Windows and Linux
780+ root . sort ( ( a , b ) => {
781+ const aIsFolder = a . type === 'folder' ? 0 : 1 ;
782+ const bIsFolder = b . type === 'folder' ? 0 : 1 ;
783+ return aIsFolder - bIsFolder ;
784+ } ) ;
785+
773786 return root ;
774787 }
788+
789+ static getFirstJavaFile ( workspaceItems ) {
790+ const javaFileNode = workspaceItems . find ( node => {
791+ return node . name && node . name . endsWith ( '.java' ) ;
792+ } ) ;
793+ return javaFileNode || workspaceItems [ 0 ] ;
794+ }
775795}
776796customElements . define ( 'qwc-workspace' , QwcWorkspace ) ;
0 commit comments