|
1 |
| -from .reference import RefInfo |
| 1 | +import collections |
| 2 | +import logging |
| 3 | + |
| 4 | +from .reference import RefInfo |
2 | 5 | from .node_client import ServerClient, WorkerClient
|
3 | 6 | from .service_proxy import ServiceProxy
|
4 | 7 | from .logger import log
|
@@ -49,19 +52,53 @@ def initialize(self):
|
49 | 52 | initialized during loading time
|
50 | 53 | """
|
51 | 54 |
|
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") |
63 |
| - log.debug("Path of tsserver.js: " + proc_file) |
64 |
| - log.debug("Path of tsc.js: " + get_tsc_path()) |
| 55 | + # Default to and fall back to the bundled SDK |
| 56 | + tsdk_location_default = os.path.join(PLUGIN_DIR, "tsserver") |
| 57 | + tsdk_location = tsdk_location_default |
| 58 | + |
| 59 | + is_tsdk_location_good = lambda x: ( |
| 60 | + os.path.isfile(os.path.join(x, "tsserver.js")) |
| 61 | + and os.path.isfile(os.path.join(x, "tsc.js")) |
| 62 | + ) |
| 63 | + |
| 64 | + active_window = sublime.active_window() |
| 65 | + settings = active_window.active_view().settings() |
| 66 | + typescript_tsdk_setting = settings.get("typescript_tsdk") |
| 67 | + if typescript_tsdk_setting: |
| 68 | + if os.path.isabs(typescript_tsdk_setting): |
| 69 | + if is_tsdk_location_good(typescript_tsdk_setting): |
| 70 | + tsdk_location = typescript_tsdk_setting |
| 71 | + else: |
| 72 | + def look_for_tsdk(x): |
| 73 | + x_appended = os.path.join(x, typescript_tsdk_setting) |
| 74 | + if is_tsdk_location_good(x_appended): |
| 75 | + return x_appended |
| 76 | + parent = os.path.dirname(x) |
| 77 | + if parent == x: |
| 78 | + return None # We have reached the root |
| 79 | + return look_for_tsdk(parent) |
| 80 | + |
| 81 | + # list(OrderedDict.fromkeys(x)) = deduped x, order preserved |
| 82 | + folders = active_window.folders() + list( |
| 83 | + collections.OrderedDict.fromkeys( |
| 84 | + [ |
| 85 | + os.path.dirname(x.file_name()) |
| 86 | + for x in active_window.views() |
| 87 | + if x.file_name() # It's None for unsaved files |
| 88 | + ] |
| 89 | + ) |
| 90 | + ) |
| 91 | + for folder in folders: |
| 92 | + x = look_for_tsdk(folder) |
| 93 | + if x != None: |
| 94 | + tsdk_location = x |
| 95 | + break |
| 96 | + |
| 97 | + proc_file = os.path.join(tsdk_location, "tsserver.js") |
| 98 | + global_vars._tsc_path = os.path.join(tsdk_location, "tsc.js") |
| 99 | + |
| 100 | + log.warn("Path of tsserver.js: " + proc_file) |
| 101 | + log.warn("Path of tsc.js: " + get_tsc_path()) |
65 | 102 |
|
66 | 103 | self.node_client = ServerClient(proc_file)
|
67 | 104 | self.worker_client = WorkerClient(proc_file)
|
|
0 commit comments