Skip to content

Commit

Permalink
feat: unstable setting as list (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Sep 10, 2024
1 parent 2c5964d commit 155255f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 106 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,13 @@ extension has the following configuration options:
To see which versions of this extension can be used with each version of the
Deno CLI, consult the following table.

| Deno CLI | vscode-deno |
| -------------- | --------------- |
| 1.37.2 onwards | 3.34.0 onwards |
| 1.37.1 | 3.32.0 - 3.33.3 |
| 1.37.0 | 3.28.0 - 3.31.0 |
| ? - 1.36.4 | 3.27.0 |
| Deno CLI | vscode-deno |
| --------------- | --------------- |
| 1.40.0 onwards | TODO onwards |
| 1.37.2 - 1.39.4 | 3.34.0 - 3.39.0 |
| 1.37.1 | 3.32.0 - 3.33.3 |
| 1.37.0 | 3.28.0 - 3.31.0 |
| ? - 1.36.4 | 3.27.0 |

Version ranges are inclusive. Incompatibilites prior to 3.27.0 were not tracked.

Expand Down
107 changes: 61 additions & 46 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import * as semver from "semver";
import * as vscode from "vscode";
import { LanguageClient, ServerOptions } from "vscode-languageclient/node";
import type { Location, Position } from "vscode-languageclient/node";
import { getWorkspacesEnabledInfo, setupCheckConfig } from "./enable";
import { getWorkspacesEnabledInfo } from "./enable";
import { denoUpgradePromptAndExecute } from "./upgrade";
import { join } from "path";
import { readFileSync } from "fs";
Expand Down Expand Up @@ -171,6 +171,20 @@ export function startLanguageServer(
serverOptions,
{
outputChannel: extensionContext.outputChannel,
middleware: {
workspace: {
configuration: (params, token, next) => {
const response = next(params, token) as Record<string, unknown>[];
for (let i = 0; i < response.length; i++) {
const item = params.items[i];
if (item.section == "deno") {
transformDenoConfiguration(extensionContext, response[i]);
}
}
return response;
},
},
},
...extensionContext.clientOptions,
},
);
Expand Down Expand Up @@ -214,51 +228,34 @@ export function startLanguageServer(
),
);

