Skip to content

Commit ed97a28

Browse files
committed
clean up for clarity
1 parent 8fbcbfc commit ed97a28

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

apps/vscode/src/main.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ import { extensionHost } from "./host";
3636
import { configuredQuartoPath } from "./core/quarto";
3737
import { activateDenoConfig } from "./providers/deno-config";
3838
import { determineMode, setEditorOpener } from "./providers/editor/toggle";
39-
import { URI } from "vscode-languageserver-types";
40-
import { config } from "vscode-nls";
4139

4240
let suppressOpenHandler = false;
4341

@@ -139,24 +137,34 @@ export async function activate(context: vscode.ExtensionContext) {
139137
setEditorOpener();
140138
}
141139
});
142-
// end if positron
143140

144141
const documentOpenHandler = vscode.workspace.onDidOpenTextDocument(async (document: vscode.TextDocument) => {
145142
// Check if the document language is "quarto"
146143
const config = vscode.workspace.getConfiguration('quarto').get<string>('defaultEditor');
144+
if (document.languageId != 'quarto') {
145+
return;
146+
}
147147
if (suppressOpenHandler) {
148148
suppressOpenHandler = false; // Reset the flag
149-
return; // Prevent further execution
149+
return;
150150
}
151-
if (document.languageId === 'quarto') {
152-
suppressOpenHandler = await determineMode(document, config);
153-
151+
const editorMode = await determineMode(document.getText());
152+
if (editorMode && editorMode != config) {
153+
suppressOpenHandler = true;
154+
const editorOpener = editorMode === 'visual' ? VisualEditorProvider.viewType : 'textEditor';
155+
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
156+
await vscode.commands.executeCommand("vscode.openWith",
157+
document.uri,
158+
editorOpener
159+
);
160+
return;
154161
}
155162
});
156163

157164
// Add the event handler to the context subscriptions
158165
context.subscriptions.push(documentOpenHandler);
159166

167+
// end if positron
160168
}
161169

162170
export async function deactivate() {

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,31 @@
1414
*/
1515
import * as vscode from "vscode";
1616
import { commands, window, workspace, TextDocument, ViewColumn } from "vscode";
17-
import { Command } from "../../core/command";
18-
import { isQuartoDoc, kQuartoLanguageId } from "../../core/doc";
17+
import * as path from 'path';
1918
import * as quarto from "quarto-core";
2019
import fs from "node:fs";
21-
import * as path from 'path';
2220
import yaml from "js-yaml";
21+
import { Command } from "../../core/command";
22+
import { isQuartoDoc, kQuartoLanguageId } from "../../core/doc";
2323
import { VisualEditorProvider } from "./editor";
2424

2525

26-
export async function determineMode(doc: TextDocument, config: string | undefined): Promise<boolean> {
27-
const text = doc.getText()
28-
26+
export async function determineMode(doc: string): Promise<string | undefined> {
2927
let editorOpener = undefined;
3028

3129
// check if file itself has a mode
32-
if (hasEditorMode(text, "source")) {
30+
if (hasEditorMode(doc, "source")) {
3331
editorOpener = "source";
3432
}
35-
else if (hasEditorMode(text, "visual")) {
33+
else if (hasEditorMode(doc, "visual")) {
3634
editorOpener = "visual";
3735
}
3836
// check if has a _quarto.yml or _quarto.yaml file with editor specified
3937
else {
4038
editorOpener = workspaceHasQuartoYaml();
4139
}
42-
if (editorOpener && editorOpener != config) {
43-
editorOpener = editorOpener === 'visual' ? VisualEditorProvider.viewType : 'textEditor';
44-
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
45-
await vscode.commands.executeCommand("vscode.openWith",
46-
doc.uri,
47-
editorOpener
48-
);
49-
return true;
50-
}
5140

52-
return false;
41+
return editorOpener;
5342
}
5443

5544
export async function setEditorOpener() {

0 commit comments

Comments
 (0)