forked from denoland/vscode_deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable.ts
More file actions
97 lines (88 loc) · 3.04 KB
/
enable.ts
File metadata and controls
97 lines (88 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { ENABLE, ENABLE_PATHS, EXTENSION_NS } from "./constants";
import * as vscode from "vscode";
import { DenoExtensionContext, EnableSettings } from "./types";
export interface WorkspaceEnabledInfo {
folder: vscode.WorkspaceFolder;
enabled: boolean | undefined;
hasDenoConfig: boolean;
}
export async function getWorkspacesEnabledInfo() {
const result: WorkspaceEnabledInfo[] = [];
for (const folder of vscode.workspace.workspaceFolders ?? []) {
result.push({
folder,
enabled: await getIsWorkspaceEnabled(folder),
hasDenoConfig:
await exists(vscode.Uri.joinPath(folder.uri, "./deno.json")) ||
await exists(vscode.Uri.joinPath(folder.uri, "./deno.jsonc")),
});
}
return result;
function getIsWorkspaceEnabled(workspaceFolder: vscode.WorkspaceFolder) {
const config = vscode.workspace.getConfiguration(
EXTENSION_NS,
workspaceFolder,
);
const enable = config.inspect<boolean>(ENABLE);
// the workspace folder, workspace or user settings have been explicitly
// enabled/disabled so we should skip
if (
typeof enable?.workspaceFolderValue !== "undefined" ||
typeof enable?.workspaceValue !== "undefined" ||
typeof enable?.globalValue !== "undefined"
) {
return config.get<boolean>(ENABLE) ?? false;
}
// check for specific paths being enabled
if (config.get<string[]>(ENABLE_PATHS)) {
return true;
}
// no setting set, so undefined
return undefined;
}
}
export function refreshEnableSettings(extensionContext: DenoExtensionContext) {
function getEnableSettings(
config: vscode.WorkspaceConfiguration,
scope: vscode.Uri | null,
): EnableSettings {
const enable = config.get<boolean | null>("enable") ?? null;
let enablePaths = null;
let disablePaths: string[] = [];
if (scope) {
enablePaths = config.get<string[] | null>("enablePaths")?.map((p) =>
vscode.Uri.joinPath(scope, p).fsPath
) ?? null;
disablePaths = config.get<string[]>("disablePaths")?.map((p) =>
vscode.Uri.joinPath(scope, p).fsPath
) ?? [];
}
return { enable, enablePaths, disablePaths };
}
extensionContext.enableSettingsUnscoped = getEnableSettings(
vscode.workspace.getConfiguration(EXTENSION_NS),
vscode.workspace.workspaceFolders?.[0]?.uri ?? null,
);
extensionContext.enableSettingsByFolder = [];
const workspaceFolders = vscode.workspace.workspaceFolders ?? [];
for (const workspaceFolder of workspaceFolders) {
extensionContext.enableSettingsByFolder.push([
workspaceFolder.uri.fsPath,
getEnableSettings(
vscode.workspace.getConfiguration(EXTENSION_NS, workspaceFolder),
workspaceFolder.uri,
),
]);
}
extensionContext.enableSettingsByFolder.sort();
extensionContext.enableSettingsByFolder.reverse();
}
async function exists(uri: vscode.Uri): Promise<boolean> {
try {
await vscode.workspace.fs.stat(uri);
} catch {
return false;
}
return true;
}