Skip to content

Commit 38a0370

Browse files
author
Andy
authored
moveToNewFile: Support selecting the name of a declaration (#24331) (#24333)
1 parent a72cf5c commit 38a0370

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/services/refactors/moveToNewFile.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ namespace ts.refactor {
2222

2323
const startNodeIndex = findIndex(statements, s => s.end > range.pos);
2424
if (startNodeIndex === -1) return undefined;
25+
26+
const startStatement = statements[startNodeIndex];
27+
if (isNamedDeclaration(startStatement) && startStatement.name && rangeContainsRange(startStatement.name, range)) {
28+
return { first: startNodeIndex, afterLast: startNodeIndex + 1 };
29+
}
30+
2531
// Can't only partially include the start node or be partially into the next node
26-
if (range.pos > statements[startNodeIndex].getStart(file)) return undefined;
32+
if (range.pos > startStatement.getStart(file)) return undefined;
2733
const afterEndNodeIndex = findIndex(statements, s => s.end > range.end, startNodeIndex);
2834
// Can't be partially into the next node
2935
if (afterEndNodeIndex !== -1 && (afterEndNodeIndex === 0 || statements[afterEndNodeIndex].getStart(file) < range.end)) return undefined;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export {};
5+
////function e[|f|]f() { gee(); }
6+
////function gee() { eff(); }
7+
8+
verify.moveToNewFile({
9+
newFileContents: {
10+
"/a.ts":
11+
`import { eff } from "./eff";
12+
13+
export {};
14+
export function gee() { eff(); }`,
15+
"/eff.ts":
16+
`import { gee } from "./a";
17+
export function eff() { gee(); }`,
18+
},
19+
});

0 commit comments

Comments
 (0)