Skip to content

Commit

Permalink
Notebook clear all output (#5497)
Browse files Browse the repository at this point in the history
Depends on the merger of #5426.

Addresses #5492


Adds a simple clear-all-outputs button that does what it says on the
tin.

<img width="546" alt="Screenshot 2024-11-25 at 3 44 30 PM"
src="https://github.com/user-attachments/assets/5cf432aa-fb5c-4202-93e1-01380158972d">



### QA Notes

The clear-all button should work to clear any outputs that are currently
visible in a notebook. Clearing should persist after closing and opening
the notebook.


<!--
  Add additional information for QA on how to validate the change,
  paying special attention to the level of risk, adjacent areas that
  could be affected by the change, and any important contextual
  information not present in the linked issues.
-->
  • Loading branch information
nstrayer authored Nov 26, 2024
1 parent 281dba7 commit e42e2cc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
Binary file modified src/vs/base/browser/ui/codicons/codicon/codicon.ttf
Binary file not shown.
4 changes: 4 additions & 0 deletions src/vs/base/common/codiconsLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ export const codiconsLibrary = {
goToSearch: register('go-to-search', 0xec32),
percentage: register('percentage', 0xec33),
attach: register('attach', 0xec34),
goToEditingSession: register('go-to-editing-session', 0xec35),
editSession: register('edit-session', 0xec36),
codeReview: register('code-review', 0xec37),
sortPercentage: register('sort-percentage', 0xec33),
positronNew: register('positron-new', 0xf230),
positronOpen: register('positron-open', 0xf231),
Expand Down Expand Up @@ -670,4 +673,5 @@ export const codiconsLibrary = {
positronTableConnection: register('positron-table-connection', 0xf288),
positronCatalogConnection: register('positron-catalog-connection', 0xf289),
positronViewConnection: register('positron-view-connection', 0xf28a),
positronClean: register('positron-clean', 0xf28b),
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export function PositronNotebookHeader({ notebookInstance }: { notebookInstance:
label={(() => localize('runAllCellsShort', 'Run All'))()}
fullLabel={(() => localize('runAllCellsLong', 'Run All Cells'))()}
onClick={() => { notebookInstance.runAllCells(); }} />
<IconedButton
codicon='positron-clean'
label={(() => localize('clearAllCellOutputsShort', 'Clear Outputs'))()}
fullLabel={(() => localize('clearAllCellOutputsLong', 'Clear All Cell Outputs'))()}
onClick={() => { notebookInstance.clearAllCellOutputs(); }} />
<div style={{ marginLeft: 'auto' }}></div>
<AddCodeCellButton notebookInstance={notebookInstance} index={0} />
<AddMarkdownCellButton notebookInstance={notebookInstance} index={0} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ILogService } from 'vs/platform/log/common/log';
import { IActiveNotebookEditorDelegate, IBaseCellEditorOptions, INotebookEditorCreationOptions, INotebookEditorViewState } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { NotebookOptions } from 'vs/workbench/contrib/notebook/browser/notebookOptions';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { CellEditType, CellKind, ICellReplaceEdit, ISelectionState, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellEditType, CellKind, ICellEditOperation, ISelectionState, SelectionStateType, ICellReplaceEdit, NotebookCellExecutionState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService';
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
import { createNotebookCell } from './PositronNotebookCells/createNotebookCell';
Expand All @@ -35,6 +35,7 @@ import { ILanguageRuntimeSession, IRuntimeSessionService } from 'vs/workbench/se
import { isEqual } from 'vs/base/common/resources';
import { IPositronWebviewPreloadService } from 'vs/workbench/services/positronWebviewPreloads/browser/positronWebviewPreloadService';


interface IPositronNotebookInstanceRequiredTextModel extends IPositronNotebookInstance {
textModel: NotebookTextModel;
}
Expand Down Expand Up @@ -724,6 +725,90 @@ export class PositronNotebookInstance extends Disposable implements IPositronNot
this._modelStore.clear();
}

/**
* Clears the output of a specific cell in the notebook.
* @param cell The cell to clear outputs from. If not provided, uses the currently selected cell.
* @param skipContentEvent If true, won't fire the content change event (useful for batch operations)
*/
clearCellOutput(cell?: IPositronNotebookCell, skipContentEvent: boolean = false): void {
this._assertTextModel();

const targetCell = cell ?? this.selectionStateMachine.getSelectedCell();
if (!targetCell) {
return;
}

const cellIndex = this.textModel.cells.indexOf(targetCell.cellModel as NotebookCellTextModel);
if (cellIndex === -1) {
return;
}

const computeUndoRedo = !this.isReadOnly;
this.textModel.applyEdits([{
editType: CellEditType.Output,
index: cellIndex,
outputs: [],
append: false
}], true, undefined, () => undefined, undefined, computeUndoRedo);

if (!skipContentEvent) {
this._onDidChangeContent.fire();
}
}

/**
* Clears the outputs of all cells in the notebook.
*/
clearAllCellOutputs(): void {
this._assertTextModel();

try {
const computeUndoRedo = !this.isReadOnly;

// Clear outputs from all cells
this.textModel.cells.forEach((cell, index) => {
this.clearCellOutput(this._cells[index], true);
});

// Clear execution metadata for non-executing cells
const clearExecutionMetadataEdits = this.textModel.cells.map((cell, index) => {
const runState = this.notebookExecutionStateService.getCellExecution(cell.uri)?.state;
if (runState !== NotebookCellExecutionState.Executing) {
return {
editType: CellEditType.PartialInternalMetadata,
index,
internalMetadata: {
runStartTime: null,
runStartTimeAdjustment: null,
runEndTime: null,
executionOrder: null,
lastRunSuccess: null
}
};
}
return undefined;
}).filter((edit): edit is ICellEditOperation & {
editType: CellEditType.PartialInternalMetadata;
index: number;
internalMetadata: {
runStartTime: null;
runStartTimeAdjustment: null;
runEndTime: null;
executionOrder: null;
lastRunSuccess: null;
};
} => !!edit);

if (clearExecutionMetadataEdits.length) {
this.textModel.applyEdits(clearExecutionMetadataEdits, true, undefined, () => undefined, undefined, computeUndoRedo);
}

} finally {
// Fire a single content change event
this._onDidChangeContent.fire();
}
}

// #endregion
}

Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ export interface IPositronNotebookInstance {
*/
runAllCells(): Promise<void>;

/**
* Clears all output from all cells in the notebook.
*/
clearAllCellOutputs(): void;

/**
* Creates and inserts a new cell into the notebook.
*
Expand Down

0 comments on commit e42e2cc

Please sign in to comment.