Skip to content

Commit 69e0b8f

Browse files
Use sublime.packages_path()
1 parent 2da99dc commit 69e0b8f

File tree

2 files changed

+74
-28
lines changed

2 files changed

+74
-28
lines changed

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,73 @@
11
import shutil
22
import os
33
import sublime
4+
import threading
5+
import subprocess
46

57
from LSP.plugin.core.handlers import LanguageHandler
68
from LSP.plugin.core.settings import ClientConfig, LanguageConfig
79

810

9-
package_path = os.path.dirname(__file__)
10-
server_path = os.path.join(package_path, 'node_modules', 'vue-language-server', 'bin', 'vls')
11-
12-
1311
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')
1414
print('LSP-vue: Server {} installed.'.format('is' if os.path.isfile(server_path) else 'is not' ))
1515

16+
# install the node_modules if not installed
1617
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]
2926
)
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
3151

3252

3353
def is_node_installed():
3454
return shutil.which('node') is not None
3555

3656

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+
3762
class LspVuePlugin(LanguageHandler):
3863
@property
3964
def name(self) -> str:
4065
return 'lsp-vue'
4166

4267
@property
4368
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')
4471
settings = sublime.load_settings("LSP-vue.sublime-settings")
4572
return ClientConfig(
4673
name='lsp-vue',

0 commit comments

Comments
 (0)