From 47ccd391f180a6380fa2031d3a167e1ab48377e1 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 28 Dec 2017 19:02:52 -0800 Subject: [PATCH 1/5] add .Save() to FileContext API --- src/features/ExtensionCommands.ts | 42 ++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/features/ExtensionCommands.ts b/src/features/ExtensionCommands.ts index 8cf49bcb76..99718339cb 100644 --- a/src/features/ExtensionCommands.ts +++ b/src/features/ExtensionCommands.ts @@ -140,6 +140,12 @@ export namespace CloseFileRequest { 'editor/closeFile'); } +export namespace SaveFileRequest { + export const type = + new RequestType( + 'editor/saveFile'); +} + export namespace ShowErrorMessageRequest { export const type = new RequestType( @@ -239,7 +245,7 @@ export class ExtensionCommandsFeature implements IFeature { NewFileRequest.type, filePath => this.newFile()); - this.languageClient.onRequest( + this.languageClient.onRequest( OpenFileRequest.type, filePath => this.openFile(filePath)); @@ -247,6 +253,10 @@ export class ExtensionCommandsFeature implements IFeature { CloseFileRequest.type, filePath => this.closeFile(filePath)); + this.languageClient.onRequest( + SaveFileRequest.type, + filePath => this.saveFile(filePath)); + this.languageClient.onRequest( ShowInformationMessageRequest.type, message => this.showInformationMessage(message)); @@ -408,6 +418,36 @@ export class ExtensionCommandsFeature implements IFeature { return promise; } + private saveFile(filePath: string): Thenable { + + var promise: Thenable; + + // 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 + var normalizedFilePath = filePath.toLowerCase(); + + if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath)) + { + promise = + vscode.workspace.openTextDocument(filePath) + .then(doc => doc.save()) + .then(_ => EditorOperationResponse.Completed); + } + else + { + promise = Promise.resolve(EditorOperationResponse.Completed); + } + + return promise; + } + private setSelection(details: SetSelectionRequestArguments): EditorOperationResponse { vscode.window.activeTextEditor.selections = [ new vscode.Selection( From 0198d8ff6c5bc9b90d4713cb191f4f84f1271f32 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 29 Dec 2017 00:18:57 -0800 Subject: [PATCH 2/5] check for different OS's and also check if the file is dirty before saving --- src/features/ExtensionCommands.ts | 86 +++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 28 deletions(-) diff --git a/src/features/ExtensionCommands.ts b/src/features/ExtensionCommands.ts index 99718339cb..76ab8268f1 100644 --- a/src/features/ExtensionCommands.ts +++ b/src/features/ExtensionCommands.ts @@ -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'; @@ -371,13 +372,7 @@ export class ExtensionCommandsFeature implements IFeature { private openFile(filePath: string): Thenable { - // 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 = vscode.workspace.openTextDocument(filePath) @@ -391,18 +386,21 @@ export class ExtensionCommandsFeature implements IFeature { var promise: Thenable; - // Make sure the file path is absolute - if (!path.win32.isAbsolute(filePath)) - { - filePath = path.win32.resolve( - vscode.workspace.rootPath, - filePath); - } + var normalizedFilePath = this.normalizeFilePath(filePath); - // Normalize file path case for comparison - var normalizedFilePath = filePath.toLowerCase(); + // since Windows is case-insensitive, we need to normalize it differently + var canFind = vscode.workspace.textDocuments.find(doc => { + var docPath; + if (os.platform() == "win32") { + // for windows paths, they are normalized to be lowercase + docPath = doc.fileName.toLowerCase(); + } else { + docPath = doc.fileName; + } + return docPath == normalizedFilePath; + }); - if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath)) + if (canFind) { promise = vscode.workspace.openTextDocument(filePath) @@ -422,22 +420,29 @@ export class ExtensionCommandsFeature implements IFeature { var promise: Thenable; - // Make sure the file path is absolute - if (!path.win32.isAbsolute(filePath)) - { - filePath = path.win32.resolve( - vscode.workspace.rootPath, - filePath); - } + var normalizedFilePath = this.normalizeFilePath(filePath); - // Normalize file path case for comparison - var normalizedFilePath = filePath.toLowerCase(); + // since Windows is case-insensitive, we need to normalize it differently + var canFind = vscode.workspace.textDocuments.find(doc => { + var docPath; + if (os.platform() == "win32") { + // for windows paths, they are normalized to be lowercase + docPath = doc.fileName.toLowerCase(); + } else { + docPath = doc.fileName; + } + return docPath == normalizedFilePath; + }); - if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath)) + if (canFind) { promise = vscode.workspace.openTextDocument(filePath) - .then(doc => doc.save()) + .then(doc => { + if (doc.isDirty) { + doc.save(); + } + }) .then(_ => EditorOperationResponse.Completed); } else @@ -448,6 +453,31 @@ export class ExtensionCommandsFeature implements IFeature { return promise; } + private normalizeFilePath(filePath: string): string { + if (os.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); + } + + return filePath; + } + } + private setSelection(details: SetSelectionRequestArguments): EditorOperationResponse { vscode.window.activeTextEditor.selections = [ new vscode.Selection( From 2fdb3e8a373f6aa61bc8af15409211399af10dfa Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 29 Dec 2017 00:51:12 -0800 Subject: [PATCH 3/5] macOS is case-insensitive --- src/features/ExtensionCommands.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/features/ExtensionCommands.ts b/src/features/ExtensionCommands.ts index 76ab8268f1..b3b7813d07 100644 --- a/src/features/ExtensionCommands.ts +++ b/src/features/ExtensionCommands.ts @@ -390,8 +390,8 @@ export class ExtensionCommandsFeature implements IFeature { // since Windows is case-insensitive, we need to normalize it differently var canFind = vscode.workspace.textDocuments.find(doc => { - var docPath; - if (os.platform() == "win32") { + var docPath, platform = os.platform(); + if (platform == "win32" || platform == "darwin") { // for windows paths, they are normalized to be lowercase docPath = doc.fileName.toLowerCase(); } else { @@ -424,8 +424,8 @@ export class ExtensionCommandsFeature implements IFeature { // since Windows is case-insensitive, we need to normalize it differently var canFind = vscode.workspace.textDocuments.find(doc => { - var docPath; - if (os.platform() == "win32") { + var docPath, platform = os.platform(); + if (platform == "win32" || platform == "darwin") { // for windows paths, they are normalized to be lowercase docPath = doc.fileName.toLowerCase(); } else { @@ -454,7 +454,8 @@ export class ExtensionCommandsFeature implements IFeature { } private normalizeFilePath(filePath: string): string { - if (os.platform() == "win32") { + var platform = os.platform(); + if (platform == "win32") { // Make sure the file path is absolute if (!path.win32.isAbsolute(filePath)) { @@ -474,7 +475,12 @@ export class ExtensionCommandsFeature implements IFeature { filePath); } - return filePath; + //macOS is case-insensitive + if (platform == "darwin") { + filePath = filePath.toLowerCase(); + } + + return filePath; } } From a65f9464f9c406e1ce242f3cfcb588f4cc0a5533 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 29 Dec 2017 12:37:54 -0800 Subject: [PATCH 4/5] pull out find code into helper function --- src/features/ExtensionCommands.ts | 50 +++++++++++-------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/features/ExtensionCommands.ts b/src/features/ExtensionCommands.ts index b3b7813d07..808492a8a4 100644 --- a/src/features/ExtensionCommands.ts +++ b/src/features/ExtensionCommands.ts @@ -385,22 +385,7 @@ export class ExtensionCommandsFeature implements IFeature { private closeFile(filePath: string): Thenable { var promise: Thenable; - - var normalizedFilePath = this.normalizeFilePath(filePath); - - // since Windows is case-insensitive, we need to normalize it differently - var canFind = vscode.workspace.textDocuments.find(doc => { - var docPath, platform = os.platform(); - if (platform == "win32" || platform == "darwin") { - // for windows paths, they are normalized to be lowercase - docPath = doc.fileName.toLowerCase(); - } else { - docPath = doc.fileName; - } - return docPath == normalizedFilePath; - }); - - if (canFind) + if (this.findTextDocument(this.normalizeFilePath(filePath))) { promise = vscode.workspace.openTextDocument(filePath) @@ -419,22 +404,7 @@ export class ExtensionCommandsFeature implements IFeature { private saveFile(filePath: string): Thenable { var promise: Thenable; - - var normalizedFilePath = this.normalizeFilePath(filePath); - - // since Windows is case-insensitive, we need to normalize it differently - var canFind = vscode.workspace.textDocuments.find(doc => { - var docPath, platform = os.platform(); - if (platform == "win32" || platform == "darwin") { - // for windows paths, they are normalized to be lowercase - docPath = doc.fileName.toLowerCase(); - } else { - docPath = doc.fileName; - } - return docPath == normalizedFilePath; - }); - - if (canFind) + if (this.findTextDocument(this.normalizeFilePath(filePath))) { promise = vscode.workspace.openTextDocument(filePath) @@ -484,6 +454,22 @@ export class ExtensionCommandsFeature implements IFeature { } } + private findTextDocument(filePath: string): boolean { + // since Windows and macOS are case-insensitive, we need to normalize them differently + var canFind = vscode.workspace.textDocuments.find(doc => { + var 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( From cc04b7d0d8289ca13f8a6bde9c2a2a8d609afd5b Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 2 Jan 2018 07:53:38 -0800 Subject: [PATCH 5/5] address Keith's feedback --- src/features/ExtensionCommands.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/features/ExtensionCommands.ts b/src/features/ExtensionCommands.ts index 808492a8a4..0032671825 100644 --- a/src/features/ExtensionCommands.ts +++ b/src/features/ExtensionCommands.ts @@ -193,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) } @@ -303,7 +303,7 @@ export class ExtensionCommandsFeature implements IFeature { return; } - var quickPickItems = + let quickPickItems = this.extensionCommands.map(command => { return { label: command.displayName, @@ -332,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), @@ -374,7 +374,7 @@ export class ExtensionCommandsFeature implements IFeature { filePath = this.normalizeFilePath(filePath); - var promise = + let promise = vscode.workspace.openTextDocument(filePath) .then(doc => vscode.window.showTextDocument(doc)) .then(_ => EditorOperationResponse.Completed); @@ -384,7 +384,7 @@ export class ExtensionCommandsFeature implements IFeature { private closeFile(filePath: string): Thenable { - var promise: Thenable; + let promise: Thenable; if (this.findTextDocument(this.normalizeFilePath(filePath))) { promise = @@ -403,12 +403,12 @@ export class ExtensionCommandsFeature implements IFeature { private saveFile(filePath: string): Thenable { - var promise: Thenable; + let promise: Thenable; if (this.findTextDocument(this.normalizeFilePath(filePath))) { promise = vscode.workspace.openTextDocument(filePath) - .then(doc => { + .then((doc) => { if (doc.isDirty) { doc.save(); } @@ -424,7 +424,7 @@ export class ExtensionCommandsFeature implements IFeature { } private normalizeFilePath(filePath: string): string { - var platform = os.platform(); + let platform = os.platform(); if (platform == "win32") { // Make sure the file path is absolute if (!path.win32.isAbsolute(filePath)) @@ -456,15 +456,15 @@ export class ExtensionCommandsFeature implements IFeature { private findTextDocument(filePath: string): boolean { // since Windows and macOS are case-insensitive, we need to normalize them differently - var canFind = vscode.workspace.textDocuments.find(doc => { - var docPath, platform = os.platform(); + 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 docPath === filePath; }); return canFind != null;