Skip to content

Commit 24ece76

Browse files
committed
Support global config as a fallback
Based on the [black docs](https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#where-black-looks-for-the-file) black will fall back to a global settings file if there is not a project scoped file. This just supports the same behavior in this plugin.
1 parent bc86305 commit 24ece76

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

pylsp_black/plugin.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import os
2+
from pathlib import Path
13
from typing import Dict
24

35
import black
46
import toml
57
from pylsp import hookimpl
68

9+
GLOBAL_CONFIG: Path
10+
if os.name == "nt":
11+
GLOBAL_CONFIG = Path.home() / ".black"
12+
elif "XDG_CONFIG_HOME" in os.environ:
13+
GLOBAL_CONFIG = Path(os.environ["XDG_CONFIG_HOME"]) / "black"
14+
else:
15+
GLOBAL_CONFIG = Path.home() / ".config/black"
16+
717

818
@hookimpl(tryfirst=True)
919
def pylsp_format_document(document):
@@ -74,7 +84,10 @@ def load_config(filename: str) -> Dict:
7484
pyproject_filename = root / "pyproject.toml"
7585

7686
if not pyproject_filename.is_file():
77-
return defaults
87+
if GLOBAL_CONFIG.exists():
88+
pyproject_filename = GLOBAL_CONFIG
89+
else:
90+
return defaults
7891

7992
try:
8093
pyproject_toml = toml.load(str(pyproject_filename))

0 commit comments

Comments
 (0)