Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enable disable command #197

Closed
wants to merge 11 commits into from
66 changes: 58 additions & 8 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "path";
import { promises as fs } from "fs";
import glob from "glob";

import {
workspace,
Expand Down Expand Up @@ -113,6 +114,9 @@ export class Extension {
},
executablePath: "",
};

private enable = false;

// get configuration of Deno
public getConfiguration(uri?: Uri): ConfigurationField {
const config: ConfigurationField = {};
Expand Down Expand Up @@ -324,14 +328,8 @@ export class Extension {
this.statusBar.hide();
return;
}

const uri = document.uri;
const enabled = workspace
.getConfiguration(this.configurationSection, uri)
.get("enable");

// if vscode-deno have been disable for workspace
if (!enabled) {
if (!this.enable) {
this.statusBar.hide();
return;
}
Expand Down Expand Up @@ -392,6 +390,34 @@ Executable ${this.denoInfo.executablePath}`;
this.client.sendNotification(Notification.diagnostic, uri.toString());
}
}

private isPartOfProject(
config: ConfigurationField,
entry_path: string,
workspace_path?: string
): boolean {
if (workspace_path === undefined) {
return false;
}

const includes =
config.include?.some((it) =>
glob
.sync(path.join(workspace_path, it))
.map((it) => path.normalize(it))
.includes(entry_path)
) ?? true;

const excludes = !config.exclude?.some((it) =>
glob
.sync(path.join(workspace_path, it))
.map((it) => path.normalize(it))
.includes(entry_path)
);

return includes && excludes;
}

private sync(document?: TextDocument) {
if (document) {
const relativeFilepath = workspace.asRelativePath(
Expand All @@ -404,12 +430,22 @@ Executable ${this.denoInfo.executablePath}`;
) {
const config = this.getConfiguration(document.uri);

this.enable =
!!config.enable &&
this.isPartOfProject(
config,
document.uri.fsPath,
workspace.getWorkspaceFolder(document.uri)?.uri?.fsPath
);

commands.executeCommand(
"setContext",
"denoExtensionActivated",
!!config.enable
this.enable
);

this.client?.sendNotification("_setEnable", this.enable);

this.tsAPI.configurePlugin(TYPESCRIPT_DENO_PLUGIN_ID, config);
this.updateDiagnostic(document.uri);
}
Expand Down Expand Up @@ -478,6 +514,20 @@ Executable ${this.denoInfo.executablePath}`;
await window.showInformationMessage(`Copied to clipboard.`);
});

this.registerCommand("_enable_vscode_deno", async () => {
this.enable = true;
const config = workspace.getConfiguration(this.configurationSection);
await config.update("enable", true);
this.sync(window.activeTextEditor?.document);
});

this.registerCommand("_disable_vscode_deno", async () => {
this.enable = false;
const config = workspace.getConfiguration(this.configurationSection);
await config.update("enable", false);
this.sync(window.activeTextEditor?.document);
});

this.registerQuickFix({
_fetch_remote_module: async (editor, text) => {
const config = this.getConfiguration(editor.document.uri);
Expand Down
19 changes: 5 additions & 14 deletions core/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ test("core / configuration / resolveFromVscode if it is a valid file", async ()
configManager.resolveFromVscode(vscodeDir);

expect(configManager.config).toEqual({
...Configuration.defaultConfiguration,
enable: true,
unstable: true,
import_map: "./import_map.json",
lint: false,
} as ConfigurationField);
});

Expand All @@ -27,10 +27,8 @@ test("core / configuration / resolveFromVscode if valid section", async () => {
configManager.resolveFromVscode(vscodeDir);

expect(configManager.config).toEqual({
...Configuration.defaultConfiguration,
enable: true,
unstable: false,
import_map: null,
lint: false,
} as ConfigurationField);
});

Expand All @@ -49,12 +47,7 @@ test("core / configuration / resolveFromVscode if config file is empty", async (

configManager.resolveFromVscode(vscodeDir);

expect(configManager.config).toEqual({
enable: false,
unstable: false,
import_map: null,
lint: false,
} as ConfigurationField);
expect(configManager.config).toEqual(Configuration.defaultConfiguration);
});

test("core / configuration / resolveFromVscode if field is invalid", async () => {
Expand All @@ -65,10 +58,10 @@ test("core / configuration / resolveFromVscode if field is invalid", async () =>
configManager.resolveFromVscode(vscodeDir);

expect(configManager.config).toEqual({
...Configuration.defaultConfiguration,
enable: true,
unstable: true,
import_map: "1,2,3",
lint: false,
} as ConfigurationField);
});

Expand All @@ -88,9 +81,7 @@ test("core / configuration / update", async () => {
expect(hasTriggerListener).toBe(true);

expect(configManager.config).toEqual({
...Configuration.defaultConfiguration,
enable: true,
unstable: false,
import_map: null,
lint: false,
} as ConfigurationField);
});
6 changes: 6 additions & 0 deletions core/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ export const DenoPluginConfigurationField: (keyof ConfigurationField)[] = [
"unstable",
"import_map",
"lint",
"include",
"exclude",
];

export type ConfigurationField = {
enable?: boolean;
unstable?: boolean;
import_map?: string | null;
lint?: boolean;
include?: string[];
exclude?: string[];
};

interface ConfigurationInterface {
Expand All @@ -34,6 +38,8 @@ export class Configuration implements ConfigurationInterface {
unstable: false,
import_map: null,
lint: false,
include: ["**/*.ts", "**/*.js", "**/*.tsx", "**/*.jsx", "**/*.mjs"],
exclude: [],
};

private readonly _configUpdatedListeners = new Set<() => void>();
Expand Down
54 changes: 53 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
"command": "deno._clear_import_enhencement_cache",
"title": "Clear import enhencement cache",
"category": "deno"
},
{
"command": "deno._enable_vscode_deno",
"title": "Enable VSCode-Deno",
"category": "deno"
},
{
"command": "deno._disable_vscode_deno",
"title": "Disable VSCode-Deno",
"category": "deno"
}
],
"views": {
Expand Down Expand Up @@ -86,6 +96,14 @@
{
"command": "deno.restart_server",
"when": "denoExtensionActivated"
},
{
"command": "deno._enable_vscode_deno",
"when": "!denoExtensionActivated"
},
{
"command": "deno._disable_vscode_deno",
"when": "denoExtensionActivated"
}
]
},
Expand Down Expand Up @@ -132,6 +150,38 @@
true,
false
]
},
"deno.include": {
"type": "array",
"default": [
"**/*.ts",
"**/*.js",
"**/*.tsx",
"**/*.jsx",
"**/*.mjs"
],
"markdownDescription": "Controls which file/folder will inlucde in the project.",
"scope": "resource",
"examples": [
[
"**/*.ts",
"**/*.js",
"**/*.tsx",
"**/*.jsx",
"**/*.mjs"
]
]
},
"deno.exclude": {
"type": "array",
"default": [],
"markdownDescription": "Controls which file/folder will be exclude from the project.",
"scope": "resource",
"examples": [
[
"**/*.d.ts"
]
]
}
}
},
Expand Down Expand Up @@ -184,11 +234,12 @@
},
"devDependencies": {
"@types/deep-equal": "1.0.1",
"@types/glob": "^7.1.3",
"@types/jest": "26.0.10",
"@types/json5": "0.0.30",
"@types/node": "14.6.2",
"@types/which": "1.3.2",
"@types/semver": "7.3.3",
"@types/which": "1.3.2",
"@typescript-eslint/eslint-plugin": "3.10.1",
"@typescript-eslint/parser": "3.10.1",
"coveralls": "3.1.0",
Expand All @@ -204,6 +255,7 @@
"dependencies": {
"deep-equal": "2.0.3",
"deepmerge": "^4.2.2",
"glob": "^7.1.6",
"got": "^11.5.2",
"json5": "^2.1.3",
"semver": "7.3.2",
Expand Down
16 changes: 14 additions & 2 deletions server/src/dependency_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,33 @@ import { ImportMap } from "../../core/import_map";
import { isHttpURL } from "../../core/util";
import { Request } from "../../core/const";

import Plugable from "./plugable";

interface URLDep {
filepath: string;
location: { start: Position; end: Position };
}

type DependencyTreeMap = { [key: string]: URLDep[] };

export class DependencyTree {
constructor(connection: IConnection, private bridge: Bridge) {
export class DependencyTree implements Plugable {
setEnabled(enabled: boolean): void {
this.enabled = enabled;
}

constructor(
private enabled: boolean,
connection: IConnection,
private bridge: Bridge
) {
this.enabled = enabled;
connection.onRequest(
Request.analysisDependency,
this.getDependencyTreeOfProject.bind(this)
);
}
async getDependencyTreeOfProject(uriStr: string): Promise<DependencyTreeMap> {
if (!this.enabled) return {};
const folderUir = URI.parse(uriStr);
const folder = folderUir.fsPath;

Expand Down
15 changes: 13 additions & 2 deletions server/src/language/code_lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ import { URI } from "vscode-uri";
import { CacheModule } from "../../../core/deno_cache";
import { isInDeno } from "../../../core/deno";

export class CodeLens {
constructor(connection: IConnection, documents: TextDocuments<TextDocument>) {
import Plugable from "../plugable";

export class CodeLens implements Plugable {
setEnabled(enabled: boolean): void {
this.enabled = enabled;
}

constructor(
private enabled: boolean,
connection: IConnection,
documents: TextDocuments<TextDocument>
) {
connection.onCodeLens((params) => {
if (!this.enabled) return;
const { textDocument } = params;

const document = documents.get(textDocument.uri);
Expand Down
10 changes: 9 additions & 1 deletion server/src/language/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { Cache } from "../../../core/cache";

import { ImportCompletionEnhanced } from "./import_completion_enhanced";

import Plugable from "../plugable";

// Cache for 30 second or 30 references
const cache = Cache.create<Deps[]>(1000 * 30, 30);

Expand All @@ -25,13 +27,19 @@ getAllDenoCachedDeps()
// do nothing
});

export class Completion {
export class Completion implements Plugable {
setEnabled(enabled: boolean): void {
this.enabled = enabled;
}

constructor(
private enabled: boolean,
connection: IConnection,
documents: TextDocuments<TextDocument>,
import_enhanced: ImportCompletionEnhanced
) {
connection.onCompletion(async (params) => {
if (this.enabled) return;
const { position, partialResultToken, textDocument } = params;

const doc = documents.get(textDocument.uri);
Expand Down
Loading