Skip to content

Commit a724a66

Browse files
Andysandersn
authored andcommitted
Fix conversion of TextChanges to FileCodeEdits for new file (#24126)
1 parent 7d43c99 commit a724a66

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

src/server/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ namespace ts.server {
964964
return this.missingFilesMap && this.missingFilesMap.has(path);
965965
}
966966

967-
getScriptInfoForNormalizedPath(fileName: NormalizedPath) {
967+
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined {
968968
const scriptInfo = this.projectService.getScriptInfoForPath(this.toPath(fileName));
969969
if (scriptInfo && !scriptInfo.isAttached(this)) {
970970
return Errors.ThrowProjectDoesNotContainDocument(fileName, this);

src/server/session.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,11 +1766,17 @@ namespace ts.server {
17661766
return textChanges.map(change => this.mapTextChangesToCodeEditsUsingScriptinfo(change, project.getScriptInfoForNormalizedPath(toNormalizedPath(change.fileName))));
17671767
}
17681768

1769-
private mapTextChangesToCodeEditsUsingScriptinfo(textChanges: FileTextChanges, scriptInfo: ScriptInfo): protocol.FileCodeEdits {
1770-
return {
1771-
fileName: textChanges.fileName,
1772-
textChanges: textChanges.textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo))
1773-
};
1769+
private mapTextChangesToCodeEditsUsingScriptinfo(textChanges: FileTextChanges, scriptInfo: ScriptInfo | undefined): protocol.FileCodeEdits {
1770+
Debug.assert(!!textChanges.isNewFile === !scriptInfo);
1771+
if (scriptInfo) {
1772+
return {
1773+
fileName: textChanges.fileName,
1774+
textChanges: textChanges.textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo))
1775+
};
1776+
}
1777+
else {
1778+
return this.convertNewFileTextChangeToCodeEdit(textChanges);
1779+
}
17741780
}
17751781

17761782
private convertTextChangeToCodeEdit(change: TextChange, scriptInfo: ScriptInfo): protocol.CodeEdit {
@@ -1781,6 +1787,13 @@ namespace ts.server {
17811787
};
17821788
}
17831789

1790+
private convertNewFileTextChangeToCodeEdit(textChanges: FileTextChanges): protocol.FileCodeEdits {
1791+
Debug.assert(textChanges.textChanges.length === 1);
1792+
const change = first(textChanges.textChanges);
1793+
Debug.assert(change.span.start === 0 && change.span.length === 0);
1794+
return { fileName: textChanges.fileName, textChanges: [{ start: { line: 0, offset: 0 }, end: { line: 0, offset: 0 }, newText: change.newText }] };
1795+
}
1796+
17841797
private getBraceMatching(args: protocol.FileLocationRequestArgs, simplifiedResult: boolean): protocol.TextSpan[] | TextSpan[] {
17851798
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
17861799
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);

src/services/textChanges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ namespace ts.textChanges {
719719

720720
export function newFileChanges(oldFile: SourceFile, fileName: string, statements: ReadonlyArray<Statement>, newLineCharacter: string): FileTextChanges {
721721
const text = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
722-
return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)] };
722+
return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)], isNewFile: true };
723723
}
724724

725725
function computeNewText(change: Change, sourceFile: SourceFile, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText): string {

src/services/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ namespace ts {
439439
export interface FileTextChanges {
440440
fileName: string;
441441
textChanges: TextChange[];
442+
isNewFile?: boolean;
442443
}
443444

444445
export interface CodeAction {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4635,6 +4635,7 @@ declare namespace ts {
46354635
interface FileTextChanges {
46364636
fileName: string;
46374637
textChanges: TextChange[];
4638+
isNewFile?: boolean;
46384639
}
46394640
interface CodeAction {
46404641
/** Description of the code action to display in the UI of the editor */
@@ -7915,7 +7916,7 @@ declare namespace ts.server {
79157916
private detachScriptInfoFromProject;
79167917
private addMissingFileWatcher;
79177918
private isWatchedMissingFile;
7918-
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo;
7919+
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined;
79197920
getScriptInfo(uncheckedFileName: string): ScriptInfo;
79207921
filesToString(writeProjectFileNames: boolean): string;
79217922
setCompilerOptions(compilerOptions: CompilerOptions): void;
@@ -8509,6 +8510,7 @@ declare namespace ts.server {
85098510
private mapTextChangesToCodeEdits;
85108511
private mapTextChangesToCodeEditsUsingScriptinfo;
85118512
private convertTextChangeToCodeEdit;
8513+
private convertNewFileTextChangeToCodeEdit;
85128514
private getBraceMatching;
85138515
private getDiagnosticsForProject;
85148516
getCanonicalFileName(fileName: string): string;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4635,6 +4635,7 @@ declare namespace ts {
46354635
interface FileTextChanges {
46364636
fileName: string;
46374637
textChanges: TextChange[];
4638+
isNewFile?: boolean;
46384639
}
46394640
interface CodeAction {
46404641
/** Description of the code action to display in the UI of the editor */

0 commit comments

Comments
 (0)