redhat-developer / yaml-language-server

Language Server for YAML Files
MIT License
1.01k stars 244 forks source link

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

Open mulztob opened 4 months ago

mulztob commented 4 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;
      }