-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notebook Outline-View and Breadcrumbs. (#13562)
* notbook outline and breadcrumbs + jupyter show Table of contents menu item working Signed-off-by: Jonah Iden <[email protected]> * some basic review comment changes Signed-off-by: Jonah Iden <[email protected]> * parse markdown cell content as plain text Signed-off-by: Jonah Iden <[email protected]> * fixed lint Signed-off-by: Jonah Iden <[email protected]> * import type instead of class Co-authored-by: Mark Sujew <[email protected]> --------- Signed-off-by: Jonah Iden <[email protected]> Co-authored-by: Mark Sujew <[email protected]>
- Loading branch information
1 parent
09051c4
commit 52ab029
Showing
11 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/notebook/src/browser/contributions/notebook-label-provider-contribution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 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 { codicon, LabelProvider, LabelProviderContribution } from '@theia/core/lib/browser'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { CellKind } 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'); | ||
|
||
@injectable() | ||
export class NotebookLabelProviderContribution implements LabelProviderContribution { | ||
|
||
@inject(NotebookService) | ||
protected readonly notebookService: NotebookService; | ||
|
||
@inject(LabelProvider) | ||
protected readonly labelProvider: LabelProvider; | ||
|
||
protected markdownIt = markdownit(); | ||
|
||
canHandle(element: object): number { | ||
if (NotebookCellOutlineNode.is(element)) { | ||
return 200; | ||
} | ||
return 0; | ||
} | ||
|
||
getIcon(element: NotebookCellOutlineNode): string { | ||
return element.notebookCell.cellKind === CellKind.Markup ? codicon('markdown') : codicon('code'); | ||
} | ||
|
||
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], {})); | ||
} | ||
|
||
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], {})); | ||
} | ||
|
||
extractPlaintext(parsedMarkdown: Token[]): string { | ||
return parsedMarkdown.map(token => token.children ? this.extractPlaintext(token.children) : token.content).join(''); | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
packages/notebook/src/browser/contributions/notebook-outline-contribution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 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 { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { codicon, FrontendApplicationContribution, LabelProvider, TreeNode } from '@theia/core/lib/browser'; | ||
import { NotebookEditorWidgetService } from '../service/notebook-editor-widget-service'; | ||
import { OutlineViewService } from '@theia/outline-view/lib/browser/outline-view-service'; | ||
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; | ||
} | ||
|
||
export namespace NotebookCellOutlineNode { | ||
export function is(element: object): element is NotebookCellOutlineNode { | ||
return TreeNode.is(element) && OutlineSymbolInformationNode.is(element) && 'notebookCell' in element; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class NotebookOutlineContribution implements FrontendApplicationContribution { | ||
|
||
@inject(NotebookEditorWidgetService) | ||
protected readonly notebookEditorWidgetService: NotebookEditorWidgetService; | ||
|
||
@inject(OutlineViewService) | ||
protected readonly outlineViewService: OutlineViewService; | ||
|
||
@inject(LabelProvider) | ||
protected readonly labelProvider: LabelProvider; | ||
|
||
@inject(NotebookService) | ||
protected readonly notebookService: NotebookService; | ||
|
||
protected currentEditor?: NotebookEditorWidget; | ||
|
||
protected editorListeners: DisposableCollection = new DisposableCollection(); | ||
protected editorModelListeners: DisposableCollection = new DisposableCollection(); | ||
|
||
onStart(): void { | ||
this.notebookEditorWidgetService.onDidChangeFocusedEditor(editor => this.updateOutline(editor)); | ||
|
||
this.outlineViewService.onDidSelect(node => this.selectCell(node)); | ||
this.outlineViewService.onDidTapNode(node => this.selectCell(node)); | ||
} | ||
|
||
protected async updateOutline(editor: NotebookEditorWidget | undefined): Promise<void> { | ||
if (editor && !editor.isDisposed) { | ||
await editor.ready; | ||
this.currentEditor = editor; | ||
this.editorListeners.dispose(); | ||
this.editorListeners.push(editor.onDidChangeVisibility(() => { | ||
if (this.currentEditor === editor && !editor.isVisible) { | ||
this.outlineViewService.publish([]); | ||
} | ||
})); | ||
if (editor.model) { | ||
this.editorModelListeners.dispose(); | ||
this.editorModelListeners.push(editor.model.onDidChangeSelectedCell(() => { | ||
if (editor === this.currentEditor) { | ||
this.updateOutline(editor); | ||
} | ||
})); | ||
const roots = editor && editor.model && await this.createRoots(editor.model); | ||
this.outlineViewService.publish(roots || []); | ||
} | ||
} | ||
} | ||
|
||
protected async createRoots(model: NotebookModel): Promise<OutlineSymbolInformationNode[] | undefined> { | ||
return model.cells.map(cell => ({ | ||
id: cell.uri.toString(), | ||
iconClass: cell.cellKind === CellKind.Markup ? codicon('markdown') : codicon('code'), | ||
parent: undefined, | ||
children: [], | ||
selected: model.selectedCell === cell, | ||
expanded: false, | ||
notebookCell: cell, | ||
uri: model.uri, | ||
} as NotebookCellOutlineNode)); | ||
} | ||
|
||
selectCell(node: object): void { | ||
if (NotebookCellOutlineNode.is(node)) { | ||
const parsed = CellUri.parse(node.notebookCell.uri); | ||
const model = parsed && this.notebookService.getNotebookEditorModel(parsed.notebook); | ||
if (model) { | ||
model.setSelectedCell(node.notebookCell); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ | |
}, | ||
{ | ||
"path": "../monaco" | ||
}, | ||
{ | ||
"path": "../outline-view" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,9 @@ | |
{ | ||
"path": "../navigator" | ||
}, | ||
{ | ||
"path": "../outline-view" | ||
}, | ||
{ | ||
"path": "../plugin" | ||
}, | ||
|