// TODO(nayeemrmn): LSP version < 1.40.0 don't support the required API for
// "deno/didChangeDenoConfiguration". Remove this eventually.
if (semver.lt(extensionContext.serverInfo.version, "1.40.0")) {
extensionContext.scopesWithDenoJson = new Set();
extensionContext.clientSubscriptions.push(
extensionContext.client.onNotification(
"deno/didChangeDenoConfiguration",
() => {
extensionContext.tasksSidebar.refresh();
},
),
);
extensionContext.clientSubscriptions.push(
await setupCheckConfig(extensionContext),
);
} else {
const scopesWithDenoJson = new Set<string>();
extensionContext.scopesWithDenoJson = scopesWithDenoJson;
extensionContext.clientSubscriptions.push(
extensionContext.client.onNotification(
"deno/didChangeDenoConfiguration",
({ changes }: DidChangeDenoConfigurationParams) => {
let changedScopes = false;
for (const change of changes) {
if (change.configurationType != "denoJson") {
continue;
}
if (change.type == "added") {
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
scopesWithDenoJson.add(scopePath);
changedScopes = true;
} else if (change.type == "removed") {
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
scopesWithDenoJson.delete(scopePath);
changedScopes = true;
}
const scopesWithDenoJson = new Set<string>();
extensionContext.scopesWithDenoJson = scopesWithDenoJson;
extensionContext.clientSubscriptions.push(
extensionContext.client.onNotification(
"deno/didChangeDenoConfiguration",
({ changes }: DidChangeDenoConfigurationParams) => {
let changedScopes = false;
for (const change of changes) {
if (change.configurationType != "denoJson") {
continue;
}
if (changedScopes) {
extensionContext.tsApi?.refresh();
if (change.type == "added") {
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
scopesWithDenoJson.add(scopePath);
changedScopes = true;
} else if (change.type == "removed") {
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
scopesWithDenoJson.delete(scopePath);
changedScopes = true;
}
extensionContext.tasksSidebar.refresh();
},
),
);
}
}
if (changedScopes) {
extensionContext.tsApi?.refresh();
}
extensionContext.tasksSidebar.refresh();
},
),
);

extensionContext.tsApi.refresh();

Expand Down Expand Up @@ -308,6 +305,20 @@ function notifyServerSemver(serverVersion: string) {
);
}

/** Mutates the `config` parameter. For compatibility currently. */
export function transformDenoConfiguration(
extensionContext: DenoExtensionContext,
config: Record<string, unknown>,
) {
// TODO(nayeemrmn): Deno > 2.0.0-rc.1 expects `deno.unstable` as
// an array of features. Remove this eventually.
if (
semver.lte(extensionContext.serverInfo?.version || "1.0.0", "2.0.0-rc.1")
) {
config.unstable = !!config.unstable;
}
}

function showWelcomePageIfFirstUse(
context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
Expand Down Expand Up @@ -363,8 +374,12 @@ export function test(
const testArgs: string[] = [
...(config.get<string[]>("codeLens.testArgs") ?? []),
];
if (config.get("unstable")) {
testArgs.push("--unstable");
const unstable = config.get("unstable") as string[] ?? [];
for (const unstableFeature of unstable) {
const flag = `--unstable-${unstableFeature}`;
if (!testArgs.includes(flag)) {
testArgs.push(flag);
}
}
if (options?.inspect) {
testArgs.push(getInspectArg(extensionContext.serverInfo?.version));
Expand Down
43 changes: 0 additions & 43 deletions client/src/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,49 +87,6 @@ export function refreshEnableSettings(extensionContext: DenoExtensionContext) {
extensionContext.enableSettingsByFolder.reverse();
}

/** Check the current workspace */
export async function setupCheckConfig(
extensionContext: DenoExtensionContext,
): Promise<vscode.Disposable> {
async function updateHasDenoConfig() {
const uri = vscode.workspace.workspaceFolders?.[0]?.uri;
if (!uri) {
return;
}
extensionContext.scopesWithDenoJson = new Set();
if (
await exists(vscode.Uri.joinPath(uri, "./deno.json")) ||
await exists(vscode.Uri.joinPath(uri, "./deno.jsonc"))
) {
extensionContext.scopesWithDenoJson.add(uri.fsPath);
}
extensionContext.tsApi?.refresh();
}

await updateHasDenoConfig();

const subscriptions: vscode.Disposable[] = [];
// create a file watcher, so if a config file is added to the workspace we
// will check enablement
const configFileWatcher = vscode.workspace.createFileSystemWatcher(
"**/deno.{json,jsonc}",
false,
true,
false,
);
subscriptions.push(configFileWatcher);
subscriptions.push(configFileWatcher.onDidCreate(updateHasDenoConfig));
subscriptions.push(configFileWatcher.onDidDelete(updateHasDenoConfig));

return {
dispose() {
for (const disposable of subscriptions) {
disposable.dispose();
}
},
};
}

async function exists(uri: vscode.Uri): Promise<boolean> {
try {
await vscode.workspace.fs.stat(uri);
Expand Down
6 changes: 5 additions & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ export async function activate(
},
diagnosticCollectionName: "deno",
initializationOptions: () => {
const denoConfiguration = vscode.workspace.getConfiguration().get(
EXTENSION_NS,
) as Record<string, unknown>;
commands.transformDenoConfiguration(extensionContext, denoConfiguration);
return {
...vscode.workspace.getConfiguration().get(EXTENSION_NS),
...denoConfiguration,
javascript: vscode.workspace.getConfiguration().get("javascript"),
typescript: vscode.workspace.getConfiguration().get("typescript"),
enableBuiltinCommands: true,
Expand Down
5 changes: 3 additions & 2 deletions client/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export async function denoUpgradePromptAndExecute(
return;
}
const args = ["upgrade"];
if (config.get("unstable")) {
args.push("--unstable");
const unstable = config.get("unstable") as string[] ?? [];
for (const unstableFeature of unstable) {
args.push(`--unstable-${unstableFeature}`);
}
if (isCanary) {
args.push("--canary");
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,13 @@
"scope": "window"
},
"deno.unstable": {
"type": "boolean",
"default": false,
"markdownDescription": "Controls if tests will be run with the the `--unstable` flag when running tests via the explorer.\n\n**Not recommended to be enabled globally.**",
"scope": "resource",
"examples": [
true,
false
]
"type": "array",
"items": {
"type": "string"
},
"default": [],
"markdownDescription": "Controls which `--unstable-*` features tests will be run with when running them via the explorer.\n\n**Not recommended to be enabled globally.**",
"scope": "resource"
},
"deno.lint": {
"type": "boolean",
Expand Down

0 comments on commit 155255f

Please sign in to comment.