diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res index aee917a3..152556b0 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res @@ -112,6 +112,25 @@ describe("project2", () => { }) }) +describe("removing sources", () => { + let project = Project.createProject() + Project.setContinues(project, "main", ["second"]) + Project.setContinues(project, "second", ["first"]) + Project.setSource(project, "first", "x=1") + Project.setSource(project, "second", "y=2") + Project.setSource(project, "main", "y") + + Project.removeSource(project, "main") + + test("can delete sources without dependents", () => { + expect(Project.getDependents(project, "second")) == [] + }) + + test("project doesn't have source", () => { + expect(Project.getSource(project, "main")) == None + }) +}) + describe("project with include", () => { let project = Project.createProject() Project.setContinues(project, "main", ["second"]) diff --git a/packages/squiggle-lang/src/js/SqProject.ts b/packages/squiggle-lang/src/js/SqProject.ts index 9e79b564..84fda5a7 100644 --- a/packages/squiggle-lang/src/js/SqProject.ts +++ b/packages/squiggle-lang/src/js/SqProject.ts @@ -22,6 +22,10 @@ export class SqProject { return RSProject.setSource(this._value, sourceId, value); } + removeSource(sourceId: string) { + RSProject.removeSource(this._value, sourceId); + } + getSource(sourceId: string) { return RSProject.getSource(this._value, sourceId); } diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res index ac9c167c..8e23a5f2 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res @@ -50,6 +50,14 @@ Sets the source for a given source Id. let setSource = (project: reducerProject, sourceId: string, value: string): unit => project->Private.setSource(sourceId, value) +/* +Removes the source for a given source Id. Only works on sources without dependents. +Returns true for success +*/ +@genType +let removeSource = (project: reducerProject, sourceId: string): unit => + project->Private.removeSource(sourceId) + /* Gets the source for a given source id. */ diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res index 1942a136..4f6b4dd9 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res @@ -61,6 +61,10 @@ let setSource = (project: t, sourceId: string, value: string): unit => { touchDependents(project, sourceId) } +let removeSource = (project: t, sourceId: string): unit => { + Belt.MutableMap.String.remove(project.items, sourceId) +} + let clean = (project: t, sourceId: string): unit => { let newItem = project->getItem(sourceId)->ProjectItem.clean project->setItem(sourceId, newItem) @@ -184,6 +188,7 @@ let linkDependencies = (project: t, sourceId: string): Reducer_T.namespace => { acc->Reducer_Namespace.set( variable, project->getBindings(includeFile)->Reducer_Namespace.toRecord, + ) ) }