Skip to content

Commit 6b1975f

Browse files
authored
fix(editor): show full statement when editing previous code MONGOSH-1018 (#1142)
1 parent c220067 commit 6b1975f

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

packages/editor/src/editor.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,50 @@ describe('Editor', () => {
518518
const shellResult = editor._input.read().toString();
519519
expect(shellResult).to.be.equal(shellModifiedInput);
520520
});
521+
522+
it('returns a proper statement when editing previous code - input is not a statement', async() => {
523+
const shellOriginalInput = 'foo';
524+
const editorOutput = '20';
525+
const shellModifiedInput = 'foo = 20';
526+
const cmd = await fakeExternalEditor({
527+
base: base.path,
528+
name: 'editor-script.js',
529+
output: editorOutput
530+
});
531+
532+
editor = makeEditor({ cmd });
533+
534+
await editor.runEditCommand(shellOriginalInput);
535+
let shellResult = editor._input.read().toString();
536+
expect(shellResult).to.be.equal(shellModifiedInput);
537+
538+
await editor.runEditCommand('');
539+
shellResult = editor._input.read().toString();
540+
expect(shellResult).to.be.equal(shellModifiedInput);
541+
});
542+
543+
it('returns a proper statement when editing previous code - input is a statement', async() => {
544+
const shellOriginalInput = 'function () {}';
545+
const editorOutput = `function () {
546+
console.log(111);
547+
}`;
548+
const shellModifiedInput = 'function () { console.log(111); }';
549+
const cmd = await fakeExternalEditor({
550+
base: base.path,
551+
name: 'editor-script.js',
552+
output: editorOutput
553+
});
554+
555+
editor = makeEditor({ cmd });
556+
557+
await editor.runEditCommand(shellOriginalInput);
558+
let shellResult = editor._input.read().toString();
559+
expect(shellResult).to.be.equal(shellModifiedInput);
560+
561+
await editor.runEditCommand('');
562+
shellResult = editor._input.read().toString();
563+
expect(shellResult).to.be.equal(shellModifiedInput);
564+
});
521565
});
522566
});
523567
});

packages/editor/src/editor.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class Editor {
2828
_instanceState: ShellInstanceState;
2929
_loadExternalCode: (input: string, filename: string) => Promise<ShellResult>;
3030
_lastContent: string;
31+
_lastInputCode: string;
3132
print: (...args: any[]) => Promise<void>;
3233

3334
constructor({ input, vscodeDir, tmpDir, instanceState, loadExternalCode }: EditorOptions) {
@@ -37,6 +38,7 @@ export class Editor {
3738
this._instanceState = instanceState;
3839
this._loadExternalCode = loadExternalCode;
3940
this._lastContent = '';
41+
this._lastInputCode = '';
4042
this.print = instanceState.context.print;
4143

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

178+
_setLastInputCode(code: string): void {
179+
if (code !== '') {
180+
this._lastInputCode = code;
181+
}
182+
}
183+
184+
_getLastInputCode(code: string): string {
185+
if (code !== '') {
186+
return code;
187+
}
188+
return this._lastInputCode;
189+
}
190+
176191
async runEditCommand(code: string): Promise<void> {
177192
await this.print('Opening an editor...');
178193

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

201+
this._setLastInputCode(code);
202+
186203
const content = await this._getEditorContent(code);
187204
const ext = await this._getExtension(editor);
188205
const tmpDoc = await this._createTempFile({ content, ext });
@@ -208,7 +225,7 @@ export class Editor {
208225

209226
if (exitCode === 0) {
210227
const modifiedCode = await this._readAndDeleteTempFile(tmpDoc);
211-
const result = this._prepareResult({ originalCode: code, modifiedCode });
228+
const result = this._prepareResult({ originalCode: this._getLastInputCode(code), modifiedCode });
212229

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

0 commit comments

Comments
 (0)