Skip to content

Commit 612c90a

Browse files
authored
Add custom plot commands to the command pallete (#3629)
1 parent 9998978 commit 612c90a

File tree

9 files changed

+231
-207
lines changed

9 files changed

+231
-207
lines changed

extension/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,16 @@
564564
"category": "DVC",
565565
"icon": "$(refresh)"
566566
},
567+
{
568+
"title": "Add Custom Plot",
569+
"command": "dvc.views.plots.addCustomPlot",
570+
"category": "DVC"
571+
},
572+
{
573+
"title": "Remove Custom Plot(s)",
574+
"command": "dvc.views.plots.removeCustomPlots",
575+
"category": "DVC"
576+
},
567577
{
568578
"title": "Reset Persisted State and Reload Window",
569579
"command": "dvc.resetState",
@@ -947,6 +957,14 @@
947957
{
948958
"command": "dvc.views.plotsPathsTree.refreshPlots",
949959
"when": "dvc.commands.available && dvc.project.available"
960+
},
961+
{
962+
"command": "dvc.views.plots.addCustomPlot",
963+
"when": "dvc.commands.available && dvc.project.available"
964+
},
965+
{
966+
"command": "dvc.views.plots.removeCustomPlots",
967+
"when": "dvc.commands.available && dvc.project.available"
950968
}
951969
],
952970
"scm/title": [

extension/src/commands/external.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export enum RegisteredCommands {
7171
PLOTS_SHOW = 'dvc.showPlots',
7272
PLOTS_SELECT = 'dvc.views.plotsPathsTree.selectPlots',
7373
PLOTS_REFRESH = 'dvc.views.plotsPathsTree.refreshPlots',
74+
PLOTS_CUSTOM_ADD = 'dvc.views.plots.addCustomPlot',
75+
PLOTS_CUSTOM_REMOVE = 'dvc.views.plots.removeCustomPlots',
7476

7577
EXPERIMENT_AND_PLOTS_SHOW = 'dvc.showExperimentsAndPlots',
7678

extension/src/plots/commands/register.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ export const registerPlotsCommands = (
2626
RegisteredCommands.PLOTS_REFRESH,
2727
(context: Context) => plots.refresh(getDvcRootFromContext(context))
2828
)
29+
30+
internalCommands.registerExternalCommand(
31+
RegisteredCommands.PLOTS_CUSTOM_ADD,
32+
(context: Context) => plots.addCustomPlot(getDvcRootFromContext(context))
33+
)
34+
35+
internalCommands.registerExternalCommand(
36+
RegisteredCommands.PLOTS_CUSTOM_REMOVE,
37+
(context: Context) =>
38+
plots.removeCustomPlots(getDvcRootFromContext(context))
39+
)
2940
}

extension/src/plots/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ErrorsModel } from './errors/model'
77
import { PlotsModel } from './model'
88
import { collectEncodingElements, collectScale } from './paths/collect'
99
import { PathsModel } from './paths/model'
10+
import { pickCustomPlots, pickMetricAndParam } from './model/quickPick'
1011
import { BaseWebview } from '../webview'
1112
import { ViewKey } from '../webview/constants'
1213
import { BaseRepository } from '../webview/repository'
@@ -19,6 +20,7 @@ import { Toast } from '../vscode/toast'
1920
import { pickPaths } from '../path/selection/quickPick'
2021
import { ErrorDecorationProvider } from '../tree/decorationProvider/error'
2122
import { DecoratableTreeItemScheme } from '../tree'
23+
import { Title } from '../vscode/title'
2224

2325
export type PlotsWebview = BaseWebview<TPlotsData>
2426

