Skip to content

Commit

Permalink
fix directory handling in zed
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcphers committed Feb 20, 2025
1 parent 1326eb3 commit 3bf2a98
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
26 changes: 21 additions & 5 deletions extensions/positron-zed/src/positronZedLanguageRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2023 Posit Software, PBC. All rights reserved.
* Copyright (C) 2023-2025 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/

Expand Down Expand Up @@ -80,6 +80,7 @@ const HelpLines = [
'preview msg - Sends a message to the preview pane',
'preview http... - Show the URL starting with http... in the preview pane',
'progress - Renders a progress bar',
'pwd - Prints the current working directory',
'restart - Simulates orderly restart',
'shutdown X - Simulates orderly shutdown, or sets the shutdown delay to X',
'static plot - Renders a static plot (image)',
Expand Down Expand Up @@ -194,6 +195,11 @@ export class PositronZedRuntimeSession implements positron.LanguageRuntimeSessio
*/
private _state: positron.RuntimeState;

/**
* The current working directory
*/
private _workingDirectory: string = '';

//#endregion Private Properties

//#region Constructor
Expand Down Expand Up @@ -875,6 +881,11 @@ export class PositronZedRuntimeSession implements positron.LanguageRuntimeSessio
break;
}

case 'pwd': {
this.simulateSuccessfulCodeExecution(id, code, this._workingDirectory);
break;
}

default: {
this.simulateUnsuccessfulCodeExecution(id, code, 'Unknown Command', `Error. '${code}' not recognized.\n`, []);
break;
Expand Down Expand Up @@ -924,7 +935,7 @@ export class PositronZedRuntimeSession implements positron.LanguageRuntimeSessio

// Immediately notify Positron of a "working directory"
if (this._ui) {
this._ui.changeDirectory('');
this._workingDirectory = this._ui.changeDirectory('');
}
break;

Expand Down Expand Up @@ -1080,7 +1091,7 @@ export class PositronZedRuntimeSession implements positron.LanguageRuntimeSessio
* @param workingDirectory The working directory to set
*/
public setWorkingDirectory(workingDirectory: string): Promise<void> {
this._ui?.changeDirectory(workingDirectory);
this._workingDirectory = this._ui?.changeDirectory(workingDirectory) || '';
return Promise.resolve();
}

Expand Down Expand Up @@ -1165,7 +1176,7 @@ export class PositronZedRuntimeSession implements positron.LanguageRuntimeSessio
/**
* Restarts the runtime.
*/
async restart(_workingDirectory?: string): Promise<void> {
async restart(workingDirectory?: string): Promise<void> {
// Let the user know what we're doing and go through the shutdown sequence.
const parentId = randomUUID();
this.simulateOutputMessage(parentId, 'Restarting.');
Expand All @@ -1180,6 +1191,11 @@ export class PositronZedRuntimeSession implements positron.LanguageRuntimeSessio
// Wait for a second before starting again.
await new Promise(resolve => setTimeout(resolve, 500));

// Apply new working directory
if (workingDirectory) {
this._workingDirectory = workingDirectory;
}

// Go through the startup sequence again.
this._onDidChangeRuntimeState.fire(positron.RuntimeState.Initializing);
this._onDidChangeRuntimeState.fire(positron.RuntimeState.Starting);
Expand Down Expand Up @@ -1423,7 +1439,7 @@ export class PositronZedRuntimeSession implements positron.LanguageRuntimeSessio
this.simulateInputMessage(parentId, code);

if (this._ui) {
this._ui.changeDirectory(directory);
this._workingDirectory = this._ui.changeDirectory(directory);
this.simulateOutputMessage(parentId, `Changed directory to '${this._ui.directory}'.`);
} else {
this.simulateErrorMessage(parentId, 'No Frontend', 'No frontend is connected.', []);
Expand Down
11 changes: 7 additions & 4 deletions extensions/positron-zed/src/positronZedUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export class ZedUi {
* Emits an event to the front end indicating a change in the working directory.
*
* @param directory The directory to change to
* @returns The name of the directory
*/
public changeDirectory(directory: string): void {
public changeDirectory(directory: string): string {
if (!directory) {
// Make up a random directory name if we don't have a truthy one
let hexDigits = Math.floor(Math.random() * 1679616).toString(16);
Expand All @@ -32,12 +33,14 @@ export class ZedUi {
// Emit the event to the front end
this._directory = directory;
this._onDidEmitData.fire({
msg_type: 'event',
name: 'working_directory',
data: {
jsonrpc: '2.0',
method: 'working_directory',
params: {
directory: directory,
}
});

return directory;
}

/**
Expand Down

0 comments on commit 3bf2a98

Please sign in to comment.