Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed storing of the notebook-outlineview state data #13648

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

import { codicon, LabelProvider, LabelProviderContribution } from '@theia/core/lib/browser';
import { inject, injectable } from '@theia/core/shared/inversify';
import { CellKind } from '../../common';
import { CellKind, CellUri } from '../../common';
import { NotebookService } from '../service/notebook-service';
import { NotebookCellOutlineNode } from './notebook-outline-contribution';
import type Token = require('markdown-it/lib/token');
import markdownit = require('@theia/core/shared/markdown-it');
import { NotebookCellModel } from '../view-model/notebook-cell-model';
import { URI } from '@theia/core';

@injectable()
export class NotebookLabelProviderContribution implements LabelProviderContribution {
Expand All @@ -41,23 +43,43 @@ export class NotebookLabelProviderContribution implements LabelProviderContribut
}

getIcon(element: NotebookCellOutlineNode): string {
return element.notebookCell.cellKind === CellKind.Markup ? codicon('markdown') : codicon('code');
const cell = this.findCellByUri(element.uri.toString());
if (cell) {
return cell.cellKind === CellKind.Markup ? codicon('markdown') : codicon('code');
}
return '';
}

getName(element: NotebookCellOutlineNode): string {
return element.notebookCell.cellKind === CellKind.Code ?
element.notebookCell.text.split('\n')[0] :
this.extractPlaintext(this.markdownIt.parse(element.notebookCell.text.split('\n')[0], {}));
const cell = this.findCellByUri(element.uri.toString());
if (cell) {
return cell.cellKind === CellKind.Code ?
cell.text.split('\n')[0] :
this.extractPlaintext(this.markdownIt.parse(cell.text.split('\n')[0], {}));
}
return '';
}

getLongName(element: NotebookCellOutlineNode): string {
return element.notebookCell.cellKind === CellKind.Code ?
element.notebookCell.text.split('\n')[0] :
this.extractPlaintext(this.markdownIt.parse(element.notebookCell.text.split('\n')[0], {}));
const cell = this.findCellByUri(element.uri);
if (cell) {
return cell.cellKind === CellKind.Code ?
cell.text.split('\n')[0] :
this.extractPlaintext(this.markdownIt.parse(cell.text.split('\n')[0], {}));
}
return '';
}

extractPlaintext(parsedMarkdown: Token[]): string {
return parsedMarkdown.map(token => token.children ? this.extractPlaintext(token.children) : token.content).join('');
}

findCellByUri(uri: string): NotebookCellModel | undefined {
const parsed = CellUri.parse(new URI(uri));
if (parsed) {
return this.notebookService.getNotebookEditorModel(parsed.notebook)?.cells.find(cell => cell.handle === parsed?.handle);
}
return undefined;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ import { OutlineViewService } from '@theia/outline-view/lib/browser/outline-view
import { NotebookModel } from '../view-model/notebook-model';
import { OutlineSymbolInformationNode } from '@theia/outline-view/lib/browser/outline-view-widget';
import { NotebookEditorWidget } from '../notebook-editor-widget';
import { NotebookCellModel } from '../view-model/notebook-cell-model';
import { DisposableCollection, URI } from '@theia/core';
import { CellKind, CellUri } from '../../common';
import { NotebookService } from '../service/notebook-service';
export interface NotebookCellOutlineNode extends OutlineSymbolInformationNode {
notebookCell: NotebookCellModel;
uri: URI;
uri: string;
jonah-iden marked this conversation as resolved.
Show resolved Hide resolved
}

export namespace NotebookCellOutlineNode {
export function is(element: object): element is NotebookCellOutlineNode {
return TreeNode.is(element) && OutlineSymbolInformationNode.is(element) && 'notebookCell' in element;
return TreeNode.is(element) && OutlineSymbolInformationNode.is(element) && 'uri' in element && (element as NotebookCellOutlineNode).uri.startsWith(CellUri.cellUriScheme);
jonah-iden marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -94,17 +92,17 @@ export class NotebookOutlineContribution implements FrontendApplicationContribut
children: [],
selected: model.selectedCell === cell,
expanded: false,
notebookCell: cell,
uri: model.uri,
uri: cell.uri.toString(),
} as NotebookCellOutlineNode));
}

selectCell(node: object): void {
if (NotebookCellOutlineNode.is(node)) {
const parsed = CellUri.parse(node.notebookCell.uri);
const parsed = CellUri.parse(new URI(node.uri));
const model = parsed && this.notebookService.getNotebookEditorModel(parsed.notebook);
if (model) {
model.setSelectedCell(node.notebookCell);
const cell = model?.cells.find(c => c.handle === parsed?.handle);
if (model && cell) {
model.setSelectedCell(cell);
}
}
}
Expand Down
Loading