Skip to content

Commit 783e061

Browse files
committed
fix(terminal): keep previous active tab when restoring sessions
1 parent 832aa7b commit 783e061

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/components/terminal/terminalManager.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,22 @@ class TerminalManager {
9797
const sessions = this.getPersistedSessions();
9898
if (!sessions.length) return;
9999

100+
const manager = window.editorManager;
101+
const activeFileId = manager?.activeFile?.id;
102+
const restoredTerminals = [];
103+
100104
for (const session of sessions) {
101105
if (!session?.pid) continue;
102106
if (this.terminals.has(session.pid)) continue;
103107

104108
try {
105-
await this.createServerTerminal({
109+
const instance = await this.createServerTerminal({
106110
pid: session.pid,
107111
name: session.name,
108112
reconnecting: true,
113+
render: false,
109114
});
115+
if (instance) restoredTerminals.push(instance);
110116
} catch (error) {
111117
console.error(
112118
`Failed to restore terminal session ${session.pid}:`,
@@ -115,6 +121,13 @@ class TerminalManager {
115121
this.removePersistedSession(session.pid);
116122
}
117123
}
124+
125+
if (activeFileId && manager?.getFile) {
126+
const fileToRestore = manager.getFile(activeFileId, "id");
127+
fileToRestore?.makeActive();
128+
} else if (!manager?.activeFile && restoredTerminals.length) {
129+
restoredTerminals[0]?.file?.makeActive();
130+
}
118131
}
119132

120133
/**
@@ -124,11 +137,15 @@ class TerminalManager {
124137
*/
125138
async createTerminal(options = {}) {
126139
try {
140+
const { render, serverMode, ...terminalOptions } = options;
141+
const shouldRender = render !== false;
142+
const isServerMode = serverMode !== false;
143+
127144
const terminalId = `terminal_${++this.terminalCounter}`;
128145
const terminalName = options.name || `Terminal ${this.terminalCounter}`;
129146

130147
// Check if terminal is installed before proceeding
131-
if (options.serverMode !== false) {
148+
if (isServerMode) {
132149
const installationResult = await this.checkAndInstallTerminal();
133150
if (!installationResult.success) {
134151
throw new Error(installationResult.error);
@@ -137,8 +154,8 @@ class TerminalManager {
137154

138155
// Create terminal component
139156
const terminalComponent = new TerminalComponent({
140-
serverMode: options.serverMode !== false,
141-
...options,
157+
serverMode: isServerMode,
158+
...terminalOptions,
142159
});
143160

144161
// Create container
@@ -162,7 +179,7 @@ class TerminalManager {
162179
type: "terminal",
163180
content: terminalContainer,
164181
tabIcon: "licons terminal",
165-
render: true,
182+
render: shouldRender,
166183
});
167184

168185
// Wait for tab creation and setup
@@ -174,7 +191,7 @@ class TerminalManager {
174191

175192
// Connect to session if in server mode
176193
if (terminalComponent.serverMode) {
177-
await terminalComponent.connectToSession(options.pid);
194+
await terminalComponent.connectToSession(terminalOptions.pid);
178195
} else {
179196
// For local mode, just write a welcome message
180197
terminalComponent.write(

0 commit comments

Comments
 (0)