Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions extension/src/experiments/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Event, EventEmitter, window } from 'vscode'
import { Disposable, Disposer } from '@hediet/std/disposable'
import { setContextValue } from '../vscode/context'
import { standardizePath } from '../fileSystem/path'

const setContextOnDidChangeParamsFiles = (
setActiveEditorContext: (paramsFileActive: boolean) => void,
onDidChangeColumns: Event<void>,
getParamsFiles: () => Set<string>
): Disposable =>
onDidChangeColumns(() => {
const path = standardizePath(window.activeTextEditor?.document.fileName)
if (!path) {
return
}

if (!getParamsFiles().has(path)) {
return
}
setActiveEditorContext(true)
})

const setContextOnDidChangeActiveEditor = (
setActiveEditorContext: (paramsFileActive: boolean) => void,
dvcRoot: string,
getParamsFiles: () => Set<string>
): Disposable =>
window.onDidChangeActiveTextEditor(event => {
const path = standardizePath(event?.document.fileName)
if (!path) {
setActiveEditorContext(false)
return
}

if (!path.includes(dvcRoot)) {
return
}

const isParamsFile = getParamsFiles().has(path)

setActiveEditorContext(isParamsFile)
})

export const setContextForEditorTitleIcons = (
dvcRoot: string,
disposer: (() => void) & Disposer,
getParamsFiles: () => Set<string>,
paramsFileFocused: EventEmitter<string | undefined>,
onDidChangeColumns: Event<void>
): void => {
const setActiveEditorContext = (paramsFileActive: boolean) => {
setContextValue('dvc.params.fileActive', paramsFileActive)
const activeDvcRoot = paramsFileActive ? dvcRoot : undefined
paramsFileFocused.fire(activeDvcRoot)
}

disposer.track(
setContextOnDidChangeParamsFiles(
setActiveEditorContext,
onDidChangeColumns,
getParamsFiles
)
)

disposer.track(
setContextOnDidChangeActiveEditor(
setActiveEditorContext,
dvcRoot,
getParamsFiles
)
)
}
51 changes: 10 additions & 41 deletions extension/src/experiments/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventEmitter, Memento, window } from 'vscode'
import { Event, EventEmitter, Memento } from 'vscode'
import { setContextForEditorTitleIcons } from './context'
import { ExperimentsModel } from './model'
import { pickExperiments } from './model/quickPicks'
import { pickAndModifyParams } from './model/modify/quickPick'
Expand All @@ -25,8 +26,6 @@ import { FileSystemData } from '../fileSystem/data'
import { Response } from '../vscode/response'
import { Title } from '../vscode/title'
import { createTypedAccumulator } from '../util/object'
import { setContextValue } from '../vscode/context'
import { standardizePath } from '../fileSystem/path'
import { pickPaths } from '../path/selection/quickPick'
import { Toast } from '../vscode/toast'

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

this.webviewMessages = this.createWebviewMessageHandler()
this.setupInitialData()
this.setActiveEditorContext()
this.watchActiveEditor()
}

public update() {
Expand Down Expand Up @@ -498,43 +497,13 @@ export class Experiments extends BaseRepository<TableData> {
return experiment?.id
}

private setActiveEditorContext() {
const setActiveEditorContext = (active: boolean) => {
setContextValue('dvc.params.fileActive', active)
const activeDvcRoot = active ? this.dvcRoot : undefined
this.paramsFileFocused.fire(activeDvcRoot)
}

this.dispose.track(
this.onDidChangeColumns(() => {
const path = standardizePath(window.activeTextEditor?.document.fileName)
if (!path) {
return
}

if (!this.columns.getParamsFiles().has(path)) {
return
}
setActiveEditorContext(true)
})
)

this.dispose.track(
window.onDidChangeActiveTextEditor(event => {
const path = standardizePath(event?.document.fileName)
if (!path) {
setActiveEditorContext(false)
return
}

if (path.includes(this.dvcRoot)) {
if (this.columns.getParamsFiles().has(path)) {
setActiveEditorContext(true)
return
}
setActiveEditorContext(false)
}
})
private watchActiveEditor() {
setContextForEditorTitleIcons(
this.dvcRoot,
this.dispose,
() => this.columns.getParamsFiles(),
this.paramsFileFocused,
this.onDidChangeColumns
)
}
}