Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Pick PR #54358 (Don't use text change's `createNewF...) into release-5.1 #54444

Merged
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
38 changes: 32 additions & 6 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ImplementationLocation,
InlayHint,
InlayHintKind,
InteractiveRefactorArguments,
isString,
JSDocTagInfo,
LanguageService,
Expand All @@ -52,6 +53,7 @@ import {
Program,
QuickInfo,
RefactorEditInfo,
RefactorTriggerReason,
ReferencedSymbol,
ReferenceEntry,
RenameInfo,
Expand Down Expand Up @@ -787,11 +789,25 @@ export class SessionClient implements LanguageService {
return { file, line, offset, endLine, endOffset };
}

getApplicableRefactors(fileName: string, positionOrRange: number | TextRange): ApplicableRefactorInfo[] {
const args = this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName);

getApplicableRefactors(
fileName: string,
positionOrRange: number | TextRange,
preferences: UserPreferences | undefined,
triggerReason?: RefactorTriggerReason,
kind?: string,
includeInteractiveActions?: boolean): ApplicableRefactorInfo[] {
if (preferences) { // Temporarily set preferences
this.configure(preferences);
}
const args: protocol.GetApplicableRefactorsRequestArgs = this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName);
args.triggerReason = triggerReason;
args.kind = kind;
args.includeInteractiveActions = includeInteractiveActions;
const request = this.processRequest<protocol.GetApplicableRefactorsRequest>(protocol.CommandTypes.GetApplicableRefactors, args);
const response = this.processResponse<protocol.GetApplicableRefactorsResponse>(request);
if (preferences) { // Restore preferences
this.configure(this.preferences || {});
}
return response.body!; // TODO: GH#18217
}

Expand All @@ -808,11 +824,17 @@ export class SessionClient implements LanguageService {
_formatOptions: FormatCodeSettings,
positionOrRange: number | TextRange,
refactorName: string,
actionName: string): RefactorEditInfo {

const args = this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName) as protocol.GetEditsForRefactorRequestArgs;
actionName: string,
preferences: UserPreferences | undefined,
interactiveRefactorArguments?: InteractiveRefactorArguments): RefactorEditInfo {
if (preferences) { // Temporarily set preferences
this.configure(preferences);
}
const args =
this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName) as protocol.GetEditsForRefactorRequestArgs;
args.refactor = refactorName;
args.action = actionName;
args.interactiveRefactorArguments = interactiveRefactorArguments;

const request = this.processRequest<protocol.GetEditsForRefactorRequest>(protocol.CommandTypes.GetEditsForRefactor, args);
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);
Expand All @@ -829,6 +851,10 @@ export class SessionClient implements LanguageService {
renameLocation = this.lineOffsetToPosition(renameFilename, response.body.renameLocation!);
}

if (preferences) { // Restore preferences
this.configure(this.preferences || {});
}

