Skip to content

Commit

Permalink
WorkspaceCodeLensProvider watches config and registers and disposes i…
Browse files Browse the repository at this point in the history
…tself
  • Loading branch information
Sandi Barr committed May 27, 2021
1 parent 4a0f06e commit 8d12d5f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 38 deletions.
38 changes: 2 additions & 36 deletions apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export function activate(c: ExtensionContext) {
manuallySelectWorkspaceDefinitionCommand
);

registerWorkspaceCodeLensProvider(context);
watchWorkspaceCodeLensConfigChange(context);
// registers itself as a CodeLensProvider and watches config to dispose/re-register
new WorkspaceCodeLensProvider(context);

getTelemetry().extensionActivated((Date.now() - startTime) / 1000);
} catch (e) {
Expand Down Expand Up @@ -269,37 +269,3 @@ function registerWorkspaceFileWatcher(

context.subscriptions.push(workspaceFileWatcher);
}

let codeLensProvider: Disposable | null;
function registerWorkspaceCodeLensProvider(context: ExtensionContext) {
if (GlobalConfigurationStore.instance.get('enableWorkspaceConfigCodeLens')) {
codeLensProvider = languages.registerCodeLensProvider(
{ pattern: '**/{workspace,angular}.json' },
new WorkspaceCodeLensProvider()
);
context.subscriptions.push(codeLensProvider);
}
}

function watchWorkspaceCodeLensConfigChange(context: ExtensionContext) {
context.subscriptions.push(
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
// if the `nxConsole` config changes, check enableWorkspaceConfigCodeLens and register or dispose
if (
event.affectsConfiguration(
GlobalConfigurationStore.configurationSection
)
) {
const enableWorkspaceConfigCodeLens = GlobalConfigurationStore.instance.get(
'enableWorkspaceConfigCodeLens'
);
if (enableWorkspaceConfigCodeLens && !codeLensProvider) {
registerWorkspaceCodeLensProvider(context);
} else if (!enableWorkspaceConfigCodeLens && codeLensProvider) {
codeLensProvider.dispose();
codeLensProvider = null;
}
}
})
);
}
72 changes: 70 additions & 2 deletions libs/vscode/nx-workspace/src/lib/workspace-codelens-provider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { CodeLens, CodeLensProvider, Command, Range } from 'vscode';
import {
CodeLens,
CodeLensProvider,
Command,
ConfigurationChangeEvent,
Disposable,
ExtensionContext,
languages,
Range,
workspace,
} from 'vscode';
import { TextDocument } from 'vscode';
import { verifyWorkspace } from './verify-workspace';
import { getProjectLocations } from './find-workspace-json-target';
import { GlobalConfigurationStore } from '@nx-console/vscode/configuration';

export class ProjectCodeLens extends CodeLens {
constructor(
range: Range,
public workspaceType: 'nx'|'ng',
public workspaceType: 'nx' | 'ng',
public project: string,
public target: string,
public configuration?: string
Expand All @@ -15,6 +26,20 @@ export class ProjectCodeLens extends CodeLens {
}
}
export class WorkspaceCodeLensProvider implements CodeLensProvider {
/**
* CodeLensProvider is disposed and re-registered on setting changes
*/
codeLensProvider: Disposable | null;

/**
* The WorkspaceCodeLensProvider adds clickable nx run targets in the workspace config file.
* It is enabled by default and can be disabled with the `enableWorkspaceConfigCodeLens` setting.
* @param context instance of ExtensionContext from activate
*/
constructor(private readonly context: ExtensionContext) {
this.registerWorkspaceCodeLensProvider(context);
this.watchWorkspaceCodeLensConfigChange(context);
}

/**
* Provides a CodeLens set for a matched document
Expand Down Expand Up @@ -86,4 +111,47 @@ export class WorkspaceCodeLensProvider implements CodeLensProvider {
}
return null;
}

/**
* Checks the enableWorkspaceConfigCodeLens setting and registers this as a CodeLensProvider.
* @param context instance of ExtensionContext from activate
*/
registerWorkspaceCodeLensProvider(context: ExtensionContext) {
const enableWorkspaceConfigCodeLens = GlobalConfigurationStore.instance.get(
'enableWorkspaceConfigCodeLens'
);
if (enableWorkspaceConfigCodeLens) {
this.codeLensProvider = languages.registerCodeLensProvider(
{ pattern: '**/{workspace,angular}.json' },
this
);
context.subscriptions.push(this.codeLensProvider);
}
}

/**
* Watches for settings/configuration changes and enables/disables the CodeLensProvider
* @param context instance of ExtensionContext from activate
*/
watchWorkspaceCodeLensConfigChange(context: ExtensionContext) {
context.subscriptions.push(
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
// if the `nxConsole` config changes, check enableWorkspaceConfigCodeLens and register or dispose
const affectsNxConsoleConfig = event.affectsConfiguration(
GlobalConfigurationStore.configurationSection
);
if (affectsNxConsoleConfig) {
const enableWorkspaceConfigCodeLens = GlobalConfigurationStore.instance.get(
'enableWorkspaceConfigCodeLens'
);
if (enableWorkspaceConfigCodeLens && !this.codeLensProvider) {
this.registerWorkspaceCodeLensProvider(this.context);
} else if (!enableWorkspaceConfigCodeLens && this.codeLensProvider) {
this.codeLensProvider.dispose();
this.codeLensProvider = null;
}
}
})
);
}
}

0 comments on commit 8d12d5f

Please sign in to comment.