redhat-developer / redhat-developer/vscode-yaml

Apply registerContributor for custom languages

Open
#722 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
829
Forks
260
Avg merge
5h 43m
Merged PRs (30d)
1

Description

Summary

I try to create a custom vscode extension for a custom language based on yaml. My extension should load specific json-schemas and pass it to the redhat.vscode-yaml extension. This works well for all *.yaml files when the vscode language mode set to YAML. Unfortunately this works not when vscode language mode set to my custom language called *.service.yml. How could I get registerContributor to work for a custom language?

{
        "id": "service.yml",
        "aliases": [
          "Service (yml)",
          "service.yml"
        ],
        "extensions": [
          ".service.yml"
        ],
        "icon": {
          "light": "./icons/png/service-light.png",
          "dark": "./icons/png/service-dark.png"
        },
        "configuration": "./language-configuration.json"
      }

Relevant information

The main problem here, is that onRequestSchemaURI and onRequestSchemaContent after

yamlPlugin.registerContributor(SCHEMA, onRequestSchemaURI, onRequestSchemaContent);

call not get triggert, when the language mode set to the custom language.

I used the code from https://github.com/redhat-developer/vscode-yaml/issues/216#issue-483735972 as minimal example.

'use strict';
import * as vscode from 'vscode';

export async function activate(context: vscode.ExtensionContext) {
    await registerYamlSchema();
}

const SCHEMA = "foozzzzzz";

const schemaJSON = JSON.stringify({
    type: "object",
    properties: {
        version: {
            type: "string",
            description: "A stringy string string"
        }
    }
});

export async function registerYamlSchema() {
    const yamlPlugin = await activateYamlExtension();
    if (!yamlPlugin) {
        vscode.window.showWarningMessage("NO YAMLS");
        return;
    }

    yamlPlugin.registerContributor(SCHEMA, onRequestSchemaURI, onRequestSchemaContent);
}

function onRequestSchemaURI(resource: string): string | undefined {
    if (resource.endsWith('porter.yaml')) {
        return `${SCHEMA}://schema/porter`;
    }
    return undefined;
}

function onRequestSchemaContent(schemaUri: string): string | undefined {
    const parsedUri = vscode.Uri.parse(schemaUri);
    if (parsedUri.scheme !== SCHEMA) {
        return undefined;
    }
    if (!parsedUri.path || !parsedUri.path.startsWith('/')) {
        return undefined;
    }

    return schemaJSON;
}

const VSCODE_YAML_EXTENSION_ID = 'redhat.vscode-yaml';

export interface YamlExtension {
    registerContributor(
        schema: string,
        requestSchema: (resource: string) => string | undefined,
        requestSchemaContent: (uri: string) => string | undefined
    ): void;
}

export async function activateYamlExtension(): Promise<YamlExtension | undefined> {
    const extension = vscode.extensions.getExtension(VSCODE_YAML_EXTENSION_ID);
    if (!extension) {
        return undefined;
    }

    const extensionAPI = await extension.activate();
    if (!extensionAPI || !extensionAPI.registerContributor) {
        return undefined;
    }

    return extensionAPI;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the activate and registerYamlSchema entry points in the supplied extension example, then inspect how registerContributor handles the custom service.yml language mode. Reproduce the case using the shown extension manifest and callbacks. Done means the schema request callbacks are triggered and the custom language receives the expected schema behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vscode
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.