mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:13:31 -06:00
Fix conversion of TextChanges to FileCodeEdits for new file (#24126)
This commit is contained in:
parent
86dce41ec0
commit
cbbe34b35e
@ -964,7 +964,7 @@ namespace ts.server {
|
||||
return this.missingFilesMap && this.missingFilesMap.has(path);
|
||||
}
|
||||
|
||||
getScriptInfoForNormalizedPath(fileName: NormalizedPath) {
|
||||
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined {
|
||||
const scriptInfo = this.projectService.getScriptInfoForPath(this.toPath(fileName));
|
||||
if (scriptInfo && !scriptInfo.isAttached(this)) {
|
||||
return Errors.ThrowProjectDoesNotContainDocument(fileName, this);
|
||||
|
||||
@ -1766,11 +1766,17 @@ namespace ts.server {
|
||||
return textChanges.map(change => this.mapTextChangesToCodeEditsUsingScriptinfo(change, project.getScriptInfoForNormalizedPath(toNormalizedPath(change.fileName))));
|
||||
}
|
||||
|
||||
private mapTextChangesToCodeEditsUsingScriptinfo(textChanges: FileTextChanges, scriptInfo: ScriptInfo): protocol.FileCodeEdits {
|
||||
return {
|
||||
fileName: textChanges.fileName,
|
||||
textChanges: textChanges.textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo))
|
||||
};
|
||||
private mapTextChangesToCodeEditsUsingScriptinfo(textChanges: FileTextChanges, scriptInfo: ScriptInfo | undefined): protocol.FileCodeEdits {
|
||||
Debug.assert(!!textChanges.isNewFile === !scriptInfo);
|
||||
if (scriptInfo) {
|
||||
return {
|
||||
fileName: textChanges.fileName,
|
||||
textChanges: textChanges.textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo))
|
||||
};
|
||||
}
|
||||
else {
|
||||
return this.convertNewFileTextChangeToCodeEdit(textChanges);
|
||||
}
|
||||
}
|
||||
|
||||
private convertTextChangeToCodeEdit(change: TextChange, scriptInfo: ScriptInfo): protocol.CodeEdit {
|
||||
@ -1781,6 +1787,13 @@ namespace ts.server {
|
||||
};
|
||||
}
|
||||
|
||||
private convertNewFileTextChangeToCodeEdit(textChanges: FileTextChanges): protocol.FileCodeEdits {
|
||||
Debug.assert(textChanges.textChanges.length === 1);
|
||||
const change = first(textChanges.textChanges);
|
||||
Debug.assert(change.span.start === 0 && change.span.length === 0);
|
||||
return { fileName: textChanges.fileName, textChanges: [{ start: { line: 0, offset: 0 }, end: { line: 0, offset: 0 }, newText: change.newText }] };
|
||||
}
|
||||
|
||||
private getBraceMatching(args: protocol.FileLocationRequestArgs, simplifiedResult: boolean): protocol.TextSpan[] | TextSpan[] {
|
||||
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
|
||||
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);
|
||||
|
||||
@ -719,7 +719,7 @@ namespace ts.textChanges {
|
||||
|
||||
export function newFileChanges(oldFile: SourceFile, fileName: string, statements: ReadonlyArray<Statement>, newLineCharacter: string): FileTextChanges {
|
||||
const text = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
|
||||
return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)] };
|
||||
return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)], isNewFile: true };
|
||||
}
|
||||
|
||||
function computeNewText(change: Change, sourceFile: SourceFile, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText): string {
|
||||
|
||||
@ -439,6 +439,7 @@ namespace ts {
|
||||
export interface FileTextChanges {
|
||||
fileName: string;
|
||||
textChanges: TextChange[];
|
||||
isNewFile?: boolean;
|
||||
}
|
||||
|
||||
export interface CodeAction {
|
||||
|
||||
@ -4618,6 +4618,7 @@ declare namespace ts {
|
||||
interface FileTextChanges {
|
||||
fileName: string;
|
||||
textChanges: TextChange[];
|
||||
isNewFile?: boolean;
|
||||
}
|
||||
interface CodeAction {
|
||||
/** Description of the code action to display in the UI of the editor */
|
||||
@ -7898,7 +7899,7 @@ declare namespace ts.server {
|
||||
private detachScriptInfoFromProject;
|
||||
private addMissingFileWatcher;
|
||||
private isWatchedMissingFile;
|
||||
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo;
|
||||
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined;
|
||||
getScriptInfo(uncheckedFileName: string): ScriptInfo;
|
||||
filesToString(writeProjectFileNames: boolean): string;
|
||||
setCompilerOptions(compilerOptions: CompilerOptions): void;
|
||||
@ -8492,6 +8493,7 @@ declare namespace ts.server {
|
||||
private mapTextChangesToCodeEdits;
|
||||
private mapTextChangesToCodeEditsUsingScriptinfo;
|
||||
private convertTextChangeToCodeEdit;
|
||||
private convertNewFileTextChangeToCodeEdit;
|
||||
private getBraceMatching;
|
||||
private getDiagnosticsForProject;
|
||||
getCanonicalFileName(fileName: string): string;
|
||||
|
||||
@ -4618,6 +4618,7 @@ declare namespace ts {
|
||||
interface FileTextChanges {
|
||||
fileName: string;
|
||||
textChanges: TextChange[];
|
||||
isNewFile?: boolean;
|
||||
}
|
||||
interface CodeAction {
|
||||
/** Description of the code action to display in the UI of the editor */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user