@@ -7,7 +7,14 @@ import {
7
7
FILESYSTEM_READONLY_SCHEMA ,
8
8
explorerProvider ,
9
9
} from "../extension" ;
10
- import { connectionTarget , terminalWithDocker , shellWithDocker , currentFile } from "../utils" ;
10
+ import {
11
+ connectionTarget ,
12
+ terminalWithDocker ,
13
+ shellWithDocker ,
14
+ currentFile ,
15
+ uriOfWorkspaceFolder ,
16
+ outputChannel ,
17
+ } from "../utils" ;
11
18
import { mainCommandMenu , mainSourceControlMenu } from "./studio" ;
12
19
import { AtelierAPI } from "../api" ;
13
20
import { getCSPToken } from "../utils/getCSPToken" ;
@@ -37,6 +44,15 @@ export async function serverActions(): Promise<void> {
37
44
label : "Refresh Connection" ,
38
45
detail : "Force attempt to connect to the server" ,
39
46
} ) ;
47
+
48
+ // Switching namespace makes only sense if the user has a local folder open and not a server-side folder!
49
+ if ( uriOfWorkspaceFolder ( ) ?. scheme === "file" ) {
50
+ actions . push ( {
51
+ id : "switchNamespace" ,
52
+ label : "Switch Namespace" ,
53
+ detail : "Switch to a different namespace in the current server" ,
54
+ } ) ;
55
+ }
40
56
}
41
57
const connectionActionsHandler = async ( action : ServerAction ) : Promise < ServerAction > => {
42
58
if ( ! action ) {
@@ -56,6 +72,49 @@ export async function serverActions(): Promise<void> {
56
72
await checkConnection ( true , undefined , true ) ;
57
73
break ;
58
74
}
75
+ case "switchNamespace" : {
76
+ // NOTE: List of all namespaces except the current one as it doesn't make sense to allow switching to the current one
77
+ const allNamespaces : string [ ] | undefined = await api
78
+ . serverInfo ( )
79
+ . then ( ( data ) =>
80
+ data . result . content . namespaces . filter ( ( ns ) => ns . toLowerCase ( ) !== api . config . ns . toLowerCase ( ) )
81
+ )
82
+ . catch ( ( error ) => {
83
+ let message = `Failed to fetch a list of namespaces.` ;
84
+ if ( error && error . errorText && error . errorText !== "" ) {
85
+ outputChannel . appendLine ( "\n" + error . errorText ) ;
86
+ outputChannel . show ( true ) ;
87
+ message += " Check 'ObjectScript' output channel for details." ;
88
+ }
89
+ vscode . window . showErrorMessage ( message , "Dismiss" ) ;
90
+ return undefined ;
91
+ } ) ;
92
+
93
+ if ( ! allNamespaces ) {
94
+ return ;
95
+ }
96
+
97
+ if ( ! allNamespaces . length ) {
98
+ vscode . window . showErrorMessage ( `You don't have access to any other namespaces.` , "Dismiss" ) ;
99
+ return ;
100
+ }
101
+
102
+ const namespace = await vscode . window . showQuickPick ( allNamespaces , {
103
+ placeHolder : `Choose the namespace to switch to` ,
104
+ ignoreFocusOut : true ,
105
+ } ) ;
106
+
107
+ if ( namespace ) {
108
+ const connConfig = config ( "" , workspaceFolder ) ;
109
+ const target = connConfig . inspect ( "conn" ) . workspaceFolderValue
110
+ ? vscode . ConfigurationTarget . WorkspaceFolder
111
+ : vscode . ConfigurationTarget . Workspace ;
112
+ const targetConfig =
113
+ connConfig . inspect ( "conn" ) . workspaceFolderValue || connConfig . inspect ( "conn" ) . workspaceValue ;
114
+ return connConfig . update ( "conn" , { ...targetConfig , ns : namespace } , target ) ;
115
+ }
116
+ break ;
117
+ }
59
118
default :
60
119
return action ;
61
120
}
0 commit comments