Skip to content

Commit

Permalink
feat: options to turn codelenses on/off via setting/command/button
Browse files Browse the repository at this point in the history
  • Loading branch information
goerlibe authored and lukasrothenberger committed Nov 14, 2023
1 parent 73e82d2 commit 4ab9643
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 15 deletions.
20 changes: 18 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,22 @@
"command": "discopop.loadHotspotResults",
"icon": "$(history)"
},
{
"title": "Toggle CodeLens Setting",
"command": "discopop.toggleCodeLens",
"category": "DiscoPoP",
"icon": "$(eye)"
},
{
"title": "Enable Recommendations CodeLens",
"command": "discopop.enableCodeLens",
"category": "DiscoPoP",
"enablement": "false",
"icon": "$(eye)"
},
{
"title": "Disable Recommendations CodeLens",
"command": "discopop.disableCodeLens",
"category": "DiscoPoP",
"enablement": "false",
"icon": "$(eye-closed)"
},
{
Expand Down Expand Up @@ -286,6 +290,18 @@
"group": "navigation"
}
],
"editor/title": [
{
"command": "discopop.enableCodeLens",
"group": "navigation",
"when": "discopop.codeLensEnabled == disabled"
},
{
"command": "discopop.disableCodeLens",
"group": "navigation",
"when": "discopop.codeLensEnabled === enabled"
}
],
"view/item/context": [
{
"command": "discopop.applySingleSuggestion",
Expand Down
57 changes: 56 additions & 1 deletion src/DiscoPoP/DiscoPoPCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export class DiscoPoPCodeLens extends vscode.CodeLens {
export class DiscoPoPCodeLensProvider
implements vscode.CodeLensProvider<DiscoPoPCodeLens>
{
public hidden: boolean = false // TODO hide suggestions if disabled in settings, or if disabled in this editor
private codeLensProviderDisposable: vscode.Disposable = undefined

public hidden: boolean = false
private suggestionsByFileId: Map<number, Suggestion[]>

// hide codelenses while we wait for the lineMapping and appliedStatus to be updated
Expand Down Expand Up @@ -71,6 +73,11 @@ export class DiscoPoPCodeLensProvider

// update lenses when settings change (codeLenses visibility might have changed)
vscode.workspace.onDidChangeConfiguration((_) => {
if (Config.codeLensEnabled()) {
this._register()
} else {
this.dispose()
}
this._onDidChangeCodeLenses.fire()
})

Expand All @@ -83,6 +90,30 @@ export class DiscoPoPCodeLensProvider
this.appliedStatus.onDidChange(() => {
this.stopWaitingForAppliedStatus()
})

// immediately register
this._register()
}

private _register() {
this.codeLensProviderDisposable?.dispose()
this.codeLensProviderDisposable =
vscode.languages.registerCodeLensProvider(
// TODO only apply this provider for files listed in the fileMapping (or even better: only for files that have suggestions)
{ scheme: 'file', language: 'cpp' },
this
)
this.show()
}

public dispose() {
vscode.commands.executeCommand(
'setContext',
'discopop.codeLensEnabled',
'undefined'
)
this.codeLensProviderDisposable?.dispose()
this.codeLensProviderDisposable = undefined
}

/**
Expand All @@ -94,6 +125,10 @@ export class DiscoPoPCodeLensProvider
this.waitForAppliedStatus = waitForAppliedStatus
this._onDidChangeCodeLenses.fire()

if (this.hidden) {
return
}

// indicate to the user that we are recomputing the codelenses
vscode.window.withProgress(
{
Expand Down Expand Up @@ -139,6 +174,26 @@ export class DiscoPoPCodeLensProvider
}
}

public hide() {
vscode.commands.executeCommand(
'setContext',
'discopop.codeLensEnabled',
'disabled'
)
this.hidden = true
this._onDidChangeCodeLenses.fire()
}

public show() {
vscode.commands.executeCommand(
'setContext',
'discopop.codeLensEnabled',
'enabled'
)
this.hidden = false
this._onDidChangeCodeLenses.fire()
}

public provideCodeLenses(
document: vscode.TextDocument,
_token: vscode.CancellationToken
Expand Down
49 changes: 41 additions & 8 deletions src/DiscoPoPExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export class DiscoPoPExtension {
private hotspotTreeDisposable: vscode.Disposable | undefined = undefined

private suggestionTreeDisposable: vscode.Disposable | undefined = undefined
private codeLensProviderDisposable: vscode.Disposable | undefined =
undefined

public constructor(private context: vscode.ExtensionContext) {
this.projectManager = new ProjectManager(context)
Expand All @@ -73,19 +71,14 @@ export class DiscoPoPExtension {

// enable code lenses for all suggestions
// TODO we should not create a new code lens provider every time,
this.codeLensProvider?.dispose()
this.codeLensProvider = new DiscoPoPCodeLensProvider(
this.dpResults.fileMapping,
this.dpResults.lineMapping,
this.dpResults.appliedStatus,
fullConfig.getDiscoPoPBuildDirectory() + '/.discopop',
Array.from(this.dpResults.suggestionsByType.values()).flat()
)
await this.codeLensProviderDisposable?.dispose()
this.codeLensProviderDisposable =
vscode.languages.registerCodeLensProvider(
{ scheme: 'file', language: 'cpp' }, // TODO only apply this provider for files listed in the fileMapping
this.codeLensProvider
)
}

public async showHotspotDetectionResults(
Expand All @@ -111,6 +104,12 @@ export class DiscoPoPExtension {
}

public activate() {
vscode.commands.executeCommand(
'setContext',
'discopop.codeLensEnabled',
'undefined'
)

this.context.subscriptions.push(
vscode.commands.registerCommand(
Commands.runDiscoPoPAndHotspotDetection,
Expand Down Expand Up @@ -394,6 +393,7 @@ export class DiscoPoPExtension {
)
)

// used by tree view
this.context.subscriptions.push(
vscode.commands.registerCommand(
Commands.applySingleSuggestion,
Expand All @@ -408,6 +408,7 @@ export class DiscoPoPExtension {
)
)

// used by tree view
this.context.subscriptions.push(
vscode.commands.registerCommand(
Commands.rollbackSingleSuggestion,
Expand All @@ -426,6 +427,38 @@ export class DiscoPoPExtension {
)
)

this.context.subscriptions.push(
vscode.commands.registerCommand(
Commands.toggleCodeLens,
async () => {
const currentValue = vscode.workspace
.getConfiguration('discopop')
.get('recommendationsCodeLens', true)
vscode.workspace
.getConfiguration('discopop')
.update('recommendationsCodeLens', !currentValue, true)
}
)
)

this.context.subscriptions.push(
vscode.commands.registerCommand(
Commands.enableCodeLens,
async () => {
this.codeLensProvider?.show()
}
)
)

this.context.subscriptions.push(
vscode.commands.registerCommand(
Commands.disableCodeLens,
async () => {
this.codeLensProvider?.hide()
}
)
)

// to allow undoing all suggestions, we need to get a hold on the .discopop directory
// once we refactor to have more state in the extension, this is simple:
// this.context.subscriptions.push(
Expand Down
8 changes: 4 additions & 4 deletions src/Utils/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class Commands {
public static rollbackAllSuggestions: string =
'discopop.rollbackAllSuggestions'

// enable/disable/change settings
// TODO
// public static enableCodeLens: string = 'discopop.enableCodeLens'
//public static disableCodeLens: string = 'discopop.disableCodeLens'
// codeLens
public static toggleCodeLens: string = 'discopop.toggleCodeLens' // global setting
public static enableCodeLens: string = 'discopop.enableCodeLens' // temporarily enable codeLens
public static disableCodeLens: string = 'discopop.disableCodeLens' // temporarily disable codeLens
}

0 comments on commit 4ab9643

Please sign in to comment.