Skip to content

Commit 6824712

Browse files
committed
track editor associations
1 parent d9bea7d commit 6824712

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

apps/vscode/src/main.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ 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 { setEditorOpener } from "./providers/editor/toggle";
39-
import { hasHooks } from "./host/hooks";
38+
import { defaultEditorOpener } from "./providers/editor/configurations"
4039

4140
export async function activate(context: vscode.ExtensionContext) {
4241

@@ -128,16 +127,9 @@ export async function activate(context: vscode.ExtensionContext) {
128127
// activate providers common to browser/node
129128
activateCommon(context, host, engine, commands);
130129

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

143135
export async function deactivate() {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* configurations.ts
3+
*
4+
* Copyright (C) 2022 by Posit Software, PBC
5+
*
6+
* Unless you have received this program directly from Posit Software pursuant
7+
* to the terms of a commercial license agreement with Posit Software, then
8+
* this program is licensed to you under the terms of version 3 of the
9+
* GNU Affero General Public License. This program is distributed WITHOUT
10+
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11+
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12+
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13+
*
14+
*/
15+
16+
import * as vscode from "vscode";
17+
18+
export function defaultEditorOpener(): string | undefined {
19+
const target = 'workbench.editorAssociations';
20+
21+
const inspectType = vscode.workspace.getConfiguration(undefined).inspect(target)?.globalValue;
22+
if (!inspectType) { return "textEditor"; }
23+
24+
if (inspectType.hasOwnProperty("*.qmd")) {
25+
return inspectType["*.qmd"]
26+
}
27+
return "textEditor"
28+
}

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
1313
*
1414
*/
15-
import * as vscode from "vscode";
15+
1616
import { commands, window, workspace, TextDocument, ViewColumn } from "vscode";
17-
import * as path from 'path';
1817
import * as quarto from "quarto-core";
19-
import fs from "node:fs";
20-
import yaml from "js-yaml";
2118
import { Command } from "../../core/command";
2219
import { isQuartoDoc, kQuartoLanguageId } from "../../core/doc";
2320
import { VisualEditorProvider } from "./editor";
@@ -27,10 +24,10 @@ export function determineMode(doc: TextDocument): string | undefined {
2724
const text = doc.getText();
2825
// check if file itself has a mode
2926
if (hasEditorMode(text, "source")) {
30-
editorOpener = "source";
27+
editorOpener = "textEditor";
3128
}
3229
else if (hasEditorMode(text, "visual")) {
33-
editorOpener = "visual";
30+
editorOpener = VisualEditorProvider.viewType;
3431
}
3532
// check if has a _quarto.yml or _quarto.yaml file with editor specified
3633
else {
@@ -40,16 +37,6 @@ export function determineMode(doc: TextDocument): string | undefined {
4037
return editorOpener;
4138
}
4239

43-
export async function setEditorOpener() {
44-
const config = vscode.workspace.getConfiguration('quarto').get<string>('defaultEditor');
45-
const viewType = config === 'visual' ? VisualEditorProvider.viewType : 'textEditor';
46-
47-
await vscode.commands.executeCommand("workbench.action.setDefaultEditor",
48-
'*.qmd',
49-
viewType
50-
);
51-
}
52-
5340
export function modeFromQuartoYaml(doc: TextDocument): string | undefined {
5441
const metadataFiles = quarto.metadataFilesForDocument(doc.uri.fsPath);
5542
if (!metadataFiles) {
@@ -58,8 +45,11 @@ export function modeFromQuartoYaml(doc: TextDocument): string | undefined {
5845
if (metadataFiles) {
5946
for (const metadataFile of metadataFiles) {
6047
const yamlText = quarto.yamlFromMetadataFile(metadataFile);
61-
if (yamlText?.editor === "source" || yamlText?.editor === "visual") {
62-
return yamlText?.editor;
48+
if (yamlText?.editor === "source") {
49+
return "textEditor"
50+
}
51+
if (yamlText?.editor === "visual") {
52+
return VisualEditorProvider.viewType;
6353
}
6454
}
6555
}

0 commit comments

Comments
 (0)