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

Improve module loading path handling at completion, liinting, and so on #712

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
Add utility to access configuration value by path
At getting the value in nested settings dict, newly added
get_config_by_path() is more efficient and readable than "get with
empty dict" chain like below, which is sometimes used in current
implementation.

  settings.get("foo", {}).get("bar", {}).get("baz")
flying-foozy committed Jan 17, 2020
commit 1d3141d5a4517352c015eeb6b7ff11e9eb9be7a7
19 changes: 19 additions & 0 deletions pyls/_utils.py
Original file line number Diff line number Diff line change
@@ -132,6 +132,25 @@ def _merge_dicts_(a, b):
return dict(_merge_dicts_(dict_a, dict_b))


def get_config_by_path(settings, path, default_value=None):
"""Get the value in settings dict at the given path.

If path is not resolvable in specified settings, this returns
default_value.
"""
paths = path.split('.')
while len(paths) > 1:
settings = settings.get(paths.pop(0))
if not isinstance(settings, dict):
# Here, at least one more looking up should be available,
# but the last retrieved was non-dict. Therefore, path is
# not resolvable in specified settings.
return default_value

# Here, paths should have only one value
return settings.get(paths[0], default_value)


def format_docstring(contents):
"""Python doc strings come in a number of formats, but LSP wants markdown.