From 27df5749742c77db8f5e14735cbc19ad14acebff Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:04:21 +0200 Subject: [PATCH] ENH: use case-sensitive sorting in `.vscode/settings.json` (#405) --- .vscode/settings.json | 2 +- src/compwa_policy/check_dev_files/cspell.py | 2 +- src/compwa_policy/utilities/vscode.py | 50 +-------------------- 3 files changed, 4 insertions(+), 50 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d65ef541..0ff169a5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -26,11 +26,11 @@ "[yaml]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, + "cSpell.enabled": true, "coverage-gutters.coverageFileNames": ["coverage.xml"], "coverage-gutters.coverageReportFileName": "**/htmlcov/index.html", "coverage-gutters.showGutterCoverage": false, "coverage-gutters.showLineCoverage": true, - "cSpell.enabled": true, "diffEditor.experimental.showMoves": true, "editor.formatOnSave": true, "files.associations": { diff --git a/src/compwa_policy/check_dev_files/cspell.py b/src/compwa_policy/check_dev_files/cspell.py index 4e06528e..7ac7d294 100644 --- a/src/compwa_policy/check_dev_files/cspell.py +++ b/src/compwa_policy/check_dev_files/cspell.py @@ -224,4 +224,4 @@ def sort_key(value: Any) -> str: return sorted(content, key=sort_key) if section_name == "ignoreWords": return sorted(content) - return vscode.sort_case_insensitive(content) + return sorted(content, key=str.casefold) diff --git a/src/compwa_policy/utilities/vscode.py b/src/compwa_policy/utilities/vscode.py index 1ed507e8..6377e1aa 100644 --- a/src/compwa_policy/utilities/vscode.py +++ b/src/compwa_policy/utilities/vscode.py @@ -2,19 +2,9 @@ from __future__ import annotations -import collections import json from collections import abc -from typing import ( - TYPE_CHECKING, - Any, - Dict, - Iterable, - OrderedDict, - TypeVar, - Union, - overload, -) +from typing import TYPE_CHECKING, Any, Dict, Iterable, TypeVar, Union from compwa_policy.errors import PrecommitError from compwa_policy.utilities import CONFIG_PATH @@ -183,46 +173,10 @@ def __to_lower(lst: list[str]) -> list[str]: def __dump_config(config: dict, path: Path) -> None: with open(path, "w") as stream: - json.dump(sort_case_insensitive(config), stream, indent=2) + json.dump(config, stream, indent=2, sort_keys=True) stream.write("\n") -@overload -def sort_case_insensitive(dct: dict[K, V]) -> OrderedDict[K, V]: ... # type: ignore[misc] -@overload -def sort_case_insensitive(dct: str) -> str: ... # type: ignore[misc] -@overload -def sort_case_insensitive(dct: Iterable[K]) -> list[K]: ... # type: ignore[misc] -@overload -def sort_case_insensitive(dct: K) -> K: ... -def sort_case_insensitive(dct): # type: ignore[no-untyped-def] - """Order a `dict` by key, **case-insensitive**. - - This function is implemented in order to :func:`~json.dump` a JSON file with a - sorting that is the same as `the one used by VS Code - `_. - - >>> import sys - >>> import pytest - >>> if sys.version_info >= (3, 12): - ... pytest.skip() - >>> sort_case_insensitive({ - ... "cSpell.enabled": True, - ... "coverage-gutters": ["test", "coverage.xml"], - ... }) - OrderedDict([('coverage-gutters', ['coverage.xml', 'test']), ('cSpell.enabled', True)]) - """ - if isinstance(dct, abc.Mapping): - return collections.OrderedDict({ - k: sort_case_insensitive(dct[k]) for k in sorted(dct, key=str.lower) - }) - if isinstance(dct, str): - return dct - if isinstance(dct, abc.Iterable): - return sorted(dct, key=lambda t: str(t).lower()) - return dct - - def __load_config(path: Path, create: bool = False) -> dict: if not path.exists() and create: path.parent.mkdir(exist_ok=True)