return {
edits,
renameFilename,
Expand Down
17 changes: 14 additions & 3 deletions src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ registerRefactor(refactorNameForMoveToFile, {
function doChange(context: RefactorContext, oldFile: SourceFile, targetFile: string, program: Program, toMove: ToMove, changes: textChanges.ChangeTracker, host: LanguageServiceHost, preferences: UserPreferences): void {
const checker = program.getTypeChecker();
const usage = getUsageInfo(oldFile, toMove.all, checker);
//For a new file or an existing blank target file
if (!host.fileExists(targetFile) || host.fileExists(targetFile) && program.getSourceFile(targetFile)?.statements.length === 0) {
//For a new file
if (!host.fileExists(targetFile)) {
changes.createNewFile(oldFile, targetFile, getNewStatementsAndRemoveFromOldFile(oldFile, targetFile, usage, changes, toMove, program, host, preferences));
addNewFileToTsconfig(program, changes, oldFile.fileName, targetFile, hostGetCanonicalFileName(host));
}
Expand All @@ -189,7 +189,15 @@ function doChange(context: RefactorContext, oldFile: SourceFile, targetFile: str
}

function getNewStatementsAndRemoveFromOldFile(
oldFile: SourceFile, targetFile: string | SourceFile, usage: UsageInfo, changes: textChanges.ChangeTracker, toMove: ToMove, program: Program, host: LanguageServiceHost, preferences: UserPreferences, importAdder?: codefix.ImportAdder
oldFile: SourceFile,
targetFile: string | SourceFile,
usage: UsageInfo,
changes: textChanges.ChangeTracker,
toMove: ToMove,
program: Program,
host: LanguageServiceHost,
preferences: UserPreferences,
importAdder?: codefix.ImportAdder
) {
const checker = program.getTypeChecker();
const prologueDirectives = takeWhile(oldFile.statements, isPrologueDirective);
Expand Down Expand Up @@ -218,6 +226,9 @@ function getNewStatementsAndRemoveFromOldFile(
if (targetFile.statements.length > 0) {
changes.insertNodesAfter(targetFile, targetFile.statements[targetFile.statements.length - 1], body);
}
else {
changes.insertNodesAtEndOfFile(targetFile, body, /*blankLineBetween*/ false);
}
if (imports.length > 0) {
insertImports(changes, targetFile, imports, /*blankLineBetween*/ true, preferences);
}
Expand Down
19 changes: 19 additions & 0 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,25 @@ export class ChangeTracker {
}
}

public insertNodesAtEndOfFile(
sourceFile: SourceFile,
newNodes: readonly Statement[],
blankLineBetween: boolean): void {
this.insertAtEndOfFile(sourceFile, newNodes, blankLineBetween);
}

private insertAtEndOfFile(
sourceFile: SourceFile,
insert: readonly Statement[],
blankLineBetween: boolean): void {
const pos = sourceFile.end + 1;
const options = {
prefix: this.newLineCharacter,
suffix: this.newLineCharacter + (blankLineBetween ? this.newLineCharacter : ""),
};
this.insertNodesAt(sourceFile, pos, insert, options);
}

private insertStatementsInNewFile(fileName: string, statements: readonly (Statement | SyntaxKind.NewLineTrivia)[], oldFile?: SourceFile): void {
if (!this.newFileChanges) {
this.newFileChanges = createMultiMap<string, NewFileInsertion>();
Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ export interface LanguageService {
* arguments for any interactive action before offering it.
*/
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[];
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, includeInteractiveActions?: InteractiveRefactorArguments): RefactorEditInfo | undefined;
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, interactiveRefactorArguments?: InteractiveRefactorArguments): RefactorEditInfo | undefined;
getMoveToRefactoringFileSuggestions(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string): { newFileName: string, files: string[] };
organizeImports(args: OrganizeImportsArgs, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[];
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10191,7 +10191,7 @@ declare namespace ts {
* arguments for any interactive action before offering it.
*/
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[];
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, includeInteractiveActions?: InteractiveRefactorArguments): RefactorEditInfo | undefined;
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, interactiveRefactorArguments?: InteractiveRefactorArguments): RefactorEditInfo | undefined;
getMoveToRefactoringFileSuggestions(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string): {
newFileName: string;
files: string[];
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6227,7 +6227,7 @@ declare namespace ts {
* arguments for any interactive action before offering it.
*/
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[];
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, includeInteractiveActions?: InteractiveRefactorArguments): RefactorEditInfo | undefined;
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, interactiveRefactorArguments?: InteractiveRefactorArguments): RefactorEditInfo | undefined;
getMoveToRefactoringFileSuggestions(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string): {
newFileName: string;
files: string[];
Expand Down
6 changes: 5 additions & 1 deletion tests/cases/fourslash/moveToFile_blankExistingFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ verify.moveToFile({
`,

"/bar.ts":
`import { b } from './other';
`//
import { p } from './a';


import { b } from "./other";


const y: Date = p + b;
`,
},
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/fourslash/moveToFile_differentDirectories2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export const a = 10;
y;`,

"/src/dir2/bar.ts":
`import { b } from '../dir1/other';
import { a } from '../dir1/a';
`import { a } from '../dir1/a';

import { b } from "../dir1/other";


export const y = b + a;
`,
Expand Down
27 changes: 27 additions & 0 deletions tests/cases/fourslash/server/moveToFile_emptyTargetFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path='../fourslash.ts' />

// @Filename: /source.ts
////[|export const a = 1;|]
////const b = 2;
////console.log(a, b);

// @Filename: /target.ts
/////** empty */

// @Filename: /tsconfig.json
///// { "compilerOptions": { "newLine": "lf" } }

verify.moveToFile({
newFileContents: {
"/source.ts":
`import { a } from "./target";

const b = 2;
console.log(a, b);`,
"/target.ts":
`/** empty */
export const a = 1;
`,
},
interactiveRefactorArguments: { targetFile: "/target.ts" },
});