Skip to content

Commit 76c5999

Browse files
authored
Extract set active editor context from experiments (#2038)
* extract set active editor context from experiments * apply review suggestion
1 parent 253394c commit 76c5999

File tree

2 files changed

+82
-41
lines changed

2 files changed

+82
-41
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Event, EventEmitter, window } from 'vscode'
2+
import { Disposable, Disposer } from '@hediet/std/disposable'
3+
import { setContextValue } from '../vscode/context'
4+
import { standardizePath } from '../fileSystem/path'
5+
6+
const setContextOnDidChangeParamsFiles = (
7+
setActiveEditorContext: (paramsFileActive: boolean) => void,
8+
onDidChangeColumns: Event<void>,
9+
getParamsFiles: () => Set<string>
10+
): Disposable =>
11+
onDidChangeColumns(() => {
12+
const path = standardizePath(window.activeTextEditor?.document.fileName)
13+
if (!path) {
14+
return
15+
}
16+
17+
if (!getParamsFiles().has(path)) {
18+
return
19+
}
20+
setActiveEditorContext(true)
21+
})
22+
23+
const setContextOnDidChangeActiveEditor = (
24+
setActiveEditorContext: (paramsFileActive: boolean) => void,
25+
dvcRoot: string,
26+
getParamsFiles: () => Set<string>
27+
): Disposable =>
28+
window.onDidChangeActiveTextEditor(event => {
29+
const path = standardizePath(event?.document.fileName)
30+
if (!path) {
31+
setActiveEditorContext(false)
32+
return
33+
}
34+
35+
if (!path.includes(dvcRoot)) {
36+
return
37+
}
38+
39+
const isParamsFile = getParamsFiles().has(path)
40+
41+
setActiveEditorContext(isParamsFile)
42+
})
43+
44+
export const setContextForEditorTitleIcons = (
45+
dvcRoot: string,
46+
disposer: (() => void) & Disposer,
47+
getParamsFiles: () => Set<string>,
48+
paramsFileFocused: EventEmitter<string | undefined>,
49+
onDidChangeColumns: Event<void>
50+
): void => {
51+
const setActiveEditorContext = (paramsFileActive: boolean) => {
52+
setContextValue('dvc.params.fileActive', paramsFileActive)
53+
const activeDvcRoot = paramsFileActive ? dvcRoot : undefined
54+
paramsFileFocused.fire(activeDvcRoot)
55+
}
56+
57+
disposer.track(
58+
setContextOnDidChangeParamsFiles(
59+
setActiveEditorContext,
60+
onDidChangeColumns,
61+
getParamsFiles
62+
)
63+
)
64+
65+
disposer.track(
66+
setContextOnDidChangeActiveEditor(
67+
setActiveEditorContext,
68+
dvcRoot,
69+
getParamsFiles
70+
)
71+
)
72+
}

extension/src/experiments/index.ts

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Event, EventEmitter, Memento, window } from 'vscode'
1+
import { Event, EventEmitter, Memento } from 'vscode'
2+
import { setContextForEditorTitleIcons } from './context'
23
import { ExperimentsModel } from './model'
34
import { pickExperiments } from './model/quickPicks'
45
import { pickAndModifyParams } from './model/modify/quickPick'
@@ -25,8 +26,6 @@ import { FileSystemData } from '../fileSystem/data'
2526
import { Response } from '../vscode/response'
2627
import { Title } from '../vscode/title'
2728
import { createTypedAccumulator } from '../util/object'
28-
import { setContextValue } from '../vscode/context'
29-
import { standardizePath } from '../fileSystem/path'
3029
import { pickPaths } from '../path/selection/quickPick'
3130
import { Toast } from '../vscode/toast'
3231

@@ -122,7 +121,7 @@ export class Experiments extends BaseRepository<TableData> {
122121

123122
this.webviewMessages = this.createWebviewMessageHandler()
124123
this.setupInitialData()
125-
this.setActiveEditorContext()
124+
this.watchActiveEditor()
126125
}
127126

128127
public update() {
@@ -498,43 +497,13 @@ export class Experiments extends BaseRepository<TableData> {
498497
return experiment?.id
499498
}
500499

501-
private setActiveEditorContext() {
502-
const setActiveEditorContext = (active: boolean) => {
503-
setContextValue('dvc.params.fileActive', active)
504-
const activeDvcRoot = active ? this.dvcRoot : undefined
505-
this.paramsFileFocused.fire(activeDvcRoot)
506-
}
507-
508-
this.dispose.track(
509-
this.onDidChangeColumns(() => {
510-
const path = standardizePath(window.activeTextEditor?.document.fileName)
511-
if (!path) {
512-
return
513-
}
514-
515-
if (!this.columns.getParamsFiles().has(path)) {
516-
return
517-
}
518-
setActiveEditorContext(true)
519-
})
520-
)
521-
522-
this.dispose.track(
523-
window.onDidChangeActiveTextEditor(event => {
524-
const path = standardizePath(event?.document.fileName)
525-
if (!path) {
526-
setActiveEditorContext(false)
527-
return
528-
}
529-
530-
if (path.includes(this.dvcRoot)) {
531-
if (this.columns.getParamsFiles().has(path)) {
532-
setActiveEditorContext(true)
533-
return
534-
}
535-
setActiveEditorContext(false)
536-
}
537-
})
500+
private watchActiveEditor() {
501+
setContextForEditorTitleIcons(
502+
this.dvcRoot,
503+
this.dispose,
504+
() => this.columns.getParamsFiles(),
505+
this.paramsFileFocused,
506+
this.onDidChangeColumns
538507
)
539508
}
540509
}

0 commit comments

Comments
 (0)