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

Conversation

TylerLeonhardt
Copy link
Member

@TylerLeonhardt TylerLeonhardt commented Dec 29, 2017

This is the VSCode portion of this change. See the PSES portion here: PowerShell/PowerShellEditorServices#590

Please let me know if I'm missing something.

To test this, you can use:

$psEditor.GetEditorContext().CurrentFile.Save()

in the Integrated Console.

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

if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath))
Copy link
Contributor

@rkeithhill rkeithhill Dec 29, 2017

Choose a reason for hiding this comment

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

Careful here. On Linux, the file system is case-sensitive so this test could find the wrong file. Probably need to do a runtime check of the OS or file-system. I wonder if there is some function in node's OS or Path modules that could help?

Copy link
Member Author

Choose a reason for hiding this comment

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

Very good point. I actually grabbed that from:
https://github.com/PowerShell/vscode-powershell/blob/master/src/features/ExtensionCommands.ts#L393

So I'll update that as well. Perhaps we should still normalize on Windows?

Copy link
Contributor

Choose a reason for hiding this comment

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

Windows and I think macOS, right? Doesn't it also have a case-insensitive file system?

Copy link
Member Author

@TylerLeonhardt TylerLeonhardt Dec 29, 2017

Choose a reason for hiding this comment

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

Ah. I didn't know macOS was case-insensitive! I'll fix the if-check.

var normalizedFilePath = this.normalizeFilePath(filePath);

// since Windows is case-insensitive, we need to normalize it differently
var canFind = vscode.workspace.textDocuments.find(doc => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably also good to make this its own function to avoid duplication

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. Looks much cleaner now. Thanks!

@TylerLeonhardt
Copy link
Member Author

@rkeithhill when you sign off, I'll get this merged in 😄


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 => {
Copy link
Contributor

Choose a reason for hiding this comment

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

tslint will complain if doc isn't in parens (doc). I've started work with tslint but man, there is soo much change. And somewhere along the line my lint warning fixes broke various commands. Anyway, we should still probably try to avoid adding new stuff for the linter to complain about. :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!


if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath))
var promise: Thenable<EditorOperationResponse>;
Copy link
Contributor

@rkeithhill rkeithhill Dec 31, 2017

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 over var. let is properly blocked scoped in JavaScript. I'm commenting on this one line about it but there are quite a few var usages in this PR that should be let.

Copy link
Member Author

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 :)

} else {
docPath = doc.fileName;
}
return docPath == filePath;
Copy link
Contributor

@rkeithhill rkeithhill Dec 31, 2017

Choose a reason for hiding this comment

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

Probably want to use a === here to prevent any type coercion although both should be strings. I tend to favor === and !== unless I know I want the comparison to do some type coercion for me. See https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed!

Copy link
Contributor

@rkeithhill rkeithhill left a comment

Choose a reason for hiding this comment

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

LGTM

@TylerLeonhardt
Copy link
Member Author

Thanks @rkeithhill and @daviwil!

@TylerLeonhardt TylerLeonhardt merged commit f5057a1 into PowerShell:master Jan 3, 2018
@TylerLeonhardt TylerLeonhardt deleted the add-save branch January 3, 2018 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants