Skip to content

Commit

Permalink
DX: remove sort-lines VSCode extension (#299)
Browse files Browse the repository at this point in the history
* ENH: ignore capitalization in extension IDs
  • Loading branch information
redeboer authored Feb 11, 2024
1 parent bad7c4a commit 1f57d48
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ vscode:
- ms-vsliveshare.vsliveshare
- redhat.vscode-yaml
- ryanluker.vscode-coverage-gutters
- Soulcode.vscode-unwanted-extensions
- soulcode.vscode-unwanted-extensions
- stkb.rewrap
- streetsidesoftware.code-spell-checker
- tamasfe.even-better-toml
- tyriar.sort-lines
- yzhang.markdown-all-in-one
6 changes: 3 additions & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
"ms-vsliveshare.vsliveshare",
"redhat.vscode-yaml",
"ryanluker.vscode-coverage-gutters",
"Soulcode.vscode-unwanted-extensions",
"soulcode.vscode-unwanted-extensions",
"stkb.rewrap",
"streetsidesoftware.code-spell-checker",
"tamasfe.even-better-toml",
"tyriar.sort-lines",
"yzhang.markdown-all-in-one"
],
"unwantedRecommendations": [
Expand All @@ -30,6 +29,7 @@
"ms-python.flake8",
"ms-python.isort",
"ms-python.pylint",
"travisillig.vscode-json-stable-stringify"
"travisillig.vscode-json-stable-stringify",
"tyriar.sort-lines"
]
}
5 changes: 5 additions & 0 deletions src/compwa_policy/check_dev_files/vscode.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def _update_extensions() -> None:
# cspell:ignore travisillig
unwanted=True,
)
executor(
vscode.remove_extension_recommendation,
"tyriar.sort-lines", # cspell:ignore tyriar
unwanted=True,
)
executor(
vscode.remove_extension_recommendation,
"garaioag.garaio-vscode-unwanted-recommendations",
Expand Down
14 changes: 10 additions & 4 deletions src/compwa_policy/utilities/vscode.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def add_unwanted_extension(extension_name: str) -> None:

def __add_extension(extension_name: str, key: str, msg: str) -> None:
config = __load_config(CONFIG_PATH.vscode_extensions, create=True)
recommended_extensions = config.get(key, [])
recommended_extensions = __to_lower(config.get(key, []))
extension_name = extension_name.lower()
if extension_name not in set(recommended_extensions):
recommended_extensions.append(extension_name)
config[key] = sorted(recommended_extensions)
Expand All @@ -131,12 +132,13 @@ def __add_extension(extension_name: str, key: str, msg: str) -> None:
def remove_extension_recommendation(
extension_name: str, *, unwanted: bool = False
) -> None:
def _remove() -> None:
def _remove(extension_name: str) -> None:
if not CONFIG_PATH.vscode_extensions.exists():
return
with open(CONFIG_PATH.vscode_extensions) as stream:
config = json.load(stream)
recommended_extensions = list(config.get("recommendations", []))
recommended_extensions = __to_lower(config.get("recommendations", []))
extension_name = extension_name.lower()
if extension_name in recommended_extensions:
recommended_extensions.remove(extension_name)
config["recommendations"] = sorted(recommended_extensions)
Expand All @@ -145,12 +147,16 @@ def _remove() -> None:
raise PrecommitError(msg)

executor = Executor()
executor(_remove)
executor(_remove, extension_name)
if unwanted:
executor(add_unwanted_extension, extension_name)
executor.finalize()


def __to_lower(lst: list[str]) -> list[str]:
return [e.lower() for e in lst]


def __dump_config(config: dict, path: Path) -> None:
with open(path, "w") as stream:
json.dump(sort_case_insensitive(config), stream, indent=2)
Expand Down

0 comments on commit 1f57d48

Please sign in to comment.