Skip to content

Commit

Permalink
ENH: set import sorting in VSCode for Ruff (#247)
Browse files Browse the repository at this point in the history
* BREAK: merge `set_setting` and `set_sub_setting` functions
* MAINT: rename `_update_settings_if_changed()`
  • Loading branch information
redeboer authored Dec 8, 2023
1 parent 0f4f583 commit 14fbd00
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.rulers": [88]
Expand Down
22 changes: 10 additions & 12 deletions src/repoma/check_dev_files/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ruamel.yaml.comments import CommentedMap

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
from repoma.utilities import CONFIG_PATH, vscode
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import (
remove_precommit_hook,
Expand All @@ -18,11 +18,6 @@
to_toml_array,
write_pyproject,
)
from repoma.utilities.vscode import (
add_extension_recommendation,
set_setting,
set_sub_setting,
)


def main(has_notebooks: bool) -> None:
Expand All @@ -42,14 +37,17 @@ def main(has_notebooks: bool) -> None:
repo_url="https://github.com/psf/black",
)
executor(_update_precommit_repo, has_notebooks)
executor(add_extension_recommendation, "ms-python.black-formatter")
executor(set_setting, {"black-formatter.importStrategy": "fromEnvironment"})
executor(vscode.add_extension_recommendation, "ms-python.black-formatter")
executor(
vscode.update_settings, {"black-formatter.importStrategy": "fromEnvironment"}
)
executor(
set_sub_setting,
"[python]",
vscode.update_settings,
{
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.rulers": [88],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.rulers": [88],
},
},
)
executor(remove_precommit_hook, "nbqa-black")
Expand Down
19 changes: 9 additions & 10 deletions src/repoma/check_dev_files/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@
)

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
from repoma.utilities import CONFIG_PATH, vscode
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import (
find_repo,
load_round_trip_precommit_config,
update_single_hook_precommit_repo,
)
from repoma.utilities.vscode import (
add_extension_recommendation,
set_sub_setting,
)


def main() -> None:
Expand All @@ -38,7 +34,7 @@ def main() -> None:
executor(remove_zenodo_json)
executor(check_citation_keys)
executor(add_json_schema_precommit)
executor(add_extension_recommendation, "redhat.vscode-yaml")
executor(vscode.add_extension_recommendation, "redhat.vscode-yaml")
executor(update_vscode_settings)
executor.finalize()

Expand Down Expand Up @@ -219,9 +215,12 @@ def add_json_schema_precommit() -> None:


