-
Notifications
You must be signed in to change notification settings - Fork 513
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
47ccd39
add .Save() to FileContext API
TylerLeonhardt 0198d8f
check for different OS's and also check if the file is dirty before s…
TylerLeonhardt 2fdb3e8
macOS is case-insensitive
TylerLeonhardt a65f946
pull out find code into helper function
TylerLeonhardt cc04b7d
address Keith's feedback
TylerLeonhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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>( | ||
|
@@ -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)); | ||
|
@@ -361,13 +372,7 @@ 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 = | ||
vscode.workspace.openTextDocument(filePath) | ||
|
@@ -381,18 +386,21 @@ export class ExtensionCommandsFeature implements IFeature { | |
|
||
var promise: Thenable<EditorOperationResponse>; | ||
|
||
// 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, 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 (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath)) | ||
if (canFind) | ||
{ | ||
promise = | ||
vscode.workspace.openTextDocument(filePath) | ||
|
@@ -408,6 +416,74 @@ export class ExtensionCommandsFeature implements IFeature { | |
return promise; | ||
} | ||
|
||
private saveFile(filePath: string): Thenable<EditorOperationResponse> { | ||
|
||
var promise: Thenable<EditorOperationResponse>; | ||
|
||
var normalizedFilePath = this.normalizeFilePath(filePath); | ||
|
||
// since Windows is case-insensitive, we need to normalize it differently | ||
var canFind = vscode.workspace.textDocuments.find(doc => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably also good to make this its own function to avoid duplication There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Looks much cleaner now. Thanks! |
||
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) | ||
{ | ||
promise = | ||
vscode.workspace.openTextDocument(filePath) | ||
.then(doc => { | ||
if (doc.isDirty) { | ||
doc.save(); | ||
} | ||
}) | ||
.then(_ => EditorOperationResponse.Completed); | ||
} | ||
else | ||
{ | ||
promise = Promise.resolve(EditorOperationResponse.Completed); | ||
} | ||
|
||
return promise; | ||
} | ||
|
||
private normalizeFilePath(filePath: string): string { | ||
var 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 setSelection(details: SetSelectionRequestArguments): EditorOperationResponse { | ||
vscode.window.activeTextEditor.selections = [ | ||
new vscode.Selection( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another linter thing and something @daviwil espoused as well - favor the use of
let
overvar
.let
is properly blocked scoped in JavaScript. I'm commenting on this one line about it but there are quite a fewvar
usages in this PR that should belet
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed all occurrences in the file :)