Skip to content

Commit

Permalink
ENH: use case-sensitive sorting in .vscode/settings.json (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Oct 8, 2024
1 parent 32ea923 commit 27df574
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/compwa_policy/check_dev_files/cspell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
50 changes: 2 additions & 48 deletions src/compwa_policy/utilities/vscode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
<https://code.visualstudio.com/updates/v1_76#_jsonc-document-sorting>`_.
>>> 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)
Expand Down

0 comments on commit 27df574

Please sign in to comment.