-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathenable.ts
More file actions
141 lines (129 loc) · 4.47 KB
/
enable.ts
File metadata and controls
141 lines (129 loc) · 4.47 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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";
import * as os from "os";
import * as path from "path";
import { isMatch } from "picomatch";
export interface WorkspaceEnabledInfo {
folder: vscode.WorkspaceFolder;
enabled: boolean | undefined;
hasDenoConfig: boolean;
}
const PARENT_RELATIVE_REGEX = os.platform() === "win32"
? /\.\.(?:[/\\]|$)/
: /\.\.(?:\/|$)/;
/** Checks if `parent` is an ancestor of `child`. */
function pathStartsWith(child: string, parent: string) {
if (path.isAbsolute(child) !== path.isAbsolute(parent)) {
return false;
}
const relative = path.relative(parent, child);
return !relative.match(PARENT_RELATIVE_REGEX);
}
export function isPathEnabled(
extensionContext: DenoExtensionContext,
filePath: string,
) {
const enableSettings =
extensionContext.enableSettingsByFolder?.find(([workspace, _]) =>
pathStartsWith(filePath, workspace)
)?.[1] ?? extensionContext.enableSettingsUnscoped ??
{ enable: null, enablePaths: null, disablePaths: [] };
const scopesWithDenoJson = extensionContext.scopesWithDenoJson ?? new Set();
if (isMatch(filePath, enableSettings.disablePaths)) return false;
for (const path of enableSettings.disablePaths) {
if (pathStartsWith(filePath, path)) {
return false;
}
}
if (enableSettings.enablePaths) {
return isMatch(filePath, enableSettings.enablePaths) ||
enableSettings.enablePaths.some((path) => pathStartsWith(filePath, path));
}
if (enableSettings.enable != null) {
return enableSettings.enable;
}
return [...scopesWithDenoJson].some((scope) =>
pathStartsWith(filePath, scope)
);
}
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;
}