22
33import logging
44from typing import Any , Dict , Generator , List , Optional , Set , Union
5+ import threading
56
67import parso
78from jedi import Script
2526MAX_RESULTS_CODE_ACTIONS = 5
2627
2728
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+
2878@hookimpl
2979def pylsp_settings () -> Dict [str , Dict [str , Dict [str , Any ]]]:
3080 # Default rope_completion to disabled
@@ -191,7 +241,7 @@ def pylsp_completions(
191241 not config .plugin_settings ("rope_autoimport" )
192242 .get ("completions" , {})
193243 .get ("enabled" , True )
194- ):
244+ ) or cache . is_blocked () :
195245 return []
196246
197247 line = document .lines [position ["line" ]]
@@ -283,7 +333,7 @@ def pylsp_code_actions(
283333 not config .plugin_settings ("rope_autoimport" )
284334 .get ("code_actions" , {})
285335 .get ("enabled" , True )
286- ):
336+ ) or cache . is_blocked () :
287337 return []
288338
289339 log .debug (f"textDocument/codeAction: { document } { range } { context } " )
@@ -319,29 +369,13 @@ def pylsp_code_actions(
319369 return code_actions
320370
321371
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-
338372@hookimpl
339373def pylsp_initialize (config : Config , workspace : Workspace ):
340374 """Initialize AutoImport.
341375
342376 Generates the cache for local and global items.
343377 """
344- _reload_cache (config , workspace )
378+ cache . reload_cache (config , workspace )
345379
346380
347381@hookimpl
@@ -350,13 +384,13 @@ def pylsp_document_did_open(config: Config, workspace: Workspace):
350384
351385 Generates the cache for local and global items.
352386 """
353- _reload_cache (config , workspace )
387+ cache . reload_cache (config , workspace )
354388
355389
356390@hookimpl
357391def pylsp_document_did_save (config : Config , workspace : Workspace , document : Document ):
358392 """Update the names associated with this document."""
359- _reload_cache (config , workspace , [document ])
393+ cache . reload_cache (config , workspace , [document ])
360394
361395
362396@hookimpl
@@ -368,6 +402,9 @@ def pylsp_workspace_configuration_changed(config: Config, workspace: Workspace):
368402 Generates the cache for local and global items.
369403 """
370404 if config .plugin_settings ("rope_autoimport" ).get ("enabled" , False ):
371- _reload_cache (config , workspace )
405+ cache . reload_cache (config , workspace )
372406 else :
373407 log .debug ("autoimport: Skipping cache reload." )
408+
409+
410+ cache : AutoimportCache = AutoimportCache ()
0 commit comments