-
Notifications
You must be signed in to change notification settings - Fork 59
Fix notebook preview #1003
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
Draft
seeM
wants to merge
20
commits into
main
Choose a base branch
from
fix/notebook-preview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Fix notebook preview #1003
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
56fc3ec
remove unused `includeVisible` arg
seeM 06a63c4
update `canPreviewDoc` to support `NotebookDocument`
seeM 36752ff
make `isNotebookUri` private
seeM f9af11c
remove version gaurds; 1.67 is 4 years old now
seeM cbf7f6e
rename `isNotebook` to `isNotebookCell`
seeM 4d63831
split quarto editor interfaces by type
seeM 897e759
extract notebookFrontMatterYaml
seeM 60b1b86
remove unused code
seeM 93feeca
set quarto notebook editor document to first cell doc
seeM 5b19c62
preview depends on editor instead of text doc
seeM d56732d
remove `isNotebookCell` and preview manager `previewDoc_`
seeM 9bd9387
extract `quartoEditor` module
seeM 0c6044e
`viewColumn` is only used by visual editor
seeM f110195
replace `QuartoNotebookEditor.notebook` with `notebookEditor`
seeM d7e5df6
extract `editorFrontMatterYaml`
seeM 23b69ef
extract `selectAndRevealRange`
seeM 3d0f273
`activate` notebooks as well
seeM 80511b5
move `selectAndRevealRange` to `QuartoEditor`
seeM 09badcc
add `QuartoEditor.preseveEditorFocus` and extract `activeQuartoEditor`
seeM d8d1730
add `QuartoEditor.frontMatterYaml`
seeM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,180 @@ | ||
| import * as vscode from "vscode"; | ||
| import { QuartoContext } from "quarto-core"; | ||
| import { MarkdownEngine } from "../markdown/engine"; | ||
| import { revealSlideIndex } from "../markdown/reveal"; | ||
| import { QuartoVisualEditor, VisualEditorProvider } from "../providers/editor/editor"; | ||
| import { notebookFrontMatterYaml } from "../markdown/notebook"; | ||
| import { documentFrontMatterYaml } from "../markdown/document"; | ||
|
|
||
| export type QuartoEditor = QuartoTextEditor | QuartoNotebookEditor | QuartoVisualEditor; | ||
|
|
||
| export interface QuartoEditorBase { | ||
| // TODO: Can we remove `document: TextDocument` from QuartoNotebookEditor? | ||
| // Many things use it for `uri` (easy) and `getText()` (harder). | ||
| document: vscode.TextDocument; | ||
| activate: () => Promise<void>; | ||
|
|
||
| /** | ||
| * Get the slide index for the current cursor position. | ||
| * | ||
| * This method is undefined for editors that don't yet support | ||
| * slide parsing e.g. `QuartoNotebookEditor`. | ||
| */ | ||
| slideIndex?: () => Promise<number>; | ||
|
|
||
| selectAndRevealRange: (range: vscode.Range) => void; | ||
|
|
||
| preserveEditorFocus(): void; | ||
|
|
||
| frontMatterYaml(engine: MarkdownEngine): string; | ||
| } | ||
|
|
||
| export interface QuartoTextEditor extends QuartoEditorBase { | ||
| type: 'text'; | ||
| textEditor: vscode.TextEditor; | ||
| } | ||
|
|
||
| export interface QuartoNotebookEditor extends QuartoEditorBase { | ||
| type: 'notebook'; | ||
| notebookEditor: vscode.NotebookEditor; | ||
| } | ||
|
|
||
| export function isQuartoNotebookEditor(editor: QuartoEditor): editor is QuartoNotebookEditor { | ||
| return editor.type === 'notebook'; | ||
| } | ||
|
|
||
| export function isQuartoTextEditor(editor: QuartoEditor): editor is QuartoTextEditor { | ||
| return editor.type === 'text'; | ||
| } | ||
|
|
||
| export function isQuartoVisualEditor(editor: QuartoEditor): editor is QuartoVisualEditor { | ||
| return editor.type === 'visual'; | ||
| } | ||
|
|
||
| export function findQuartoEditor( | ||
| engine: MarkdownEngine, | ||
| context: QuartoContext, | ||
| filter: (doc: vscode.TextDocument | vscode.NotebookDocument) => boolean = () => true, | ||
| ): QuartoEditor | undefined { | ||
| const activeEditor = activeQuartoEditor(filter, engine, context); | ||
| if (activeEditor) { | ||
| return activeEditor; | ||
| } | ||
|
|
||
| // visible visual editor (sometime it loses track of 'active' so we need to use 'visible') | ||
| const visibleVisualEditor = VisualEditorProvider.activeEditor(true); | ||
| if (visibleVisualEditor && filter(visibleVisualEditor.document)) { | ||
| return visibleVisualEditor; | ||
| } | ||
|
|
||
| // visible text editors | ||
| const visibleEditor = vscode.window.visibleTextEditors.find((editor) => filter(editor.document)); | ||
| if (visibleEditor) { | ||
| return quartoTextEditor(visibleEditor, engine, context); | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| export function activeQuartoEditor( | ||
| filter: (doc: vscode.TextDocument | vscode.NotebookDocument) => boolean = () => true, | ||
| engine?: MarkdownEngine, | ||
| context?: QuartoContext | ||
| ): QuartoEditor | undefined { | ||
| // first check for an active visual editor | ||
| const activeVisualEditor = VisualEditorProvider.activeEditor(); | ||
| if (activeVisualEditor && filter(activeVisualEditor.document)) { | ||
| return activeVisualEditor; | ||
| } | ||
|
|
||
| // then check for active notebook editor | ||
| const notebookEditor = vscode.window.activeNotebookEditor; | ||
| if (notebookEditor && filter(notebookEditor.notebook)) { | ||
| return quartoNotebookEditor(notebookEditor); | ||
| } | ||
|
|
||
| // active text editor | ||
| const textEditor = vscode.window.activeTextEditor; | ||
| if (textEditor && filter(textEditor.document)) { | ||
| return quartoTextEditor(textEditor, engine, context); | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| export function quartoTextEditor( | ||
| editor: vscode.TextEditor, | ||
| engine?: MarkdownEngine, | ||
| context?: QuartoContext): QuartoTextEditor { | ||
| const activate = async () => { | ||
| await vscode.window.showTextDocument( | ||
| editor.document, | ||
| editor.viewColumn, | ||
| false | ||
| ); | ||
| }; | ||
|
|
||
| return { | ||
| type: 'text', | ||
| document: editor.document, | ||
| activate, | ||
| slideIndex: async () => { | ||
| if (engine && context) { | ||
| return await revealSlideIndex( | ||
| editor.selection.active, | ||
| editor.document, | ||
| engine, | ||
| context | ||
| ); | ||
| } else { | ||
| return 0; | ||
| } | ||
| }, | ||
| selectAndRevealRange: (range: vscode.Range) => { | ||
| // if the current selection is outside of the error region then | ||
| // navigate to the top of the error region | ||
| if ( | ||
| editor.selection.active.isBefore(range.start) || | ||
| editor.selection.active.isAfter(range.end) | ||
| ) { | ||
| editor.selection = new vscode.Selection(range.start, range.start); | ||
| editor.revealRange( | ||
| range, | ||
| vscode.TextEditorRevealType.InCenterIfOutsideViewport | ||
| ); | ||
| } | ||
| }, | ||
| preserveEditorFocus: () => { | ||
| setTimeout(() => { | ||
| activate(); | ||
| }, 200); | ||
| }, | ||
| frontMatterYaml: (engine) => documentFrontMatterYaml(engine, editor.document), | ||
| textEditor: editor, | ||
| }; | ||
| } | ||
|
|
||
| function quartoNotebookEditor( | ||
| notebookEditor: vscode.NotebookEditor, | ||
| ): QuartoNotebookEditor { | ||
| // TODO: Why is cellAt always defined?... | ||
| const firstCellDoc = notebookEditor.notebook.cellAt(0)?.document; | ||
| return { | ||
| type: 'notebook', | ||
| document: firstCellDoc, | ||
| activate: async () => { | ||
| await vscode.window.showNotebookDocument( | ||
| notebookEditor.notebook, | ||
| { preserveFocus: false } | ||
| ); | ||
| }, | ||
| selectAndRevealRange: () => { | ||
| // Not implemented yet. | ||
| }, | ||
| preserveEditorFocus: () => { | ||
| // Not implemented yet. | ||
| }, | ||
| frontMatterYaml: () => notebookFrontMatterYaml(notebookEditor.notebook), | ||
| notebookEditor, | ||
| }; | ||
| } |
This file contains hidden or 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 hidden or 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,5 @@ | ||
| import * as vscode from "vscode"; | ||
|
|
||
| export function notebookFrontMatterYaml(notebook: vscode.NotebookDocument): string { | ||
| return notebook.cellAt(0)?.document.getText() || ""; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: prefer
dog !== undefinedover!!docfor messiness of truthiness reasonsCould you add a comment explaining why
'notebookType' in docis the thing to check forisNotebook? Is notebook that same as ipynb? If we're changingisNotebookUritoisIpynbUri, should we also changeisNotebooktoisIpynb?