From a3b6ab04b3588732cc87e7de08844c3b2a66c66f Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Fri, 6 Oct 2023 15:06:46 +0200 Subject: [PATCH] ENH: disable word wrap if `.prettierrc` has been removed --- .vscode/settings.json | 3 +-- src/repoma/check_dev_files/prettier.py | 16 ++++++++++++---- src/repoma/utilities/vscode.py | 23 ++++++++++++++++++++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 85ecb76a..136178a5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,8 +12,7 @@ "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[markdown]": { - "editor.defaultFormatter": "esbenp.prettier-vscode", - "editor.wordWrap": "off" + "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[python]": { "editor.codeActionsOnSave": { diff --git a/src/repoma/check_dev_files/prettier.py b/src/repoma/check_dev_files/prettier.py index 5e4711f2..f2988228 100644 --- a/src/repoma/check_dev_files/prettier.py +++ b/src/repoma/check_dev_files/prettier.py @@ -47,10 +47,10 @@ def _remove_configuration() -> None: def _fix_config_content(no_prettierrc: bool) -> None: if no_prettierrc: - if CONFIG_PATH.prettier.exists(): - os.remove(CONFIG_PATH.prettier) - msg = f"Removed {CONFIG_PATH.prettier} as requested by --no-prettierrc" - raise PrecommitError(msg) + executor = Executor() + executor(__remove_prettierrc) + executor(vscode.remove_setting, {"[markdown]": "editor.wordWrap"}) + executor.finalize() else: if not CONFIG_PATH.prettier.exists(): existing_content = "" @@ -77,6 +77,14 @@ def _fix_config_content(no_prettierrc: bool) -> None: raise PrecommitError(msg) +def __remove_prettierrc() -> None: + if not CONFIG_PATH.prettier.exists(): + return + CONFIG_PATH.prettier.unlink() + msg = f"Removed {CONFIG_PATH.prettier} as requested by --no-prettierrc" + raise PrecommitError(msg) + + def _update_prettier_ignore() -> None: __remove_forbidden_paths() __insert_expected_paths() diff --git a/src/repoma/utilities/vscode.py b/src/repoma/utilities/vscode.py index 18e4fc64..ede02456 100644 --- a/src/repoma/utilities/vscode.py +++ b/src/repoma/utilities/vscode.py @@ -1,8 +1,9 @@ """Helper functions for modifying a VSCode configuration.""" import json +from copy import deepcopy from pathlib import Path -from typing import Iterable +from typing import Iterable, Union from repoma.errors import PrecommitError from repoma.utilities.executor import Executor @@ -10,6 +11,26 @@ from . import CONFIG_PATH +def remove_setting(key: Union[str, dict]) -> None: + old = __load_config(CONFIG_PATH.vscode_settings, create=True) + new = deepcopy(old) + _recursive_remove_setting(key, new) + _update_settings(old, new) + + +def _recursive_remove_setting(nested_keys: Union[str, dict], settings: dict) -> None: + if isinstance(nested_keys, str) and nested_keys in settings: + settings.pop(nested_keys) + elif isinstance(nested_keys, dict): + for key, sub_keys in nested_keys.items(): + if key not in settings: + continue + if isinstance(sub_keys, str): + sub_keys = [sub_keys] + for sub_key in sub_keys: + _recursive_remove_setting(sub_key, settings[key]) + + def remove_settings(keys: Iterable[str]) -> None: removed_keys = set(keys) settings = __load_config(CONFIG_PATH.vscode_settings, create=True)