Skip to content

Commit 47ccd39

Browse files
add .Save() to FileContext API
1 parent 7a00934 commit 47ccd39

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/features/ExtensionCommands.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ export namespace CloseFileRequest {
140140
'editor/closeFile');
141141
}
142142

143+
export namespace SaveFileRequest {
144+
export const type =
145+
new RequestType<string, EditorOperationResponse, void, void>(
146+
'editor/saveFile');
147+
}
148+
143149
export namespace ShowErrorMessageRequest {
144150
export const type =
145151
new RequestType<string, EditorOperationResponse, void, void>(
@@ -239,14 +245,18 @@ export class ExtensionCommandsFeature implements IFeature {
239245
NewFileRequest.type,
240246
filePath => this.newFile());
241247

242-
this.languageClient.onRequest(
248+
this.languageClient.onRequest(
243249
OpenFileRequest.type,
244250
filePath => this.openFile(filePath));
245251

246252
this.languageClient.onRequest(
247253
CloseFileRequest.type,
248254
filePath => this.closeFile(filePath));
249255

256+
this.languageClient.onRequest(
257+
SaveFileRequest.type,
258+
filePath => this.saveFile(filePath));
259+
250260
this.languageClient.onRequest(
251261
ShowInformationMessageRequest.type,
252262
message => this.showInformationMessage(message));
@@ -408,6 +418,36 @@ export class ExtensionCommandsFeature implements IFeature {
408418
return promise;
409419
}
410420

421+
private saveFile(filePath: string): Thenable<EditorOperationResponse> {
422+
423+
var promise: Thenable<EditorOperationResponse>;
424+
425+
// Make sure the file path is absolute
426+
if (!path.win32.isAbsolute(filePath))
427+
{
428+
filePath = path.win32.resolve(
429+
vscode.workspace.rootPath,
430+
filePath);
431+
}
432+
433+
// Normalize file path case for comparison
434+
var normalizedFilePath = filePath.toLowerCase();
435+
436+
if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath))
437+
{
438+
promise =
439+
vscode.workspace.openTextDocument(filePath)
440+
.then(doc => doc.save())
441+
.then(_ => EditorOperationResponse.Completed);
442+
}
443+
else
444+
{
445+
promise = Promise.resolve(EditorOperationResponse.Completed);
446+
}
447+
448+
return promise;
449+
}
450+
411451
private setSelection(details: SetSelectionRequestArguments): EditorOperationResponse {
412452
vscode.window.activeTextEditor.selections = [
413453
new vscode.Selection(

0 commit comments

Comments
 (0)