Markdown document links should reveal cell in notebook (#153147)

Fixes #141024
This commit is contained in:
Matt Bierner 2022-06-24 12:49:15 -07:00 committed by GitHub
parent 45427b2346
commit 00ad6bc3d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 10 deletions

View file

@ -77,21 +77,25 @@ export class TableOfContents {
.find(notebook => notebook.getCells().some(cell => cell.document === document));
if (notebook) {
const entries: TocEntry[] = [];
for (const cell of notebook.getCells()) {
if (cell.kind === vscode.NotebookCellKind.Markup && isMarkdownFile(cell.document)) {
entries.push(...(await this.buildToc(parser, cell.document)));
}
}
return new TableOfContents(entries, parser.slugifier);
return TableOfContents.createForNotebook(parser, notebook);
}
}
return this.create(parser, document);
}
public static async createForNotebook(parser: IMdParser, notebook: vscode.NotebookDocument): Promise<TableOfContents> {
const entries: TocEntry[] = [];
for (const cell of notebook.getCells()) {
if (cell.kind === vscode.NotebookCellKind.Markup && isMarkdownFile(cell.document)) {
entries.push(...(await this.buildToc(parser, cell.document)));
}
}
return new TableOfContents(entries, parser.slugifier);
}
private static async buildToc(parser: IMdParser, document: ITextDocument): Promise<TocEntry[]> {
const toc: TocEntry[] = [];
const tokens = await parser.tokenize(document);
@ -184,7 +188,7 @@ export class MdTableOfContentsProvider extends Disposable {
private readonly _cache: MdDocumentInfoCache<TableOfContents>;
constructor(
parser: IMdParser,
private readonly parser: IMdParser,
workspace: IMdWorkspace,
private readonly logger: ILogger,
) {
@ -202,4 +206,8 @@ export class MdTableOfContentsProvider extends Disposable {
public getForDocument(doc: ITextDocument): Promise<TableOfContents> {
return this._cache.getForDocument(doc);
}
public createForNotebook(notebook: vscode.NotebookDocument): Promise<TableOfContents> {
return TableOfContents.createForNotebook(this.parser, notebook);
}
}

View file

@ -77,6 +77,13 @@ async function tryOpenMdFile(tocProvider: MdTableOfContentsProvider, resource: v
}
async function tryNavigateToFragmentInActiveEditor(tocProvider: MdTableOfContentsProvider, resource: vscode.Uri): Promise<boolean> {
const notebookEditor = vscode.window.activeNotebookEditor;
if (notebookEditor?.notebook.uri.fsPath === resource.fsPath) {
if (await tryRevealLineInNotebook(tocProvider, notebookEditor, resource.fragment)) {
return true;
}
}
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor?.document.uri.fsPath === resource.fsPath) {
if (isMarkdownFile(activeEditor.document)) {
@ -87,6 +94,7 @@ async function tryNavigateToFragmentInActiveEditor(tocProvider: MdTableOfContent
tryRevealLineUsingLineFragment(activeEditor, resource.fragment);
return true;
}
return false;
}
@ -102,6 +110,24 @@ function getViewColumn(resource: vscode.Uri): vscode.ViewColumn {
}
}
async function tryRevealLineInNotebook(tocProvider: MdTableOfContentsProvider, editor: vscode.NotebookEditor, fragment: string): Promise<boolean> {
const toc = await tocProvider.createForNotebook(editor.notebook);
const entry = toc.lookup(fragment);
if (!entry) {
return false;
}
const cell = editor.notebook.getCells().find(cell => cell.document.uri.toString() === entry.sectionLocation.uri.toString());
if (!cell) {
return false;
}
const range = new vscode.NotebookRange(cell.index, cell.index);
editor.selection = range;
editor.revealRange(range);
return true;
}
async function tryRevealLineUsingTocFragment(tocProvider: MdTableOfContentsProvider, editor: vscode.TextEditor, fragment: string): Promise<boolean> {
const toc = await tocProvider.getForDocument(editor.document);
const entry = toc.lookup(fragment);