@@ -49,17 +49,46 @@ def initialize(self):
4949 initialized during loading time
5050 """
5151
52- # retrieve the path to tsserver.js
53- # first see if user set the path to the file
54- settings = sublime .load_settings ("Preferences.sublime-settings" )
55- tsdk_location = settings .get ("typescript_tsdk" )
56- if tsdk_location :
57- proc_file = os .path .join (tsdk_location , "tsserver.js" )
58- global_vars ._tsc_path = os .path .join (tsdk_location , "tsc.js" )
59- else :
60- # otherwise, get tsserver.js from package directory
61- proc_file = os .path .join (PLUGIN_DIR , "tsserver" , "tsserver.js" )
62- global_vars ._tsc_path = os .path .join (PLUGIN_DIR , "tsserver" , "tsc.js" )
52+ # Fall back to the bundled TypeScript server if the one provided by
53+ # user doesn't exist.
54+ proc_file = os .path .join (PLUGIN_DIR , "tsserver" , "tsserver.js" )
55+ global_vars ._tsc_path = os .path .join (PLUGIN_DIR , "tsserver" , "tsc.js" )
56+
57+ is_tsdk_location_good = lambda x : (
58+ os .path .isfile (os .path .join (x , "tsserver.js" )) and
59+ os .path .isfile (os .path .join (x , "tsc.js" ))
60+ )
61+
62+ active_view = sublime .active_window ().active_view ()
63+ settings = active_view .settings ()
64+ custom_tsdk_setting = settings .get ("typescript_tsdk" )
65+ if custom_tsdk_setting :
66+ tsdk_location = None
67+ if os .path .isabs (custom_tsdk_setting ):
68+ if is_tsdk_location_good (custom_tsdk_setting ):
69+ tsdk_location = custom_tsdk_setting
70+ else :
71+ folders = sublime .active_window ().folders ()
72+ for folder in folders :
73+ x = os .path .join (folder , custom_tsdk_setting )
74+ if is_tsdk_location_good (x ):
75+ tsdk_location = x
76+ break
77+
78+ if not tsdk_location :
79+ active_file = active_view .file_name ()
80+ if active_file :
81+ x = os .path .join (
82+ os .path .dirname (active_file ),
83+ custom_tsdk_setting
84+ )
85+ if is_tsdk_location_good (x ):
86+ tsdk_location = x
87+
88+ if tsdk_location :
89+ proc_file = os .path .join (tsdk_location , "tsserver.js" )
90+ global_vars ._tsc_path = os .path .join (tsdk_location , "tsc.js" )
91+
6392 log .debug ("Path of tsserver.js: " + proc_file )
6493 log .debug ("Path of tsc.js: " + get_tsc_path ())
6594
0 commit comments