-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
Don't use text change's createNewFile
for existing empty file
#54358
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { GetApplicableRefactorsRequestArgs, GetEditsForRefactorRequestArgs } from "../server/protocol.js"; | ||
import { | ||
ApplicableRefactorInfo, | ||
CallHierarchyIncomingCall, | ||
|
@@ -36,6 +37,7 @@ import { | |
ImplementationLocation, | ||
InlayHint, | ||
InlayHintKind, | ||
InteractiveRefactorArguments, | ||
isString, | ||
JSDocTagInfo, | ||
LanguageService, | ||
|
@@ -52,6 +54,7 @@ import { | |
Program, | ||
QuickInfo, | ||
RefactorEditInfo, | ||
RefactorTriggerReason, | ||
ReferencedSymbol, | ||
ReferenceEntry, | ||
RenameInfo, | ||
|
@@ -787,11 +790,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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fourslash server tests previously didn't work with move to file |
||
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: 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 | ||
} | ||
|
||
|
@@ -808,11 +825,16 @@ 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: GetEditsForRefactorRequestArgs = 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); | ||
|
@@ -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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding the extra new lines, we could also change this code to pass import { a } from "./a";
const x = 2; instead of import { a } from "./a";
const x = 2; |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drive-by fix, I thought this parameter had a misleading name, since it's not a boolean. |
||
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[]; | ||
|
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" }, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
protocol
is already imported from a namespace file, and I think that may still matter for initialization order in some cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did a .js extension show up here?
(I would use the existing import if possible)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this was auto-imports with non-default VS Code settings (
importModuleSpecifierEnding=js
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I had
importModuleSpecifierEnding
set tojs
on my user settings (I think I accidentally changed the user one while trying to repro a bug in the past). Thanks for pointing that out