Skip to content

Commit ed8cfad

Browse files
committed
refactor
1 parent 3c585c0 commit ed8cfad

File tree

7 files changed

+74
-24
lines changed

7 files changed

+74
-24
lines changed

extension/src/data/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { InternalCommands } from '../commands/internal'
77
import { ExpShowOutput, PlotsOutputOrError } from '../cli/dvc/contract'
88
import { uniqueValues } from '../util/array'
99
import { DeferredDisposable } from '../class/deferred'
10-
import { isSameOrChild } from '../fileSystem'
10+
import { isPathInSubProject, isSameOrChild } from '../fileSystem'
1111

1212
export type ExperimentsOutput = {
1313
availableNbCommits: { [branch: string]: number }
@@ -86,14 +86,12 @@ export abstract class BaseData<
8686
path => {
8787
const relPath = relative(this.dvcRoot, path)
8888
if (
89-
!this.relSubProjects.some(subProject =>
90-
relPath.startsWith(subProject)
91-
) &&
9289
this.getWatchedFiles().some(
9390
watchedRelPath =>
9491
path.endsWith(watchedRelPath) ||
9592
isSameOrChild(relPath, watchedRelPath)
96-
)
93+
) &&
94+
!isPathInSubProject(relPath, this.relSubProjects)
9795
) {
9896
void this.managedUpdate(path)
9997
}

extension/src/experiments/context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Event, EventEmitter, window } from 'vscode'
22
import { Disposable, Disposer } from '@hediet/std/disposable'
33
import { ContextKey, setContextValue } from '../vscode/context'
44
import { standardizePossiblePath } from '../fileSystem/path'
5-
import { isFileInSubProject } from '../fileSystem'
5+
import { isPathInProject, isPathInSubProject } from '../fileSystem'
66

