Skip to content

Support global config as a fallback #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pylsp_black/plugin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import logging
from typing import Dict
import os
from pathlib import Path
from typing import Dict, Optional

import black
import toml
from pylsp import hookimpl

logger = logging.getLogger(__name__)

GLOBAL_CONFIG: Optional[Path] = None
try:
if os.name == "nt":
GLOBAL_CONFIG = Path.home() / ".black"
elif "XDG_CONFIG_HOME" in os.environ:
GLOBAL_CONFIG = Path(os.environ["XDG_CONFIG_HOME"]) / "black"
else:
GLOBAL_CONFIG = Path.home() / ".config" / "black"
except Exception as e:
logger.error("Error determining black global config file path: %s", e)
else:
if GLOBAL_CONFIG is not None and GLOBAL_CONFIG.exists():
logger.info("Found black global config file at %s", GLOBAL_CONFIG)


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

if not pyproject_filename.is_file():
logger.info("Using defaults: %r", defaults)
return defaults
if GLOBAL_CONFIG is not None and GLOBAL_CONFIG.exists():
pyproject_filename = GLOBAL_CONFIG
logger.info("Using global black config at %s", pyproject_filename)
else:
logger.info("Using defaults: %r", defaults)
return defaults

try:
pyproject_toml = toml.load(str(pyproject_filename))
Expand Down