Skip to content

Commit 358a829

Browse files
committed
Introduce config source for "pyls"
1 parent 0114c6c commit 358a829

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

pyls/config/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def __init__(self, root_uri, init_opts, process_id, capabilities):
4040
except ImportError:
4141
pass
4242

43+
from .pyls_conf import PylsConfig
44+
self._config_sources['pyls'] = PylsConfig(self._root_path)
45+
4346
self._pm = pluggy.PluginManager(PYLS)
4447
self._pm.trace.root.setwriter(log.debug)
4548
self._pm.enable_tracing()
@@ -104,7 +107,7 @@ def settings(self, document_path=None):
104107
settings.cache_clear() when the config is updated
105108
"""
106109
settings = {}
107-
sources = self._settings.get('configurationSources', DEFAULT_CONFIG_SOURCES)
110+
sources = self._settings.get('configurationSources', DEFAULT_CONFIG_SOURCES) + ['pyls']
108111

109112
for source_name in reversed(sources):
110113
source = self._config_sources.get(source_name)

pyls/config/pyls_conf.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import logging
2+
import os
3+
from pyls._utils import find_parents, merge_dicts
4+
from .source import ConfigSource
5+
6+
log = logging.getLogger(__name__)
7+
8+
CONFIG_KEY = 'pyls'
9+
PROJECT_CONFIGS = ['setup.cfg', 'tox.ini']
10+
11+
OPTIONS = [
12+
]
13+
14+
15+
class PylsConfig(ConfigSource):
16+
"""Parse pyls configuration"""
17+
18+
def __init__(self, root_path):
19+
super(PylsConfig, self).__init__(root_path)
20+
21+
# If workspace URI has trailing '/', root_path does, too.
22+
# (e.g. "lsp" on Emacs sends such URI). To avoid normpath at
23+
# each normalization, os.path.normpath()-ed root_path is
24+
# cached here.
25+
self._norm_root_path = os.path.normpath(self.root_path)
26+
27+
def user_config(self):
28+
# pyls specific configuration mainly focuses on per-project
29+
# configuration
30+
return {}
31+
32+
def project_config(self, document_path):
33+
settings = {}
34+
seen = set()
35+
36+
# To read config files in root_path even if any config file is
37+
# found by find_parents() in the directory other than
38+
# root_path, root_path is listed below as one of targets.
39+
#
40+
# On the other hand, "seen" is used to manage name of already
41+
# evaluated files, in order to avoid multiple evaluation of
42+
# config files in root_path.
43+
for target in self.root_path, document_path:
44+
# os.path.normpath is needed to treat that
45+
# "root_path/./foobar" and "root_path/foobar" are
46+
# identical.
47+
sources = map(os.path.normpath, find_parents(self.root_path, target, PROJECT_CONFIGS))
48+
files = []
49+
for source in sources:
50+
if source not in seen:
51+
files.append(source)
52+
seen.add(source)
53+
if not files:
54+
continue # there is no file to be read in
55+
56+
parsed = self.parse_config(self.read_config_from_files(files),
57+
CONFIG_KEY, OPTIONS)
58+
if not parsed:
59+
continue # no pyls specific configuration
60+
61+
settings = merge_dicts(settings, parsed)
62+
63+
return settings

0 commit comments

Comments
 (0)