Skip to content

Commit 0198d8f

Browse files
check for different OS's and also check if the file is dirty before saving
1 parent 47ccd39 commit 0198d8f

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

src/features/ExtensionCommands.ts

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (C) Microsoft Corporation. All rights reserved.
33
*--------------------------------------------------------*/
44

5+
import os = require('os');
56
import path = require('path');
67
import vscode = require('vscode');
78
import { IFeature } from '../feature';
@@ -371,13 +372,7 @@ export class ExtensionCommandsFeature implements IFeature {
371372

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

374-
// Make sure the file path is absolute
375-
if (!path.win32.isAbsolute(filePath))
376-
{
377-
filePath = path.win32.resolve(
378-
vscode.workspace.rootPath,
379-
filePath);
380-
}
375+
filePath = this.normalizeFilePath(filePath);
381376

382377
var promise =
383378
vscode.workspace.openTextDocument(filePath)
@@ -391,18 +386,21 @@ export class ExtensionCommandsFeature implements IFeature {
391386

392387
var promise: Thenable<EditorOperationResponse>;
393388

394-
// Make sure the file path is absolute
395-
if (!path.win32.isAbsolute(filePath))
396-
{
397-
filePath = path.win32.resolve(
398-
vscode.workspace.rootPath,
399-
filePath);
400-
}
389+
var normalizedFilePath = this.normalizeFilePath(filePath);
401390

402-
// Normalize file path case for comparison
403-
var normalizedFilePath = filePath.toLowerCase();
391+
// since Windows is case-insensitive, we need to normalize it differently
392+
var canFind = vscode.workspace.textDocuments.find(doc => {
393+
var docPath;
394+
if (os.platform() == "win32") {
395+
// for windows paths, they are normalized to be lowercase
396+
docPath = doc.fileName.toLowerCase();
397+
} else {
398+
docPath = doc.fileName;
399+
}
400+
return docPath == normalizedFilePath;
401+
});
404402

405-
if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath))
403+
if (canFind)
406404
{
407405
promise =
408406
vscode.workspace.openTextDocument(filePath)
@@ -422,22 +420,29 @@ export class ExtensionCommandsFeature implements IFeature {
422420

423421
var promise: Thenable<EditorOperationResponse>;
424422

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-
}
423+
var normalizedFilePath = this.normalizeFilePath(filePath);
432424

433-
// Normalize file path case for comparison
434-
var normalizedFilePath = filePath.toLowerCase();
425+
// since Windows is case-insensitive, we need to normalize it differently
426+
var canFind = vscode.workspace.textDocuments.find(doc => {
427+
var docPath;
428+
if (os.platform() == "win32") {
429+
// for windows paths, they are normalized to be lowercase
430+
docPath = doc.fileName.toLowerCase();
431+
} else {
432+
docPath = doc.fileName;
433+
}
434+
return docPath == normalizedFilePath;
435+
});
435436

436-
if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath))
437+
if (canFind)
437438
{
438439
promise =
439440
vscode.workspace.openTextDocument(filePath)
440-
.then(doc => doc.save())
441+
.then(doc => {
442+
if (doc.isDirty) {
443+
doc.save();
444+
}
445+
})
441446
.then(_ => EditorOperationResponse.Completed);
442447
}
443448
else
@@ -448,6 +453,31 @@ export class ExtensionCommandsFeature implements IFeature {
448453
return promise;
449454
}
450455

456+
private normalizeFilePath(filePath: string): string {
457+
if (os.platform() == "win32") {
458+
// Make sure the file path is absolute
459+
if (!path.win32.isAbsolute(filePath))
460+
{
461+
filePath = path.win32.resolve(
462+
vscode.workspace.rootPath,
463+
filePath);
464+
}
465+
466+
// Normalize file path case for comparison for Windows
467+
return filePath.toLowerCase();
468+
} else {
469+
// Make sure the file path is absolute
470+
if (!path.isAbsolute(filePath))
471+
{
472+
filePath = path.resolve(
473+
vscode.workspace.rootPath,
474+
filePath);
475+
}
476+
477+
return filePath;
478+
}
479+
}
480+
451481
private setSelection(details: SetSelectionRequestArguments): EditorOperationResponse {
452482
vscode.window.activeTextEditor.selections = [
453483
new vscode.Selection(

0 commit comments

Comments
 (0)