Skip to content

Commit 5d88ec7

Browse files
committed
move listener to VisualEditorProvider
1 parent d2d8bcd commit 5d88ec7

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

apps/vscode/src/providers/editor/editor.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
*/
1515

1616
import path, { extname } from "path";
17-
17+
import { determineMode } from "./toggle"
1818
import debounce from "lodash.debounce";
1919

20-
import {
20+
import {
2121
window,
22-
workspace,
23-
ExtensionContext,
24-
Disposable,
25-
CustomTextEditorProvider,
26-
TextDocument,
27-
WebviewPanel,
28-
CancellationToken,
29-
Uri,
22+
workspace,
23+
ExtensionContext,
24+
Disposable,
25+
CustomTextEditorProvider,
26+
TextDocument,
27+
WebviewPanel,
28+
CancellationToken,
29+
Uri,
3030
Webview,
3131
Range,
3232
env,
@@ -118,6 +118,26 @@ export class VisualEditorProvider implements CustomTextEditorProvider {
118118

119119
// setup request transport
120120
const lspRequest = lspClientTransport(lspClient);
121+
context.subscriptions.push(window.onDidChangeActiveTextEditor(async () => {
122+
// Check if the document language is "quarto"
123+
124+
const document = window.activeTextEditor?.document
125+
if (!document || document.languageId != 'quarto') {
126+
return;
127+
}
128+
const config = workspace.getConfiguration('quarto').get<string>('defaultEditor');
129+
130+
const editorMode = await determineMode(document);
131+
if (editorMode && editorMode != config) {
132+
const editorOpener = editorMode === 'visual' ? VisualEditorProvider.viewType : 'textEditor';
133+
await commands.executeCommand('workbench.action.closeActiveEditor');
134+
await commands.executeCommand("vscode.openWith",
135+
document.uri,
136+
editorOpener
137+
);
138+
return;
139+
}
140+
}));
121141

122142
// track edits in the active editor if its untitled. this enables us to recover the
123143
// content when we switch to an untitled document, which otherwise are just dropped
@@ -149,7 +169,6 @@ export class VisualEditorProvider implements CustomTextEditorProvider {
149169
const document = editor.document;
150170
if (document && isQuartoDoc(document)) {
151171
const uri = document.uri.toString();
152-
153172
// check for switch (one shot)
154173
const isSwitch = this.visualEditorPendingSwitchToSource.has(uri);
155174
this.visualEditorPendingSwitchToSource.delete(uri);

0 commit comments

Comments
 (0)