|
1 | 1 | import shutil |
2 | 2 | import os |
3 | 3 | import sublime |
| 4 | +import threading |
| 5 | +import subprocess |
4 | 6 |
|
5 | 7 | from LSP.plugin.core.handlers import LanguageHandler |
6 | 8 | from LSP.plugin.core.settings import ClientConfig, LanguageConfig |
7 | 9 |
|
8 | 10 |
|
9 | | -package_path = os.path.dirname(__file__) |
10 | | -server_path = os.path.join(package_path, 'node_modules', 'vue-language-server', 'bin', 'vls') |
11 | | - |
12 | | - |
13 | 11 | def plugin_loaded(): |
| 12 | + package_path = os.path.join(sublime.packages_path(), 'LSP-vue') |
| 13 | + server_path = os.path.join(package_path, 'node_modules', 'vue-language-server', 'bin', 'vls') |
14 | 14 | print('LSP-vue: Server {} installed.'.format('is' if os.path.isfile(server_path) else 'is not' )) |
15 | 15 |
|
| 16 | + # install the node_modules if not installed |
16 | 17 | if not os.path.isdir(os.path.join(package_path, 'node_modules')): |
17 | | - # install server if no node_modules |
18 | | - print('LSP-vue: Installing server.') |
19 | | - sublime.active_window().run_command( |
20 | | - "exec", { |
21 | | - "cmd": [ |
22 | | - "npm", |
23 | | - "install", |
24 | | - "--verbose", |
25 | | - "--prefix", |
26 | | - package_path |
27 | | - ] |
28 | | - } |
| 18 | + # this will be called only when the plugin gets: |
| 19 | + # - installed for the first time, |
| 20 | + # - or when updated on package control |
| 21 | + logAndShowMessage('LSP-vue: Installing server.') |
| 22 | + |
| 23 | + runCommand( |
| 24 | + onCommandDone, |
| 25 | + ["npm", "install", "--verbose", "--prefix", package_path] |
29 | 26 | ) |
30 | | - sublime.message_dialog('LSP-vue\n\nRestart sublime after the server has been installed successfully.') |
| 27 | + |
| 28 | + |
| 29 | +def onCommandDone(): |
| 30 | + logAndShowMessage('LSP-vue: Server installed.') |
| 31 | + |
| 32 | + |
| 33 | +def runCommand(onExit, popenArgs): |
| 34 | + """ |
| 35 | + Runs the given args in a subprocess.Popen, and then calls the function |
| 36 | + onExit when the subprocess completes. |
| 37 | + onExit is a callable object, and popenArgs is a list/tuple of args that |
| 38 | + would give to subprocess.Popen. |
| 39 | + """ |
| 40 | + def runInThread(onExit, popenArgs): |
| 41 | + try: |
| 42 | + subprocess.check_call(popenArgs) |
| 43 | + onExit() |
| 44 | + except subprocess.CalledProcessError as error: |
| 45 | + logAndShowMessage('LSP-vue: Error while installing the server.', error) |
| 46 | + return |
| 47 | + thread = threading.Thread(target=runInThread, args=(onExit, popenArgs)) |
| 48 | + thread.start() |
| 49 | + # returns immediately after the thread starts |
| 50 | + return thread |
31 | 51 |
|
32 | 52 |
|
33 | 53 | def is_node_installed(): |
34 | 54 | return shutil.which('node') is not None |
35 | 55 |
|
36 | 56 |
|
| 57 | +def logAndShowMessage(msg, additional_logs=None): |
| 58 | + print(msg, '\n', additional_logs) if additional_logs else print(msg) |
| 59 | + sublime.active_window().status_message(msg) |
| 60 | + |
| 61 | + |
37 | 62 | class LspVuePlugin(LanguageHandler): |
38 | 63 | @property |
39 | 64 | def name(self) -> str: |
40 | 65 | return 'lsp-vue' |
41 | 66 |
|
42 | 67 | @property |
43 | 68 | def config(self) -> ClientConfig: |
| 69 | + package_path = os.path.join(sublime.packages_path(), 'LSP-vue') |
| 70 | + server_path = os.path.join(package_path, 'node_modules', 'vue-language-server', 'bin', 'vls') |
44 | 71 | settings = sublime.load_settings("LSP-vue.sublime-settings") |
45 | 72 | return ClientConfig( |
46 | 73 | name='lsp-vue', |
|
0 commit comments