@@ -16,13 +16,16 @@ export interface SnippetConfig {
1616 defaultImports ?: Record < string , string [ ] > ;
1717}
1818
19+ interface SnippetResult {
20+ name : string ;
21+ languages : string [ ] ;
22+ defaultLanguage : string ;
23+ imports ?: Record < string , string [ ] > ;
24+ content : Record < string , string > ;
25+ }
26+
1927interface SnippetManager {
20- getSnippet : ( name : string , language : string ) => Promise < string > ;
21- getSnippetDisplayInfo : ( name : string ) => {
22- languages : string [ ] ;
23- defaultLanguage : string ;
24- imports : Record < string , string [ ] > ;
25- } ;
28+ getSnippet : ( name : string ) => Promise < SnippetResult > ;
2629 formatSnippet : (
2730 content : string ,
2831 options : { language : string ; showLineNumbers ?: boolean }
@@ -31,8 +34,14 @@ interface SnippetManager {
3134}
3235
3336class SnippetManagerImpl implements SnippetManager {
34- private cache : Map < string , string > = new Map ( ) ;
37+ private cache : Map < string , SnippetResult > = new Map ( ) ;
3538 private config : SnippetConfig ;
39+ private languageToDirectory : Record < string , string > = {
40+ python : 'py' ,
41+ typescript : 'ts' ,
42+ kotlin : 'kt' ,
43+ javascript : 'js' ,
44+ } ;
3645
3746 constructor ( config : Partial < SnippetConfig > = { } ) {
3847 this . config = {
@@ -51,36 +60,13 @@ class SnippetManagerImpl implements SnippetManager {
5160 this . cache . clear ( ) ;
5261 }
5362
54- async getSnippet ( name : string , language : string ) : Promise < string > {
55- const key = `${ name } -${ language } ` ;
56-
57- if ( this . cache . has ( key ) ) {
58- return this . cache . get ( key ) ! ;
63+ async getSnippet ( name : string ) : Promise < SnippetResult > {
64+ if ( this . cache . has ( name ) ) {
65+ return this . cache . get ( name ) ! ;
5966 }
6067
61- try {
62- const url = `${ this . config . baseUrl } /${ language } /${ name } .snippet.txt` ;
63- console . log ( 'Fetching from:' , url ) ;
64- const response = await fetch ( url ) ;
65- console . log ( 'Response status:' , response . status ) ;
66- console . log ( 'Response type:' , response . type ) ;
67-
68- if ( ! response . ok ) {
69- throw new Error ( `Failed to fetch snippet: ${ name } for language: ${ language } ` ) ;
70- }
71-
72- const content = await response . text ( ) ;
73- console . log ( 'Received content:' , content ) ;
74- this . cache . set ( key , content ) ;
75- return content ;
76- } catch ( error ) {
77- console . error ( `Error fetching snippet ${ name } for language ${ language } :` , error ) ;
78- throw error ;
79- }
80- }
81-
82- getSnippetDisplayInfo ( name : string ) {
8368 const languages = this . config . supportedLanguages || [ 'python' , 'kotlin' ] ;
69+ const content : Record < string , string > = { } ;
8470 const imports : Record < string , string [ ] > = { } ;
8571
8672 // Use configured imports or defaults
@@ -89,16 +75,43 @@ class SnippetManagerImpl implements SnippetManager {
8975 kotlin : [ 'import java.util.*' ] ,
9076 } ;
9177
92- // Add imports for each supported language
93- languages . forEach ( ( lang ) => {
94- imports [ lang ] = defaultImports [ lang ] || [ ] ;
95- } ) ;
78+ try {
79+ // Fetch content for each language
80+ for ( const language of languages ) {
81+ try {
82+ // Map language names to directory names
83+ const languageDir = this . languageToDirectory [ language ] || language ;
84+
85+ const url = `${ this . config . baseUrl } /${ languageDir } /${ name } .snippet.txt` ;
86+ const response = await fetch ( url ) ;
87+
88+ if ( response . ok ) {
89+ content [ language ] = await response . text ( ) ;
90+ // Only add imports if they exist for this language
91+ if ( defaultImports [ language ] ) {
92+ imports [ language ] = defaultImports [ language ] ;
93+ }
94+ }
95+ } catch ( error ) {
96+ console . error ( `Error fetching ${ language } snippet for ${ name } :` , error ) ;
97+ }
98+ }
9699
97- return {
98- languages,
99- defaultLanguage : languages [ 0 ] ,
100- imports,
101- } ;
100+ const result : SnippetResult = {
101+ name,
102+ languages : Object . keys ( content ) ,
103+ defaultLanguage : languages [ 0 ] ,
104+ content,
105+ // Only include imports if we have any
106+ ...( Object . keys ( imports ) . length > 0 && { imports } ) ,
107+ } ;
108+
109+ this . cache . set ( name , result ) ;
110+ return result ;
111+ } catch ( error ) {
112+ console . error ( `Error fetching snippet ${ name } :` , error ) ;
113+ throw error ;
114+ }
102115 }
103116
104117 formatSnippet ( content : string , options : { language : string ; showLineNumbers ?: boolean } ) : string {
0 commit comments