From c8b996944a049f1da173c0ba1d930ef06e2a1542 Mon Sep 17 00:00:00 2001 From: Jonah Iden Date: Tue, 24 Oct 2023 14:18:43 +0200 Subject: [PATCH] started reorganizing Types from notebook-common and type converters Signed-off-by: Jonah Iden --- .../notebook/src/browser/notebook-types.ts | 106 +++++++++++++++ .../browser/view-model/notebook-cell-model.ts | 19 ++- .../src/browser/view-model/notebook-model.ts | 5 +- .../notebook/src/common/notebook-common.ts | 87 ------------- .../main/browser/notebooks/notebook-dto.ts | 58 ++++----- .../notebooks/notebook-kernels-main.ts | 2 +- .../plugin-ext/src/plugin/type-converters.ts | 123 +----------------- 7 files changed, 153 insertions(+), 247 deletions(-) create mode 100644 packages/notebook/src/browser/notebook-types.ts diff --git a/packages/notebook/src/browser/notebook-types.ts b/packages/notebook/src/browser/notebook-types.ts new file mode 100644 index 0000000000000..658d2bda43e86 --- /dev/null +++ b/packages/notebook/src/browser/notebook-types.ts @@ -0,0 +1,106 @@ +// ***************************************************************************** +// Copyright (C) 2023 TypeFox and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0. +// +// This Source Code may also be made available under the following Secondary +// Licenses when the conditions for such availability set forth in the Eclipse +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 +// with the GNU Classpath Exception which is available at +// https://www.gnu.org/software/classpath/license.html. +// +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 +// ***************************************************************************** + +import { + CellData, CellOutput, CellOutputItem, CellRange, NotebookCellContentChangeEvent, + NotebookCellsChangeInternalMetadataEvent, + NotebookCellsChangeLanguageEvent, + NotebookCellsChangeMetadataEvent, + NotebookCellsChangeType, NotebookCellTextModelSplice, NotebookDocumentMetadata +} from '../common'; +import { NotebookCell } from './view-model/notebook-cell-model'; + +export interface NotebookTextModelChangedEvent { + readonly rawEvents: NotebookContentChangedEvent[]; + // readonly versionId: number; + readonly synchronous?: boolean; + readonly endSelectionState?: SelectionState; +}; + +export type NotebookContentChangedEvent = (NotebookCellsInitializeEvent | NotebookDocumentChangeMetadataEvent | NotebookCellContentChangeEvent | + NotebookCellsModelChangedEvent | NotebookCellsModelMoveEvent | NotebookOutputChangedEvent | NotebookOutputItemChangedEvent | + NotebookCellsChangeLanguageEvent | NotebookCellsChangeMetadataEvent | + NotebookCellsChangeInternalMetadataEvent | NotebookDocumentUnknownChangeEvent); // & { transient: boolean }; + +export interface NotebookCellsInitializeEvent { + readonly kind: NotebookCellsChangeType.Initialize; + readonly changes: NotebookCellTextModelSplice[]; +} + +export interface NotebookDocumentChangeMetadataEvent { + readonly kind: NotebookCellsChangeType.ChangeDocumentMetadata; + readonly metadata: NotebookDocumentMetadata; +} + +export interface NotebookCellsModelChangedEvent { + readonly kind: NotebookCellsChangeType.ModelChange; + readonly changes: NotebookCellTextModelSplice[]; +} + +export interface NotebookModelWillAddRemoveEvent { + readonly rawEvent: NotebookCellsModelChangedEvent; +}; + +export interface NotebookCellsModelMoveEvent { + readonly kind: NotebookCellsChangeType.Move; + readonly index: number; + readonly length: number; + readonly newIdx: number; + readonly cells: T[]; +} + +export interface NotebookOutputChangedEvent { + readonly kind: NotebookCellsChangeType.Output; + readonly index: number; + readonly outputs: CellOutput[]; + readonly append: boolean; +} + +export interface NotebookOutputItemChangedEvent { + readonly kind: NotebookCellsChangeType.OutputItem; + readonly index: number; + readonly outputId: string; + readonly outputItems: CellOutputItem[]; + readonly append: boolean; +} + +export interface NotebookDocumentUnknownChangeEvent { + readonly kind: NotebookCellsChangeType.Unknown; +} + +export enum SelectionStateType { + Handle = 0, + Index = 1 +} + +export interface SelectionHandleState { + kind: SelectionStateType.Handle; + primary: number | null; + selections: number[]; +} + +export interface SelectionIndexState { + kind: SelectionStateType.Index; + focus: CellRange; + selections: CellRange[]; +} + +export type SelectionState = SelectionHandleState | SelectionIndexState; + +export interface NotebookModelWillAddRemoveEvent { + readonly newCellIds?: number[]; + readonly rawEvent: NotebookCellsModelChangedEvent; +}; diff --git a/packages/notebook/src/browser/view-model/notebook-cell-model.ts b/packages/notebook/src/browser/view-model/notebook-cell-model.ts index 35fea219a32b9..639ade33657be 100644 --- a/packages/notebook/src/browser/view-model/notebook-cell-model.ts +++ b/packages/notebook/src/browser/view-model/notebook-cell-model.ts @@ -25,7 +25,7 @@ import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service'; import { CellInternalMetadataChangedEvent, CellKind, NotebookCellCollapseState, NotebookCellInternalMetadata, - NotebookCellMetadata, NotebookCellOutputsSplice, CellOutput, CellData, NotebookCell, CellOutputItem + NotebookCellMetadata, NotebookCellOutputsSplice, CellOutput, CellData, CellOutputItem } from '../../common'; import { NotebookCellOutputModel } from './notebook-cell-output-model'; @@ -51,6 +51,23 @@ interface NotebookCellContextManager { onDidChangeContext: Event; } +export interface NotebookCell { + readonly uri: URI; + handle: number; + language: string; + cellKind: CellKind; + outputs: CellOutput[]; + metadata: NotebookCellMetadata; + internalMetadata: NotebookCellInternalMetadata; + text: string; + onDidChangeOutputs?: Event; + onDidChangeOutputItems?: Event; + onDidChangeLanguage: Event; + onDidChangeMetadata: Event; + onDidChangeInternalMetadata: Event; + +} + const NotebookCellModelProps = Symbol('NotebookModelProps'); export interface NotebookCellModelProps { readonly uri: URI, diff --git a/packages/notebook/src/browser/view-model/notebook-model.ts b/packages/notebook/src/browser/view-model/notebook-model.ts index d2600c24050b2..dafb1271fede3 100644 --- a/packages/notebook/src/browser/view-model/notebook-model.ts +++ b/packages/notebook/src/browser/view-model/notebook-model.ts @@ -20,9 +20,10 @@ import { CellData, CellEditOperation, CellEditType, CellUri, NotebookCellInternalMetadata, NotebookCellsChangeType, NotebookCellTextModelSplice, NotebookData, - NotebookDocumentMetadata, NotebookModelWillAddRemoveEvent, - NullablePartialNotebookCellInternalMetadata, NotebookContentChangedEvent + NotebookDocumentMetadata, + NullablePartialNotebookCellInternalMetadata } from '../../common'; +import { NotebookContentChangedEvent, NotebookModelWillAddRemoveEvent } from '../notebook-types'; import { NotebookSerializer } from '../service/notebook-service'; import { FileService } from '@theia/filesystem/lib/browser/file-service'; import { NotebookCellModel, NotebookCellModelFactory } from './notebook-cell-model'; diff --git a/packages/notebook/src/common/notebook-common.ts b/packages/notebook/src/common/notebook-common.ts index 250fed8d6ae42..52d35d7c3f841 100644 --- a/packages/notebook/src/common/notebook-common.ts +++ b/packages/notebook/src/common/notebook-common.ts @@ -18,7 +18,6 @@ import { CancellationToken, Command, Event, URI } from '@theia/core'; import { MarkdownString } from '@theia/core/lib/common/markdown-rendering/markdown-string'; import { BinaryBuffer } from '@theia/core/lib/common/buffer'; import { UriComponents } from '@theia/core/lib/common/uri'; -import { CellRange } from './notebook-range'; export enum CellKind { Markup = 1, @@ -88,23 +87,6 @@ export interface NotebookCellCollapseState { outputCollapsed?: boolean; } -export interface NotebookCell { - readonly uri: URI; - handle: number; - language: string; - cellKind: CellKind; - outputs: CellOutput[]; - metadata: NotebookCellMetadata; - internalMetadata: NotebookCellInternalMetadata; - text: string; - onDidChangeOutputs?: Event; - onDidChangeOutputItems?: Event; - onDidChangeLanguage: Event; - onDidChangeMetadata: Event; - onDidChangeInternalMetadata: Event; - -} - export interface CellData { source: string; language: string; @@ -182,62 +164,12 @@ export enum NotebookCellsChangeType { Unknown = 100 } -export enum SelectionStateType { - Handle = 0, - Index = 1 -} -export interface SelectionHandleState { - kind: SelectionStateType.Handle; - primary: number | null; - selections: number[]; -} - -export interface SelectionIndexState { - kind: SelectionStateType.Index; - focus: CellRange; - selections: CellRange[]; -} - -export type SelectionState = SelectionHandleState | SelectionIndexState; - -export interface NotebookCellsInitializeEvent { - readonly kind: NotebookCellsChangeType.Initialize; - readonly changes: NotebookCellTextModelSplice[]; -} - export interface NotebookCellsChangeLanguageEvent { readonly kind: NotebookCellsChangeType.ChangeCellLanguage; readonly index: number; readonly language: string; } -export interface NotebookCellsModelChangedEvent { - readonly kind: NotebookCellsChangeType.ModelChange; - readonly changes: NotebookCellTextModelSplice[]; -} - -export interface NotebookCellsModelMoveEvent { - readonly kind: NotebookCellsChangeType.Move; - readonly index: number; - readonly length: number; - readonly newIdx: number; - readonly cells: T[]; -} - -export interface NotebookOutputChangedEvent { - readonly kind: NotebookCellsChangeType.Output; - readonly index: number; - readonly outputs: CellOutput[]; - readonly append: boolean; -} - -export interface NotebookOutputItemChangedEvent { - readonly kind: NotebookCellsChangeType.OutputItem; - readonly index: number; - readonly outputId: string; - readonly outputItems: CellOutputItem[]; - readonly append: boolean; -} export interface NotebookCellsChangeMetadataEvent { readonly kind: NotebookCellsChangeType.ChangeCellMetadata; readonly index: number; @@ -250,30 +182,11 @@ export interface NotebookCellsChangeInternalMetadataEvent { readonly internalMetadata: NotebookCellInternalMetadata; } -export interface NotebookDocumentChangeMetadataEvent { - readonly kind: NotebookCellsChangeType.ChangeDocumentMetadata; - readonly metadata: NotebookDocumentMetadata; -} - -export interface NotebookDocumentUnknownChangeEvent { - readonly kind: NotebookCellsChangeType.Unknown; -} - export interface NotebookCellContentChangeEvent { readonly kind: NotebookCellsChangeType.ChangeCellContent; readonly index: number; } -export type NotebookContentChangedEvent = (NotebookCellsInitializeEvent | NotebookDocumentChangeMetadataEvent | NotebookCellContentChangeEvent | - NotebookCellsModelChangedEvent | NotebookCellsModelMoveEvent | NotebookOutputChangedEvent | NotebookOutputItemChangedEvent | - NotebookCellsChangeLanguageEvent | NotebookCellsChangeMetadataEvent | - NotebookCellsChangeInternalMetadataEvent | NotebookDocumentUnknownChangeEvent); // & { transient: boolean }; - -export interface NotebookModelWillAddRemoveEvent { - readonly newCellIds?: number[]; - readonly rawEvent: NotebookCellsModelChangedEvent; -}; - export enum NotebookCellExecutionState { Unconfirmed = 1, Pending = 2, diff --git a/packages/plugin-ext/src/main/browser/notebooks/notebook-dto.ts b/packages/plugin-ext/src/main/browser/notebooks/notebook-dto.ts index e6afbb8d79ed6..491613ff55267 100644 --- a/packages/plugin-ext/src/main/browser/notebooks/notebook-dto.ts +++ b/packages/plugin-ext/src/main/browser/notebooks/notebook-dto.ts @@ -18,6 +18,8 @@ import { OS } from '@theia/core'; import * as notebookCommon from '@theia/notebook/lib/common'; import { NotebookCellModel } from '@theia/notebook/lib/browser/view-model/notebook-cell-model'; import * as rpc from '../../../common'; +import { CellExecutionUpdateType } from '@theia/notebook/lib/common'; +import { CellExecuteUpdate, CellExecutionComplete } from '@theia/notebook/lib/browser'; export namespace NotebookDto { @@ -102,40 +104,28 @@ export namespace NotebookDto { }; } - // export function fromCellExecuteUpdateDto(data: extHostProtocol.ICellExecuteUpdateDto): ICellExecuteUpdate { - // if (data.editType === CellExecutionUpdateType.Output) { - // return { - // editType: data.editType, - // cellHandle: data.cellHandle, - // append: data.append, - // outputs: data.outputs.map(fromNotebookOutputDto) - // }; - // } else if (data.editType === CellExecutionUpdateType.OutputItems) { - // return { - // editType: data.editType, - // append: data.append, - // outputId: data.outputId, - // items: data.items.map(fromNotebookOutputItemDto) - // }; - // } else { - // return data; - // } - // } + export function fromCellExecuteUpdateDto(data: rpc.CellExecuteUpdateDto): CellExecuteUpdate { + if (data.editType === CellExecutionUpdateType.Output) { + return { + editType: data.editType, + cellHandle: data.cellHandle, + append: data.append, + outputs: data.outputs.map(fromNotebookOutputDto) + }; + } else if (data.editType === CellExecutionUpdateType.OutputItems) { + return { + editType: data.editType, + outputId: data.outputId, + append: data.append, + items: data.items.map(fromNotebookOutputItemDto) + }; + } else { + return data; + } + } - // export function fromCellExecuteCompleteDto(data: extHostProtocol.ICellExecutionCompleteDto): ICellExecutionComplete { - // return data; - // } + export function fromCellExecuteCompleteDto(data: rpc.CellExecutionCompleteDto): CellExecutionComplete { + return data; + } - // export function fromCellEditOperationDto(edit: extHostProtocol.ICellEditOperationDto): notebookCommon.ICellEditOperation { - // if (edit.editType === notebookCommon.CellEditType.Replace) { - // return { - // editType: edit.editType, - // index: edit.index, - // count: edit.count, - // cells: edit.cells.map(fromNotebookCellDataDto) - // }; - // } else { - // return edit; - // } - // } } diff --git a/packages/plugin-ext/src/main/browser/notebooks/notebook-kernels-main.ts b/packages/plugin-ext/src/main/browser/notebooks/notebook-kernels-main.ts index 6f9e735f1f112..d3ea21f53c753 100644 --- a/packages/plugin-ext/src/main/browser/notebooks/notebook-kernels-main.ts +++ b/packages/plugin-ext/src/main/browser/notebooks/notebook-kernels-main.ts @@ -27,7 +27,7 @@ import { CellExecution, NotebookExecutionStateService, NotebookKernelChangeEvent import { combinedDisposable } from '@theia/monaco-editor-core/esm/vs/base/common/lifecycle'; import { interfaces } from '@theia/core/shared/inversify'; import { NotebookKernelSourceAction } from '@theia/notebook/lib/common'; -import { NotebookDto } from '../../../plugin/type-converters'; +import { NotebookDto } from './notebook-dto'; abstract class NotebookKernel { private readonly onDidChangeEmitter = new Emitter(); diff --git a/packages/plugin-ext/src/plugin/type-converters.ts b/packages/plugin-ext/src/plugin/type-converters.ts index 9b8626c48d912..18074dec578f3 100644 --- a/packages/plugin-ext/src/plugin/type-converters.ts +++ b/packages/plugin-ext/src/plugin/type-converters.ts @@ -32,8 +32,7 @@ import { DisposableCollection, isEmptyObject, isObject } from '@theia/core/lib/c import * as notebooks from '@theia/notebook/lib/common'; import { CommandsConverter } from './command-registry'; import { BinaryBuffer } from '@theia/core/lib/common/buffer'; -import { CellData, CellExecutionUpdateType, CellOutput, CellOutputItem, CellRange, isTextStreamMime } from '@theia/notebook/lib/common'; -import { CellExecuteUpdate, CellExecutionComplete } from '@theia/notebook/lib/browser'; +import { CellRange, isTextStreamMime } from '@theia/notebook/lib/common'; const SIDE_GROUP = -2; const ACTIVE_GROUP = -1; @@ -1627,123 +1626,3 @@ export namespace NotebookKernelSourceAction { }; } } - -export namespace NotebookDto { - - export function toNotebookOutputItemDto(item: CellOutputItem): rpc.NotebookOutputItemDto { - return { - mime: item.mime, - valueBytes: item.data - }; - } - - export function toNotebookOutputDto(output: CellOutput): rpc.NotebookOutputDto { - return { - outputId: output.outputId, - metadata: output.metadata, - items: output.outputs.map(toNotebookOutputItemDto) - }; - } - - export function toNotebookCellDataDto(cell: CellData): rpc.NotebookCellDataDto { - return { - cellKind: cell.cellKind, - language: cell.language, - source: cell.source, - internalMetadata: cell.internalMetadata, - metadata: cell.metadata, - outputs: cell.outputs.map(toNotebookOutputDto) - }; - } - - // export function toNotebookDataDto(data: NotebookData): rpc.NotebookDataDto { - // return { - // metadata: data.metadata, - // cells: data.cells.map(toNotebookCellDataDto) - // }; - // } - - export function fromNotebookOutputItemDto(item: rpc.NotebookOutputItemDto): CellOutputItem { - return { - mime: item.mime, - data: item.valueBytes - }; - } - - export function fromNotebookOutputDto(output: rpc.NotebookOutputDto): CellOutput { - return { - outputId: output.outputId, - metadata: output.metadata, - outputs: output.items.map(fromNotebookOutputItemDto) - }; - } - - export function fromNotebookCellDataDto(cell: rpc.NotebookCellDataDto): CellData { - return { - cellKind: cell.cellKind, - language: cell.language, - source: cell.source, - outputs: cell.outputs.map(fromNotebookOutputDto), - metadata: cell.metadata, - internalMetadata: cell.internalMetadata - }; - } - - // export function fromNotebookDataDto(data: rpc.NotebookDataDto): NotebookData { - // return { - // metadata: data.metadata, - // cells: data.cells.map(fromNotebookCellDataDto) - // }; - // } - - // export function toNotebookCellDto(cell: Cell): rpc.NotebookCellDto { - // return { - // handle: cell.handle, - // uri: cell.uri, - // source: cell.textBuffer.getLinesContent(), - // eol: cell.textBuffer.getEOL(), - // language: cell.language, - // cellKind: cell.cellKind, - // outputs: cell.outputs.map(toNotebookOutputDto), - // metadata: cell.metadata, - // internalMetadata: cell.internalMetadata, - // }; - // } - - export function fromCellExecuteUpdateDto(data: rpc.CellExecuteUpdateDto): CellExecuteUpdate { - if (data.editType === CellExecutionUpdateType.Output) { - return { - editType: data.editType, - cellHandle: data.cellHandle, - append: data.append, - outputs: data.outputs.map(fromNotebookOutputDto) - }; - } else if (data.editType === CellExecutionUpdateType.OutputItems) { - return { - editType: data.editType, - append: data.append, - outputId: data.outputId, - items: data.items.map(fromNotebookOutputItemDto) - }; - } else { - return data; - } - } - - export function fromCellExecuteCompleteDto(data: rpc.CellExecutionCompleteDto): CellExecutionComplete { - return data; - } - - // export function fromCellEditOperationDto(edit: rpc.CellEditOperationDto): CellEditOperation { - // if (edit.editType === CellEditType.Replace) { - // return { - // editType: edit.editType, - // index: edit.index, - // count: edit.count, - // cells: edit.cells.map(fromNotebookCellDataDto) - // }; - // } else { - // return edit; - // } - // } -}