@@ -7,7 +7,14 @@ import {
77 FILESYSTEM_READONLY_SCHEMA ,
88 explorerProvider ,
99} 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" ;
1118import { mainCommandMenu , mainSourceControlMenu } from "./studio" ;
1219import { AtelierAPI } from "../api" ;
1320import { getCSPToken } from "../utils/getCSPToken" ;
@@ -37,6 +44,15 @@ export async function serverActions(): Promise<void> {
3744 label : "Refresh Connection" ,
3845 detail : "Force attempt to connect to the server" ,
3946 } ) ;
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+ }
4056 }
4157 const connectionActionsHandler = async ( action : ServerAction ) : Promise < ServerAction > => {
4258 if ( ! action ) {
@@ -56,6 +72,49 @@ export async function serverActions(): Promise<void> {
5672 await checkConnection ( true , undefined , true ) ;
5773 break ;
5874 }
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+ }
59118 default :
60119 return action ;
61120 }
0 commit comments