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 CurrentFile SaveAs() support #1261

Merged
merged 5 commits into from
Apr 18, 2018
Merged
Changes from 1 commit
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
Next Next commit
Add file saveas support
  • Loading branch information
rjmholt committed Apr 6, 2018
commit c07f5e904f274e938a10b3836e08760fa07e9101
68 changes: 52 additions & 16 deletions src/features/ExtensionCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const CloseFileRequestType =
"editor/closeFile");

export const SaveFileRequestType =
new RequestType<string, EditorOperationResponse, void, void>(
new RequestType<ISaveFileDetails, EditorOperationResponse, void, void>(
"editor/saveFile");

export const ShowErrorMessageRequestType =
Expand All @@ -151,6 +151,11 @@ export const SetStatusBarMessageRequestType =
new RequestType<IStatusBarMessageDetails, EditorOperationResponse, void, void>(
"editor/setStatusBarMessage");

export interface ISaveFileDetails {
filePath: string;
newPath?: string;
}

export interface IStatusBarMessageDetails {
message: string;
timeout?: number;
Expand Down Expand Up @@ -238,7 +243,7 @@ export class ExtensionCommandsFeature implements IFeature {

this.languageClient.onRequest(
SaveFileRequestType,
(filePath) => this.saveFile(filePath));
(saveFileDetails) => this.saveFile(saveFileDetails));

this.languageClient.onRequest(
ShowInformationMessageRequestType,
Expand Down Expand Up @@ -382,23 +387,54 @@ export class ExtensionCommandsFeature implements IFeature {
return promise;
}

private saveFile(filePath: string): Thenable<EditorOperationResponse> {
private saveFile(saveFileDetails: ISaveFileDetails): Thenable<EditorOperationResponse> {

let promise: Thenable<EditorOperationResponse>;
if (this.findTextDocument(this.normalizeFilePath(filePath))) {
promise =
vscode.workspace.openTextDocument(filePath)
.then((doc) => {
if (doc.isDirty) {
doc.save();
}
})
.then((_) => EditorOperationResponse.Completed);
} else {
promise = Promise.resolve(EditorOperationResponse.Completed);
// If the file to save can't be found, just complete the request
if (!this.findTextDocument(this.normalizeFilePath(saveFileDetails.filePath))) {
return Promise.resolve(EditorOperationResponse.Completed);
}

return promise;
// If no newFile is given, just save the current file
if (!saveFileDetails.newPath) {
return vscode.workspace.openTextDocument(saveFileDetails.filePath)
.then((doc) => {
if (doc.isDirty) {
doc.save();
}
})
.then((_) => EditorOperationResponse.Completed);
}

// Otherwise we want to save as a new file

// First turn the path we were given into an absolute path
// Relative paths are interpreted as relative to the original file
const newFileAbsolutePath = path.isAbsolute(saveFileDetails.newPath) ?
saveFileDetails.newPath :
path.resolve(path.dirname(saveFileDetails.filePath), saveFileDetails.newPath);

// Create an "untitled-scheme" path so that VSCode will let us create a new file with a given path
const newFileUri = vscode.Uri.parse("untitled:" + newFileAbsolutePath);

// Now we need to copy the content of the current file,
// then create a new file at the given path, insert the content,
// save it and open the document
return vscode.workspace.openTextDocument(saveFileDetails.filePath)
.then((oldDoc) => {
return vscode.workspace.openTextDocument(newFileUri)
.then((newDoc) => {
return vscode.window.showTextDocument(newDoc, 1, false)
.then((editor) => {
return editor.edit((editBuilder) => {
editBuilder.insert(new vscode.Position(0, 0), oldDoc.getText());
})
.then(() => {
return newDoc.save()
.then(() => EditorOperationResponse.Completed);
});
});
});
});
Copy link
Member

Choose a reason for hiding this comment

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

triggered

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needs a nice syntactic sugar I think...

}

private normalizeFilePath(filePath: string): string {
Expand Down