diff --git a/plugin/core/clients.py b/plugin/core/clients.py index cf08e81b9..232f9c777 100644 --- a/plugin/core/clients.py +++ b/plugin/core/clients.py @@ -1,5 +1,6 @@ import sublime import os +import tempfile from .sessions import create_session, Session # typing only @@ -9,9 +10,11 @@ try: - from typing import Any, List, Dict, Tuple, Callable, Optional, Set - assert Any and List and Dict and Tuple and Callable and Optional and Set + from typing import Any, List, Dict, Tuple, Callable, Optional, Set, TypeVar + assert Any and List and Dict and Tuple and Callable and Optional and Set and TypeVar assert Session + + T = TypeVar('T') except ImportError: pass @@ -19,7 +22,7 @@ def get_window_env(window: sublime.Window, config: ClientConfig) -> 'Tuple[List[str], Dict[str, str]]': # Create a dictionary of Sublime Text variables - variables = window.extract_variables() + variables = get_expanding_variables(window) # Expand language server command line environment variables expanded_args = list( @@ -36,6 +39,31 @@ def get_window_env(window: sublime.Window, config: ClientConfig) -> 'Tuple[List[ return expanded_args, env +def get_expanding_variables(window: sublime.Window) -> dict: + variables = window.extract_variables() + variables.update({ + "home": os.path.expanduser('~'), + "temp_dir": tempfile.gettempdir(), + }) + + return variables + + +def lsp_expand_variables(window: sublime.Window, var: 'T') -> 'T': + if isinstance(var, dict): + for key, value in var.items(): + if isinstance(value, (dict, list, str)): + var[key] = lsp_expand_variables(window, value) + elif isinstance(var, list): + for idx, value in enumerate(var): + if isinstance(value, (dict, list, str)): + var[idx] = lsp_expand_variables(window, value) + elif isinstance(var, str): + var = sublime.expand_variables(var, get_expanding_variables(window)) + + return var + + def start_window_config(window: sublime.Window, project_path: str, config: ClientConfig, @@ -44,6 +72,7 @@ def start_window_config(window: sublime.Window, on_post_exit: 'Callable[[str], None]') -> 'Optional[Session]': args, env = get_window_env(window, config) config.binary_args = args + config.init_options = lsp_expand_variables(window, config.init_options) return create_session(config=config, project_path=project_path, env=env,