vscode-modelines icon indicating copy to clipboard operation
vscode-modelines copied to clipboard

Allow specification of jsonschema via modeline

Open phrrngtn opened this issue 3 years ago • 3 comments

It would be awesome if one could specify the jsonschema of a file via a modeline. I have a lot of schemata for JSON and YAML fles

the YAML extension supports this via: # yaml-language-server: $schema=<urlToTheSchema>

I would like to be able to do it similarly for blobs of JSON // code: language=javascript insertSpaces=true tabSize=4 $schema=https://json.schemastore.org/foobar

I assume that this is a straightforward addition to the parser? I have no experience with truescript nor with vs code extensions so I assume learning the packaging and deployment process would be a lot more effort than writing the code? In any case, I am not all that confident of being able to do it myself although I will probably give it a try if there is no activity on this ticket.

Thanks in advance for any advice/code.

phrrngtn avatar May 31 '21 15:05 phrrngtn

nvm install stable
npm install -g vsce
npm install --save-dev @types/node
npm i [email protected]
vsce package

I don't know where the output of console.log is going so I don't know if my hacked code is getting executed. I do know which key of editor.options to set to specify the json schema for this particular window. I added a branch to recognize schema and then tried to update the '$schema' key of editor.options but without being able to see the logging output, I don't think I will be able to make any progress.

+                               case 'schema':
+                                       return { '$schema': parsedVal };

extend(editor.options, options);
// assignment is necessary to trigger the change
editor.options = editor.options;

phrrngtn avatar May 31 '21 18:05 phrrngtn

I got the logging working (by creating the output channel (based on https://stackoverflow.com/a/58139566/40387 ) in the activate function and then passing it in to all of the closures that call applyModeLines) and I saw that editor.options does not appear to have any key directly relating to schemas. I will take a look at the Red Hat YAML extension (which supports modelines for YAML files) and see if I can figure out how it does it. Also, it may work out nicer for my users to edit their files in YAML and have them auto-converted to JSON as needed.

In any case, this is a very cool extension! It is short and simple enough that a newbie like myself can figure out how to modify it.

phrrngtn avatar May 31 '21 20:05 phrrngtn

I suspect there is no schema property on editor.options because schemas are specific to data languages like json/yaml. If you figure out how to do it you can submit a PR. You will have to make sure the $schema property doesn't cause any issues if it is present with some other language.

ctlajoie avatar Jun 03 '21 16:06 ctlajoie