redhat-developer / vscode-yaml

YAML support for VS Code with built-in kubernetes syntax support
MIT License
652 stars 221 forks source link

Rank schemas based on specificity #1034

Open SethFalco opened 3 months ago

SethFalco commented 3 months ago

Is your enhancement related to a problem? Please describe.

I have a GitHub Workflow at the file location .github/workflows/deploy.yml.

This is picked up as one of two schemas:

I haven't reviewed the code yet, so I don't know why, but it's effectively using the first, which is not what I want.

Describe the solution you would like

Perhaps, borrowing from CSS, we could rank globs with a specificity score to select the most likely candidate?

For example, here are the following globs:

If we to use a simple metric such as how many elements are there in the path, then Deployer Recipe would have a specificity of 1 while GitHub Workflow would have a specificity of 4. Since both match, but GitHub Workflow is more specific, we assume the desired schema is more likely GitHub Workflow.

Alternatively, and I think this is a better approach, we could use 2 numbers. This will account for fileMatches with many wildcards, or where two fileMatches have the same number of items in the path. The major number would increment on concrete paths, while the minor would increment on globs. So, Deployer Recipe gets a specificity of (1, 0) while GitHub Workflow gets (2, 2), where GitHub Workflow is chosen. Using a made up example, **/.github/**/*.yml gets (1, 3) while **/.github/workflows/*.yml gets (2, 2), so the latter is chosen.

Schemas defined in yaml.schemas should always get priority, but if there are also multiple matches there, it may be worth using the same logic to pick which one to use.

Describe alternatives you have considered

N/A

Additional context

You can see in this screenshot that I'm not getting proper code completion due to it using the wrong schema.

I had assumed that by pressing one of the schemas at the top, it would set the file to that schema, but it just opens it instead.

I've worked around the issue by setting the schema myself:

  "yaml.schemas": {
    "https://json.schemastore.org/github-workflow.json": ".github/workflows/**"
  },

I'm available to work on this if the feature is desirable, just thought I should get feedback first. Just not sure if this would need to be worked on in redhat-developer/vscode-yaml or redhat-developer/yaml-language-server.