Skip to content

Commit

Permalink
feat: "deno.disablePaths" setting (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Sep 14, 2023
1 parent a5dffc3 commit 74c8c2b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 25 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ extension has the following configuration options:
the extension will disable the built-in VSCode JavaScript and TypeScript
language services, and will use the Deno Language Server (`deno lsp`) instead.
_boolean, default `false`_
- `deno.enablePaths`: Controls if the Deno Language Server is enabled for only
- `deno.disablePaths`: Controls if the Deno Language Server is disabled for
specific paths of the workspace folder. Defaults to an empty list.
- `deno.enablePaths`: Controls if the Deno Language Server is enabled for only
specific paths of the workspace folder.
- `deno.path`: A path to the `deno` executable. If unset, the extension will use
the environment path to resolve the `deno` executable. If set, the extension
will use the supplied path. The path should include the executable name (e.g.
Expand Down
1 change: 1 addition & 0 deletions client/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

export const ENABLE = "enable";
export const DISABLE_PATHS = "disablePaths";
export const ENABLE_PATHS = "enablePaths";
export const ENABLEMENT_FLAG = "deno:lspReady";
export const EXTENSION_ID = "denoland.vscode-deno";
Expand Down
2 changes: 2 additions & 0 deletions client/src/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export async function getWorkspacesEnabledInfo() {

// check for specific paths being enabled
const enabledPaths = config.get<string[]>(ENABLE_PATHS);
// We convert `enablePaths: []` to `enablePaths: null` for now.
// See https://github.com/denoland/vscode_deno/issues/908.
if (enabledPaths && enabledPaths.length) {
return true;
}
Expand Down
37 changes: 24 additions & 13 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as commands from "./commands";
import {
DISABLE_PATHS,
ENABLE_PATHS,
ENABLEMENT_FLAG,
EXTENSION_NS,
Expand All @@ -11,7 +12,7 @@ import { DenoTextDocumentContentProvider, SCHEME } from "./content_provider";
import { DenoDebugConfigurationProvider } from "./debug_config_provider";
import { setupCheckConfig } from "./enable";
import * as semver from "semver";
import type { EnabledPaths } from "./shared_types";
import type { PathFilter } from "./shared_types";
import { DenoStatusBar } from "./status_bar";
import { activateTaskProvider } from "./tasks";
import { getTsApi } from "./ts_api";
Expand All @@ -38,6 +39,7 @@ const workspaceSettingsKeys: Array<keyof Settings> = [
"documentPreloadLimit",
"maxTsServerMemory",
"enable",
"disablePaths",
"enablePaths",
"importMap",
"inlayHints",
Expand All @@ -56,6 +58,7 @@ const workspaceSettingsKeys: Array<keyof Settings> = [
const resourceSettingsKeys: Array<keyof Settings> = [
"codeLens",
"enable",
"disablePaths",
"enablePaths",
];

Expand Down Expand Up @@ -96,8 +99,8 @@ function configToResourceSettings(
return resourceSettings;
}

function getEnabledPaths(): EnabledPaths[] {
const items = [] as EnabledPaths[];
function getPathFilters(): PathFilter[] {
const items = [] as PathFilter[];
if (!vscode.workspace.workspaceFolders) {
return items;
}
Expand All @@ -106,16 +109,23 @@ function getEnabledPaths(): EnabledPaths[] {
EXTENSION_NS,
workspaceFolder,
);
const enabledPaths = config.get<string[]>(ENABLE_PATHS);
if (!enabledPaths || !enabledPaths.length) {
const disabled_ = config.get<string[]>(DISABLE_PATHS) ?? [];
const disabled = disabled_.map((p) =>
vscode.Uri.joinPath(workspaceFolder.uri, p).fsPath
);
const enabled_ = config.get<string[]>(ENABLE_PATHS);
// We convert `enablePaths: []` to `enablePaths: null` for now.
// See https://github.com/denoland/vscode_deno/issues/908.
const enabled = enabled_?.length
? enabled_.map((p) => vscode.Uri.joinPath(workspaceFolder.uri, p).fsPath)
: null;
if (disabled.length === 0 && enabled == null) {
continue;
}
const paths = enabledPaths.map(
(folder) => vscode.Uri.joinPath(workspaceFolder.uri, folder).fsPath,
);
items.push({
workspace: workspaceFolder.uri.fsPath,
paths,
disabled,
enabled,
});
}
return items;
Expand Down Expand Up @@ -148,13 +158,14 @@ function handleConfigurationChange(event: vscode.ConfigurationChangeEvent) {
),
};
}
extensionContext.enabledPaths = getEnabledPaths();
extensionContext.pathFilters = getPathFilters();
extensionContext.tsApi.refresh();
extensionContext.statusBar.refresh(extensionContext);

// restart when certain config changes
if (
event.affectsConfiguration("deno.enable") ||
event.affectsConfiguration("deno.disablePaths") ||
event.affectsConfiguration("deno.enablePaths") ||
event.affectsConfiguration("deno.path") ||
event.affectsConfiguration("deno.maxTsServerMemory")
Expand All @@ -165,7 +176,7 @@ function handleConfigurationChange(event: vscode.ConfigurationChangeEvent) {
}

function handleChangeWorkspaceFolders() {
extensionContext.enabledPaths = getEnabledPaths();
extensionContext.pathFilters = getPathFilters();
extensionContext.tsApi.refresh();
}

Expand Down Expand Up @@ -300,13 +311,13 @@ export async function activate(

extensionContext.tsApi = getTsApi(() => ({
documents: extensionContext.documentSettings,
enabledPaths: extensionContext.enabledPaths,
pathFilters: extensionContext.pathFilters,
hasDenoConfig: extensionContext.hasDenoConfig,
workspace: extensionContext.workspaceSettings,
}));

extensionContext.documentSettings = {};
extensionContext.enabledPaths = getEnabledPaths();
extensionContext.pathFilters = getPathFilters();
extensionContext.workspaceSettings = getWorkspaceSettings();

// when we activate, it might have been because a document was opened that
Expand Down
10 changes: 7 additions & 3 deletions client/src/shared_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface Settings {
enable: boolean | null;
/** Controls if the extension should cache the active document's dependencies on save. */
cacheOnSave: boolean;
/** Paths in the workspace that should be Deno enabled. */
disablePaths: string[];
/** If set, indicates that only the paths in the workspace should be Deno
* enabled. */
enablePaths: string[];
Expand Down Expand Up @@ -87,16 +89,18 @@ export interface Settings {
unstable: boolean;
}

export interface EnabledPaths {
export interface PathFilter {
/** The file system path of the workspace folder that is partially enabled. */
workspace: string;
/** The file system paths that are Deno disabled. */
disabled: string[];
/** The file system paths that are Deno enabled. */
paths: string[];
enabled: string[] | null;
}

export interface PluginSettings {
documents: Record<string, DocumentSettings>;
enabledPaths: EnabledPaths[];
pathFilters: PathFilter[];
/** Whether or not there is a `deno.json{c,}` at the workspace root. */
hasDenoConfig: boolean;
workspace: Settings;
Expand Down
2 changes: 1 addition & 1 deletion client/src/status_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DenoStatusBar {
if (
((extensionContext.workspaceSettings.enable ??
extensionContext.hasDenoConfig) ||
extensionContext.enabledPaths.length !== 0) &&
extensionContext.pathFilters.length !== 0) &&
extensionContext.client &&
extensionContext.serverInfo
) {
Expand Down
4 changes: 2 additions & 2 deletions client/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
LanguageClientOptions,
} from "vscode-languageclient/node";
import type { DenoServerInfo } from "./server_info";
import type { DocumentSettings, EnabledPaths, Settings } from "./shared_types";
import type { DocumentSettings, PathFilter, Settings } from "./shared_types";
import type { DenoStatusBar } from "./status_bar";
import type * as vscode from "vscode";

Expand All @@ -29,7 +29,7 @@ export interface DenoExtensionContext {
clientOptions: LanguageClientOptions;
/** A record of filepaths and their document settings. */
documentSettings: Record<string, DocumentSettings>;
enabledPaths: EnabledPaths[];
pathFilters: PathFilter[];
serverInfo: DenoServerInfo | undefined;
/** The capabilities returned from the server. */
serverCapabilities:
Expand Down
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@
false
]
},
"deno.disablePaths": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"markdownDescription": "Disables the Deno Language Server for specific paths. This will leave the built in TypeScript/JavaScript language server enabled for those paths. Takes priority over `deno.enablePaths`.\n\n**Not recommended to be enabled in user settings.**",
"scope": "resource",
"examples": [
[
"./worker"
]
]
},
"deno.enablePaths": {
"type": "array",
"items": {
Expand Down
20 changes: 15 additions & 5 deletions typescript-deno-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const defaultSettings: Settings = {
cacheOnSave: false,
certificateStores: null,
enable: null,
disablePaths: [],
enablePaths: [],
codeLens: null,
config: null,
Expand Down Expand Up @@ -90,12 +91,21 @@ class Plugin implements ts.server.PluginModule {
fileName = fileName.replace(/\//g, "\\");
}
const settings = projectSettings.get(this.#projectName);
if (settings?.enabledPaths) {
const paths = settings.enabledPaths.find(({ workspace }) =>
if (settings?.pathFilters) {
const pathFilter = settings.pathFilters.find(({ workspace }) =>
pathStartsWith(fileName, workspace)
)?.paths;
if (paths && paths.length) {
return paths.some((path) => pathStartsWith(fileName, path));
);
if (pathFilter) {
for (const path of pathFilter.disabled) {
if (pathStartsWith(fileName, path)) {
return false;
}
}
if (pathFilter?.enabled) {
return pathFilter.enabled.some((path) =>
pathStartsWith(fileName, path)
);
}
}
}
// TODO(@kitsonk): rework all of this to be more like the workspace folders
Expand Down

0 comments on commit 74c8c2b

Please sign in to comment.