Skip to content

Commit

Permalink
Run server from cache (#17)
Browse files Browse the repository at this point in the history
- add `Preferences: LSP-vue Settings` command
 - enable `templateInterpolationService` in vetur settings and update some
 - flatten default settings and migrate user's to new structure
 - fix setting of `tabSize` and `useTabs` (didn't work previously)
  • Loading branch information
rchl authored Mar 19, 2020
1 parent 8e0f258 commit 817dccd
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 195 deletions.
Empty file removed .no-sublime-package
Empty file.
10 changes: 10 additions & 0 deletions LSP-vue.sublime-commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"caption": "Preferences: LSP-vue Settings",
"command": "edit_settings",
"args": {
"base_file": "${packages}/LSP-vue/LSP-vue.sublime-settings",
"default": "// Settings in here override those in \"LSP-vue/LSP-vue.sublime-settings\",\n\n{\n\t$0\n}\n"
}
},
]
93 changes: 48 additions & 45 deletions LSP-vue.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
{
"client" : {
"enabled": true,
"languages": [
{
"languageId": "vue",
"scopes": ["text.html.vue"],
"syntaxes": ["Packages/Vue Syntax Highlight/Vue Component.sublime-syntax"]
}
],
"initializationOptions": {
"config": {
"vetur": {
"completion": {
"autoImport": false,
"tagCasing": "kebab",
"useScaffoldSnippets": false
"languages": [
{
"languageId": "vue",
"scopes": ["text.html.vue"],
"syntaxes": ["Packages/Vue Syntax Highlight/Vue Component.sublime-syntax"],
}
],
"initializationOptions": {
"config": {
"vetur": {
"completion": {
"autoImport": false,
"tagCasing": "kebab",
"useScaffoldSnippets": false,
},
"experimental": {
"templateInterpolationService": true,
},
"format": {
"enable": true,
"defaultFormatter": {
"js": "none",
"ts": "none",
},
"format": {
"defaultFormatter": {
"js": "none",
"ts": "none"
},
"defaultFormatterOptions": {},
"scriptInitialIndent": false,
"styleInitialIndent": false,
"options": {}
"defaultFormatterOptions": {},
"scriptInitialIndent": false,
"styleInitialIndent": false,
"options": {
// tabSize and useTabs will be automatically inferred from the workspace
},
"useWorkspaceDependencies": false,
"validation": {
"script": true,
"style": true,
"template": true
}
},
"css": {},
"emmet": {},
"stylusSupremacy": {},
"html": {
"suggest": {}
},
"javascript": {
"format": {}
"useWorkspaceDependencies": false,
"validation": {
"script": true,
"style": true,
"template": true,
},
"typescript": {
"format": {}
}
},
"css": {},
"emmet": {},
"stylusSupremacy": {},
"html": {
"suggest": {},
},
"javascript": {
"format": {},
},
"typescript": {
"format": {},
}
},
"settings": {}
}
}
},
"settings": {}
}
2 changes: 1 addition & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"command": "edit_settings",
"args": {
"base_file": "${packages}/LSP-vue/LSP-vue.sublime-settings",
"default": "{\n\t$0\n}\n"
"default": "// Settings in here override those in \"LSP-vue/LSP-vue.sublime-settings\",\n\n{\n\t$0\n}\n"
}
}
]
Expand Down
8 changes: 8 additions & 0 deletions dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"*": {
"*": [
"lsp_utils",
"sublime_lib"
]
}
}
22 changes: 0 additions & 22 deletions package.json

This file was deleted.

183 changes: 58 additions & 125 deletions plugin.py
Original file line number Diff line number Diff line change
@@ -1,153 +1,86 @@
import shutil
import os
import shutil
import sublime
import threading
import subprocess

from LSP.plugin.core.handlers import LanguageHandler
from LSP.plugin.core.settings import ClientConfig, LanguageConfig, read_client_config
from LSP.plugin.core.settings import ClientConfig, read_client_config
from lsp_utils import ServerNpmResource

PACKAGE_NAME = 'LSP-vue'
SETTINGS_FILENAME = 'LSP-vue.sublime-settings'
SERVER_DIRECTORY = 'server'
SERVER_BINARY_PATH = os.path.join(SERVER_DIRECTORY, 'node_modules', 'vue-language-server', 'bin', 'vls')

package_path = os.path.dirname(__file__)
server_path = os.path.join(package_path, 'node_modules', 'vue-language-server', 'bin', 'vls')
server = ServerNpmResource(PACKAGE_NAME, SERVER_DIRECTORY, SERVER_BINARY_PATH)


def plugin_loaded():
is_server_installed = os.path.isfile(server_path)
print('LSP-vue: Server {} installed.'.format('is' if is_server_installed else 'is not' ))

# install if not installed
if not is_server_installed:
# this will be called only when the plugin gets:
# - installed for the first time,
# - or when updated on package control
logAndShowMessage('LSP-vue: Installing server.')

runCommand(
onCommandDone,
["npm", "install", "--verbose", "--prefix", package_path, package_path]
)


def onCommandDone():
logAndShowMessage('LSP-vue: Server installed.')


def runCommand(onExit, popenArgs):
"""
Runs the given args in a subprocess.Popen, and then calls the function
onExit when the subprocess completes.
onExit is a callable object, and popenArgs is a list/tuple of args that
would give to subprocess.Popen.
"""
def runInThread(onExit, popenArgs):
try:
if sublime.platform() == 'windows':
subprocess.check_call(popenArgs, shell=True)
else:
subprocess.check_call(popenArgs)
onExit()
except subprocess.CalledProcessError as error:
logAndShowMessage('LSP-vue: Error while installing the server.', error)
return
thread = threading.Thread(target=runInThread, args=(onExit, popenArgs))
thread.start()
# returns immediately after the thread starts
return thread

server.setup()

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

def plugin_unloaded():
server.cleanup()

def logAndShowMessage(msg, additional_logs=None):
print(msg, '\n', additional_logs) if additional_logs else print(msg)
sublime.active_window().status_message(msg)

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

def update_to_new_configuration(settings, old_config, new_config):
# add old config to new config
new_config['initializationOptions']['config'] = old_config
settings.set('client', new_config)
# remove old config
settings.erase('config')
sublime.save_settings("LSP-vue.sublime-settings")

class LspVuePlugin(LanguageHandler):
@property
def name(self) -> str:
return 'lsp-vue'
return PACKAGE_NAME.lower()

@property
def config(self) -> ClientConfig:
settings = sublime.load_settings("LSP-vue.sublime-settings")
# TODO: remove update_to_new_configuration after 1 November.
old_config = settings.get('config')
client_configuration = settings.get('client')
if old_config:
update_to_new_configuration(settings, old_config, client_configuration)
# Calling setup() also here as this might run before `plugin_loaded`.
# Will be a no-op if already ran.
# See https://github.com/sublimelsp/LSP/issues/899
server.setup()

configuration = self.migrate_and_read_configuration()

default_configuration = {
"command": [
'node',
server_path,
'--stdio'
],
"languages": [
{
"languageId": "vue",
"scopes": ["text.html.vue"],
"syntaxes": ["Packages/Vue Syntax Highlight/Vue Component.sublime-syntax"]
}
],
"initializationOptions": {
"config": {
"vetur": {
"completion": {
"autoImport": False,
"tagCasing": "kebab",
"useScaffoldSnippets": False
},
"format": {
"defaultFormatter": {
"js": "none",
"ts": "none"
},
"defaultFormatterOptions": {},
"scriptInitialIndent": False,
"styleInitialIndent": False,
"options": {}
},
"useWorkspaceDependencies": False,
"validation": {
"script": True,
"style": True,
"template": True
}
},
"css": {},
"emmet": {},
"stylusSupremacy": {},
"html": {
"suggest": {}
},
"javascript": {
"format": {}
},
"typescript": {
"format": {}
}
}
}
'enabled': True,
'command': ['node', server.binary_path, '--stdio'],
}
default_configuration.update(client_configuration)

default_configuration.update(configuration)

view = sublime.active_window().active_view()
if view is not None:
options = default_configuration.get('initializationOptions', {}) .get('config',{}) .get('vetur',{}).get('format',{}).get('options',{
"tabSize": view.settings().get("tab_size", 4),
"useTabs": not view.settings().get("translate_tabs_to_spaces", False)
})
return read_client_config('lsp-vue', default_configuration)
if view:
view_settings = view.settings()
default_configuration \
.setdefault('initializationOptions', {}) \
.setdefault('config', {}) \
.setdefault('vetur', {}) \
.setdefault('format', {}) \
.setdefault('options', {}) \
.update({
'tabSize': view_settings.get('tab_size', 4),
'useTabs': not view_settings.get('translate_tabs_to_spaces', False)
})

return read_client_config(self.name, default_configuration)

def migrate_and_read_configuration(self) -> dict:
settings = {}
loaded_settings = sublime.load_settings(SETTINGS_FILENAME)

if loaded_settings:
if loaded_settings.has('client'):
client = loaded_settings.get('client')
loaded_settings.erase('client')
# Migrate old keys
for key in client:
loaded_settings.set(key, client[key])
sublime.save_settings(SETTINGS_FILENAME)

# Read configuration keys
for key in ['languages', 'initializationOptions', 'settings']:
settings[key] = loaded_settings.get(key)

return settings

def on_start(self, window) -> bool:
if not is_node_installed():
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json → server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "vue-language-server",
"version": "0.0.67",
"dependencies": {
"vue-language-server": "0.0.67"
}
}
Loading

0 comments on commit 817dccd

Please sign in to comment.