Skip to content

Commit

Permalink
add support for ruff extension option
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-cw committed Nov 10, 2023
1 parent be27747 commit b6b12b6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ def build_arguments(
if not PurePath(document_path).match(path):
continue
args.append(f"--ignore={','.join(errors)}")
if settings.extension:
extension_pairs = ",".join(
f"{ext}:{lang}" for ext, lang in settings.extension.items()
)
args.append(f"--extension={extension_pairs}")

if extra_arguments:
args.extend(extra_arguments)
Expand Down
2 changes: 2 additions & 0 deletions pylsp_ruff/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PluginSettings:

severities: Optional[Dict[str, str]] = None

extension: Optional[Dict[str, str]] = None


def to_camel_case(snake_str: str) -> str:
components = snake_str.split("_")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
requires-python = ">=3.7"
license = {text = "MIT"}
dependencies = [
"ruff>=0.1.0, <0.2.0",
"ruff>=0.1.5, <0.2.0",
"python-lsp-server",
"lsprotocol>=2022.0.0a1",
"tomli>=1.1.0; python_version < '3.11'",
Expand Down
36 changes: 36 additions & 0 deletions tests/test_ruff_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_ruff_config_param(workspace):
"config": ruff_conf,
"extendSelect": ["D", "F"],
"extendIgnore": ["E"],
"extension": {"ipynb": "python"},
}
}
}
Expand All @@ -96,6 +97,7 @@ def test_ruff_config_param(workspace):
assert f"--config={ruff_conf}" in call_args
assert "--extend-select=D,F" in call_args
assert "--extend-ignore=E" in call_args
assert "--extension=ipynb:python" in call_args


def test_ruff_executable_param(workspace):
Expand Down Expand Up @@ -242,3 +244,37 @@ def f():
assert diag["code"] != "F401"

os.unlink(os.path.join(workspace.root_path, "pyproject.toml"))


def test_notebook_input(workspace):
doc_str = r"""
print('hi')
import os
def f():
a = 2
"""
# attribute the python code to a notebook file name per jupyterlab-lsp
doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "Untitled.ipynb"))
workspace.put_document(doc_uri, doc_str)
doc = workspace.get_document(doc_uri)

diags = ruff_lint.pylsp_lint(workspace, doc)
# without the extension option, we get a syntax error because ruff expects JSON
assert len(diags)
assert diags[0]["code"] == "E999"

workspace._config.update(
{
"plugins": {
"ruff": {
"extension": {"ipynb": "python"},
}
}
}
)
diags = ruff_lint.pylsp_lint(workspace, doc)
diag_codes = [diag["code"] for diag in diags]
assert "E999" not in diag_codes
assert "E402" in diag_codes
assert "F401" in diag_codes
assert "F841" in diag_codes

0 comments on commit b6b12b6

Please sign in to comment.