Skip to content

Commit 88c00c5

Browse files
committed
only have one editor listener
1 parent 5d88ec7 commit 88c00c5

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

apps/vscode/src/main.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ import { activateQuartoAssistPanel } from "./providers/assist/panel";
2424
import { activateCommon } from "./extension";
2525
import { activatePreview } from "./providers/preview/preview";
2626
import { activateRender } from "./providers/render";
27-
import { initQuartoContext, quartoContextUnavailable } from "quarto-core";
27+
import { initQuartoContext } from "quarto-core";
2828
import { activateStatusBar } from "./providers/statusbar";
2929
import { walkthroughCommands } from "./providers/walkthrough";
3030
import { activateLuaTypes } from "./providers/lua-types";
3131
import { activateCreate } from "./providers/create/create";
32-
import { activateEditor, VisualEditorProvider } from "./providers/editor/editor";
32+
import { activateEditor } from "./providers/editor/editor";
3333
import { activateCopyFiles } from "./providers/copyfiles";
3434
import { activateZotero } from "./providers/zotero/zotero";;
3535
import { extensionHost } from "./host";
3636
import { configuredQuartoPath } from "./core/quarto";
3737
import { activateDenoConfig } from "./providers/deno-config";
38-
import { modeFromQuartoYaml, setEditorOpener } from "./providers/editor/toggle";
38+
import { setEditorOpener } from "./providers/editor/toggle";
39+
import { hasHooks } from "./host/hooks";
3940

4041
export async function activate(context: vscode.ExtensionContext) {
4142

@@ -127,16 +128,17 @@ export async function activate(context: vscode.ExtensionContext) {
127128
// activate providers common to browser/node
128129
activateCommon(context, host, engine, commands);
129130

130-
// if positron
131-
setEditorOpener();
132131

133-
const defaultEditor = vscode.workspace.onDidChangeConfiguration(async (event) => {
134-
if (event.affectsConfiguration('quarto.defaultEditor')) {
135-
setEditorOpener();
136-
}
137-
});
138-
context.subscriptions.push(defaultEditor);
139-
// end if positron
132+
if (hasHooks()) {
133+
// Positron allows user to set visual or source as default mode
134+
setEditorOpener();
135+
const defaultEditor = vscode.workspace.onDidChangeConfiguration(async (event) => {
136+
if (event.affectsConfiguration('quarto.defaultEditor')) {
137+
setEditorOpener();
138+
}
139+
});
140+
context.subscriptions.push(defaultEditor);
141+
}
140142
}
141143

142144
export async function deactivate() {

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function activateEditor(
7878
quartoContext: QuartoContext,
7979
lspClient: LanguageClient,
8080
engine: MarkdownEngine
81-
) : Command[] {
81+
): Command[] {
8282
// register the provider
8383
context.subscriptions.push(VisualEditorProvider.register(context, host, quartoContext, lspClient, engine));
8484

@@ -114,30 +114,10 @@ export class VisualEditorProvider implements CustomTextEditorProvider {
114114
quartoContext: QuartoContext,
115115
lspClient: LanguageClient,
116116
engine: MarkdownEngine
117-
) : Disposable {
117+
): Disposable {
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-
}));
141121

142122
// track edits in the active editor if its untitled. this enables us to recover the
143123
// content when we switch to an untitled document, which otherwise are just dropped
@@ -160,7 +140,7 @@ export class VisualEditorProvider implements CustomTextEditorProvider {
160140
}));
161141

162142
// when the active editor changes see if we have a visual editor position for it
163-
context.subscriptions.push(window.onDidChangeActiveTextEditor(debounce(() => {
143+
context.subscriptions.push(window.onDidChangeActiveTextEditor(debounce(async () => {
164144
// resolve active editor
165145
const editor = window.activeTextEditor;
166146
if (!editor) {
@@ -171,6 +151,20 @@ export class VisualEditorProvider implements CustomTextEditorProvider {
171151
const uri = document.uri.toString();
172152
// check for switch (one shot)
173153
const isSwitch = this.visualEditorPendingSwitchToSource.has(uri);
154+
155+
// see if user has specified visual or source mode
156+
const config = workspace.getConfiguration('quarto').get<string>('defaultEditor');
157+
const editorMode = await determineMode(document);
158+
if (editorMode && editorMode != config && !isSwitch) {
159+
const editorOpener = editorMode === 'visual' ? VisualEditorProvider.viewType : 'textEditor';
160+
await commands.executeCommand('workbench.action.closeActiveEditor');
161+
await commands.executeCommand("vscode.openWith",
162+
document.uri,
163+
editorOpener
164+
);
165+
return;
166+
}
167+
174168
this.visualEditorPendingSwitchToSource.delete(uri);
175169

176170
// check for pos (one shot)

0 commit comments

Comments
 (0)