Skip to content

Commit

Permalink
Set up jupyter/jupyter_lab_config.py with ruff format (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
dycw authored Apr 15, 2024
1 parent 89576a1 commit 6d1e3d9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[bumpversion]
current_version = 0.1.105
current_version = 0.1.106
8 changes: 4 additions & 4 deletions git/aliases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ if command -v git >/dev/null 2>&1; then
# __amend, not reuse
if [ -n "${__force}" ]; then
if [ -n "${__no_verify}" ] && [ $# -eq 0 ]; then
git commit -n --__amend
git commit -n --amend
elif [ -n "${__no_verify}" ] && [ $# -eq 1 ]; then
git commit -nm "$1" --__amend
git commit -nm "$1" --amend
elif [ -z "${__no_verify}" ] && [ $# -eq 0 ]; then
git commit --__amend
git commit --amend
elif [ -z "${__no_verify}" ] && [ $# -eq 1 ]; then
git commit -m "$1" --__amend
git commit -m "$1" --amend
else
echo "Since --amend, expected at most 1 argument; got $#"
fi
Expand Down
46 changes: 9 additions & 37 deletions jupyter/jupyter_lab_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations # noqa: INP001

from contextlib import suppress
from subprocess import run
from typing import Any

with suppress(NameError):
c = get_config() # type: ignore[] # noqa: F821
Expand All @@ -11,50 +9,24 @@
try:
from jupyterlab_code_formatter.formatters import ( # type: ignore[]
SERVER_FORMATTERS,
BaseFormatter,
handle_line_ending_and_magic,
logger,
CommandLineFormatter,
)
except ModuleNotFoundError:
pass
else:

class RuffFormatFormatter(BaseFormatter):
label = "Apply Ruff Format Formatter - Confirmed working for 0.1.3"

class MyRuffFormat(CommandLineFormatter):
def __init__(self) -> None:
try:
from ruff.__main__ import find_ruff_bin # type: ignore[]

self.ruff_bin = find_ruff_bin()
ruff = find_ruff_bin()
except (ImportError, FileNotFoundError):
self.ruff_bin = "ruff"
ruff = "ruff"
self.command = [ruff, "format", "-"]

@property
def importable(self) -> bool:
return True

@handle_line_ending_and_magic
def format_code(
self,
code: str,
_notebook: bool, # noqa: FBT001
args: list[str] | None = None,
**_kwargs: Any,
) -> str:
_ = (_notebook, _kwargs)
if args is None:
args = []
process = run(
[self.ruff_bin, "format", "-"], # noqa: S603
input=code,
capture_output=True,
text=True,
check=False,
)
if process.stderr:
logger.info(process.stderr)
return code
return process.stdout

SERVER_FORMATTERS["ruff_format"] = RuffFormatFormatter()
def label(self) -> str:
return "Apply `ruff format`"

SERVER_FORMATTERS["my_ruff_format"] = MyRuffFormat()
9 changes: 1 addition & 8 deletions jupyter/jupyterlab_code_formatter.jsonc
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
{
"preferences": {
"default_formatter": {
"python": ["ruff", "ruff_format"],
"python": "my_ruff_format",
},
},
"formatOnSave": true,
"ruff": {
"args": ["--select=I"],
},
"suppressFormatterErrors": false,
"cacheFormatters": false,
"astyle": {
"args": [],
},
}

0 comments on commit 6d1e3d9

Please sign in to comment.