Skip to content

Commit

Permalink
lsp-devtools: Add json and json-compact formatters
Browse files Browse the repository at this point in the history
This gives more control over how items can be formatted.
  • Loading branch information
alcarney committed Jan 25, 2024
1 parent 7f9567c commit fd71659
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
26 changes: 17 additions & 9 deletions docs/lsp-devtools/guide/record-command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pytest-lsp-supported-clients>`! 😉


::

lsp-devtools record -f '{{"clientInfo": {.params.clientInfo}, "capabilities": {.params.capabilities}}}' --to-file <client_name>_v<version>.json
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/lsp-devtools/changes/130.enhancement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added formatters `json` and `json-compact` that can be used within format strings.
10 changes: 7 additions & 3 deletions lib/lsp-devtools/lsp_devtools/record/formatters.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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),
}


Expand Down
14 changes: 14 additions & 0 deletions lib/lsp-devtools/tests/record/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
{
Expand Down

0 comments on commit fd71659

Please sign in to comment.