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
2 changed files with 44 additions and 10 deletions

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);