77
const setContextOnDidChangeParamsFiles = (
88
setActiveEditorContext: (paramsFileActive: boolean) => void,
@@ -20,7 +20,7 @@ const setContextOnDidChangeParamsFiles = (
2020

2121
if (
2222
(!getParamsFiles().has(path) && !path.endsWith('dvc.yaml')) ||
23-
isFileInSubProject(path, subProjects)
23+
isPathInSubProject(path, subProjects)
2424
) {
2525
return
2626
}
@@ -40,7 +40,7 @@ const setContextOnDidChangeActiveEditor = (
4040
return
4141
}
4242

43-
if (!path.includes(dvcRoot) || isFileInSubProject(path, subProjects)) {
43+
if (!isPathInProject(path, dvcRoot, subProjects)) {
4444
return
4545
}
4646

extension/src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ class Extension extends Disposable {
277277
public async initialize() {
278278
this.resetMembers()
279279

280-
const subProjects = this.getSubProjects()
281280
const dvcRoots = this.getRoots()
281+
const subProjects = this.getSubProjects()
282282

283283
await Promise.all([
284284
this.repositories.create(dvcRoots, subProjects),

extension/src/fileSystem/index.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
getModifiedTime,
1717
findOrCreateDvcYamlFile,
1818
writeJson,
19-
writeCsv
19+
writeCsv,
20+
isPathInProject
2021
} from '.'
2122
import { dvcDemoPath } from '../test/util'
2223
import { DOT_DVC } from '../cli/dvc/constants'
@@ -408,3 +409,51 @@ describe('findOrCreateDvcYamlFile', () => {
408409
)
409410
})
410411
})
412+
413+
describe('isPathInProject', () => {
414+
it('should return true if the path is in the project', () => {
415+
const path = join(dvcDemoPath, 'dvc.yaml')
416+
const dvcRoot = dvcDemoPath
417+
const subProjects: string[] = []
418+
expect(isPathInProject(path, dvcRoot, subProjects)).toBe(true)
419+
})
420+
it('should return false if the path is not in the project', () => {
421+
const path = resolve(dvcDemoPath, '..', 'dvc.yaml')
422+
const dvcRoot = dvcDemoPath
423+
const subProjects: string[] = []
424+
expect(isPathInProject(path, dvcRoot, subProjects)).toBe(false)
425+
})
426+
427+
it('should return false if the path is the project', () => {
428+
const path = dvcDemoPath
429+
const dvcRoot = dvcDemoPath
430+
const subProjects: string[] = []
431+
expect(isPathInProject(path, dvcRoot, subProjects)).toBe(false)
432+
})
433+
434+
it('should return false if the path is in the project but also in a sub-project', () => {
435+
const path = join(dvcDemoPath, 'nested1', 'dvc.yaml')
436+
const dvcRoot = dvcDemoPath
437+
const subProjects: string[] = [join(dvcDemoPath, 'nested1')]
438+
expect(isPathInProject(path, dvcRoot, subProjects)).toBe(false)
439+
})
440+
441+
it('should return false if the path is in the project but also in one of many sub-projects', () => {
442+
const path = join(dvcDemoPath, 'nested2', 'dvc.yaml')
443+
const dvcRoot = dvcDemoPath
444+
const subProjects: string[] = [
445+
join(dvcDemoPath, 'nested1'),
446+
join(dvcDemoPath, 'nested2'),
447+
join(dvcDemoPath, 'nested3'),
448+
join(dvcDemoPath, 'nested4')
449+
]
450+
expect(isPathInProject(path, dvcRoot, subProjects)).toBe(false)
451+
})
452+
453+
it('should return true if the path is in the project but not in a sub-project', () => {
454+
const path = join(dvcDemoPath, 'nested1', 'dvc.yaml')
455+
const dvcRoot = dvcDemoPath
456+
const subProjects: string[] = [join(dvcDemoPath, 'nested2')]
457+
expect(isPathInProject(path, dvcRoot, subProjects)).toBe(true)
458+
})
459+
})

extension/src/fileSystem/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,19 @@ export const isSameOrChild = (root: string, path: string) => {
126126
return !rel.startsWith('..')
127127
}
128128

129-
export const isFileInSubProject = (
130-
file: string,
129+
export const isPathInSubProject = (
130+
path: string,
131+
subProjects: string[]
132+
): boolean => subProjects.some(dvcRoot => path.startsWith(dvcRoot))
133+
134+
export const isPathInProject = (
135+
path: string | undefined,
136+
dvcRoot: string,
131137
subProjects: string[]
132-
): boolean => subProjects.some(dvcRoot => file.startsWith(dvcRoot))
138+
): path is string =>
139+
!!path?.startsWith(dvcRoot) &&
140+
path !== dvcRoot &&
141+
!isPathInSubProject(path, subProjects)
133142

134143
export type Out =
135144
| string

extension/src/pipeline/context.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EventEmitter, window } from 'vscode'
33
import { Disposable, Disposer } from '@hediet/std/disposable'
44
import { ContextKey, setContextValue } from '../vscode/context'
55
import { standardizePossiblePath } from '../fileSystem/path'
6-
import { isFileInSubProject } from '../fileSystem'
6+
import { isPathInProject } from '../fileSystem'
77

88
const setContextOnDidChangeActiveEditor = (
99
setActiveEditorContext: (path: string) => void,
@@ -17,7 +17,7 @@ const setContextOnDidChangeActiveEditor = (
1717
return
1818
}
1919

20-
if (!path.includes(dvcRoot) || isFileInSubProject(path, subProjects)) {
20+
if (!isPathInProject(path, dvcRoot, subProjects)) {
2121
return
2222
}
2323

@@ -37,10 +37,7 @@ export const setContextForEditorTitleIcons = (
3737
}
3838

3939
const activePath = window.activeTextEditor?.document?.fileName
40-
if (
41-
activePath?.startsWith(dvcRoot) &&
42-
!isFileInSubProject(activePath, subProjects)
43-
) {
40+
if (isPathInProject(activePath, dvcRoot, subProjects)) {
4441
setActiveEditorContext(activePath)
4542
}
4643

extension/src/pipeline/data.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname } from 'path'
22
import { AvailableCommands, InternalCommands } from '../commands/internal'
33
import { BaseData } from '../data'
44
import { findFiles } from '../fileSystem/workspace'
5-
import { isFileInSubProject } from '../fileSystem'
5+
import { isPathInProject } from '../fileSystem'
66

77
export class PipelineData extends BaseData<{
88
dag: string
@@ -38,10 +38,7 @@ export class PipelineData extends BaseData<{
3838

3939
const dvcYamlsDirs = new Set<string>()
4040
for (const file of fileList) {
41-
if (
42-
file.startsWith(this.dvcRoot) &&
43-
!isFileInSubProject(file, this.subProjects)
44-
) {
41+
if (isPathInProject(file, this.dvcRoot, this.subProjects)) {
4542
dvcYamlsDirs.add(dirname(file))
4643
}
4744
}

0 commit comments

Comments
 (0)