redhat-developer / yaml-language-server

Language Server for YAML Files
MIT License
1.1k stars 264 forks source link

fix: render examples as yaml on hover #947

Closed twelvelabs closed 11 months ago

twelvelabs commented 11 months ago

What does this PR do?

This MR improves the rendering of examples on hover by:

  1. Rendering the examples as YAML rather than JSON.
  2. Adding an info string to the fenced code block so that the examples are properly syntax highlighted. According to the LSP spec, markdown content should be expected to be GFM, so this shouldn't be an issue for clients.
  3. Moving the indentation escaping logic to toMarkdown, so that we only escape non-markdown strings. Currently the escaping is done on the entire hover content, regardless of whether that content contains actual markdown. This change also fixes a bug where intentional indentation in markdownDescription is erroneously escaped.

Before:

Screenshot 2023-12-27 at 3 17 58 PM

After:

Screenshot 2023-12-27 at 3 12 24 PM

Note: I originally tried to keep the single header (rather than repeating it per-example), but the code fences don't appear to have a margin applied so the examples were all bunched together. Repeating the header looked much nicer.

What issues does this PR fix or reference?

Is it tested? How?

I updated an existing unit test. Also tested manually with the following (see screenshots):

{
    "$id": "schema.json",
    "$schema": "https://json-schema.org/draft-07/schema",
    "title": "Example Schema",
    "properties": {
        "example": {
            "title": "Example Property",
            "description": "This property has examples that should be rendered as YAML code blocks.",
            "examples": [
                "string value",
                {
                    "object_value": {
                        "enabled": true
                    }
                }
            ],
            "type": [
                "object",
                "string"
            ]
        }
    },
    "type": "object"
}
# yaml-language-server: $schema=schema.json
example: "hello"
coveralls commented 11 months ago

Coverage Status

coverage: 84.076% (+0.005%) from 84.071% when pulling 7ff405ffa63f9979d254833363c630492808af5f on twelvelabs:examples-on-hover into 5458ed334db24c36b341d296b5a39e1cc9cead95 on redhat-developer:main.

twelvelabs commented 11 months ago

ping @msivasubramaniaan and @gorkem