ctlajoie / vscode-modelines

Set editor options for individual files. Supports Vim and Emacs modelines plus a Code-specific modeline syntax.
https://marketplace.visualstudio.com/items/chrislajoie.vscode-modelines
MIT License
40 stars 11 forks source link

Allow specification of jsonschema via modeline #12

Open phrrngtn opened 3 years ago

phrrngtn commented 3 years ago

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 commented 3 years ago
nvm install stable
npm install -g vsce
npm install --save-dev @types/node
npm i typescript@3.5.1
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 commented 3 years ago

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.

ctlajoie commented 3 years ago

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.