From faa0575673d95706932dcc848eb5c084bc23d7f0 Mon Sep 17 00:00:00 2001 From: Basit Chonka Date: Mon, 25 Oct 2021 10:45:31 +0200 Subject: [PATCH 1/3] fix: show full statement when editing previous code --- packages/editor/src/editor.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/editor.ts b/packages/editor/src/editor.ts index c36a7eeb37..3e4aea8308 100644 --- a/packages/editor/src/editor.ts +++ b/packages/editor/src/editor.ts @@ -28,6 +28,7 @@ export class Editor { _instanceState: ShellInstanceState; _loadExternalCode: (input: string, filename: string) => Promise; _lastContent: string; + _lastCode: string; print: (...args: any[]) => Promise; constructor({ input, vscodeDir, tmpDir, instanceState, loadExternalCode }: EditorOptions) { @@ -37,6 +38,7 @@ export class Editor { this._instanceState = instanceState; this._loadExternalCode = loadExternalCode; this._lastContent = ''; + this._lastCode = ''; this.print = instanceState.context.print; // Add edit command support to shell api. @@ -173,6 +175,19 @@ export class Editor { return `${originalCode} = ${modifiedCode}`; } + _setExecutedCode(code: string): void { + if (code !== '') { + this._lastCode = code; + } + } + + _getExecutedCode(code: string): string { + if (code !== '') { + return code; + } + return this._lastCode; + } + async runEditCommand(code: string): Promise { await this.print('Opening an editor...'); @@ -183,6 +198,8 @@ export class Editor { throw new Error('Command failed with an error: please define an external editor'); } + this._setExecutedCode(code); + const content = await this._getEditorContent(code); const ext = await this._getExtension(editor); const tmpDoc = await this._createTempFile({ content, ext }); @@ -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._getExecutedCode(code), modifiedCode }); // Write a content from the editor to the parent readable stream. this._input.unshift(result); From ea75b03a7a6e0be79135cdd649f05f43688abffd Mon Sep 17 00:00:00 2001 From: Basit Chonka Date: Mon, 25 Oct 2021 11:31:54 +0200 Subject: [PATCH 2/3] test: add tests for editing previous code --- packages/editor/src/editor.spec.ts | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/editor/src/editor.spec.ts b/packages/editor/src/editor.spec.ts index 7fe986c0d3..647bd34a5f 100644 --- a/packages/editor/src/editor.spec.ts +++ b/packages/editor/src/editor.spec.ts @@ -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); + }); }); }); }); From be9fcb5e4f708619a61c380b92112968c0fd525a Mon Sep 17 00:00:00 2001 From: Basit Chonka Date: Mon, 25 Oct 2021 14:13:58 +0200 Subject: [PATCH 3/3] feat: better var name --- packages/editor/src/editor.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/editor/src/editor.ts b/packages/editor/src/editor.ts index 3e4aea8308..b3ec5d873e 100644 --- a/packages/editor/src/editor.ts +++ b/packages/editor/src/editor.ts @@ -28,7 +28,7 @@ export class Editor { _instanceState: ShellInstanceState; _loadExternalCode: (input: string, filename: string) => Promise; _lastContent: string; - _lastCode: string; + _lastInputCode: string; print: (...args: any[]) => Promise; constructor({ input, vscodeDir, tmpDir, instanceState, loadExternalCode }: EditorOptions) { @@ -38,7 +38,7 @@ export class Editor { this._instanceState = instanceState; this._loadExternalCode = loadExternalCode; this._lastContent = ''; - this._lastCode = ''; + this._lastInputCode = ''; this.print = instanceState.context.print; // Add edit command support to shell api. @@ -175,17 +175,17 @@ export class Editor { return `${originalCode} = ${modifiedCode}`; } - _setExecutedCode(code: string): void { + _setLastInputCode(code: string): void { if (code !== '') { - this._lastCode = code; + this._lastInputCode = code; } } - _getExecutedCode(code: string): string { + _getLastInputCode(code: string): string { if (code !== '') { return code; } - return this._lastCode; + return this._lastInputCode; } async runEditCommand(code: string): Promise { @@ -198,7 +198,7 @@ export class Editor { throw new Error('Command failed with an error: please define an external editor'); } - this._setExecutedCode(code); + this._setLastInputCode(code); const content = await this._getEditorContent(code); const ext = await this._getExtension(editor); @@ -225,7 +225,7 @@ export class Editor { if (exitCode === 0) { const modifiedCode = await this._readAndDeleteTempFile(tmpDoc); - const result = this._prepareResult({ originalCode: this._getExecutedCode(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);