Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cbbe34b

Browse filesBrowse files
author
Andy
authored
Fix conversion of TextChanges to FileCodeEdits for new file (microsoft#24126)
1 parent 86dce41 commit cbbe34b
Copy full SHA for cbbe34b

6 files changed

+25-8Lines changed: 25 additions & 8 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/server/project.ts‎

Copy file name to clipboardExpand all lines: src/server/project.ts
+1-1Lines 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);
Collapse file

‎src/server/session.ts‎

Copy file name to clipboardExpand all lines: src/server/session.ts
+18-5Lines 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);
Collapse file

‎src/services/textChanges.ts‎

Copy file name to clipboardExpand all lines: src/services/textChanges.ts
+1-1Lines 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 {
Collapse file

‎src/services/types.ts‎

Copy file name to clipboardExpand all lines: src/services/types.ts
+1Lines 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 {
Collapse file

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

Copy file name to clipboardExpand all lines: tests/baselines/reference/api/tsserverlibrary.d.ts
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4618,6 +4618,7 @@ declare namespace ts {
46184618
interface FileTextChanges {
46194619
fileName: string;
46204620
textChanges: TextChange[];
4621+
isNewFile?: boolean;
46214622
}
46224623
interface CodeAction {
46234624
/** Description of the code action to display in the UI of the editor */
@@ -7898,7 +7899,7 @@ declare namespace ts.server {
78987899
private detachScriptInfoFromProject;
78997900
private addMissingFileWatcher;
79007901
private isWatchedMissingFile;
7901-
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo;
7902+
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined;
79027903
getScriptInfo(uncheckedFileName: string): ScriptInfo;
79037904
filesToString(writeProjectFileNames: boolean): string;
79047905
setCompilerOptions(compilerOptions: CompilerOptions): void;
@@ -8492,6 +8493,7 @@ declare namespace ts.server {
84928493
private mapTextChangesToCodeEdits;
84938494
private mapTextChangesToCodeEditsUsingScriptinfo;
84948495
private convertTextChangeToCodeEdit;
8496+
private convertNewFileTextChangeToCodeEdit;
84958497
private getBraceMatching;
84968498
private getDiagnosticsForProject;
84978499
getCanonicalFileName(fileName: string): string;
Collapse file

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

Copy file name to clipboardExpand all lines: tests/baselines/reference/api/typescript.d.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4618,6 +4618,7 @@ declare namespace ts {
46184618
interface FileTextChanges {
46194619
fileName: string;
46204620
textChanges: TextChange[];
4621+
isNewFile?: boolean;
46214622
}
46224623
interface CodeAction {
46234624
/** Description of the code action to display in the UI of the editor */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.