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

Add 'renameFile' command to services #23573

Merged
merged 3 commits into from Apr 20, 2018
Merged

Add 'renameFile' command to services #23573

merged 3 commits into from Apr 20, 2018

Conversation

@andy-ms
Copy link
Contributor

@andy-ms andy-ms commented Apr 20, 2018

Here's how I think it would work:

  • First, the user renames a file normally using the file explorer in their editor.
  • Tsserver will pick up on this change and update the project (which will break a lot of imports to the old location).
  • After the rename, the editor will offer the user to update references to the file.
  • If the user says yes, it will then ask tsserver to get code edits, passing in the old and new paths to the file.
  • Tsserver will walk all imports, see which ones are unresolved now and could have resolved to the old path (but don't since that's been moved now), and update them to the new path instead, and send these edits to the editor.
  • The editor will apply the edits to all files. (Hopefully with undo functionality if the user decides against renaming the file.)

This is a bit tricky on the tsserver side since we need to be able to figure out what unresolved imports could have resolved to. This meant I had to leave moduleResolutionCache around so we could look at the failed lookup locations for failed imports.

Fully expecting to need a lot of revision on this. CC @mjbvz for protocol review.

@andy-ms andy-ms requested review from mhegazy, sheetalkamat and amcasey Apr 20, 2018
@mhegazy
Copy link

@mhegazy mhegazy commented Apr 20, 2018

//cc @mjbvz

@mhegazy
Copy link

@mhegazy mhegazy commented Apr 20, 2018

We also need to consider /// <reference path="..." /> comments. bonus point for tsconfig.json "files" entries.

@andy-ms andy-ms force-pushed the renameFile branch from 95525be to 505189f Apr 20, 2018
@mjbvz
Copy link
Contributor

@mjbvz mjbvz commented Apr 20, 2018

Ok, I think this flow makes sense and the api looks good.

We may also need an api to check if any renames are necessary so we can avoid prompting the user in cases with loose files

@@ -63,6 +63,7 @@
"navigateTo.ts",
"navigationBar.ts",
"organizeImports.ts",
"../services/renameFile.ts",

This comment has been minimized.

@mhegazy

mhegazy Apr 20, 2018

jsut renameFile.ts

@@ -1950,6 +1950,10 @@ namespace ts {
return OrganizeImports.organizeImports(sourceFile, formatContext, host, program, preferences);
}

function renameFile(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings): ReadonlyArray<FileTextChanges> {

This comment has been minimized.

@mhegazy

mhegazy Apr 20, 2018

the name makes me think there is will rename the file as well.. how about getEditsForFileRename

@andy-ms
Copy link
Contributor Author

@andy-ms andy-ms commented Apr 20, 2018

@sheetalkamat What's a good way to get access to the tsconfig.json source file given a Program object, so that we can make edits for it?

@andy-ms andy-ms merged commit 5c94bef into master Apr 20, 2018
8 checks passed
8 checks passed
TypeScript Test Run typescript_node.6 Build finished.
Details
TypeScript Test Run typescript_node.8 Build finished.
Details
TypeScript Test Run typescript_node.stable Build finished.
Details
ci/circleci: node6 Your tests passed on CircleCI!
Details
ci/circleci: node8 Your tests passed on CircleCI!
Details
ci/circleci: node9 Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla All CLA requirements met.
Details
@andy-ms andy-ms deleted the renameFile branch Apr 20, 2018
@mhegazy
Copy link

@mhegazy mhegazy commented Apr 20, 2018

@andy-ms the soruceFile for the tsconfig.json is stored in options.configFile, assuming one exists.

@mhegazy mhegazy mentioned this pull request May 3, 2018
@rauschma
Copy link

@rauschma rauschma commented May 3, 2018

FWIW: most IDEs I’ve used, asked me – before the file was actually moved: “Do you also want to update references?”
Naively, it seems like that could simplify the algorithm.

@MgSam
Copy link

@MgSam MgSam commented Jun 12, 2018

Is this supposed to work for renaming folders as well? It doesn't seem to in VS Code 1.24 / TypeScript 2.9.1.

@mhegazy
Copy link

@mhegazy mhegazy commented Jun 12, 2018

Folder rename should be working in typescript@2.9.2 and next VSCode release.

@mhegazy
Copy link

@mhegazy mhegazy commented Jun 12, 2018

Filed #24904 to track folder renames.

@williamboman williamboman mentioned this pull request May 6, 2019
0 of 3 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.