Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/editor/src/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,50 @@ describe('Editor', () => {
const shellResult = editor._input.read().toString();
expect(shellResult).to.be.equal(shellModifiedInput);
});

it('returns a proper statement when editing previous code - input is not a statement', async() => {
const shellOriginalInput = 'foo';
const editorOutput = '20';
const shellModifiedInput = 'foo = 20';
const cmd = await fakeExternalEditor({
base: base.path,
name: 'editor-script.js',
output: editorOutput
});

editor = makeEditor({ cmd });

await editor.runEditCommand(shellOriginalInput);
let shellResult = editor._input.read().toString();
expect(shellResult).to.be.equal(shellModifiedInput);

await editor.runEditCommand('');
shellResult = editor._input.read().toString();
expect(shellResult).to.be.equal(shellModifiedInput);
});

it('returns a proper statement when editing previous code - input is a statement', async() => {
const shellOriginalInput = 'function () {}';
const editorOutput = `function () {
console.log(111);
}`;
const shellModifiedInput = 'function () { console.log(111); }';
const cmd = await fakeExternalEditor({
base: base.path,
name: 'editor-script.js',
output: editorOutput
});

editor = makeEditor({ cmd });

await editor.runEditCommand(shellOriginalInput);
let shellResult = editor._input.read().toString();
expect(shellResult).to.be.equal(shellModifiedInput);

await editor.runEditCommand('');
shellResult = editor._input.read().toString();
expect(shellResult).to.be.equal(shellModifiedInput);
});
});
});
});
19 changes: 18 additions & 1 deletion packages/editor/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class Editor {
_instanceState: ShellInstanceState;
_loadExternalCode: (input: string, filename: string) => Promise<ShellResult>;
_lastContent: string;
_lastInputCode: string;
print: (...args: any[]) => Promise<void>;

constructor({ input, vscodeDir, tmpDir, instanceState, loadExternalCode }: EditorOptions) {
Expand All @@ -37,6 +38,7 @@ export class Editor {
this._instanceState = instanceState;
this._loadExternalCode = loadExternalCode;
this._lastContent = '';
this._lastInputCode = '';
this.print = instanceState.context.print;

// Add edit command support to shell api.
Expand Down Expand Up @@ -173,6 +175,19 @@ export class Editor {
return `${originalCode} = ${modifiedCode}`;
}

_setLastInputCode(code: string): void {
if (code !== '') {
this._lastInputCode = code;
}
}

_getLastInputCode(code: string): string {
if (code !== '') {
return code;
}
return this._lastInputCode;
}

async runEditCommand(code: string): Promise<void> {
await this.print('Opening an editor...');

Expand All @@ -183,6 +198,8 @@ export class Editor {
throw new Error('Command failed with an error: please define an external editor');
}

this._setLastInputCode(code);

const content = await this._getEditorContent(code);
const ext = await this._getExtension(editor);
const tmpDoc = await this._createTempFile({ content, ext });
Expand All @@ -208,7 +225,7 @@ export class Editor {

if (exitCode === 0) {
const modifiedCode = await this._readAndDeleteTempFile(tmpDoc);
const result = this._prepareResult({ originalCode: code, modifiedCode });
const result = this._prepareResult({ originalCode: this._getLastInputCode(code), modifiedCode });

// Write a content from the editor to the parent readable stream.
this._input.unshift(result);
Expand Down