Skip to content

Commit 80dc15f

Browse files
committed
Add functions to retrieve project info from server
1 parent b0924a9 commit 80dc15f

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

typescript/commands/build.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
import sublime_plugin
2-
import sublime
3-
import os
4-
from ..libs.global_vars import IS_ST2
2+
3+
from ..libs.global_vars import *
4+
from ..libs import cli
55

66

77
class TypescriptBuildCommand(sublime_plugin.WindowCommand):
88
def run(self):
9+
if get_node_path() is None:
10+
print("Cannot found node. Build cancelled.")
11+
return
12+
913
file_name = self.window.active_view().file_name()
10-
directory = os.path.dirname(file_name)
11-
if "tsconfig.json" in os.listdir(directory):
12-
self.window.run_command("exec", {
13-
"cmd": ["tsc"],
14-
"file_regex": "^(.+?)\\((\\d+),(\\d+)\\): (.+)$",
15-
"shell": True
16-
})
17-
else:
18-
sublime.active_window().show_input_panel(
19-
"Build parameters: ",
20-
"", # initial text
21-
self.compile_inferred_project,
22-
None, # on change
23-
None # on cancel
24-
)
14+
project_info = cli.service.project_info(file_name)
15+
if project_info["success"]:
16+
if "configFileName" in project_info["body"]:
17+
tsconfig_dir = dirname(project_info["body"]["configFileName"])
18+
self.window.run_command("exec", {
19+
"cmd": [get_node_path(), TSC_PATH, "-p", tsconfig_dir],
20+
"file_regex": "^(.+?)\\((\\d+),(\\d+)\\): (.+)$",
21+
"shell": True
22+
})
23+
else:
24+
sublime.active_window().show_input_panel(
25+
"Build parameters: ",
26+
"", # initial text
27+
self.compile_inferred_project,
28+
None, # on change
29+
None # on cancel
30+
)
2531

2632
def compile_inferred_project(self, params=""):
2733
file_name = self.window.active_view().file_name()
28-
if not IS_ST2:
29-
cmd = "tsc {0} {1}".format(file_name, params)
30-
self.window.run_command("exec", {
31-
"shell_cmd": cmd,
32-
"file_regex": "^(.+?)\\((\\d+),(\\d+)\\): (.+)$"
33-
})
34-
else:
35-
cmd = "tsc {0} {1}".format(file_name, params)
36-
self.window.run_command("exec", {
37-
"cmd": [cmd],
38-
"file_regex": "^(.+?)\\((\\d+),(\\d+)\\): (.+)$",
39-
"shell": True
40-
})
34+
cmd = [get_node_path(), TSC_PATH, file_name]
35+
if params != "":
36+
cmd.extend(params.split(' '))
37+
self.window.run_command("exec", {
38+
"cmd": cmd,
39+
"file_regex": "^(.+?)\\((\\d+),(\\d+)\\): (.+)$",
40+
"shell": True
41+
})

typescript/libs/global_vars.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
PLUGIN_DIR = dirname(MODULE_DIR)
1616
PLUGIN_NAME = os.path.basename(PLUGIN_DIR)
1717

18+
# The node path will be initialized in the node_client.py module
19+
_node_path = None
20+
def get_node_path():
21+
return _node_path
22+
23+
TSC_PATH = os.path.join(PLUGIN_DIR, "tsserver", "tsc.js")
24+
1825
# only Sublime Text 3 build after 3072 support tooltip
1926
TOOLTIP_SUPPORT = int(sublime.version()) >= 3072
2027

typescript/libs/node_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from .logger import log
1010
from . import json_helpers
11+
from . import global_vars
1112

1213
# queue module name changed from Python 2 to 3
1314
if int(sublime.version()) < 3000:
@@ -60,6 +61,7 @@ def __init__(self, scriptPath):
6061
print("To specify the node executable file name, use the 'node_path' setting")
6162
self.__serverProc = None
6263
else:
64+
global_vars._node_path = node_path
6365
print("Found node executable at " + node_path)
6466
try:
6567
if os.name == "nt":

typescript/libs/service_proxy.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ def nav_to(self, search_text, file_name):
178178
response_dict = self.__comm.sendCmdSync(json_str, req_dict["seq"])
179179
return response_dict
180180

181+
def project_info(self, file_name, need_file_name_list=False):
182+
args = {"file": file_name, "needFileNameList": need_file_name_list}
183+
req_dict = self.create_req_dict("projectInfo", args)
184+
json_str = json_helpers.encode(req_dict)
185+
return self.__comm.sendCmdSync(json_str, req_dict["seq"])
186+
181187
def create_req_dict(self, command_name, args=None):
182188
req_dict = {
183189
"command": command_name,

0 commit comments

Comments
 (0)