2
2
3
3
import logging
4
4
from typing import Any , Dict , Generator , List , Optional , Set , Union
5
+ import threading
5
6
6
7
import parso
7
8
from jedi import Script
25
26
MAX_RESULTS_CODE_ACTIONS = 5
26
27
27
28
29
+ class AutoimportCache :
30
+ """Handles the cache creation."""
31
+
32
+ def __init__ (self ):
33
+ self .thread = None
34
+
35
+ def reload_cache (
36
+ self ,
37
+ config : Config ,
38
+ workspace : Workspace ,
39
+ files : Optional [List [Document ]] = None ,
40
+ single_thread : Optional [bool ] = False ,
41
+ ):
42
+ if self .is_blocked ():
43
+ return
44
+
45
+ memory : bool = config .plugin_settings ("rope_autoimport" ).get ("memory" , False )
46
+ rope_config = config .settings ().get ("rope" , {})
47
+ autoimport = workspace ._rope_autoimport (rope_config , memory )
48
+ resources : Optional [List [Resource ]] = (
49
+ None
50
+ if files is None
51
+ else [document ._rope_resource (rope_config ) for document in files ]
52
+ )
53
+
54
+ if single_thread :
55
+ self ._reload_cache (workspace , autoimport , resources )
56
+ else :
57
+ # Creating the cache may take 10-20s for a environment with 5k python modules. That's
58
+ # why we decided to move cache creation into its own thread.
59
+ self .thread = threading .Thread (
60
+ target = self ._reload_cache , args = (workspace , autoimport , resources )
61
+ )
62
+ self .thread .start ()
63
+
64
+ def _reload_cache (
65
+ self ,
66
+ workspace : Workspace ,
67
+ autoimport : AutoImport ,
68
+ resources : Optional [List [Resource ]] = None ,
69
+ ):
70
+ task_handle = PylspTaskHandle (workspace )
71
+ autoimport .generate_cache (task_handle = task_handle , resources = resources )
72
+ autoimport .generate_modules_cache (task_handle = task_handle )
73
+
74
+ def is_blocked (self ):
75
+ return self .thread and self .thread .is_alive ()
76
+
77
+
28
78
@hookimpl
29
79
def pylsp_settings () -> Dict [str , Dict [str , Dict [str , Any ]]]:
30
80
# Default rope_completion to disabled
@@ -191,7 +241,7 @@ def pylsp_completions(
191
241
not config .plugin_settings ("rope_autoimport" )
192
242
.get ("completions" , {})
193
243
.get ("enabled" , True )
194
- ):
244
+ ) or cache . is_blocked () :
195
245
return []
196
246
197
247
line = document .lines [position ["line" ]]
@@ -283,7 +333,7 @@ def pylsp_code_actions(
283
333
not config .plugin_settings ("rope_autoimport" )
284
334
.get ("code_actions" , {})
285
335
.get ("enabled" , True )
286
- ):
336
+ ) or cache . is_blocked () :
287
337
return []
288
338
289
339
log .debug (f"textDocument/codeAction: { document } { range } { context } " )
@@ -319,29 +369,13 @@ def pylsp_code_actions(
319
369
return code_actions
320
370
321
371
322
- def _reload_cache (
323
- config : Config , workspace : Workspace , files : Optional [List [Document ]] = None
324
- ):
325
- memory : bool = config .plugin_settings ("rope_autoimport" ).get ("memory" , False )
326
- rope_config = config .settings ().get ("rope" , {})
327
- autoimport = workspace ._rope_autoimport (rope_config , memory )
328
- task_handle = PylspTaskHandle (workspace )
329
- resources : Optional [List [Resource ]] = (
330
- None
331
- if files is None
332
- else [document ._rope_resource (rope_config ) for document in files ]
333
- )
334
- autoimport .generate_cache (task_handle = task_handle , resources = resources )
335
- autoimport .generate_modules_cache (task_handle = task_handle )
336
-
337
-
338
372
@hookimpl
339
373
def pylsp_initialize (config : Config , workspace : Workspace ):
340
374
"""Initialize AutoImport.
341
375
342
376
Generates the cache for local and global items.
343
377
"""
344
- _reload_cache (config , workspace )
378
+ cache . reload_cache (config , workspace )
345
379
346
380
347
381
@hookimpl
@@ -350,13 +384,13 @@ def pylsp_document_did_open(config: Config, workspace: Workspace):
350
384
351
385
Generates the cache for local and global items.
352
386
"""
353
- _reload_cache (config , workspace )
387
+ cache . reload_cache (config , workspace )
354
388
355
389
356
390
@hookimpl
357
391
def pylsp_document_did_save (config : Config , workspace : Workspace , document : Document ):
358
392
"""Update the names associated with this document."""
359
- _reload_cache (config , workspace , [document ])
393
+ cache . reload_cache (config , workspace , [document ])
360
394
361
395
362
396
@hookimpl
@@ -368,6 +402,9 @@ def pylsp_workspace_configuration_changed(config: Config, workspace: Workspace):
368
402
Generates the cache for local and global items.
369
403
"""
370
404
if config .plugin_settings ("rope_autoimport" ).get ("enabled" , False ):
371
- _reload_cache (config , workspace )
405
+ cache . reload_cache (config , workspace )
372
406
else :
373
407
log .debug ("autoimport: Skipping cache reload." )
408
+
409
+
410
+ cache : AutoimportCache = AutoimportCache ()
0 commit comments