Skip to content

Show default and examples in hover - #193

Draft
drishtyraghav0001 wants to merge 2 commits into
hyperjump-io:mainfrom
drishtyraghav0001:hover-default-examples
Draft

drishtyraghav0001 wants to merge 2 commits into
hyperjump-io:mainfrom
drishtyraghav0001:hover-default-examples

Conversation

@drishtyraghav0001

Copy link
Copy Markdown

Fixes #88

Hover now shows default and examples alongside the title/description, so you can see what a property falls back to and what valid values look like.

  • added default() and examples() accessors to JsonSchemaAnnotation. examples also reads the unknown-keyword annotation in draft-04 schemas (same pattern as deprecated), and ignores it if it isn't an array
  • each schema's default/examples render right after its own title/description as ```json blocks (pretty-printed), so the hover order still follows the schema. default: null still shows
  • the fence gets longer if the value itself has backticks in it, so a string can't break the code block
  • hover only; completions documentation is unchanged

Tests 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 lint and npm run type-check are 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!

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 thread language-server/src/features/Hover.ts Outdated
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}`;
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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\`\`\`"
\`\`\`\``));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with the plain three backtick fence, this test would need to expect three backticks.

@srivastava-diya

Copy link
Copy Markdown
Collaborator

Hi @drishtyraghav0001, thanks for the contribution. I've left a few comments. Please ping if you need any help.

@drishtyraghav0001

Copy link
Copy Markdown
Author

thanks @diyasrivastava, switched to a plain ``` fence and updated the test in 48210dd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show default and examples in hover

2 participants