Skip to content
Open
Changes from all commits
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
87 changes: 60 additions & 27 deletions apps/vscode/src/core/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function preserveEditorFocus(editor?: QuartoEditor) {
editor =
editor ||
(vscode.window.activeTextEditor
? quartoEditor(vscode.window.activeTextEditor)
? quartoTextEditor(vscode.window.activeTextEditor)
: undefined);
if (editor) {
if (!isNotebook(editor?.document)) {
Expand Down Expand Up @@ -217,47 +217,53 @@ export function findQuartoEditor(
| vscode.NotebookDocument
| undefined;
if (notebookDocument) {
const textEditor = vscode.window.visibleTextEditors.find((editor) => {
return editor.document.uri.fsPath.includes(notebookDocument.uri.fsPath);
});
if (textEditor && filter(textEditor.document)) {
return quartoEditor(textEditor, engine, context, notebookDocument);
const firstCellDocument = notebookDocument.cellAt(0)?.document;
if (firstCellDocument && filter(firstCellDocument)) {
return quartoNotebookEditor(notebookEditor, firstCellDocument);
}
}
}

// active text editor
const textEditor = vscode.window.activeTextEditor;
if (textEditor && filter(textEditor.document)) {
return quartoEditor(textEditor, engine, context);
// check visible text editors
} else if (includeVisible) {
// 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;
}
return quartoTextEditor(textEditor, engine, context);
}

// visible text editors
const visibleEditor = vscode.window.visibleTextEditors.find((editor) =>
filter(editor.document)
);
if (visibleEditor) {
return quartoEditor(visibleEditor, engine, context);
} else {
return undefined;
}
} else {
return undefined;
// check visible editors

// 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 notebook editors
const visibleNotebookEditor = vscode.window.visibleNotebookEditors.find((editor) =>
filter(editor.notebook.cellAt(0)?.document)
);
if (visibleNotebookEditor) {
const firstCellDocument = visibleNotebookEditor.notebook.cellAt(0).document;
return quartoNotebookEditor(visibleNotebookEditor, firstCellDocument);
}

// visible text editors
const visibleEditor = vscode.window.visibleTextEditors.find((editor) =>
filter(editor.document)
);
if (visibleEditor) {
return quartoTextEditor(visibleEditor, engine, context);
}

return undefined;
}

export function quartoEditor(
function quartoTextEditor(
editor: vscode.TextEditor,
engine?: MarkdownEngine,
context?: QuartoContext,
notebook?: NotebookDocument
) {
): QuartoEditor {
return {
document: editor.document,
activate: async () => {
Expand Down Expand Up @@ -285,6 +291,33 @@ export function quartoEditor(
};
}

function quartoNotebookEditor(
editor: vscode.NotebookEditor,
firstCellDocument: vscode.TextDocument,
): QuartoEditor {
return {
document: firstCellDocument,
activate: async () => {
// TODO: This should probably use showNotebookDocument.
// And we could probably also activate() notebook editors
// in many places where we currently skip notebooks.
// We're leaving it as showTextDocument for now to minimize
// the number of changes in this PR focused on #1006.
await vscode.window.showTextDocument(
firstCellDocument,
editor.viewColumn,
false
);
},
slideIndex: async () => {
// Throwing is safe, since the only place slideIndex is called today
// skips notebooks.
throw new Error("slideIndex not supported for notebook editors");
},
notebook: editor.notebook,
};
}

async function tryResolveUriToQuartoDoc(
resource: vscode.Uri
): Promise<vscode.TextDocument | undefined> {
Expand Down
Loading