|
1 | 1 | import logging
|
2 |
| -from typing import Dict |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from typing import Dict, Optional |
3 | 5 |
|
4 | 6 | import black
|
5 | 7 | import toml
|
6 | 8 | from pylsp import hookimpl
|
7 | 9 |
|
8 | 10 | logger = logging.getLogger(__name__)
|
9 | 11 |
|
| 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 | + |
10 | 26 |
|
11 | 27 | @hookimpl(tryfirst=True)
|
12 | 28 | def pylsp_format_document(document):
|
@@ -82,8 +98,12 @@ def load_config(filename: str) -> Dict:
|
82 | 98 | pyproject_filename = root / "pyproject.toml"
|
83 | 99 |
|
84 | 100 | 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 |
87 | 107 |
|
88 | 108 | try:
|
89 | 109 | pyproject_toml = toml.load(str(pyproject_filename))
|
|
0 commit comments