Skip to content
Open
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions src/service/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as fs from "fs-extra";
import * as path from "path";
import * as recursiveRead from "recursive-readdir";
import * as vscode from "vscode";
import { CustomConfig } from "../models/customConfig.model";

export class File {
Expand Down Expand Up @@ -95,6 +96,23 @@ export class FileService {
}
}

public static async CloseOpenFile(filePath: string): Promise<void> {
const matchingEditors = vscode.window.visibleTextEditors.filter(editor => {
return FileService.IsSamePath(editor.document.uri.fsPath, filePath);
});

for (const editor of matchingEditors) {
await vscode.window.showTextDocument(
editor.document,
editor.viewColumn,
false
);
await vscode.commands.executeCommand(
"workbench.action.closeActiveEditor"
);
}
}

public static async ListFiles(
directory: string,
customSettings: CustomConfig
Expand Down Expand Up @@ -237,4 +255,17 @@ export class FileService {
public static ConcatPath(...filePaths: string[]): string {
return filePaths.join(path.sep);
}

private static IsSamePath(firstPath: string, secondPath: string): boolean {
const first = FileService.NormalizePath(firstPath);
const second = FileService.NormalizePath(secondPath);
return first === second;
}

private static NormalizePath(filePath: string): string {
const normalizedPath = path.normalize(path.resolve(filePath));
return process.platform === "win32"
? normalizedPath.toLowerCase()
: normalizedPath;
}
}
2 changes: 2 additions & 0 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ export class Sync {
}
}

await FileService.CloseOpenFile(filePath);

actionList.push(
FileService.WriteFile(filePath, content)
.then(() => {
Expand Down
21 changes: 21 additions & 0 deletions test/service/fileService/fileService.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { expect } from "chai";
import * as fs from "fs-extra";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";

import { File, FileService } from "../../../src/service/file.service";

Expand Down Expand Up @@ -33,4 +37,21 @@ describe("FileService", () => {
);
expect(actual).to.be.equals("/User/path/to/hoge/piyo/hoge.txt");
});

it("should close an open editor for a file path", async () => {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "settings-sync-"));
const filePath = path.join(directory, "settings.json");
fs.writeFileSync(filePath, "{}");

const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);

await FileService.CloseOpenFile(filePath);

const openEditor = vscode.window.visibleTextEditors.find(editor => {
return editor.document.uri.fsPath === filePath;
});

expect(openEditor).to.be.equals(undefined);
});
});