Skip to content
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

[LiquidDoc] Improve hover behaviour for multi-line descriptions and examples #850

Merged
merged 1 commit into from
Mar 13, 2025

Conversation

jamesmengo
Copy link
Contributor

@jamesmengo jamesmengo commented Mar 11, 2025

What are you adding in this PR?

Closes https://github.com/Shopify/developer-tools-team/issues/600

The Problem:
1: Hovering renders extra indentation for multi-line examples and descriptionsimage

2: Rendering descriptions as plaintext results in formatting inconsistencies image

This PR:

  • 1: Removes the minimum common indentation for multi-line description and example content
  • 2: Reverts the description to be rendered as markdown on hover. This prevents the formatting inconsistency while still allowing users to render newlines if they desire, though it would be manual.

image
image

What did you learn?

  • With the way we currently parse multiline content, the first line's content starts at the first character and does not include leading whitespace. I tried modifying this but didn't get anywhere within a reasonable amount of time.

Before you deploy

  • I included a patch bump changeset

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@jamesmengo jamesmengo changed the title Improve liquid doc indentation and hover formatting [LiquidDoc] Improve hover behaviour for multi-line descriptions and examples Mar 11, 2025
@jamesmengo jamesmengo force-pushed the jm/indentation_hover branch 2 times, most recently from 663ce62 to 81a3bef Compare March 11, 2025 19:59
@jamesmengo jamesmengo self-assigned this Mar 11, 2025
@jamesmengo jamesmengo added the #gsd:44310 LiquidDoc label Mar 11, 2025
@jamesmengo jamesmengo marked this pull request as ready for review March 11, 2025 20:00
return line.slice(minIndent);
}),
].join('\n');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a similar utility that exists in the prettier plugin but I chose to implement this here instead.

  1. The only shared dependancy is liquid-html-parser
  2. I explored moving it into a shared utility into liquid-html-parser and didn't feel that the benefit was > the cost of duplication here.
  3. I highly doubt this will change, and if it does we don't want to change the prettier behaviour

@@ -51,7 +51,7 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {

if (liquidDoc.description) {
const description = liquidDoc.description.content;
parts.push('', '**Description:**', '\n', `\`\`\`plaintext\n${description}\n\`\`\``);
parts.push('', '**Description:**', '\n', description);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

@jamesmengo jamesmengo force-pushed the jm/indentation_hover branch from 81a3bef to 278a457 Compare March 11, 2025 20:07
@jamesmengo jamesmengo requested a review from a team as a code owner March 11, 2025 20:07
Comment on lines 112 to 134
function fixIndentation(text: string): string {
const lines = text.split('\n');

if (lines.length <= 1) return text;

const nonEmptyLines = lines.slice(1).filter((line) => line.trim().length > 0);
const indentLengths = nonEmptyLines.map((line) => {
const match = line.match(/^\s*/);
return match ? match[0].length : 0;
});

if (indentLengths.length === 0) return text;

const minIndent = Math.min(...indentLengths);

return [
lines[0],
...lines.slice(1).map((line) => {
if (line.trim().length === 0) return line; // Skip empty lines
return line.slice(minIndent);
}),
].join('\n');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we missing some logic here? I can't properly render multiline description.

image image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great question! This is intentional. Example 2 in the PR description illustrates an example of how I came to the realization that our intention to allow for easy indenting in the description was creating more problems than value.

  1. This required us to render as plaintext, which resulted in the issue where subsequent lines were unexpectedly indented.

  2. When content overflows outside of the tooltip width, a new line is automatically added. This, to me, breaks the paradigm of allowing users to format their text as desired. If we provide users with the ability to manage new lines and indenting, new lines should only be added IFF the user manually inputs them.

Rationale

I think the large majority of users will benefit more from multi-line values not having a weird indent + rendering with a consistent font.

The previous approach also felt 'broken' when the hover tooltip was adding its own newlines when content was overflowing.

With this approach, it's still possible for users to add newlines / indentation if they want to, though this now feels hacky.

I'm ok with this tradeoff, as I no longer think that control over indentation / whitespace for descriptions is a first-class feature.

image.png

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the large majority of users will benefit more from multi-line values not having a weird indent + rendering with a consistent font.

it's still possible for users to add newlines / indentation if they want to, though this now feels hacky.

I think we could revisit this if requested by someone. But looks like a fair tradeoff for now.

----

- Render Description as markdown instead of plaintext
- Fix indentation for multi-line examples and descriptions
@jamesmengo jamesmengo force-pushed the jm/indentation_hover branch from 278a457 to d42ed61 Compare March 12, 2025 22:56
@jamesmengo jamesmengo merged commit 40f5d93 into main Mar 13, 2025
7 checks passed
@jamesmengo jamesmengo deleted the jm/indentation_hover branch March 13, 2025 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#gsd:44310 LiquidDoc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants