Repository navigation
Show default and examples in hover - #193
Draft
drishtyraghav0001 wants to merge 2 commits into
Draft
drishtyraghav0001 wants to merge 2 commits into
drishtyraghav0001 wants to merge 2 commits into
Conversation
Hover now renders the default and examples annotations of each schema as JSON code blocks after its title and description. Examples are also read from draft-04 schemas, where the keyword is commonly used before it was standardized. Fixes hyperjump-io#88
Comment on lines
+77
to
+85
| // The fence is longer than any run of backticks in the JSON so a string value | ||
| // can't close the code block early | ||
| const jsonCodeBlock = (value: Json) => { | ||
| const json = JSON.stringify(value, null, 2); | ||
| const longestBacktickRun = Math.max(0, ...(json.match(/`+/g) ?? []).map((run) => run.length)); | ||
| const fence = "`".repeat(Math.max(3, longestBacktickRun + 1)); | ||
|
|
||
| return `${fence}json\n${json}\n${fence}`; | ||
| }; |
Collaborator
There was a problem hiding this comment.
Suggested change
| // The fence is longer than any run of backticks in the JSON so a string value | |
| // can't close the code block early | |
| const jsonCodeBlock = (value: Json) => { | |
| const json = JSON.stringify(value, null, 2); | |
| const longestBacktickRun = Math.max(0, ...(json.match(/`+/g) ?? []).map((run) => run.length)); | |
| const fence = "`".repeat(Math.max(3, longestBacktickRun + 1)); | |
| return `${fence}json\n${json}\n${fence}`; | |
| }; | |
| const jsonCodeBlock = (value: Json) => { | |
| const json = JSON.stringify(value, null, 2); | |
| return `\`\`\`json\n${json}\n\`\`\``; | |
| }; | |
I think we can keep this simpler. A code block only ends on a line that's just backticks, but every line JSON.stringify prints has quotes or brackets on it. So a value can never end the block early, and a plain three backtick always works. We don't need to count backticks.
Comment on lines
+1647
to
+1658
| test("uses a longer fence when the JSON has a code fence in it", async () => { | ||
| const result = await hover({ | ||
| properties: { | ||
| snippet: { default: "```js```" } | ||
| } | ||
| }, ` "snippet": ""`, { line: 2, character: 13 }); | ||
|
|
||
| expect(result).toEqual(hoverMarkdown(`Default: | ||
|
|
||
| \`\`\`\`json | ||
| "\`\`\`js\`\`\`" | ||
| \`\`\`\``)); |
Collaborator
There was a problem hiding this comment.
If we go with the plain three backtick fence, this test would need to expect three backticks.
Collaborator
|
Hi @drishtyraghav0001, thanks for the contribution. I've left a few comments. Please ping if you need any help. |
Author
|
thanks @diyasrivastava, switched to a plain ``` fence and updated the test in 48210dd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #88
Hover now shows
defaultandexamplesalongside the title/description, so you can see what a property falls back to and what valid values look like.default()andexamples()accessors toJsonSchemaAnnotation.examplesalso reads the unknown-keyword annotation in draft-04 schemas (same pattern asdeprecated), and ignores it if it isn't an array```jsonblocks (pretty-printed), so the hover order still follows the schema.default: nullstill showsTests in
Hover.test.ts: default with title/description, default with no title/description, null default, object default, multiple examples, default + examples together, empty examples, a$ref'd schema plus a sibling, property key hover, backtick fence, and draft-04 examples (valid + non-array).npm test(628 passing),npm run lintandnpm run type-checkare green, and the vscode type-check/lint/build still pass. Haven't done the VS Code / Neovim manual check yet, will do before marking ready.@jdesrosiers would love your review whenever you get a chance!