def update_vscode_settings() -> None:
set_sub_setting(
key="yaml.schemas",
values={
"https://citation-file-format.github.io/1.2.0/schema.json": "CITATION.cff"
vscode.update_settings(
{
"yaml.schemas": {
"https://citation-file-format.github.io/1.2.0/schema.json": (
"CITATION.cff"
)
}
},
)
3 changes: 1 addition & 2 deletions src/repoma/check_dev_files/cspell.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
update_single_hook_precommit_repo,
)
from repoma.utilities.readme import add_badge, remove_badge
from repoma.utilities.vscode import sort_case_insensitive

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -234,4 +233,4 @@ def sort_key(value: Any) -> str:
return sorted(content, key=sort_key)
if section_name == "ignoreWords":
return sorted(content)
return sort_case_insensitive(content)
return vscode.sort_case_insensitive(content)
5 changes: 2 additions & 3 deletions src/repoma/check_dev_files/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import os

from repoma.errors import PrecommitError
from repoma.utilities import remove_configs, remove_from_gitignore
from repoma.utilities import remove_configs, remove_from_gitignore, vscode
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import remove_precommit_hook
from repoma.utilities.vscode import remove_extension_recommendation


def remove_deprecated_tools(keep_issue_templates: bool) -> None:
Expand All @@ -33,7 +32,7 @@ def _remove_markdownlint() -> None:
executor(remove_configs, [".markdownlint.json", ".markdownlint.yaml"])
executor(remove_from_gitignore, ".markdownlint.json")
executor(
remove_extension_recommendation,
vscode.remove_extension_recommendation,
# cspell:ignore davidanson markdownlint
extension_name="davidanson.vscode-markdownlint",
unwanted=True,
Expand Down
13 changes: 4 additions & 9 deletions src/repoma/check_dev_files/github_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
from ruamel.yaml.scalarstring import DoubleQuotedScalarString

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH, REPOMA_DIR, hash_file, write
from repoma.utilities import CONFIG_PATH, REPOMA_DIR, hash_file, vscode, write
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import PrecommitConfig
from repoma.utilities.project_info import PythonVersion, get_pypi_name
from repoma.utilities.vscode import (
add_extension_recommendation,
remove_extension_recommendation,
set_setting,
)
from repoma.utilities.yaml import create_prettier_round_trip_yaml

if TYPE_CHECKING:
Expand Down Expand Up @@ -275,14 +270,14 @@ def _recommend_vscode_extension() -> None:
return
# cspell:ignore cschleiden
executor = Executor()
executor(remove_extension_recommendation, "cschleiden.vscode-github-actions")
executor(add_extension_recommendation, "github.vscode-github-actions")
executor(vscode.remove_extension_recommendation, "cschleiden.vscode-github-actions")
executor(vscode.add_extension_recommendation, "github.vscode-github-actions")
ci_workflow = CONFIG_PATH.github_workflow_dir / "ci.yml"
if ci_workflow.exists():
action_settings = {
"github-actions.workflows.pinned.workflows": [str(ci_workflow)],
}
set_setting(action_settings)
vscode.update_settings(action_settings)
executor.finalize()


Expand Down
16 changes: 5 additions & 11 deletions src/repoma/check_dev_files/mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@
from ini2toml.api import Translator

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
from repoma.utilities import CONFIG_PATH, vscode
from repoma.utilities.executor import Executor
from repoma.utilities.pyproject import get_sub_table, load_pyproject, write_pyproject
from repoma.utilities.vscode import (
add_extension_recommendation,
remove_extension_recommendation,
remove_settings,
set_setting,
)


def main() -> None:
Expand All @@ -30,20 +24,20 @@ def _update_vscode_settings() -> None:
executor = Executor()
if mypy_config is None:
executor(
remove_extension_recommendation,
vscode.remove_extension_recommendation,
"ms-python.mypy-type-checker",
unwanted=True,
)
executor(remove_settings, ["mypy-type-checker.importStrategy"])
executor(vscode.remove_settings, ["mypy-type-checker.importStrategy"])
else:
executor(add_extension_recommendation, "ms-python.mypy-type-checker")
executor(vscode.add_extension_recommendation, "ms-python.mypy-type-checker")
settings = {
"mypy-type-checker.args": [
"--config-file=${workspaceFolder}/pyproject.toml"
],
"mypy-type-checker.importStrategy": "fromEnvironment",
}
executor(set_setting, settings)
executor(vscode.update_settings, settings)
executor.finalize()


Expand Down
29 changes: 14 additions & 15 deletions src/repoma/check_dev_files/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
has_setup_cfg_build_system,
)
from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH, natural_sorting, remove_configs
from repoma.utilities import CONFIG_PATH, natural_sorting, remove_configs, vscode
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import (
remove_precommit_hook,
Expand All @@ -34,12 +34,6 @@
write_pyproject,
)
from repoma.utilities.readme import add_badge, remove_badge
from repoma.utilities.vscode import (
add_extension_recommendation,
remove_extension_recommendation,
remove_settings,
set_setting,
)

if TYPE_CHECKING:
from tomlkit.items import Array, Table
Expand Down Expand Up @@ -106,11 +100,11 @@ def _remove_flake8() -> None:
executor(__remove_nbqa_option, "flake8")
executor(__uninstall, "flake8")
executor(__uninstall, "pep8-naming")
executor(remove_extension_recommendation, "ms-python.flake8", unwanted=True)
executor(vscode.remove_extension_recommendation, "ms-python.flake8", unwanted=True)
executor(remove_precommit_hook, "autoflake") # cspell:ignore autoflake
executor(remove_precommit_hook, "flake8")
executor(remove_precommit_hook, "nbqa-flake8")
executor(remove_settings, ["flake8.importStrategy"])
executor(vscode.remove_settings, ["flake8.importStrategy"])
executor.finalize()


Expand All @@ -119,10 +113,10 @@ def _remove_isort() -> None:
executor(__remove_isort_settings)
executor(__remove_nbqa_option, "black")
executor(__remove_nbqa_option, "isort")
executor(remove_extension_recommendation, "ms-python.isort", unwanted=True)
executor(vscode.remove_extension_recommendation, "ms-python.isort", unwanted=True)
executor(remove_precommit_hook, "isort")
executor(remove_precommit_hook, "nbqa-isort")
executor(remove_settings, ["isort.check", "isort.importStrategy"])
executor(vscode.remove_settings, ["isort.check", "isort.importStrategy"])
executor(remove_badge, r".*https://img\.shields\.io/badge/%20imports\-isort")
executor.finalize()

Expand Down Expand Up @@ -170,10 +164,10 @@ def _remove_pylint() -> None:
executor = Executor()
executor(remove_configs, [".pylintrc"]) # cspell:ignore pylintrc
executor(__uninstall, "pylint")
executor(remove_extension_recommendation, "ms-python.pylint", unwanted=True)
executor(vscode.remove_extension_recommendation, "ms-python.pylint", unwanted=True)
executor(remove_precommit_hook, "pylint")
executor(remove_precommit_hook, "nbqa-pylint")
executor(remove_settings, ["pylint.importStrategy"])
executor(vscode.remove_settings, ["pylint.importStrategy"])
executor.finalize()


Expand Down Expand Up @@ -532,10 +526,15 @@ def _update_precommit_hook(has_notebooks: bool) -> None:
def _update_vscode_settings() -> None:
# cspell:ignore charliermarsh
executor = Executor()
executor(add_extension_recommendation, "charliermarsh.ruff")
executor(vscode.add_extension_recommendation, "charliermarsh.ruff")
executor(
set_setting,
vscode.update_settings,
{
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
}
},
"ruff.enable": True,
"ruff.organizeImports": True,
},
Expand Down
17 changes: 9 additions & 8 deletions src/repoma/check_dev_files/vscode.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ def _update_extensions() -> None:
def _update_settings(has_notebooks: bool) -> None:
executor = Executor()
executor(
vscode.set_setting,
vscode.update_settings,
{
"diffEditor.experimental.showMoves": True,
"editor.formatOnSave": True,
"rewrap.wrappingColumn": 88, # black
},
)
executor(
vscode.set_sub_setting,
"[git-commit]",
vscode.update_settings,
{
"editor.rulers": [72],
"rewrap.wrappingColumn": 72,
"[git-commit]": {
"editor.rulers": [72],
"rewrap.wrappingColumn": 72,
},
},
)
executor(_remove_outdated_settings)
Expand Down Expand Up @@ -86,7 +87,7 @@ def _update_doc_settings() -> None:
"livePreview.defaultPreviewPath": "docs/_build/html",
}
executor = Executor()
executor(vscode.set_setting, settings)
executor(vscode.update_settings, settings)
executor(
vscode.add_extension_recommendation,
"executablebookproject.myst-highlight", # cspell:ignore executablebookproject
Expand All @@ -102,7 +103,7 @@ def _update_notebook_settings() -> None:
settings = {
"notebook.gotoSymbols.showAllSymbols": True,
}
vscode.set_setting(settings)
vscode.update_settings(settings)


def _update_pytest_settings() -> None:
Expand All @@ -113,4 +114,4 @@ def _update_pytest_settings() -> None:
"python.testing.pytestEnabled": True,
"python.testing.unittestEnabled": False,
}
vscode.set_setting(pytest_settings)
vscode.update_settings(pytest_settings)
Loading

0 comments on commit 14fbd00

Please sign in to comment.