22import logging
33import re
44import sys
5+ from concurrent .futures import ThreadPoolExecutor
56import importmagic
67from pyls import hookimpl , lsp , _utils
78
1516UNRES_RE = re .compile (r"Unresolved import '(?P<unresolved>[\w.]+)'" )
1617UNREF_RE = re .compile (r"Unreferenced import '(?P<unreferenced>[\w.]+)'" )
1718
18- _index_cache = {}
19+ _index_cache = None
1920
2021
21- def _get_index ( sys_path ):
22+ def _build_index ( paths ):
2223 """Build index of symbols from python modules.
23- Cache the index so we don't build it multiple times unnecessarily.
2424 """
25- key = tuple (sys_path )
26- if key not in _index_cache :
27- log .info ("Started building importmagic index" )
28- index = importmagic .SymbolIndex ()
29- # The build tend to be noisy
30- index .build_index (paths = sys_path )
31- _index_cache [key ] = index
32- log .info ("Finished building importmagic index" )
33- return _index_cache [key ]
25+ log .info ("Started building importmagic index" )
26+ index = importmagic .SymbolIndex ()
27+ index .build_index (paths = paths )
28+ log .info ("Finished building importmagic index" )
29+ return index
30+
31+
32+ def _cache_index_callback (future ):
33+ global _index_cache
34+ # Cache the index
35+ _index_cache = future .result ()
36+
37+
38+ def _get_index ():
39+ """Get the cached index if built and index project files on each call.
40+ Return an empty index if not built yet.
41+ """
42+ # Index haven't been built yet
43+ if _index_cache is None :
44+ return importmagic .SymbolIndex ()
45+
46+ # Index project files
47+ # TODO(youben) index project files
48+ #index.build_index(paths=[])
49+ return _index_cache
3450
3551
3652def _get_imports_list (source , index = None ):
@@ -46,6 +62,13 @@ def _get_imports_list(source, index=None):
4662 return imported
4763
4864
65+ @hookimpl
66+ def pyls_initialize ():
67+ pool = ThreadPoolExecutor ()
68+ builder = pool .submit (_build_index , (sys .path ))
69+ builder .add_done_callback (_cache_index_callback )
70+
71+
4972@hookimpl
5073def pyls_commands ():
5174 return [ADD_IMPORT_COMMAND , REMOVE_IMPORT_COMMAND ]
@@ -146,9 +169,8 @@ def pyls_code_actions(config, document):
146169 log .debug ("Got importmagic settings: %s" , conf )
147170 importmagic .Imports .set_style (** {_utils .camel_to_underscore (k ): v for k , v in conf .items ()})
148171
149- # Might be slow but is cached once built
150- # TODO (youben): add project path for indexing
151- index = _get_index (sys .path )
172+ # Get empty index while it's building so we don't block here
173+ index = _get_index ()
152174 actions = []
153175
154176 diagnostics = pyls_lint (document )
0 commit comments