Skip to content

Commit 8c947da

Browse files
committed
preserve focus for tab
1 parent cc9419a commit 8c947da

File tree

2 files changed

+110
-104
lines changed

2 files changed

+110
-104
lines changed

src/panels/RequestPanel.ts

Lines changed: 106 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@ export class Lama2Panel {
3737
public static render(extensionUri: vscode.Uri) {
3838
if (Lama2Panel.currentPanel) {
3939
// If the webview panel already exists reveal it
40-
Lama2Panel.currentPanel._panel.reveal(vscode.ViewColumn.Two)
40+
console.log("revealing existing panel")
41+
Lama2Panel.currentPanel._panel.reveal(vscode.ViewColumn.Two, true)
4142
} else {
43+
console.log("opening new panel")
4244
// If a webview panel does not already exist create and show a new one
4345
const panel = vscode.window.createWebviewPanel(
4446
// Panel view type
4547
"showLama2Output",
4648
// Panel title
4749
"Lama2 Output",
4850
// The editor column the panel should be displayed in
49-
vscode.ViewColumn.Two,
51+
52+
{ preserveFocus: true, viewColumn: vscode.ViewColumn.Two },
53+
5054
// Extra panel configurations
5155
{
5256
// Enable JavaScript in the webview
57+
5358
enableScripts: true,
5459
// Restrict the webview to only load resources from the `out` and `webview-ui/build` directories
5560
localResourceRoots: [
@@ -68,58 +73,58 @@ export class Lama2Panel {
6873
}
6974

7075
public async executeLama2Command() {
71-
this._panel.webview.postMessage({
72-
command: "update",
73-
status: "starting",
74-
})
75-
76-
const lama2Command = getLama2Command();
77-
if (!lama2Command) {
78-
console.error("Failed to generate Lama2 command");
79-
return;
80-
}
81-
82-
const { cmd, rflag, rfile, rlog } = lama2Command;
83-
this.rfile = rfile;
84-
this.rlog = rlog;
85-
this.setLama2Watch(rflag);
86-
87-
88-
// Execute command and capture output
89-
let terminal = getShowLama2Term("AutoLama2")
90-
terminal.sendText(cmd, true)
91-
let isVisible = false
92-
93-
this._panel.webview.onDidReceiveMessage((message) => {
94-
switch (message.command) {
95-
case "toggleTerminal":
96-
console.log("isVisble", isVisible)
97-
if (isVisible) {
98-
terminal.hide()
99-
isVisible = false
100-
} else {
101-
terminal.show()
102-
isVisible = true
103-
}
104-
this._panel.webview.postMessage({ type: "terminalVisibility", isVisible })
105-
return
76+
try {
77+
this._panel.webview.postMessage({
78+
command: "update",
79+
status: "starting",
80+
})
81+
82+
const lama2Command = getLama2Command()
83+
if (!lama2Command) {
84+
console.error("Failed to generate Lama2 command")
85+
return
10686
}
107-
}, undefined)
10887

109-
vscode.window.onDidCloseTerminal(t => {
110-
if (t.exitStatus && t.exitStatus.code) {
111-
vscode.window.showInformationMessage(`Exit code: ${t.exitStatus.code}`);
112-
}
113-
});
88+
const { cmd, rflag, rfile, rlog } = lama2Command
89+
this.rfile = rfile
90+
this.rlog = rlog
91+
this.setLama2Watch(rflag)
11492

93+
// Execute command and capture output
94+
let terminal = getShowLama2Term("AutoLama2")
95+
terminal.sendText(cmd, true)
96+
let isVisible = false
11597

98+
this._panel.webview.onDidReceiveMessage((message) => {
99+
switch (message.command) {
100+
case "toggleTerminal":
101+
console.log("isVisble", isVisible)
102+
if (isVisible) {
103+
terminal.hide()
104+
isVisible = false
105+
} else {
106+
terminal.show()
107+
isVisible = true
108+
}
109+
this._panel.webview.postMessage({ type: "terminalVisibility", isVisible })
110+
return
111+
}
112+
}, undefined)
113+
114+
vscode.window.onDidCloseTerminal((t) => {
115+
if (t.exitStatus && t.exitStatus.code) {
116+
vscode.window.showInformationMessage(`Exit code: ${t.exitStatus.code}`)
117+
}
118+
})
119+
} catch (error) {
120+
console.error("Error executing Lama2 command:", error)
121+
}
116122
}
117123

118124
private setLama2Watch(rflag: string) {
119125
let c = new ChokiExtension()
120126
c.pathAddTrigger(rflag, this.onLama2Finish, this)
121127
}
122-
123128

124129
private handleCommandError(errorMessage: string) {
125130
// Remove ANSI color codes and other formatting
@@ -170,9 +175,9 @@ export class Lama2Panel {
170175
}
171176
} catch (error) {
172177
console.log(this.rlog)
173-
const stderr = await vscode.workspace.fs.readFile(vscode.Uri.file(this.rlog));
174-
const stderrString = new TextDecoder().decode(stderr);
175-
console.error("Error processing Lama2 output:", stderrString);
178+
const stderr = await vscode.workspace.fs.readFile(vscode.Uri.file(this.rlog))
179+
const stderrString = new TextDecoder().decode(stderr)
180+
console.error("Error processing Lama2 output:", stderrString)
176181
this.handleCommandError(
177182
error instanceof Error ? stderrString : "An error occurred while processing the Lama2 output"
178183
)
@@ -236,9 +241,9 @@ export class Lama2Panel {
236241
case "alert":
237242
vscode.window.showErrorMessage(message.text)
238243
return
239-
case 'showNotification':
240-
vscode.window.showInformationMessage(message.text);
241-
break;
244+
case "showNotification":
245+
vscode.window.showInformationMessage(message.text)
246+
break
242247
}
243248
},
244249
undefined,
@@ -247,54 +252,54 @@ export class Lama2Panel {
247252
}
248253
}
249254

250-
// fs.access(rfile, fs.constants.F_OK, (err) => {
251-
// console.log('Checking if output file exists', err);
252-
// if (err) {
253-
// console.log('Output file does not exist');
254-
// this.handleCommandError("Output file not created");
255-
// } else {
256-
// this.onLama2Finish(rfile);
257-
// }
258-
// });
259-
// return;
260-
261-
// exec(cmd, (error, stdout, stderr) => {
262-
// console.log('exec', cmd)
263-
// console.log('stdout', stdout)
264-
// console.log('stderr', stderr)
265-
266-
// // Send the command to the terminal
267-
268-
// if (stdout) {
269-
// // Check if stdout is HTML
270-
// if (stdout.trim().startsWith('<')) {
271-
// // If it's HTML, call onLama2Finish directly with stdout
272-
// this.onLama2Finish(stdout);
273-
// return;
274-
// }
275-
276-
// // If it's not HTML, proceed with the file check
277-
// fs.access(rfile, fs.constants.F_OK, (err) => {
278-
// console.log('Checking if output file exists', err);
279-
// if (err) {
280-
// console.log('Output file does not exist');
281-
// this.handleCommandError("Output file not created");
282-
// } else {
283-
// this.onLama2Finish(rfile);
284-
// }
285-
// });
286-
// return;
287-
// }
288-
289-
// if (error) {
290-
// console.error(`exec error: ${error}`);
291-
// this.handleCommandError(stderr);
292-
// return;
293-
// }
294-
295-
// if (stderr) {
296-
// console.error(`stderr: ${stderr}`);
297-
// this.handleCommandError(stderr);
298-
// return;
299-
// }
300-
// });
255+
// fs.access(rfile, fs.constants.F_OK, (err) => {
256+
// console.log('Checking if output file exists', err);
257+
// if (err) {
258+
// console.log('Output file does not exist');
259+
// this.handleCommandError("Output file not created");
260+
// } else {
261+
// this.onLama2Finish(rfile);
262+
// }
263+
// });
264+
// return;
265+
266+
// exec(cmd, (error, stdout, stderr) => {
267+
// console.log('exec', cmd)
268+
// console.log('stdout', stdout)
269+
// console.log('stderr', stderr)
270+
271+
// // Send the command to the terminal
272+
273+
// if (stdout) {
274+
// // Check if stdout is HTML
275+
// if (stdout.trim().startsWith('<')) {
276+
// // If it's HTML, call onLama2Finish directly with stdout
277+
// this.onLama2Finish(stdout);
278+
// return;
279+
// }
280+
281+
// // If it's not HTML, proceed with the file check
282+
// fs.access(rfile, fs.constants.F_OK, (err) => {
283+
// console.log('Checking if output file exists', err);
284+
// if (err) {
285+
// console.log('Output file does not exist');
286+
// this.handleCommandError("Output file not created");
287+
// } else {
288+
// this.onLama2Finish(rfile);
289+
// }
290+
// });
291+
// return;
292+
// }
293+
294+
// if (error) {
295+
// console.error(`exec error: ${error}`);
296+
// this.handleCommandError(stderr);
297+
// return;
298+
// }
299+
300+
// if (stderr) {
301+
// console.error(`stderr: ${stderr}`);
302+
// this.handleCommandError(stderr);
303+
// return;
304+
// }
305+
// });

webview/src/pages/Response.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ const Response: React.FC = () => {
6464
useEffect(() => {
6565
const messageListener = (event: MessageEvent) => {
6666
const message = event.data
67+
console.log("message", message)
6768
if (message.command === "update") {
6869
// console.log("message", message);
6970

7071
if (message.status === "starting") {
7172
setIsLoading(true)
7273
setError(null)
7374
return
74-
}
75-
76-
if (message.status === "error") {
75+
} else if (message.status === "error") {
7776
setIsLoading(false)
7877
setError(message.error)
7978
return
@@ -128,6 +127,8 @@ const Response: React.FC = () => {
128127
} finally {
129128
setIsLoading(false)
130129
}
130+
} else {
131+
console.log("Unknown command", message)
131132
}
132133
}
133134

0 commit comments

Comments
 (0)