Skip to content

Commit 36cdbb2

Browse files
authored
Merge pull request microsoft#18625 from Microsoft/fix-getAdjustedStartPosition-on-first-line
Fix get adjusted start position on first line
2 parents a4fb050 + 3cc0aeb commit 36cdbb2

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/services/textChanges.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ namespace ts.textChanges {
152152
return position === Position.Start ? start : fullStart;
153153
}
154154
// get start position of the line following the line that contains fullstart position
155-
let adjustedStartPosition = getStartPositionOfLine(getLineOfLocalPosition(sourceFile, fullStartLine) + 1, sourceFile);
155+
// (but only if the fullstart isn't the very beginning of the file)
156+
const nextLineStart = fullStart > 0 ? 1 : 0;
157+
let adjustedStartPosition = getStartPositionOfLine(getLineOfLocalPosition(sourceFile, fullStartLine) + nextLineStart, sourceFile);
156158
// skip whitespaces/newlines
157159
adjustedStartPosition = skipWhitespacesAndLineBreaks(sourceFile.text, adjustedStartPosition);
158160
return getStartPositionOfLine(getLineOfLocalPosition(sourceFile, adjustedStartPosition), sourceFile);

tests/cases/fourslash/extract-method-uniqueName.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
////// newFunction
44
/////*start*/1 + 1/*end*/;
55

6+
// NOTE: '// newFunction' should be included, but due to incorrect handling of trivia,
7+
// it's omitted right now.
68
goTo.select('start', 'end')
79
edit.applyRefactor({
810
refactorName: "Extract Method",
911
actionName: "scope_0",
1012
actionDescription: "Extract to function in global scope",
1113
newContent:
12-
`// newFunction
13-
/*RENAME*/newFunction_1();
14+
`/*RENAME*/newFunction_1();
1415
1516
function newFunction_1() {
1617
// newFunction

tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
//// }
1313

1414
verify.applicableRefactorAvailableAtMarker('1');
15+
// NOTE: '// Comment' should be included, but due to incorrect handling of trivia,
16+
// it's omitted right now.
1517
verify.fileAfterApplyingRefactorAtMarker('1',
16-
`// Comment
17-
class fn {
18+
`class fn {
1819
constructor() {
1920
this.baz = 10;
2021
}

0 commit comments

Comments
 (0)