-
Notifications
You must be signed in to change notification settings - Fork 212
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
feat: workspace codelens #1075
feat: workspace codelens #1075
Changes from 10 commits
ed12144
9e3732a
bb35285
00ecf05
140bbb5
5131e94
9680eed
78cd9c3
228cb56
4a0f06e
8d12d5f
4d6e53f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,11 @@ import { existsSync } from 'fs'; | |
import { dirname, join, parse } from 'path'; | ||
import { | ||
commands, | ||
ConfigurationChangeEvent, | ||
Disposable, | ||
ExtensionContext, | ||
FileSystemWatcher, | ||
languages, | ||
tasks, | ||
TreeView, | ||
Uri, | ||
|
@@ -33,7 +36,7 @@ import { | |
RunTargetTreeItem, | ||
RunTargetTreeProvider, | ||
} from '@nx-console/vscode/nx-run-target-view'; | ||
import { verifyNodeModules, verifyWorkspace } from '@nx-console/vscode/verify'; | ||
import { verifyNodeModules } from '@nx-console/vscode/verify'; | ||
import { | ||
NxCommandsTreeItem, | ||
NxCommandsTreeProvider, | ||
|
@@ -42,6 +45,10 @@ import { | |
NxProjectTreeItem, | ||
NxProjectTreeProvider, | ||
} from '@nx-console/vscode/nx-project-view'; | ||
import { | ||
verifyWorkspace, | ||
WorkspaceCodeLensProvider, | ||
} from '@nx-console/vscode/nx-workspace'; | ||
import { environment } from './environments/environment'; | ||
|
||
let runTargetTreeView: TreeView<RunTargetTreeItem>; | ||
|
@@ -99,7 +106,7 @@ export function activate(c: ExtensionContext) { | |
); | ||
|
||
const manuallySelectWorkspaceDefinitionCommand = commands.registerCommand( | ||
LOCATE_YOUR_WORKSPACE.command!.command, | ||
LOCATE_YOUR_WORKSPACE.command?.command || '', | ||
async () => { | ||
return manuallySelectWorkspaceDefinition(); | ||
} | ||
|
@@ -117,6 +124,9 @@ export function activate(c: ExtensionContext) { | |
manuallySelectWorkspaceDefinitionCommand | ||
); | ||
|
||
registerWorkspaceCodeLensProvider(context); | ||
watchWorkspaceCodeLensConfigChange(context); | ||
|
||
getTelemetry().extensionActivated((Date.now() - startTime) / 1000); | ||
} catch (e) { | ||
window.showErrorMessage( | ||
|
@@ -259,3 +269,37 @@ 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; | ||
} | ||
} | ||
}) | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! This looks like it works properly 😄 Do you think this logic should be handled in the workspace lib? Like have a main entry to that lib, and then do all this logic in there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. Can't keep throwing everything in main/activate. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './lib/find-workspace-json-target'; | ||
export * from './lib/reveal-workspace-json'; | ||
export * from './lib/workspace-codelens-provider'; | ||
export * from './lib/verify-workspace'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { CodeLens, CodeLensProvider, Command, Range } from 'vscode'; | ||
import { TextDocument } from 'vscode'; | ||
import { verifyWorkspace } from './verify-workspace'; | ||
import { getProjectLocations } from './find-workspace-json-target'; | ||
|
||
export class ProjectCodeLens extends CodeLens { | ||
constructor( | ||
range: Range, | ||
public workspaceType: 'nx'|'ng', | ||
public project: string, | ||
public target: string, | ||
public configuration?: string | ||
) { | ||
super(range); | ||
} | ||
} | ||
export class WorkspaceCodeLensProvider implements CodeLensProvider { | ||
|
||
/** | ||
* Provides a CodeLens set for a matched document | ||
* @param document a document matched by the pattern passed to registerCodeLensProvider | ||
* @returns ProjectCodeLens Range locations and properties for the document | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. took out async on this function because it was no longer returning a Promise |
||
provideCodeLenses(document: TextDocument): CodeLens[] | undefined { | ||
const lens: CodeLens[] = []; | ||
|
||
const projectLocations = getProjectLocations(document); | ||
const { validWorkspaceJson, workspaceType } = verifyWorkspace(); | ||
if (!validWorkspaceJson) { | ||
return; | ||
} | ||
|
||
for (const projectName in projectLocations) { | ||
const project = projectLocations[projectName]; | ||
for (const target in project) { | ||
const position = document.positionAt(project[target].position); | ||
|
||
lens.push( | ||
new ProjectCodeLens( | ||
new Range(position, position), | ||
workspaceType, | ||
projectName, | ||
target | ||
) | ||
); | ||
const configurations = project[target].configurations; | ||
if (configurations) { | ||
for (const configuration in configurations) { | ||
const configurationPosition = document.positionAt( | ||
configurations[configuration].position | ||
); | ||
|
||
lens.push( | ||
new ProjectCodeLens( | ||
new Range(configurationPosition, configurationPosition), | ||
workspaceType, | ||
projectName, | ||
target, | ||
configuration | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
return lens; | ||
} | ||
|
||
/** | ||
* Resolves and sets the command on visible CodeLens | ||
* @param lens lens to be resolve | ||
* @returns ProjectCodeLens with command | ||
*/ | ||
// https://github.com/microsoft/vscode-extension-samples/blob/main/codelens-sample/src/CodelensProvider.ts | ||
resolveCodeLens(lens: CodeLens): CodeLens | Promise<CodeLens> | null { | ||
if (lens instanceof ProjectCodeLens) { | ||
const command: Command = { | ||
command: 'nx.run', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't actually implement ng.run 😬 because I didn't realize at the time we'd also be able to use that with angular cli workspaces as well. I should add it and use it here as well though; won't be too much. Good point. |
||
title: lens.configuration | ||
? `${lens.workspaceType} run ${lens.project}:${lens.target}:${lens.configuration}` | ||
: `${lens.workspaceType} run ${lens.project}:${lens.target}`, | ||
arguments: [lens.project, lens.target, lens.configuration], | ||
}; | ||
lens.command = command; | ||
return lens; | ||
} | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved
verifyWorkspace
from from theverify
lib to thenx-workspace
lib to resolve a circular dependency between the two.