Skip to content

Commit 352f992

Browse files
committed
Improve typescript_tsdk lookup
1 parent f21aba5 commit 352f992

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

README.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,32 @@ The plug-in uses **Node.js** to run the TypeScript server. The plug-in looks fo
1313
If the `node_path` setting is present, this will override the PATH environment variable and the plug-in will use the value of the `node_path` setting as the node executable to run.
1414
See more information in [our Tips and Known Issues](https://github.com/Microsoft/TypeScript-Sublime-Plugin/wiki/Tips-and-Known-Issues) wiki page.
1515

16-
Note: Using different versions of TypeScript
16+
Using different versions of TypeScript
1717
--------------
18-
This plugin can be configured to load an alternate version of TypeScript.
19-
This is typically useful for trying out nightly builds, or prototyping with custom builds.
20-
To do that, update the `Settings - User` file with the following:
18+
19+
This plugin can be configured to load an alternate version of TypeScript. This is typically useful for trying out nightly builds, prototyping with custom builds, or compatibility with node_modules-free package managers like Yarn 2. To do that, update the `Settings - User` file or your project settings with the following:
2120

2221
```json5
2322
"typescript_tsdk": "<path to your folder>/node_modules/typescript/lib"
2423
```
2524

25+
The path may be relative. In such case the plugin will look in the following locations in this order:
26+
27+
```
28+
/foo/project_folder_1/<typescript_tsdk>
29+
/foo/<typescript_tsdk>
30+
/<typescript_tsdk>
31+
/bar/project_folder_2/<typescript_tsdk>
32+
/bar/<typescript_tsdk>
33+
/baz/open_file_1_folder/<typescript_tsdk>
34+
/baz/<typescript_tsdk>
35+
/baz/open_file_2_folder/<typescript_tsdk>
36+
```
37+
38+
In case of Yarn 2, just [install its editor SDK](https://yarnpkg.com/advanced/editor-sdks) and set `typescript_tsdk` to `.vscode/pnpify/typescript/lib`.
39+
40+
**Note.** The plugin isn't reloaded when switching projects or updating settings at the moment, so you must restart Sublime Text when doing so, unfortunately. When in doubt, check the console for "Path of tsserver.js" and "Path of tsc.js", and see if they point where you expect them to point.
41+
2642
Installation
2743
------------
2844
If using [Package Control](https://packagecontrol.io/) for Sublime Text, simply install the `TypeScript` package.

typescript/libs/editor_client.py

+51-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from .reference import RefInfo
1+
import collections
2+
import logging
3+
4+
from .reference import RefInfo
25
from .node_client import ServerClient, WorkerClient
36
from .service_proxy import ServiceProxy
47
from .logger import log
@@ -49,19 +52,53 @@ def initialize(self):
4952
initialized during loading time
5053
"""
5154

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())
65102

66103
self.node_client = ServerClient(proc_file)
67104
self.worker_client = WorkerClient(proc_file)

0 commit comments

Comments
 (0)