yaml-language-server icon indicating copy to clipboard operation
yaml-language-server copied to clipboard

feat: property snippets

Open p-spacek opened this issue 7 months ago • 4 comments

What does this PR do?

This PR solves many intellisense issues in defaultSnippets It should be ok to invoke intellisense in all these combinations: yaml cursor position, different types of schemas, and different types of snippets themselves.

should support all these combinations:

  • yaml position: after colon, in new line, in new line with hyphen, 2nd position of the item
  • schema type: object, array, anyOf, ...
  • snippets body type: array, object, array with primitives, simple value
  • snippetDefinition: in property, in array item

A new feature in this PR is to support property snippet intellisense

image

will insert the value from the snippet

image

A similar approach is used in different situations when the single snippet is used as a default value

This PR includes/replaces related changes from PRs: https://github.com/redhat-developer/yaml-language-server/pull/755 https://github.com/redhat-developer/yaml-language-server/pull/901 and include some other snippet fixes - examples are in new tests

So if you accept this PR, we can close the 2 previous PRs

Let me know if I can add more info, please. It would be nice to include these changes in your repo. We use snippets In our schemas a lot as a rich set of examples and sometimes as a replacement for autogenerated code structure. So, I believe that the code is well-tested.

What issues does this PR fix or reference?

no ref

Is it tested? How?

existing and a few new unit tests that combine the most probable combinations of the configurations tested by QA in our separate branch

Some definitions that are already used in tests

    "snippets": {
      "properties": {
        "arrayStringSnippet": {
          "type": "object",
          "defaultSnippets": [
            {
              "label": "Array",
              "body": {
                "fruits": ["banana", "orange"]
              }
            }
          ]
        },
        "snippetObject": {
          "type": "object",
          "properties": {
            "item1": { "type": "string" }
          },
          "required": ["item1"],
          "defaultSnippets": [
            {
              "label": "labelSnippetObject",
              "body": {
                "item1": "value",
                "item2": {
                  "item3": "value nested"
                }
              }
            }
          ]
        },
        "snippetArray": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "item1": { "type": "string" }
            }
          },
          "defaultSnippets": [
            {
              "label": "labelSnippetArray",
              "body": {
                "item1": "value",
                "item2": "value2"
              }
            }
          ]
        },
        "snippetArray2": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true,
            "defaultSnippets": [
              {
                "label": "labelSnippetArray",
                "body": {
                  "item1": "value",
                  "item2": "value2"
                }
              }
            ]
          }
        },
        "snippetArrayComplex": {
          "type": "array",
          "items": {
            "type": "object",
            "defaultSnippets": [
              {
                "body": [
                  {
                    "item1": "value",
                    "item2": {
                      "item2.1": "value3"
                    }
                  },
                  {
                    "item3": [
                      {
                        "item4": "value4",
                        "item5": "value4",
                        "item6": ["value6", "value7"]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        },
        "snippetAnyOfArray": {
          "anyOf": [
            {
              "items": {
                "type": "object",
                "properties": {
                  "item1": { "type": "string" }
                }
              }
            },
            {
              "type": "object"
            }
          ],

          "defaultSnippets": [
            {
              "label": "labelSnippetAnyOfArray",
              "body": [
                {
                  "item1": "value",
                  "item2": "value"
                }
              ]
            }
          ]
        },
        "snippetAnyOfObject": {
          "anyOf": [
            {
              "items": {
                "type": "object",
                "properties": {
                  "item1": { "type": "string" }
                }
              }
            },
            {
              "type": "object"
            }
          ],

          "defaultSnippets": [
            {
              "label": "labelSnippetAnyOfArray",
              "body": {
                "item1": "value",
                "item2": "value"
              }
            }
          ]
        },
        "snippetArray2Objects": {
          "type": "array",
          "items": {
            "type": "object"
          },
          "defaultSnippets": [
            {
              "body": [
                {
                  "item1": "value",
                  "item2": "value2"
                },
                {
                  "item3": "value"
                }
              ]
            }
          ]
        },
        "snippetArrayPrimitives": {
          "type": "array",
          "items": {
            "type": ["string", "boolean", "number", "null"],
            "defaultSnippets": [
              {
                "body": ["value", 5, null, false]
              }
            ]
          }
        }
      }
    }

p-spacek avatar Mar 21 '25 09:03 p-spacek