Introduce helper method to detect folder in workspace
This commit is contained in:
@@ -255,6 +255,15 @@ export function getOnDiskWorkspaceFolders() {
|
||||
return diskWorkspaceFolders;
|
||||
}
|
||||
|
||||
/** Check if folder is already present in workspace */
|
||||
export function isFolderAlreadyInWorkspace(folderName: string) {
|
||||
const workspaceFolders = workspace.workspaceFolders || [];
|
||||
|
||||
return !!workspaceFolders.find(
|
||||
(workspaceFolder) => workspaceFolder.name == folderName,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a utility method to invoke a function only if a minimum time interval has elapsed since
|
||||
* the last invocation of that function.
|
||||
|
||||
@@ -9,6 +9,8 @@ import {
|
||||
SecretStorageChangeEvent,
|
||||
Uri,
|
||||
window,
|
||||
workspace,
|
||||
WorkspaceFolder,
|
||||
} from "vscode";
|
||||
import { dump } from "js-yaml";
|
||||
import * as tmp from "tmp";
|
||||
@@ -19,6 +21,7 @@ import { DirResult } from "tmp";
|
||||
import {
|
||||
getInitialQueryContents,
|
||||
InvocationRateLimiter,
|
||||
isFolderAlreadyInWorkspace,
|
||||
isLikelyDatabaseRoot,
|
||||
isLikelyDbLanguageFolder,
|
||||
showBinaryChoiceDialog,
|
||||
@@ -533,3 +536,21 @@ describe("walkDirectory", () => {
|
||||
expect(files.sort()).toEqual([file1, file2, file3, file4, file5, file6]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isFolderAlreadyInWorkspace", () => {
|
||||
beforeEach(() => {
|
||||
const folders = [
|
||||
{ name: "/first/path" },
|
||||
{ name: "/second/path" },
|
||||
] as WorkspaceFolder[];
|
||||
|
||||
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue(folders);
|
||||
});
|
||||
it("should return true if the folder is already in the workspace", () => {
|
||||
expect(isFolderAlreadyInWorkspace("/first/path")).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if the folder is not in the workspace", () => {
|
||||
expect(isFolderAlreadyInWorkspace("/third/path")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user