Skip to content

add .Save() to FileContext API #1139

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

Merged
merged 5 commits into from
Jan 3, 2018
Merged
Changes from all commits
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
114 changes: 88 additions & 26 deletions src/features/ExtensionCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import os = require('os');
import path = require('path');
import vscode = require('vscode');
import { IFeature } from '../feature';
Expand Down Expand Up @@ -140,6 +141,12 @@ export namespace CloseFileRequest {
'editor/closeFile');
}

export namespace SaveFileRequest {
export const type =
new RequestType<string, EditorOperationResponse, void, void>(
'editor/saveFile');
}

export namespace ShowErrorMessageRequest {
export const type =
new RequestType<string, EditorOperationResponse, void, void>(
Expand Down Expand Up @@ -186,9 +193,9 @@ export class ExtensionCommandsFeature implements IFeature {
return;
}

var editor = vscode.window.activeTextEditor;
var start = editor.selection.start;
var end = editor.selection.end;
let editor = vscode.window.activeTextEditor;
let start = editor.selection.start;
let end = editor.selection.end;
if (editor.selection.isEmpty) {
start = new vscode.Position(start.line, 0)
}
Expand Down Expand Up @@ -239,14 +246,18 @@ export class ExtensionCommandsFeature implements IFeature {
NewFileRequest.type,
filePath => this.newFile());

this.languageClient.onRequest(
this.languageClient.onRequest(
OpenFileRequest.type,
filePath => this.openFile(filePath));

this.languageClient.onRequest(
CloseFileRequest.type,
filePath => this.closeFile(filePath));

this.languageClient.onRequest(
SaveFileRequest.type,
filePath => this.saveFile(filePath));

this.languageClient.onRequest(
ShowInformationMessageRequest.type,
message => this.showInformationMessage(message));
Expand Down Expand Up @@ -292,7 +303,7 @@ export class ExtensionCommandsFeature implements IFeature {
return;
}

var quickPickItems =
let quickPickItems =
this.extensionCommands.map<ExtensionCommandQuickPickItem>(command => {
return {
label: command.displayName,
Expand Down Expand Up @@ -321,7 +332,7 @@ export class ExtensionCommandsFeature implements IFeature {
}

private insertText(details: InsertTextRequestArguments): EditorOperationResponse {
var edit = new vscode.WorkspaceEdit();
let edit = new vscode.WorkspaceEdit();

edit.set(
vscode.Uri.parse(details.filePath),
Expand Down Expand Up @@ -361,15 +372,9 @@ export class ExtensionCommandsFeature implements IFeature {

private openFile(filePath: string): Thenable<EditorOperationResponse> {

// Make sure the file path is absolute
if (!path.win32.isAbsolute(filePath))
{
filePath = path.win32.resolve(
vscode.workspace.rootPath,
filePath);
}
filePath = this.normalizeFilePath(filePath);

var promise =
let promise =
vscode.workspace.openTextDocument(filePath)
.then(doc => vscode.window.showTextDocument(doc))
.then(_ => EditorOperationResponse.Completed);
Expand All @@ -379,25 +384,35 @@ export class ExtensionCommandsFeature implements IFeature {

private closeFile(filePath: string): Thenable<EditorOperationResponse> {

var promise: Thenable<EditorOperationResponse>;

// Make sure the file path is absolute
if (!path.win32.isAbsolute(filePath))
let promise: Thenable<EditorOperationResponse>;
if (this.findTextDocument(this.normalizeFilePath(filePath)))
{
filePath = path.win32.resolve(
vscode.workspace.rootPath,
filePath);
promise =
vscode.workspace.openTextDocument(filePath)
.then(doc => vscode.window.showTextDocument(doc))
.then(editor => vscode.commands.executeCommand("workbench.action.closeActiveEditor"))
.then(_ => EditorOperationResponse.Completed);
}
else
{
promise = Promise.resolve(EditorOperationResponse.Completed);
}

// Normalize file path case for comparison
var normalizedFilePath = filePath.toLowerCase();
return promise;
}

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

if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath))
let promise: Thenable<EditorOperationResponse>;
if (this.findTextDocument(this.normalizeFilePath(filePath)))
{
promise =
vscode.workspace.openTextDocument(filePath)
.then(doc => vscode.window.showTextDocument(doc))
.then(editor => vscode.commands.executeCommand("workbench.action.closeActiveEditor"))
.then((doc) => {
if (doc.isDirty) {
doc.save();
}
})
.then(_ => EditorOperationResponse.Completed);
}
else
Expand All @@ -408,6 +423,53 @@ export class ExtensionCommandsFeature implements IFeature {
return promise;
}

private normalizeFilePath(filePath: string): string {
let platform = os.platform();
if (platform == "win32") {
// Make sure the file path is absolute
if (!path.win32.isAbsolute(filePath))
{
filePath = path.win32.resolve(
vscode.workspace.rootPath,
filePath);
}

// Normalize file path case for comparison for Windows
return filePath.toLowerCase();
} else {
// Make sure the file path is absolute
if (!path.isAbsolute(filePath))
{
filePath = path.resolve(
vscode.workspace.rootPath,
filePath);
}

//macOS is case-insensitive
if (platform == "darwin") {
filePath = filePath.toLowerCase();
}

return filePath;
}
}

private findTextDocument(filePath: string): boolean {
// since Windows and macOS are case-insensitive, we need to normalize them differently
let canFind = vscode.workspace.textDocuments.find(doc => {
let docPath, platform = os.platform();
if (platform == "win32" || platform == "darwin") {
// for Windows and macOS paths, they are normalized to be lowercase
docPath = doc.fileName.toLowerCase();
} else {
docPath = doc.fileName;
}
return docPath === filePath;
});

return canFind != null;
}

private setSelection(details: SetSelectionRequestArguments): EditorOperationResponse {
vscode.window.activeTextEditor.selections = [
new vscode.Selection(
Expand Down