@@ -98,6 +98,7 @@ async function connectViaWebSocket(
9898 isJackIn = false
9999) : Promise < ConnectResult > {
100100 let activeClient : nrepl . NReplClient | undefined ;
101+ let preservedRenames : Partial < sessionRoleUtils . SessionRoleKeys > | undefined ;
101102 const baseSessionNames = sessionRoleUtils . deriveSessionRoleKeys ( connectSequence ) ;
102103 const projectRootPath = state . getProjectRootUri ( ) . fsPath ;
103104 const projectRoot = state . getProjectRootUri ( ) . toString ( ) ;
@@ -193,6 +194,7 @@ async function connectViaWebSocket(
193194 connectSequence,
194195 baseSessionNames,
195196 suffix : resolution . suffix ,
197+ renamedSessionNames : preservedRenames ,
196198 } ,
197199 } ) ;
198200
@@ -211,19 +213,29 @@ async function connectViaWebSocket(
211213 } ) ;
212214 clientRegistry . setCljcTargetForConnection ( client . clientKey , 'primary' ) ;
213215
216+ // Apply preserved rename from previous browser connection
217+ const renamedPrimary = preservedRenames ?. primary ;
218+ if ( renamedPrimary && renamedPrimary !== mainKey ) {
219+ sessionRegistry . renameSession ( mainKey , renamedPrimary ) ;
220+ }
221+ const effectiveMainKey = renamedPrimary ?? mainKey ;
222+ preservedRenames = undefined ;
223+
214224 status . update ( ) ;
215- output . appendLineOtherOut ( `Connected session: ${ mainKey } , ws://${ wsHost } :${ currentPort } ` ) ;
225+ output . appendLineOtherOut (
226+ `Connected session: ${ effectiveMainKey } , ws://${ wsHost } :${ currentPort } `
227+ ) ;
216228 replSession . updateReplSessionType ( ) ;
217229
218- outputWindow . setSession ( mainSession , client . ns , mainKey ) ;
230+ outputWindow . setSession ( mainSession , client . ns , effectiveMainKey ) ;
219231
220232 if ( config . getConfig ( ) . autoEvaluateCode . onConnect . clj ) {
221233 output . appendLineOtherOut (
222234 `Evaluating code from settings: 'calva.autoEvaluateCode.onConnect.clj'`
223235 ) ;
224236 await evaluate . evaluateInOutputWindow (
225237 config . getConfig ( ) . autoEvaluateCode . onConnect . clj ,
226- mainKey ,
238+ effectiveMainKey ,
227239 outputWindow . getNs ( ) ,
228240 { }
229241 ) ;
@@ -234,7 +246,12 @@ async function connectViaWebSocket(
234246 connectSequence . afterPrimaryReplConnectedCode ?? connectSequence . afterCLJReplJackInCode ;
235247 if ( afterMainReplCode ) {
236248 output . appendLineOtherOut ( `Evaluating 'afterPrimaryReplConnectedCode'` ) ;
237- await evaluate . evaluateInOutputWindow ( afterMainReplCode , mainKey , outputWindow . getNs ( ) , { } ) ;
249+ await evaluate . evaluateInOutputWindow (
250+ afterMainReplCode ,
251+ effectiveMainKey ,
252+ outputWindow . getNs ( ) ,
253+ { }
254+ ) ;
238255 }
239256 if ( ! connectSequence . cljsType || connectSequence . cljsType === 'none' ) {
240257 output . maybePrintLegacyREPLWindowOutputMessage ( ) ;
@@ -305,6 +322,9 @@ async function connectViaWebSocket(
305322 output . appendLineOtherOut ( 'Browser REPL disconnected, waiting for reconnection...' ) ;
306323 state . connectionLogChannel ( ) . appendLine ( 'Browser REPL disconnected' ) ;
307324
325+ // Preserve renames across browser reconnections
326+ preservedRenames = clientRegistry . getConnectionState ( clientKey ) ?. renamedSessionNames ;
327+
308328 // Clean up client and sessions but keep server alive
309329 clientTeardown . releaseClientSuffix ( clientKey ) ;
310330 const wasRegistered = clientRegistry . unregisterClient ( clientKey ) ;
@@ -466,6 +486,7 @@ async function connectToHost(
466486 connectSequence,
467487 baseSessionNames,
468488 suffix : resolution . suffix ,
489+ renamedSessionNames : resolution . renamedSessionNames ,
469490 } ,
470491 } ) ;
471492 localClient . addOnCloseHandler ( ( c ) => {
@@ -504,19 +525,26 @@ async function connectToHost(
504525 } ) ;
505526 clientRegistry . setCljcTargetForConnection ( localClient . clientKey , 'primary' ) ;
506527
528+ // Apply preserved rename from previous connection
529+ const renamedPrimary = resolution . renamedSessionNames ?. primary ;
530+ if ( renamedPrimary && renamedPrimary !== mainKey ) {
531+ sessionRegistry . renameSession ( mainKey , renamedPrimary ) ;
532+ }
533+ const effectivePrimaryKey = renamedPrimary ?? mainKey ;
534+
507535 status . update ( ) ;
508- output . appendLineOtherOut ( `Connected session: ${ mainKey } , port: ${ port } ` ) ;
536+ output . appendLineOtherOut ( `Connected session: ${ effectivePrimaryKey } , port: ${ port } ` ) ;
509537 replSession . updateReplSessionType ( ) ;
510538
511- outputWindow . setSession ( mainSession , localClient . ns , mainKey ) ;
539+ outputWindow . setSession ( mainSession , localClient . ns , effectivePrimaryKey ) ;
512540
513541 if ( config . getConfig ( ) . autoEvaluateCode . onConnect . clj ) {
514542 output . appendLineOtherOut (
515543 `Evaluating code from settings: 'calva.autoEvaluateCode.onConnect.clj'`
516544 ) ;
517545 await evaluate . evaluateInOutputWindow (
518546 config . getConfig ( ) . autoEvaluateCode . onConnect . clj ,
519- mainKey ,
547+ effectivePrimaryKey ,
520548 outputWindow . getNs ( ) ,
521549 { }
522550 ) ;
@@ -527,14 +555,19 @@ async function connectToHost(
527555 connectSequence . afterPrimaryReplConnectedCode ?? connectSequence . afterCLJReplJackInCode ;
528556 if ( afterMainReplCode ) {
529557 output . appendLineOtherOut ( `Evaluating 'afterPrimaryReplConnectedCode'` ) ;
530- await evaluate . evaluateInOutputWindow ( afterMainReplCode , mainKey , outputWindow . getNs ( ) , { } ) ;
558+ await evaluate . evaluateInOutputWindow (
559+ afterMainReplCode ,
560+ effectivePrimaryKey ,
561+ outputWindow . getNs ( ) ,
562+ { }
563+ ) ;
531564 }
532565 if ( ! connectSequence . cljsType || connectSequence . cljsType === 'none' ) {
533566 output . maybePrintLegacyREPLWindowOutputMessage ( ) ;
534567 }
535568 void output . replWindowAppendPrompt ( ) ;
536569
537- clojureDocs . probeAndSetSession ( mainSession , mainKey ) ;
570+ clojureDocs . probeAndSetSession ( mainSession , effectivePrimaryKey ) ;
538571
539572 let cljsSession = null ,
540573 cljsBuild = null ;
@@ -700,24 +733,34 @@ async function setUpCljsRepl(
700733 } ) ;
701734 clientRegistry . setCljcTargetForConnection ( clientKey , 'secondary' ) ;
702735
703- clojureDocs . probeAndSetSession ( session , cljsKey ) ;
736+ // Apply preserved rename from previous connection
737+ const renamedSecondary =
738+ clientRegistry . getConnectionState ( clientKey ) ?. renamedSessionNames ?. secondary ;
739+ const effectiveCljsKey =
740+ renamedSecondary && renamedSecondary !== cljsKey
741+ ? ( sessionRegistry . renameSession ( cljsKey , renamedSecondary ) , renamedSecondary )
742+ : cljsKey ;
743+
744+ clojureDocs . probeAndSetSession ( session , effectiveCljsKey ) ;
704745
705746 status . update ( ) ;
706- output . appendLineOtherOut ( `Connected session: ${ cljsKey } ${ build ? ', repl: ' + build : '' } ` ) ;
747+ output . appendLineOtherOut (
748+ `Connected session: ${ effectiveCljsKey } ${ build ? ', repl: ' + build : '' } `
749+ ) ;
707750 outputWindow . appendLine (
708751 resultsOutputUtil . formatAsLineComments ( outputWindow . CLJS_CONNECT_GREETINGS )
709752 ) ;
710753 const description = await session . describe ( true ) ;
711754 const ns = description . aux ?. [ 'current-ns' ] || 'user' ;
712755 await session . eval ( `(in-ns '${ ns } )` , 'user' ) . value ;
713- outputWindow . setSession ( session , ns , cljsKey ) ;
756+ outputWindow . setSession ( session , ns , effectiveCljsKey ) ;
714757 if ( config . getConfig ( ) . autoEvaluateCode . onConnect . cljs ) {
715758 output . appendLineOtherOut (
716759 `Evaluating code from settings: 'calva.autoEvaluateCode.onConnect.cljs'`
717760 ) ;
718761 await evaluate . evaluateInOutputWindow (
719762 config . getConfig ( ) . autoEvaluateCode . onConnect . cljs ,
720- cljsKey ,
763+ effectiveCljsKey ,
721764 ns ,
722765 { }
723766 ) ;
0 commit comments