Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand variables on initializationOptions #709

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions plugin/core/clients.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sublime
import os
import tempfile
from .sessions import create_session, Session

# typing only
Expand All @@ -9,17 +10,19 @@


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


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(
Expand All @@ -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,
Expand All @@ -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,
Expand Down