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
3 commits merged into from
Apr 20, 2018
Merged

Add 'renameFile' command to services #23573

3 commits merged into from
Apr 20, 2018

Conversation

ghost
Copy link

@ghost ghost 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.

@ghost ghost requested review from mhegazy, sheetalkamat and amcasey April 20, 2018 17:15
@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2018

//cc @mjbvz

@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2018

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

@mjbvz
Copy link
Contributor

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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ghost
Copy link
Author

ghost 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?

@ghost ghost merged commit 5c94bef into master Apr 20, 2018
@ghost ghost deleted the renameFile branch April 20, 2018 20:43
@mhegazy
Copy link
Contributor

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 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 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
Contributor

mhegazy commented Jun 12, 2018

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

@mhegazy
Copy link
Contributor

mhegazy commented Jun 12, 2018

Filed #24904 to track folder renames.

atscott added a commit to atscott/vscode-ng-language-service that referenced this pull request May 18, 2021
atscott added a commit to atscott/vscode-ng-language-service that referenced this pull request May 18, 2021
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants