Skip to content

Commit 2eb2d6f

Browse files
authored
Format signatures in LSP hover and completion documentation (#4864)
## 📝 Summary - formats signatures using ruff so that they are easier to read - depends on python-lsp/python-lsp-server#631 ## 🔍 Description of Changes Adds `pylsp_signatures_to_markdown` hook, re-using existing `ruff` formatting utility. | Before | After | |--|--| | ![image](https://github.com/user-attachments/assets/2ffb4e27-6a26-4f69-ae7a-ed51faa5afc2) | ![Screenshot from 2025-05-09 22-01-22](https://github.com/user-attachments/assets/56bde23c-193d-4800-a2c6-81b80ab72025) | Note: this does not help with `pd.read_csv`; I am not sure why it is not hitting this code path, but notably it happens on the completion items which do not have a docstring either. ## 📋 Checklist - [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md). - [ ] For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on [Discord](https://marimo.io/discord?ref=pr), or the community [discussions](https://github.com/marimo-team/marimo/discussions) (Please provide a link if applicable). - [ ] I have added tests for the changes made. - [x] I have run the code and verified that it works as expected. ## 📜 Reviewers @mscolnick
1 parent aa3a7f6 commit 2eb2d6f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

marimo/_server/lsp.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from marimo._messaging.ops import Alert
1515
from marimo._server.utils import find_free_port
1616
from marimo._tracer import server_tracer
17+
from marimo._types.ids import CellId_t
18+
from marimo._utils.formatter import DefaultFormatter, FormatError
1719
from marimo._utils.paths import marimo_package_path
1820

1921
LOGGER = _loggers.marimo_logger()
@@ -295,3 +297,27 @@ def any_lsp_server_running(config: MarimoConfig) -> bool:
295297
for server in language_servers.values()
296298
)
297299
return (copilot_enabled is not False) or language_servers_enabled
300+
301+
302+
if DependencyManager.pylsp.has():
303+
from pylsp import hookimpl
304+
305+
formatter = DefaultFormatter(line_length=88)
306+
307+
def format_signature(signature: str) -> str:
308+
try:
309+
signature_as_func = f"def {signature.strip()}:\n pass"
310+
dummy_cell_id = cast(CellId_t, "")
311+
reformatted = formatter.format({dummy_cell_id: signature_as_func})[
312+
dummy_cell_id
313+
]
314+
signature = reformatted.removeprefix("def ").removesuffix(
315+
":\n pass"
316+
)
317+
except (ModuleNotFoundError, FormatError):
318+
pass
319+
return "```python\n" + signature + "\n```\n"
320+
321+
@hookimpl
322+
def pylsp_signatures_to_markdown(signatures: list[str]) -> str:
323+
return format_signature("\n".join(signatures))

pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ homepage = "https://github.com/marimo-team/marimo"
7878
[project.entry-points."docstring_to_markdown"]
7979
marimo_converter = "marimo._utils.docs:MarimoConverter"
8080

81+
[project.entry-points."pylsp"]
82+
marimo_plugin = "marimo._server.lsp"
83+
8184
[project.optional-dependencies]
8285
sql = [
8386
"duckdb>=1.0.0",
@@ -192,6 +195,7 @@ dependencies = [
192195
"sqlglot>=23.4",
193196
"sqlalchemy>=2.0.40",
194197
"loro>=1.5.0",
198+
"python-lsp-server>=1.12.1",
195199
"pandas-stubs>=1.5.3.230321",
196200
"pyiceberg>=0.9.0",
197201
"types-Pillow~=10.2.0.20240520",
@@ -470,6 +474,11 @@ exclude = [
470474
]
471475
warn_unused_ignores = false
472476

477+
[[tool.mypy.overrides]]
478+
module = "pylsp"
479+
# until https://github.com/python-lsp/python-lsp-server/pull/641 is released
480+
follow_untyped_imports = true
481+
473482
[tool.pytest.ini_options]
474483
minversion = "6.0"
475484
addopts = "-ra -q -v --ignore tests/_cli/ipynb_data --ignore tests/_ast/codegen_data --ignore tests/_ast/app_data"

0 commit comments

Comments
 (0)