@@ -11,6 +11,8 @@ import {
11
11
AutocompleteSnippetType ,
12
12
} from "./types" ;
13
13
14
+ const IDE_SNIPPETS_ENABLED = false ; // ideSnippets is not used, so it's temporarily disabled
15
+
14
16
export interface SnippetPayload {
15
17
rootPathSnippets : AutocompleteCodeSnippet [ ] ;
16
18
importDefinitionSnippets : AutocompleteCodeSnippet [ ] ;
@@ -29,6 +31,27 @@ function racePromise<T>(promise: Promise<T[]>): Promise<T[]> {
29
31
return Promise . race ( [ promise , timeoutPromise ] ) ;
30
32
}
31
33
34
+ class DiffSnippetsCache {
35
+ private cache : Map < number , any > = new Map ( ) ;
36
+ private lastTimestamp : number = 0 ;
37
+
38
+ public set < T > ( timestamp : number , value : T ) : T {
39
+ // Clear old cache entry if exists
40
+ if ( this . lastTimestamp !== timestamp ) {
41
+ this . cache . clear ( ) ;
42
+ }
43
+ this . lastTimestamp = timestamp ;
44
+ this . cache . set ( timestamp , value ) ;
45
+ return value ;
46
+ }
47
+
48
+ public get ( timestamp : number ) : any | undefined {
49
+ return this . cache . get ( timestamp ) ;
50
+ }
51
+ }
52
+
53
+ const diffSnippetsCache = new DiffSnippetsCache ( ) ;
54
+
32
55
// Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems,
33
56
// or they might separately track recently edited ranges)
34
57
async function getIdeSnippets (
@@ -90,19 +113,31 @@ const getClipboardSnippets = async (
90
113
const getDiffSnippets = async (
91
114
ide : IDE ,
92
115
) : Promise < AutocompleteDiffSnippet [ ] > => {
116
+ const currentTimestamp = ide . getLastFileSaveTimestamp ?
117
+ ide . getLastFileSaveTimestamp ( ) :
118
+ Math . floor ( Date . now ( ) / 10000 ) * 10000 ; // Defaults to update once in every 10 seconds
119
+
120
+ // Check cache first
121
+ const cached = diffSnippetsCache . get ( currentTimestamp ) as AutocompleteDiffSnippet [ ] ;
122
+
123
+ if ( cached ) {
124
+ return cached ;
125
+ }
126
+
93
127
let diff : string [ ] = [ ] ;
94
128
try {
95
129
diff = await ide . getDiff ( true ) ;
96
130
} catch ( e ) {
97
131
console . error ( "Error getting diff for autocomplete" , e ) ;
98
132
}
99
133
100
- return diff . map ( ( item ) => {
134
+ return diffSnippetsCache . set ( currentTimestamp , diff . map ( ( item ) => {
101
135
return {
102
136
content : item ,
103
137
type : AutocompleteSnippetType . Diff ,
104
138
} ;
105
- } ) ;
139
+ } ) ) ;
140
+
106
141
} ;
107
142
108
143
export const getAllSnippets = async ( {
@@ -127,10 +162,8 @@ export const getAllSnippets = async ({
127
162
clipboardSnippets ,
128
163
] = await Promise . all ( [
129
164
racePromise ( contextRetrievalService . getRootPathSnippets ( helper ) ) ,
130
- racePromise (
131
- contextRetrievalService . getSnippetsFromImportDefinitions ( helper ) ,
132
- ) ,
133
- racePromise ( getIdeSnippets ( helper , ide , getDefinitionsFromLsp ) ) ,
165
+ racePromise ( contextRetrievalService . getSnippetsFromImportDefinitions ( helper ) ) ,
166
+ IDE_SNIPPETS_ENABLED ? racePromise ( getIdeSnippets ( helper , ide , getDefinitionsFromLsp ) ) : [ ] ,
134
167
racePromise ( getDiffSnippets ( ide ) ) ,
135
168
racePromise ( getClipboardSnippets ( ide ) ) ,
136
169
] ) ;
0 commit comments