-
Notifications
You must be signed in to change notification settings - Fork 60
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
feat(config): add JSON schema for validation,completion,documentation #228
feat(config): add JSON schema for validation,completion,documentation #228
Conversation
Just realized one setting defaulted to a different value in the schema than what the actual default is. This is now fixed. |
Love the goal of this! I wonder if this could be dynamically generated by using something like schemars. e.g. run My concern is this will eventually drift off from the config when new parameters are added and/or it is a bit harder for people to use given this is standalone file in the repo that they don't get if they download a binary artifact. |
schema.json
Outdated
"anyOf": [ | ||
{ | ||
"type": "string", | ||
"enum": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an example of what I mean by drifting off, this list can easily get out of sync with the list of supported themes.
Oh I see your point, and I think you are 100% correct. I'll see if I can build the schema automatically based on the rust types 🤔 👍🏻 |
question: It looks like |
It should be configurable. This works for me, as in, starting presenterm with this config will complain because I don't have sixel enabled during compile time: defaults:
image_protocol: sixel |
3b26707
to
abd24f1
Compare
Ok, I have a working proof of concept available for review and feedback now. This version is able to generate the schema using schemars. The schema seems to work and should now be easy to keep in sync with the code. Open question: how should you think the build system should generate this schema?
Do you have an idea how things like this are usually managed in rust projects? |
I wouldn't worry about this. I'm generally cautious about adding dependencies but this is only pulling a couple and I think it's for a good cause so I don't mind the extra binary size. What I was thinking though, is not to have the schema file in the repo at all and let people generate it manually if they feel like using it. e.g. if you're interested in using a yaml lsp, you can run |
src/custom.rs
Outdated
schema.metadata().description = | ||
Some("The theme to use by default in every presentation unless overridden.".to_string()); | ||
schema.instance_type = Some(SingleOrVec::Vec(vec![schemars::schema::InstanceType::String])); | ||
schema.enum_values = Some(vec![ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you spent some time to get this enum to work, but I'm not sure if it's the right way to go. Because you can have your own custom themes, this end up being misleading to users. I see that you do allow an arbitrary string as well, but I think if I see my autocompletion show a list of things (e.g. the build in themes), I would always assume I need to use a value from that list so I may get confused if I have a local custom theme and it's not showing up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can leave it out too. I'll revert it back to just a string with no suggestions for the built in themes.
Okay 👍🏻
I would go for the second option (shipping a file), but I guess a switch for creating the schema might also be a good option for advanced users. It's almost no extra work to do it as you said you are fine with having the schema related dependencies being shipped. I forgot to mention this, but here's my initial idea for how the schema could be built and used:
This is essentially the setup that the lazygit project uses for their configuration (see their docs https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md?plain=1#L19) |
Sorry, I've been busy and had no time to look into this.
All of that sounds great! We can add a step in the CI in a separate PR but I think all of that sounds great 👌 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
src/main.rs
Outdated
@@ -31,6 +31,10 @@ struct Cli { | |||
#[clap(long, hide = true)] | |||
generate_pdf_metadata: bool, | |||
|
|||
/// Generate a JSON schema for the configuration file. | |||
#[clap(long, hide = true)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if I'd hide = true
this, this should be visible for people doing a --help
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, nice catch. Probably a copy paste mistake, now fixed.
src/custom.rs
Outdated
@@ -45,17 +48,24 @@ pub enum ConfigLoadError { | |||
Invalid(#[from] serde_yaml::Error), | |||
} | |||
|
|||
#[derive(Clone, Debug, Deserialize)] | |||
// TODO(JsonSchema): suggest the valid themes based on themes.presentation.theme_names() but allow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I commented before, I think this isn't necessary (this may be a leftover comment!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep 😄
fixed now.
This adds a new command line flag that makes presenterm print out a JSON schema for the configuration. Usage: presenterm --generate-config-file-schema > schema.json The schema can be used for validation, completion and documentation of the configuration file. To use it, you have to have yaml-language-server set up for your editor. Add a magic comment to the top of the configuration file with the path to the schema: # yaml-language-server: $schema=schema.json
3dbcccf
to
da8cdbf
Compare
Ok I think things are looking good now. Just rebased to the main branch and cleaned the commit history. I'll see if I can work on the CI part next, but as far as I'm concerned, this PR is ready to be merged. |
Thanks again! |
I can do the CI part later today, no worries 👌 |
I think I'm going to rename the |
Help users write and maintain their configuration
This change introduces a JSON schema for the configuration file. This schema is used to validate the configuration file and provide feedback to the user if the configuration file is invalid.
presenterm-json-schema-demo2.mov
To use this schema, the schema must be referenced in the yaml configuration file like this:
# yaml-language-server: $schema=./schema.json
This configures the yaml-language-server to use the schema.json file to validate the configuration file. The language server is well supported in editors such as
Supported features
Testing the feature
To test this feature, you can do the following steps:
# yaml-language-server: $schema=./schema.json
to develop this feature