redhat-developer / yaml-language-server

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

Use inline JSON Schema w/o modeline - make `yaml-language-server` string optional #950

Open mulztob opened 9 months ago

mulztob commented 9 months ago

Is your enhancement related to a problem? Please describe.

Not really. Feature works as expected, I just always need to look up the right way to use inline schema (https://github.com/redhat-developer/yaml-language-server?tab=readme-ov-file#using-inlined-schema)

Describe the solution you would like

If possible I would like to inline schema like this: # $schema=...

Would be more consistent with the way you do it in JSON and would be more compatible with Intellij and maybe other IDEs

Describe alternatives you have considered


Additional context

I use yaml with JSON Schema a lot in VS Code. Many of my colleagues use Intellij for YAML editing. Both IDE's have a slightly different way to inline the JSON Schema.

Probably the place that needs changing/extending

In doComplete of yamlCompletion.ts For commitId [dfccc6f] it was around the line 319 if (isModeline(lineContent) || isInComment(doc.tokens, offset)) {

IsModeline is pretty straight forward. Couldn't figure out how isInComment was expected to behave here

Additionally the docs need a small update for this change and maybe the unit tests

The full relevant code fragment

      if (!schema || schema.errors.length) {
        if (position.line === 0 && position.character === 0 && !isModeline(lineContent)) {
          const inlineSchemaCompletion = {
            kind: CompletionItemKind.Text,
            label: 'Inline schema',
            insertText: '# yaml-language-server: $schema=',
            insertTextFormat: InsertTextFormat.PlainText,
          };
          result.items.push(inlineSchemaCompletion);
        }
      }

      if (isModeline(lineContent) || isInComment(doc.tokens, offset)) {
        const schemaIndex = lineContent.indexOf('$schema=');
        if (schemaIndex !== -1 && schemaIndex + '$schema='.length <= position.character) {
          this.schemaService.getAllSchemas().forEach((schema) => {
            const schemaIdCompletion: CompletionItem = {
              kind: CompletionItemKind.Constant,
              label: schema.name ?? schema.uri,
              detail: schema.description,
              insertText: schema.uri,
              insertTextFormat: InsertTextFormat.PlainText,
              insertTextMode: InsertTextMode.asIs,
            };
            result.items.push(schemaIdCompletion);
          });
        }
        return result;
      }
cromefire commented 3 months ago
  • Intellij: # $schema=....

You made a huge (and at the same time small) mistake there, # $schema= ... doesn't work at all in JetBrains' IDEs.

Instead JetBains' IDEs support both a JSON schema standard equivalent $schema: ... and the commented out version # $schema: ... (because : in YAML makes more sense as $schema= wouldn't be valid YAML and so the commended out form mimics the form in YAML directly)

I do agree that it's a huge pain though, most of us use JetBrains, but we have that one colleague that uses VSCode, so be always have to have

# $schema: ...
# yaml-language-server: $schema=...

on top of our language files.

I think it'd also probably make sense to make $schema: ... the new default then as it has virtually no downsides and better compatibility.