From bf2c3ce296adc1af8f184bf6b57ec7e295a134b5 Mon Sep 17 00:00:00 2001 From: Ivan Panchishin Date: Sun, 28 Jan 2024 00:42:49 +0300 Subject: [PATCH] The 'match' option in a config file isn't ignored and small refactoring --- pylsp/plugins/pydocstyle_lint.py | 43 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/pylsp/plugins/pydocstyle_lint.py b/pylsp/plugins/pydocstyle_lint.py index 7d04bdf0..d24c6ae2 100644 --- a/pylsp/plugins/pydocstyle_lint.py +++ b/pylsp/plugins/pydocstyle_lint.py @@ -3,9 +3,9 @@ import contextlib import logging -import os import re import sys +from pathlib import Path import pydocstyle @@ -17,7 +17,6 @@ pydocstyle_logger = logging.getLogger(pydocstyle.utils.__name__) pydocstyle_logger.setLevel(logging.INFO) -DEFAULT_MATCH_RE = pydocstyle.config.ConfigurationParser.DEFAULT_MATCH_RE DEFAULT_MATCH_DIR_RE = pydocstyle.config.ConfigurationParser.DEFAULT_MATCH_DIR_RE @@ -33,30 +32,36 @@ def pylsp_lint(config, workspace, document): settings = config.plugin_settings("pydocstyle", document_path=document.path) log.debug("Got pydocstyle settings: %s", settings) - # Explicitly passing a path to pydocstyle means it doesn't respect the --match flag, so do it ourselves - filename_match_re = re.compile(settings.get("match", DEFAULT_MATCH_RE) + "$") - if not filename_match_re.match(os.path.basename(document.path)): - return [] - - # Likewise with --match-dir + # We explicitly pass the path to `pydocstyle`, so it ignores `--match-dir`. But + # we can match the directory ourselves. dir_match_re = re.compile(settings.get("matchDir", DEFAULT_MATCH_DIR_RE) + "$") - if not dir_match_re.match(os.path.basename(os.path.dirname(document.path))): + if not dir_match_re.match(Path(document.path).parent.name): return [] args = [document.path] - if settings.get("convention"): - args.append("--convention=" + settings["convention"]) + def append_if_exists(setting_name, arg_name=None): + """Append an argument if it exists in `settings`.""" + if setting_name not in settings: + return False + + if isinstance(settings[setting_name], str): + value = settings[setting_name] + else: + value = ",".join(settings[setting_name]) + + args.append(f"--{arg_name or setting_name}={value}") + return True - if settings.get("addSelect"): - args.append("--add-select=" + ",".join(settings["addSelect"])) - if settings.get("addIgnore"): - args.append("--add-ignore=" + ",".join(settings["addIgnore"])) + if append_if_exists("convention"): + append_if_exists("addSelect", "add-select") + append_if_exists("addIgnore", "add-ignore") + elif append_if_exists("select"): + pass + else: + append_if_exists("ignore") - elif settings.get("select"): - args.append("--select=" + ",".join(settings["select"])) - elif settings.get("ignore"): - args.append("--ignore=" + ",".join(settings["ignore"])) + append_if_exists("match") log.info("Using pydocstyle args: %s", args)