Skip to content

Commit b41310f

Browse files
committed
Allow clients to configure if loading Black's config is cached or not
Also add a test for that.
1 parent 568a9f5 commit b41310f

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

pylsp_black/plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def format_text(*, text, config):
106106

107107

108108
@lru_cache(100)
109-
def load_config(filename: str, client_config: Config) -> Dict:
109+
def _load_config(filename: str, client_config: Config) -> Dict:
110110
settings = client_config.plugin_settings("black")
111111

112112
defaults = {
@@ -173,3 +173,13 @@ def load_config(filename: str, client_config: Config) -> Dict:
173173
logger.info("Using config from %s: %r", pyproject_filename, config)
174174

175175
return config
176+
177+
178+
def load_config(filename: str, client_config: Config) -> Dict:
179+
settings = client_config.plugin_settings("black")
180+
181+
# Use the original, not cached function to load settings if requested
182+
if not settings.get("cache_config", True):
183+
return _load_config.__wrapped__(filename, client_config)
184+
185+
return _load_config(filename, client_config)

tests/test_plugin.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
from pylsp.workspace import Document, Workspace
1515

1616
# Local imports
17-
from pylsp_black.plugin import load_config, pylsp_format_document, pylsp_format_range
17+
from pylsp_black.plugin import (
18+
_load_config,
19+
load_config,
20+
pylsp_format_document,
21+
pylsp_format_range,
22+
)
1823

1924
here = Path(__file__).parent
2025
fixtures_dir = here / "fixtures"
@@ -30,7 +35,9 @@ def workspace(tmpdir):
3035
def config(workspace):
3136
"""Return a config object."""
3237
cfg = Config(workspace.root_uri, {}, 0, {})
33-
cfg._plugin_settings = {"plugins": {"black": {"line_length": 88}}}
38+
cfg._plugin_settings = {
39+
"plugins": {"black": {"line_length": 88}, "black": {"cache_config": True}}
40+
}
3441
return cfg
3542

3643

@@ -301,3 +308,19 @@ def test_pylsp_format_line_length(
301308
"newText": formatted_line_length.source,
302309
}
303310
]
311+
312+
313+
def test_cache_config(config, unformatted_document):
314+
# Cache should be working by default
315+
for _ in range(5):
316+
pylsp_format_document(config, unformatted_document)
317+
assert _load_config.cache_info().hits == 4
318+
319+
# Clear cache and disable it
320+
_load_config.cache_clear()
321+
config.update({"plugins": {"black": {"cache_config": False}}})
322+
323+
# Cache should not be working now
324+
for _ in range(5):
325+
pylsp_format_document(config, unformatted_document)
326+
assert _load_config.cache_info().hits == 0

0 commit comments

Comments
 (0)