Skip to content

Commit 9b6e716

Browse files
authored
Merge pull request #19 from jdost/master
Support global config as a fallback
2 parents f31dbf6 + b1239d1 commit 9b6e716

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

pylsp_black/plugin.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import logging
2-
from typing import Dict
2+
import os
3+
from pathlib import Path
4+
from typing import Dict, Optional
35

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

810
logger = logging.getLogger(__name__)
911

12+
GLOBAL_CONFIG: Optional[Path] = None
13+
try:
14+
if os.name == "nt":
15+
GLOBAL_CONFIG = Path.home() / ".black"
16+
elif "XDG_CONFIG_HOME" in os.environ:
17+
GLOBAL_CONFIG = Path(os.environ["XDG_CONFIG_HOME"]) / "black"
18+
else:
19+
GLOBAL_CONFIG = Path.home() / ".config" / "black"
20+
except Exception as e:
21+
logger.error("Error determining black global config file path: %s", e)
22+
else:
23+
if GLOBAL_CONFIG is not None and GLOBAL_CONFIG.exists():
24+
logger.info("Found black global config file at %s", GLOBAL_CONFIG)
25+
1026

1127
@hookimpl(tryfirst=True)
1228
def pylsp_format_document(document):
@@ -82,8 +98,12 @@ def load_config(filename: str) -> Dict:
8298
pyproject_filename = root / "pyproject.toml"
8399

84100
if not pyproject_filename.is_file():
85-
logger.info("Using defaults: %r", defaults)
86-
return defaults
101+
if GLOBAL_CONFIG is not None and GLOBAL_CONFIG.exists():
102+
pyproject_filename = GLOBAL_CONFIG
103+
logger.info("Using global black config at %s", pyproject_filename)
104+
else:
105+
logger.info("Using defaults: %r", defaults)
106+
return defaults
87107

88108
try:
89109
pyproject_toml = toml.load(str(pyproject_filename))

0 commit comments

Comments
 (0)