@@ -32,6 +32,7 @@ export function getPreferredSnippet(
3232 const filteredSnippets = filterSnippetDefinitions (
3333 snippetDescription . snippets ,
3434 languageId ,
35+ snippetDescription . fallbackLanguage ,
3536 ) ;
3637 filteredSnippets . sort ( compareSnippetDefinitions ) ;
3738 const preferredSnippet = filteredSnippets [ 0 ] ;
@@ -60,11 +61,29 @@ function getUniqueLanguagesString(snippets: CustomInsertSnippetArg[]): string {
6061 */
6162function filterSnippetDefinitions <
6263 T extends CustomInsertSnippetArg | CustomWrapWithSnippetArg ,
63- > ( snippetDescriptions : T [ ] , languageId : string ) : T [ ] {
64- return snippetDescriptions . filter ( ( snippetDescription ) => {
65- if ( snippetDescription . languages == null ) {
66- return true ;
67- }
68- return snippetDescription . languages . includes ( languageId ) ;
64+ > (
65+ snippetDescriptions : T [ ] ,
66+ languageId : string ,
67+ fallbackLanguage : string | undefined ,
68+ ) : T [ ] {
69+ // First try to find snippet matching language id
70+ let snippets = snippetDescriptions . filter ( ( snippetDescription ) => {
71+ return snippetDescription . languages ?. includes ( languageId ) ;
6972 } ) ;
73+
74+ // Secondly try to find snippet matching fallback language
75+ if ( snippets . length === 0 && fallbackLanguage != null ) {
76+ snippets = snippetDescriptions . filter ( ( snippetDescription ) => {
77+ return snippetDescription . languages ?. includes ( fallbackLanguage ) ;
78+ } ) ;
79+ }
80+
81+ // Finally use global snippets
82+ if ( snippets . length === 0 ) {
83+ snippets = snippetDescriptions . filter ( ( snippetDescription ) => {
84+ return snippetDescription . languages == null ;
85+ } ) ;
86+ }
87+
88+ return snippets ;
7089}
0 commit comments