diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index f3ad7df1607..5a01752f38e 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -152,7 +152,9 @@ namespace ts.textChanges { return position === Position.Start ? start : fullStart; } // get start position of the line following the line that contains fullstart position - let adjustedStartPosition = getStartPositionOfLine(getLineOfLocalPosition(sourceFile, fullStartLine) + 1, sourceFile); + // (but only if the fullstart isn't the very beginning of the file) + const nextLineStart = fullStart > 0 ? 1 : 0; + let adjustedStartPosition = getStartPositionOfLine(getLineOfLocalPosition(sourceFile, fullStartLine) + nextLineStart, sourceFile); // skip whitespaces/newlines adjustedStartPosition = skipWhitespacesAndLineBreaks(sourceFile.text, adjustedStartPosition); return getStartPositionOfLine(getLineOfLocalPosition(sourceFile, adjustedStartPosition), sourceFile); diff --git a/tests/cases/fourslash/extract-method-uniqueName.ts b/tests/cases/fourslash/extract-method-uniqueName.ts index 44f026ae8a8..8f024ad6a47 100644 --- a/tests/cases/fourslash/extract-method-uniqueName.ts +++ b/tests/cases/fourslash/extract-method-uniqueName.ts @@ -3,14 +3,15 @@ ////// newFunction /////*start*/1 + 1/*end*/; +// NOTE: '// newFunction' should be included, but due to incorrect handling of trivia, +// it's omitted right now. goTo.select('start', 'end') edit.applyRefactor({ refactorName: "Extract Method", actionName: "scope_0", actionDescription: "Extract to function in global scope", newContent: -`// newFunction -/*RENAME*/newFunction_1(); +`/*RENAME*/newFunction_1(); function newFunction_1() { // newFunction diff --git a/tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts b/tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts index 5782b4a1f41..437bf6dadf4 100644 --- a/tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts +++ b/tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts @@ -12,9 +12,10 @@ //// } verify.applicableRefactorAvailableAtMarker('1'); +// NOTE: '// Comment' should be included, but due to incorrect handling of trivia, +// it's omitted right now. verify.fileAfterApplyingRefactorAtMarker('1', -`// Comment -class fn { +`class fn { constructor() { this.baz = 10; }