@@ -22,20 +22,19 @@ import { Command } from "../../core/command";
2222import { isQuartoDoc , kQuartoLanguageId } from "../../core/doc" ;
2323import { VisualEditorProvider } from "./editor" ;
2424
25-
26- export async function determineMode ( doc : string ) : Promise < string | undefined > {
25+ export function determineMode ( doc : TextDocument ) : string | undefined {
2726 let editorOpener = undefined ;
28-
27+ const text = doc . getText ( ) ;
2928 // check if file itself has a mode
30- if ( hasEditorMode ( doc , "source" ) ) {
29+ if ( hasEditorMode ( text , "source" ) ) {
3130 editorOpener = "source" ;
3231 }
33- else if ( hasEditorMode ( doc , "visual" ) ) {
32+ else if ( hasEditorMode ( text , "visual" ) ) {
3433 editorOpener = "visual" ;
3534 }
3635 // check if has a _quarto.yml or _quarto.yaml file with editor specified
3736 else {
38- editorOpener = workspaceHasQuartoYaml ( ) ;
37+ editorOpener = modeFromQuartoYaml ( doc ) ;
3938 }
4039
4140 return editorOpener ;
@@ -44,49 +43,38 @@ export async function determineMode(doc: string): Promise<string | undefined> {
4443export async function setEditorOpener ( ) {
4544 const config = vscode . workspace . getConfiguration ( 'quarto' ) . get < string > ( 'defaultEditor' ) ;
4645 const viewType = config === 'visual' ? VisualEditorProvider . viewType : 'textEditor' ;
47- vscode . workspace . getConfiguration ( 'workbench' ) . update ( 'editor.defaultView' , viewType , true ) ;
46+
4847 await vscode . commands . executeCommand ( "workbench.action.setDefaultEditor" ,
49- vscode . Uri . file ( 'filename .qmd') ,
48+ '* .qmd',
5049 viewType
5150 ) ;
5251}
5352
54- export function workspaceHasQuartoYaml ( ) {
55- const workspaceFolders = vscode . workspace . workspaceFolders ;
56-
57- if ( workspaceFolders && workspaceFolders . length > 0 ) {
58- const rootPath = workspaceFolders [ 0 ] . uri . fsPath ; // Only look in the root directory of the first workspace folder
59-
60- const quartoFilePathYml = path . join ( rootPath , '_quarto.yml' ) ;
61- const quartoFilePathYaml = path . join ( rootPath , '_quarto.yaml' ) ;
62-
63- let fileContent : string | null = null ;
64-
65- if ( fs . existsSync ( quartoFilePathYml ) ) {
66- fileContent = fs . readFileSync ( quartoFilePathYml , 'utf8' ) ;
67- } else if ( fs . existsSync ( quartoFilePathYaml ) ) {
68- fileContent = fs . readFileSync ( quartoFilePathYaml , 'utf8' ) ;
69- }
70-
71- if ( fileContent ) {
72- const parsedYaml = yaml . load ( fileContent ) as any ;
73- if ( parsedYaml . editor === 'visual' || parsedYaml . editor === 'source' ) {
74- return parsedYaml . editor ;
53+ export function modeFromQuartoYaml ( doc : TextDocument ) : string | undefined {
54+ const metadataFiles = quarto . metadataFilesForDocument ( doc . uri . fsPath ) ;
55+ if ( ! metadataFiles ) {
56+ return undefined ;
57+ }
58+ if ( metadataFiles ) {
59+ for ( const metadataFile of metadataFiles ) {
60+ const yamlText = quarto . yamlFromMetadataFile ( metadataFile ) ;
61+ if ( yamlText ?. editor === "source" || yamlText ?. editor === "visual" ) {
62+ return yamlText ?. editor ;
7563 }
7664 }
7765 }
78-
7966 return undefined ;
8067}
8168
82- export function hasEditorMode ( doc : string , mode : string ) {
69+ export function hasEditorMode ( doc : string , mode : string ) : boolean {
70+
8371 if ( doc ) {
8472 const match = doc . match ( quarto . kRegExYAML ) ;
8573 if ( match ) {
8674 const yaml = match [ 0 ] ;
8775 return (
88- ! ! yaml . match ( new RegExp ( "^ editor:\\s+" + mode + "\\s*$" , "gm" ) ) ||
89- ! ! yaml . match ( new RegExp ( "^[ \\t]*" + mode + " :\\s*(default)? \\s*$", "gm" ) )
76+ ! ! yaml . match ( new RegExp ( "editor:\\s+" + mode + "\\s*$" , "gm" ) ) ||
77+ ! ! yaml . match ( new RegExp ( "^[ \\t]*" + "mode :\\s*" + mode + " \\s*$", "gm" ) )
9078 ) ;
9179 }
9280 }
0 commit comments