Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mulztob opened this issue Feb 9, 2024 · 0 comments

Comments

@mulztob
Copy link

mulztob commented Feb 9, 2024

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.

  • VSCODE/yaml-language-server: # yaml-language-server: $schema=....
  • Intellij: # $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;
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant