From 003c28f1ef8ef093d2927901af8fafd3b388dc88 Mon Sep 17 00:00:00 2001 From: Mine Starks Date: Tue, 11 Jul 2017 13:32:56 -0700 Subject: [PATCH] Fix caret update logic in fourslash tests --- src/harness/fourslash.ts | 13 ++++++++----- tests/cases/fourslash/formattingOnEnter.ts | 6 +++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index fc8299f57e9..9688c565f57 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1790,17 +1790,20 @@ namespace FourSlash { this.editScriptAndUpdateMarkers(fileName, offsetStart, offsetEnd, edit.newText); const editDelta = edit.newText.length - edit.span.length; if (offsetStart <= this.currentCaretPosition) { - this.currentCaretPosition += editDelta; + if (offsetEnd <= this.currentCaretPosition) { + // The entirety of the edit span falls before the caret position, shift the caret accordingly + this.currentCaretPosition += editDelta; + } + else { + // The span being replaced includes the caret position, place the caret at the beginning of the span + this.currentCaretPosition = offsetStart; + } } runningOffset += editDelta; // TODO: Consider doing this at least some of the time for higher fidelity. Currently causes a failure (bug 707150) // this.languageService.getScriptLexicalStructure(fileName); } - if (this.currentCaretPosition < 0) { - this.currentCaretPosition = 0; - } - if (isFormattingEdit) { const newContent = this.getFileContent(fileName); diff --git a/tests/cases/fourslash/formattingOnEnter.ts b/tests/cases/fourslash/formattingOnEnter.ts index 1c0915e839e..0e7d5af1a6d 100644 --- a/tests/cases/fourslash/formattingOnEnter.ts +++ b/tests/cases/fourslash/formattingOnEnter.ts @@ -6,4 +6,8 @@ goTo.marker(); edit.insertLine(""); -verify.currentLineContentIs('class bar {'); +verify.currentFileContentIs( +`class foo { } +class bar { +} +// new line here`);