Skip to content

Commit

Permalink
exclude sub project files from file and editor watchers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Jul 17, 2023
1 parent 1b72b59 commit 3c585c0
Show file tree
Hide file tree
Showing 24 changed files with 222 additions and 52 deletions.
8 changes: 8 additions & 0 deletions extension/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export abstract class BaseData<
protected readonly internalCommands: InternalCommands
protected collectedFiles: string[] = []

private readonly relSubProjects: string[]
private readonly staticFiles: string[]

private readonly updated: EventEmitter<T> = this.dispose.track(
Expand All @@ -39,6 +40,7 @@ export abstract class BaseData<
dvcRoot: string,
internalCommands: InternalCommands,
updateProcesses: { name: string; process: () => Promise<unknown> }[],
subProjects: string[],
staticFiles: string[] = []
) {
super()
Expand All @@ -49,6 +51,9 @@ export abstract class BaseData<
)
this.internalCommands = internalCommands
this.onDidUpdate = this.updated.event
this.relSubProjects = subProjects.map(subProject =>
relative(this.dvcRoot, subProject)
)
this.staticFiles = staticFiles

this.watchFiles()
Expand Down Expand Up @@ -81,6 +86,9 @@ export abstract class BaseData<
path => {
const relPath = relative(this.dvcRoot, path)
if (
!this.relSubProjects.some(subProject =>
relPath.startsWith(subProject)
) &&
this.getWatchedFiles().some(
watchedRelPath =>
path.endsWith(watchedRelPath) ||
Expand Down
23 changes: 16 additions & 7 deletions extension/src/experiments/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Event, EventEmitter, window } from 'vscode'
import { Disposable, Disposer } from '@hediet/std/disposable'
import { ContextKey, setContextValue } from '../vscode/context'
import { standardizePossiblePath } from '../fileSystem/path'
import { isFileInSubProject } from '../fileSystem'

const setContextOnDidChangeParamsFiles = (
setActiveEditorContext: (paramsFileActive: boolean) => void,
onDidChangeColumns: Event<void>,
getParamsFiles: () => Set<string>
getParamsFiles: () => Set<string>,
subProjects: string[]
): Disposable =>
onDidChangeColumns(() => {
const path = standardizePossiblePath(
Expand All @@ -16,7 +18,10 @@ const setContextOnDidChangeParamsFiles = (
return
}

if (!getParamsFiles().has(path) && !path.endsWith('dvc.yaml')) {
if (
(!getParamsFiles().has(path) && !path.endsWith('dvc.yaml')) ||
isFileInSubProject(path, subProjects)
) {
return
}
setActiveEditorContext(true)
Expand All @@ -25,7 +30,8 @@ const setContextOnDidChangeParamsFiles = (
const setContextOnDidChangeActiveEditor = (
setActiveEditorContext: (paramsFileActive: boolean) => void,
dvcRoot: string,
getParamsFiles: () => Set<string>
getParamsFiles: () => Set<string>,
subProjects: string[]
): Disposable =>
window.onDidChangeActiveTextEditor(event => {
const path = standardizePossiblePath(event?.document.fileName)
Expand All @@ -34,7 +40,7 @@ const setContextOnDidChangeActiveEditor = (
return
}

if (!path.includes(dvcRoot)) {
if (!path.includes(dvcRoot) || isFileInSubProject(path, subProjects)) {
return
}

Expand All @@ -49,7 +55,8 @@ export const setContextForEditorTitleIcons = (
disposer: (() => void) & Disposer,
getParamsFiles: () => Set<string>,
experimentsFileFocused: EventEmitter<string | undefined>,
onDidChangeColumns: Event<void>
onDidChangeColumns: Event<void>,
subProjects: string[]
): void => {
const setActiveEditorContext = (experimentsFileActive: boolean) => {
void setContextValue(
Expand All @@ -64,15 +71,17 @@ export const setContextForEditorTitleIcons = (
setContextOnDidChangeParamsFiles(
setActiveEditorContext,
onDidChangeColumns,
getParamsFiles
getParamsFiles,
subProjects
)
)

disposer.track(
setContextOnDidChangeActiveEditor(
setActiveEditorContext,
dvcRoot,
getParamsFiles
getParamsFiles,
subProjects
)
)
}
4 changes: 3 additions & 1 deletion extension/src/experiments/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ export class ExperimentsData extends BaseData<ExperimentsOutput> {
constructor(
dvcRoot: string,
internalCommands: InternalCommands,
experiments: ExperimentsModel
experiments: ExperimentsModel,
subProjects: string[]
) {
super(
dvcRoot,
internalCommands,
[{ name: 'update', process: () => this.update() }],
subProjects,
['dvc.lock', 'dvc.yaml', 'params.yaml', DOT_DVC]
)

Expand Down
16 changes: 12 additions & 4 deletions extension/src/experiments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class Experiments extends BaseRepository<TableData> {
selectBranches: (
branchesSelected: string[]
) => Promise<string[] | undefined>,
subProjects: string[],
data?: ExperimentsData
) {
super(dvcRoot, resourceLocator.beaker)
Expand Down Expand Up @@ -143,7 +144,13 @@ export class Experiments extends BaseRepository<TableData> {
)

this.data = this.dispose.track(
data || new ExperimentsData(dvcRoot, internalCommands, this.experiments)
data ||
new ExperimentsData(
dvcRoot,
internalCommands,
this.experiments,
subProjects
)
)

this.dispose.track(this.data.onDidUpdate(data => this.setState(data)))
Expand All @@ -158,7 +165,7 @@ export class Experiments extends BaseRepository<TableData> {

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

public update() {
Expand Down Expand Up @@ -600,13 +607,14 @@ export class Experiments extends BaseRepository<TableData> {
)
}

private watchActiveEditor() {
private watchActiveEditor(subProjects: string[]) {
setContextForEditorTitleIcons(
this.dvcRoot,
this.dispose,
() => this.columns.getParamsFiles(),
this.experimentsFileFocused,
this.onDidChangeColumns
this.onDidChangeColumns,
subProjects
)
}

Expand Down
4 changes: 3 additions & 1 deletion extension/src/experiments/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<

public createRepository(
dvcRoot: string,
subProjects: string[],
pipeline: WorkspacePipeline,
resourceLocator: ResourceLocator
) {
Expand All @@ -283,7 +284,8 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<
pipeline.getRepository(dvcRoot),
resourceLocator,
this.workspaceState,
(branchesSelected: string[]) => this.selectBranches(branchesSelected)
(branchesSelected: string[]) => this.selectBranches(branchesSelected),
subProjects
)
)

Expand Down
23 changes: 18 additions & 5 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,26 @@ class Extension extends Disposable {
public async initialize() {
this.resetMembers()

const subProjects = this.getSubProjects()
const dvcRoots = this.getRoots()

await Promise.all([
this.repositories.create(this.getRoots()),
this.repositoriesTree.initialize(this.getRoots()),
this.pipelines.create(this.getRoots())
this.repositories.create(dvcRoots, subProjects),
this.repositoriesTree.initialize(dvcRoots),
this.pipelines.create(dvcRoots, subProjects)
])
this.experiments.create(
this.getRoots(),
dvcRoots,
subProjects,
this.pipelines,
this.resourceLocator
)
this.plots.create(this.getRoots(), this.resourceLocator, this.experiments)
this.plots.create(
dvcRoots,
subProjects,
this.resourceLocator,
this.experiments
)

return Promise.all([
this.experiments.isReady(),
Expand All @@ -308,6 +317,10 @@ class Extension extends Disposable {
private getRoots() {
return this.setup.getRoots()
}

private getSubProjects() {
return this.setup.getSubProjects()
}
}

let extension: undefined | Extension
Expand Down
5 changes: 5 additions & 0 deletions extension/src/fileSystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export const isSameOrChild = (root: string, path: string) => {
return !rel.startsWith('..')
}

export const isFileInSubProject = (
file: string,
subProjects: string[]
): boolean => subProjects.some(dvcRoot => file.startsWith(dvcRoot))

export type Out =
| string
| Record<string, { checkpoint?: boolean; cache?: boolean }>
Expand Down
20 changes: 15 additions & 5 deletions extension/src/pipeline/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { EventEmitter, window } from 'vscode'
import { Disposable, Disposer } from '@hediet/std/disposable'
import { ContextKey, setContextValue } from '../vscode/context'
import { standardizePossiblePath } from '../fileSystem/path'
import { isFileInSubProject } from '../fileSystem'

const setContextOnDidChangeActiveEditor = (
setActiveEditorContext: (path: string) => void,
dvcRoot: string
dvcRoot: string,
subProjects: string[]
): Disposable =>
window.onDidChangeActiveTextEditor(event => {
const path = standardizePossiblePath(event?.document.fileName)
Expand All @@ -15,7 +17,7 @@ const setContextOnDidChangeActiveEditor = (
return
}

if (!path.includes(dvcRoot)) {
if (!path.includes(dvcRoot) || isFileInSubProject(path, subProjects)) {
return
}

Expand All @@ -25,7 +27,8 @@ const setContextOnDidChangeActiveEditor = (
export const setContextForEditorTitleIcons = (
dvcRoot: string,
disposer: (() => void) & Disposer,
pipelineFileFocused: EventEmitter<string | undefined>
pipelineFileFocused: EventEmitter<string | undefined>,
subProjects: string[]
): void => {
const setActiveEditorContext = (path: string) => {
const pipeline = path.endsWith('dvc.yaml') ? dirname(path) : undefined
Expand All @@ -34,11 +37,18 @@ export const setContextForEditorTitleIcons = (
}

const activePath = window.activeTextEditor?.document?.fileName
if (activePath?.startsWith(dvcRoot)) {
if (
activePath?.startsWith(dvcRoot) &&
!isFileInSubProject(activePath, subProjects)
) {
setActiveEditorContext(activePath)
}

disposer.track(
setContextOnDidChangeActiveEditor(setActiveEditorContext, dvcRoot)
setContextOnDidChangeActiveEditor(
setActiveEditorContext,
dvcRoot,
subProjects
)
)
}
17 changes: 15 additions & 2 deletions extension/src/pipeline/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ import { dirname } from 'path'
import { AvailableCommands, InternalCommands } from '../commands/internal'
import { BaseData } from '../data'
import { findFiles } from '../fileSystem/workspace'
import { isFileInSubProject } from '../fileSystem'

export class PipelineData extends BaseData<{
dag: string
stages: { [pipeline: string]: string | undefined }
}> {
constructor(dvcRoot: string, internalCommands: InternalCommands) {
private readonly subProjects: string[]

constructor(
dvcRoot: string,
internalCommands: InternalCommands,
subProjects: string[]
) {
super(
dvcRoot,
internalCommands,
[{ name: 'update', process: () => this.update() }],
subProjects,
['dvc.yaml']
)

this.subProjects = subProjects
}

public managedUpdate() {
Expand All @@ -28,7 +38,10 @@ export class PipelineData extends BaseData<{

const dvcYamlsDirs = new Set<string>()
for (const file of fileList) {
if (file.startsWith(this.dvcRoot)) {
if (
file.startsWith(this.dvcRoot) &&
!isFileInSubProject(file, this.subProjects)
) {
dvcYamlsDirs.add(dirname(file))
}
}
Expand Down
10 changes: 6 additions & 4 deletions extension/src/pipeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ export class Pipeline extends DeferredDisposable {
constructor(
dvcRoot: string,
internalCommands: InternalCommands,
subProjects: string[],
data?: PipelineData
) {
super()
this.dvcRoot = dvcRoot
this.data = this.dispose.track(
data || new PipelineData(dvcRoot, internalCommands)
data || new PipelineData(dvcRoot, internalCommands, subProjects)
)
this.model = this.dispose.track(new PipelineModel())
this.updated = this.dispose.track(new EventEmitter<void>())
Expand All @@ -67,7 +68,7 @@ export class Pipeline extends DeferredDisposable {
this.onDidFocusProject = this.projectFocused.event

void this.initialize()
this.watchActiveEditor()
this.watchActiveEditor(subProjects)
}

public hasPipeline() {
Expand Down Expand Up @@ -218,11 +219,12 @@ export class Pipeline extends DeferredDisposable {
return this.focusedPipeline
}

private watchActiveEditor() {
private watchActiveEditor(subProjects: string[]) {
setContextForEditorTitleIcons(
this.dvcRoot,
this.dispose,
this.pipelineFileFocused
this.pipelineFileFocused,
subProjects
)

this.dispose.track(
Expand Down
4 changes: 2 additions & 2 deletions extension/src/pipeline/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export class WorkspacePipeline extends BaseWorkspace<Pipeline> {
)
}

public createRepository(dvcRoot: string) {
public createRepository(dvcRoot: string, subProjects: string[]) {
const pipeline = this.dispose.track(
new Pipeline(dvcRoot, this.internalCommands)
new Pipeline(dvcRoot, this.internalCommands, subProjects)
)

this.setRepository(dvcRoot, pipeline)
Expand Down
Loading

0 comments on commit 3c585c0

Please sign in to comment.