Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 18 additions & 5 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1744,11 +1744,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 {
Expand All @@ -1759,6 +1765,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);
Expand Down
2 changes: 1 addition & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ namespace ts {
export interface FileTextChanges {
fileName: string;
textChanges: TextChange[];
isNewFile?: boolean;
}

export interface CodeAction {
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4617,6 +4617,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 */
Expand Down Expand Up @@ -7892,7 +7893,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;
Expand Down Expand Up @@ -8485,6 +8486,7 @@ declare namespace ts.server {
private mapTextChangesToCodeEdits;
private mapTextChangesToCodeEditsUsingScriptinfo;
private convertTextChangeToCodeEdit;
private convertNewFileTextChangeToCodeEdit;
private getBraceMatching;
private getDiagnosticsForProject;
getCanonicalFileName(fileName: string): string;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4617,6 +4617,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 */
Expand Down