@@ -29,6 +31,7 @@ export class Plots extends BaseRepository<TPlotsData> {
2931

3032
private readonly pathsChanged = this.dispose.track(new EventEmitter<void>())
3133

34+
private readonly experiments: Experiments
3235
private readonly plots: PlotsModel
3336
private readonly paths: PathsModel
3437
private readonly data: PlotsData
@@ -56,6 +59,7 @@ export class Plots extends BaseRepository<TPlotsData> {
5659
this.paths = this.dispose.track(
5760
new PathsModel(this.dvcRoot, this.errors, workspaceState)
5861
)
62+
this.experiments = experiments
5963

6064
this.webviewMessages = this.createWebviewMessageHandler(
6165
this.paths,
@@ -112,6 +116,37 @@ export class Plots extends BaseRepository<TPlotsData> {
112116
this.triggerDataUpdate()
113117
}
114118

119+
public async addCustomPlot() {
120+
const metricAndParam = await pickMetricAndParam(
121+
this.experiments.getColumnTerminalNodes(),
122+
this.plots.getCustomPlotsOrder()
123+
)
124+
125+
if (!metricAndParam) {
126+
return
127+
}
128+
129+
this.plots.addCustomPlot(metricAndParam)
130+
void this.sendPlots()
131+
}
132+
133+
public async removeCustomPlot() {
134+
const selectedPlotsIds = await pickCustomPlots(
135+
this.plots.getCustomPlotsOrder(),
136+
'There are no plots to remove.',
137+
{
138+
title: Title.SELECT_CUSTOM_PLOTS_TO_REMOVE
139+
}
140+
)
141+
142+
if (!selectedPlotsIds) {
143+
return
144+
}
145+
146+
this.plots.removeCustomPlots(selectedPlotsIds)
147+
void this.sendPlots()
148+
}
149+
115150
public getChildPaths(path: string | undefined) {
116151
const multiSourceEncoding = this.plots.getMultiSourceData()
117152

extension/src/plots/webview/messages.ts

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { commands } from 'vscode'
12
import isEmpty from 'lodash.isempty'
23
import {
34
ComparisonPlot,
@@ -21,12 +22,11 @@ import {
2122
import { PlotsModel } from '../model'
2223
import { PathsModel } from '../paths/model'
2324
import { BaseWebview } from '../../webview'
24-
import { pickCustomPlots, pickMetricAndParam } from '../model/quickPick'
2525
import { getModifiedTime, openImageFileInEditor } from '../../fileSystem'
26-
import { Title } from '../../vscode/title'
2726
import { reorderObjectList } from '../../util/array'
2827
import { CustomPlotsOrderValue } from '../model/custom'
2928
import { getCustomPlotId } from '../model/collect'
29+
import { RegisteredCommands } from '../../commands/external'
3030
import { ErrorsModel } from '../errors/model'
3131

3232
export class WebviewMessages {
@@ -74,7 +74,7 @@ export class WebviewMessages {
7474
public handleMessageFromWebview(message: MessageFromWebview) {
7575
switch (message.type) {
7676
case MessageFromWebviewType.ADD_CUSTOM_PLOT:
77-
return this.addCustomPlot()
77+
return commands.executeCommand(RegisteredCommands.PLOTS_CUSTOM_ADD)
7878
case MessageFromWebviewType.RESIZE_PLOTS:
7979
return this.setPlotSize(
8080
message.payload.section,
@@ -96,7 +96,7 @@ export class WebviewMessages {
9696
case MessageFromWebviewType.SELECT_EXPERIMENTS:
9797
return this.selectExperimentsFromWebview()
9898
case MessageFromWebviewType.REMOVE_CUSTOM_PLOTS:
99-
return this.removeCustomPlots()
99+
return commands.executeCommand(RegisteredCommands.PLOTS_CUSTOM_REMOVE)
100100
case MessageFromWebviewType.REFRESH_REVISIONS:
101101
return this.refreshData()
102102
case MessageFromWebviewType.TOGGLE_EXPERIMENT:
@@ -183,37 +183,6 @@ export class WebviewMessages {
183183
)
184184
}
185185

186-
private async addCustomPlot() {
187-
const metricAndParam = await pickMetricAndParam(
188-
this.experiments.getColumnTerminalNodes(),
189-
this.plots.getCustomPlotsOrder()
190-
)
191-
192-
if (!metricAndParam) {
193-
return
194-
}
195-
196-
this.plots.addCustomPlot(metricAndParam)
197-
this.sendCustomPlotsAndEvent(EventName.VIEWS_PLOTS_CUSTOM_PLOT_ADDED)
198-
}
199-
200-
private async removeCustomPlots() {
201-
const selectedPlotsIds = await pickCustomPlots(
202-
this.plots.getCustomPlotsOrder(),
203-
'There are no plots to remove.',
204-
{
205-
title: Title.SELECT_CUSTOM_PLOTS_TO_REMOVE
206-
}
207-
)
208-
209-
if (!selectedPlotsIds) {
210-
return
211-
}
212-
213-
this.plots.removeCustomPlots(selectedPlotsIds)
214-
this.sendCustomPlotsAndEvent(EventName.VIEWS_PLOTS_CUSTOM_PLOT_REMOVED)
215-
}
216-
217186
private setCustomPlotsOrder(plotIds: string[]) {
218187
const customPlotsOrderWithId = this.plots
219188
.getCustomPlotsOrder()
@@ -232,17 +201,12 @@ export class WebviewMessages {
232201
}))
233202

234203
this.plots.setCustomPlotsOrder(newOrder)
235-
this.sendCustomPlotsAndEvent(EventName.VIEWS_REORDER_PLOTS_CUSTOM)
236-
}
237-
238-
private sendCustomPlotsAndEvent(
239-
event:
240-
| typeof EventName.VIEWS_PLOTS_CUSTOM_PLOT_ADDED
241-
| typeof EventName.VIEWS_PLOTS_CUSTOM_PLOT_REMOVED
242-
| typeof EventName.VIEWS_REORDER_PLOTS_CUSTOM
243-
) {
244204
this.sendCustomPlots()
245-
sendTelemetryEvent(event, undefined, undefined)
205+
sendTelemetryEvent(
206+
EventName.VIEWS_REORDER_PLOTS_CUSTOM,
207+
undefined,
208+
undefined
209+
)
246210
}
247211

248212
private selectPlotsFromWebview() {

extension/src/plots/workspace.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ export class WorkspacePlots extends BaseWorkspaceWebviews<Plots, PlotsData> {
5858
return this.getRepository(dvcRoot).selectPlots()
5959
}
6060

61+
public async addCustomPlot(overrideRoot?: string) {
62+
const dvcRoot = await this.getDvcRoot(overrideRoot)
63+
if (!dvcRoot) {
64+
return
65+
}
66+
return this.getRepository(dvcRoot).addCustomPlot()
67+
}
68+
69+
public async removeCustomPlots(overrideRoot?: string) {
70+
const dvcRoot = await this.getDvcRoot(overrideRoot)
71+
if (!dvcRoot) {
72+
return
73+
}
74+
return this.getRepository(dvcRoot).removeCustomPlot()
75+
}
76+
6177
public getFocusedOrOnlyOrPickProject() {
6278
return this.focusedWebviewDvcRoot || this.getOnlyOrPickProject()
6379
}

extension/src/telemetry/constants.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ export const EventName = Object.assign(
6363
VIEWS_PLOTS_COMPARISON_ROWS_REORDERED:
6464
'views.plots.comparisonRowsReordered',
6565
VIEWS_PLOTS_CREATED: 'views.plots.created',
66-
VIEWS_PLOTS_CUSTOM_PLOT_ADDED: 'views.plots.addCustomPlot',
67-
VIEWS_PLOTS_CUSTOM_PLOT_REMOVED: 'views.plots.removeCustomPlot',
6866
VIEWS_PLOTS_EXPERIMENT_TOGGLE: 'views.plots.toggleExperimentStatus',
6967
VIEWS_PLOTS_FOCUS_CHANGED: 'views.plots.focusChanged',
7068
VIEWS_PLOTS_MANUAL_REFRESH: 'views.plots.manualRefresh',
@@ -172,6 +170,8 @@ export interface IEventNamePropertyMapping {
172170
[EventName.PLOTS_SHOW]: undefined
173171
[EventName.PLOTS_SELECT]: undefined
174172
[EventName.PLOTS_REFRESH]: undefined
173+
[EventName.PLOTS_CUSTOM_ADD]: undefined
174+
[EventName.PLOTS_CUSTOM_REMOVE]: undefined
175175

176176
[EventName.ADD_TARGET]: undefined
177177
[EventName.CHECKOUT_TARGET]: undefined
@@ -240,8 +240,6 @@ export interface IEventNamePropertyMapping {
240240

241241
[EventName.VIEWS_PLOTS_CLOSED]: undefined
242242
[EventName.VIEWS_PLOTS_CREATED]: undefined
243-
[EventName.VIEWS_PLOTS_CUSTOM_PLOT_REMOVED]: undefined
244-
[EventName.VIEWS_PLOTS_CUSTOM_PLOT_ADDED]: undefined
245243
[EventName.VIEWS_PLOTS_FOCUS_CHANGED]: WebviewFocusChangedProperties
246244
[EventName.VIEWS_PLOTS_MANUAL_REFRESH]: undefined
247245
[EventName.VIEWS_PLOTS_REVISIONS_REORDERED]: undefined

0 commit comments

Comments
 (0)