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

Fixes #198566: ellipsize left-cut search preview #198567

Merged

Conversation

jacekkopecky
Copy link
Contributor

This fixes #198566

The second screenshot in the issue is done with this PR in place.

Comments:

  • In strings.ts, perhaps lcut() should have the prefix default to '…' – it seems that the search preview generation is the only user of this function, and it would be consistent with truncate().
  • In searchModel.ts, we don't need to do trimStart() on before because lcut() already does that (and already did it before this PR, too).

@andreamah andreamah added this to the December 2023/January 2024 milestone Dec 7, 2023

before = lcut(before, 26);
before = before.trimStart();
Copy link
Contributor

Choose a reason for hiding this comment

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

We still need trimStart() for cases where the string's length is less than 26.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andreamah yes, quite right, I missed that.

However, looking at this, I've improved my understanding of lcut() - it seems that n is not a maximum but a target, given a string that's longer than n:

  • with whitespace characters before, it can return less than n characters,
  • but given non-whitespace characters, it can return more. The original tests show that with assert.strictEqual(strings.lcut('foo bar', 5), 'foo bar');

I'm adding new tests that show when lcut() can return more than n characters, which is behaviour not affected by my PR.

Therefore, it feels that lcut() could also trim the start of a string that's already shorter. I've implemented your comment that way.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still see a case where an ellipsis is present where it shouldn't be. See below:
image

I'm assuming that the ... represents the leading whitespace, which is inconsistent with other cases where we trim whitespace and don't have a ....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you again, yes, this wasn't handled well. I've committed a fix. The code feels cleaner now, too.

@jacekkopecky jacekkopecky force-pushed the bugfix/ellipsize-left-cut-search-preview branch from 780a2a5 to 833ac60 Compare December 10, 2023 18:46
Copy link
Contributor

@andreamah andreamah left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks so much for contributing this!

Thinking about this change, I do wonder if people will disagree with it since it takes up space in the search preview that would otherwise not be present. However, we can discuss this more if it actually comes up. For now, we can try out this change and see what the reception is from it. 🙂

after = this._oneLinePreviewText.substring(this._rangeInPreviewText.endColumn - 1);
preview(): { before: string; fullBefore: string; inside: string; after: string } {
const fullBefore = this._oneLinePreviewText.substring(0, this._rangeInPreviewText.startColumn - 1),
before = lcut(fullBefore, 26, '…');
Copy link
Member

Choose a reason for hiding this comment

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

I won't block on this... but I wonder why this is done here and not in CSS using text-overflow.

Copy link
Contributor

Choose a reason for hiding this comment

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

As a short answer, the reason it's not like that is because it was easier to modify the existing code to add an ellipsis.
Taking a quick skim of the spec for text-overflow, it seems that it would be based on an offset based on px whereas we seem to want to measure the offset by character. I might consider changing it to use text-overflow in the future, but it's probably not in the scope of this particular PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

text-overflow is also for the end of the string; we could abuse RTL text direction to cut from the start but then the ellipsis wouldn't reliably start at the start of the line.

@andreamah andreamah merged commit 9517ed4 into microsoft:main Dec 13, 2023
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Front-trimmed search result preview should be ellipsized
3 participants