@@ -8,7 +8,7 @@ export function activate(context: vscode.ExtensionContext) {
8
8
9
9
// sets context to show "AsyncAPI Preview" button on Editor Title Bar
10
10
function setAsyncAPIPreviewContext ( document : vscode . TextDocument ) {
11
- const isAsyncAPI = ( document . languageId === 'yml' || document . languageId === 'yaml' ) && isAsyncAPIFile ( document . getText ( ) ) ;
11
+ const isAsyncAPI = isAsyncAPIFile ( document ) ;
12
12
console . log ( 'Setting context for asyncapi.isAsyncAPI' , isAsyncAPI , document . uri . fsPath ) ;
13
13
vscode . commands . executeCommand ( 'setContext' , 'asyncapi.isAsyncAPI' , isAsyncAPI ) ;
14
14
}
@@ -41,8 +41,22 @@ export function activate(context: vscode.ExtensionContext) {
41
41
context . subscriptions . push ( disposable ) ;
42
42
}
43
43
44
- function isAsyncAPIFile ( text : string ) {
45
- return text . includes ( 'asyncapi:' ) ;
44
+ function isAsyncAPIFile ( document ?: vscode . TextDocument ) {
45
+ if ( ! document ) {
46
+ return false ;
47
+ }
48
+ if ( document . languageId === 'json' ) {
49
+ try {
50
+ const json = JSON . parse ( document . getText ( ) ) ;
51
+ return json . asyncapi ;
52
+ } catch ( e ) {
53
+ return false ;
54
+ }
55
+ }
56
+ if ( document . languageId === 'yml' || document . languageId === 'yaml' ) {
57
+ return document . getText ( ) . match ( '^asyncapi:' ) !== null ;
58
+ }
59
+ return false ;
46
60
}
47
61
48
62
function openAsyncAPI ( context : vscode . ExtensionContext , uri : vscode . Uri ) {
@@ -73,7 +87,7 @@ function openAsyncAPI(context: vscode.ExtensionContext, uri: vscode.Uri) {
73
87
}
74
88
75
89
async function promptForAsyncapiFile ( ) {
76
- if ( isAsyncAPIFile ( vscode . window . activeTextEditor ?. document . getText ( ) || '' ) ) {
90
+ if ( isAsyncAPIFile ( vscode . window . activeTextEditor ?. document ) ) {
77
91
return vscode . window . activeTextEditor ?. document . uri ;
78
92
}
79
93
return await vscode . window . showOpenDialog ( {
0 commit comments