diff --git a/docs/lsp-devtools/guide/record-command.rst b/docs/lsp-devtools/guide/record-command.rst index d0dece1..0465cac 100644 --- a/docs/lsp-devtools/guide/record-command.rst +++ b/docs/lsp-devtools/guide/record-command.rst @@ -29,6 +29,7 @@ Here are some example usages of the ``record`` command that you may find useful. The following command will save to a JSON file only the client's info and :class:`pygls:lsprotocol.types.ClientCapabilities` sent during the ``initialize`` request - useful for :ref:`adding clients to pytest-lsp `! 😉 + :: lsp-devtools record -f '{{"clientInfo": {.params.clientInfo}, "capabilities": {.params.capabilities}}}' --to-file _v.json @@ -291,21 +292,28 @@ Formatting messages Formatters ^^^^^^^^^^ -``lsp-devtools`` knows how to format the following LSP Types +``lsp-devtools`` provides the following formatters + +``json`` (default) + Renders objects as "pretty" JSON, equivalent to ``json.dumps(obj, indent=2)`` -``Position`` +``json-compact`` + Renders objects as JSON with no additional formatting, equivalent to ``json.dumps(obj)`` + +``position`` ``{"line": 1, "character": 2}`` will be rendered as ``1:2`` -``Range`` +``range`` ``{"start": {"line": 1, "character": 2}, "end": {"line": 3, "character": 4}}`` will be rendered as ``1:2-3:4`` -Additionally, any enum type can be used as a formatter in which case a number will be replaced with the corresponding name, for example:: + +Additionally, any enum type can be used as a formatter, where numbers will be replaced with their corresponding name, for example:: Format String: "{.type|MessageType}" - Value: Result: - 1 Error - 2 Warning - 3 Info - 4 Log + Value: Result: + {"type": 1} Error + {"type": 2} Warning + {"type": 3} Info + {"type": 4} Log diff --git a/lib/lsp-devtools/changes/130.enhancement.md b/lib/lsp-devtools/changes/130.enhancement.md new file mode 100644 index 0000000..21abf61 --- /dev/null +++ b/lib/lsp-devtools/changes/130.enhancement.md @@ -0,0 +1 @@ +Added formatters `json` and `json-compact` that can be used within format strings. diff --git a/lib/lsp-devtools/lsp_devtools/record/formatters.py b/lib/lsp-devtools/lsp_devtools/record/formatters.py index d8cb5cb..abd5c00 100644 --- a/lib/lsp-devtools/lsp_devtools/record/formatters.py +++ b/lib/lsp-devtools/lsp_devtools/record/formatters.py @@ -1,7 +1,9 @@ import json import re +from functools import partial from typing import Any from typing import Callable +from typing import Dict from typing import List from typing import Optional from typing import Tuple @@ -17,11 +19,11 @@ cache = lru_cache(None) -def format_json(obj: dict) -> str: +def format_json(obj: dict, *, indent: Union[str, int, None] = 2) -> str: if isinstance(obj, str): return obj - return json.dumps(obj, indent=2) + return json.dumps(obj, indent=indent) def format_position(position: dict) -> str: @@ -32,9 +34,11 @@ def format_range(range_: dict) -> str: return f"{format_position(range_['start'])}-{format_position(range_['end'])}" -FORMATTERS = { +FORMATTERS: Dict[str, Callable[[Any], str]] = { "position": format_position, "range": format_range, + "json": format_json, + "json-compact": partial(format_json, indent=None), } diff --git a/lib/lsp-devtools/tests/record/test_formatters.py b/lib/lsp-devtools/tests/record/test_formatters.py index 1f38e6d..13f01d3 100644 --- a/lib/lsp-devtools/tests/record/test_formatters.py +++ b/lib/lsp-devtools/tests/record/test_formatters.py @@ -18,6 +18,20 @@ {"method": "textDocument/completion"}, "The method 'textDocument/completion' was called", ), + ( + "{.position|json}", + { + "position": {"line": 1, "character": 2}, + }, + '{\n "line": 1,\n "character": 2\n}', + ), + ( + "{.position|json-compact}", + { + "position": {"line": 1, "character": 2}, + }, + '{"line": 1, "character": 2}', + ), ( "{.method} {.params.textDocument.uri}:{.params.position}", {