Do not add extra spaces to hints (#2671)

Closes
https://linear.app/zed-industries/issue/Z-2526/inlay-hints-in-typescript-types-have-extra-space-before#comment-ac88a101

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2023-07-03 11:18:07 +03:00 committed by GitHub
commit 8efb66be67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 10 deletions

View file

@ -49,6 +49,19 @@ pub struct InlayProperties<T> {
pub text: T,
}
impl InlayProperties<String> {
pub fn new(position: Anchor, hint: &project::InlayHint) -> Self {
let mut text = hint.text();
if hint.padding_right && !text.ends_with(' ') {
text.push(' ');
}
if hint.padding_left && !text.starts_with(' ') {
text.insert(0, ' ');
}
Self { position, text }
}
}
impl sum_tree::Item for Transform {
type Summary = TransformSummary;
@ -1095,6 +1108,7 @@ mod tests {
use super::*;
use crate::{InlayId, MultiBuffer};
use gpui::AppContext;
use project::{InlayHint, InlayHintLabel};
use rand::prelude::*;
use settings::SettingsStore;
use std::{cmp::Reverse, env, sync::Arc};
@ -1102,6 +1116,81 @@ mod tests {
use text::Patch;
use util::post_inc;
#[test]
fn test_inlay_properties_label_padding() {
assert_eq!(
InlayProperties::new(
Anchor::min(),
&InlayHint {
label: InlayHintLabel::String("a".to_string()),
buffer_id: 0,
position: text::Anchor::default(),
padding_left: false,
padding_right: false,
tooltip: None,
kind: None,
},
)
.text,
"a",
"Should not pad label if not requested"
);
assert_eq!(
InlayProperties::new(
Anchor::min(),
&InlayHint {
label: InlayHintLabel::String("a".to_string()),
buffer_id: 0,
position: text::Anchor::default(),
padding_left: true,
padding_right: true,
tooltip: None,
kind: None,
},
)
.text,
" a ",
"Should pad label for every side requested"
);
assert_eq!(
InlayProperties::new(
Anchor::min(),
&InlayHint {
label: InlayHintLabel::String(" a ".to_string()),
buffer_id: 0,
position: text::Anchor::default(),
padding_left: false,
padding_right: false,
tooltip: None,
kind: None,
},
)
.text,
" a ",
"Should not change already padded label"
);
assert_eq!(
InlayProperties::new(
Anchor::min(),
&InlayHint {
label: InlayHintLabel::String(" a ".to_string()),
buffer_id: 0,
position: text::Anchor::default(),
padding_left: true,
padding_right: true,
tooltip: None,
kind: None,
},
)
.text,
" a ",
"Should not change already padded label"
);
}
#[gpui::test]
fn test_basic_inlays(cx: &mut AppContext) {
let buffer = MultiBuffer::build_simple("abcdefghi", cx);

View file

@ -2712,16 +2712,7 @@ impl Editor {
let buffer = self.buffer.read(cx).read(cx);
let new_inlays = to_insert
.into_iter()
.map(|(position, id, hint)| {
let mut text = hint.text();
if hint.padding_right {
text.push(' ');
}
if hint.padding_left {
text.insert(0, ' ');
}
(id, InlayProperties { position, text })
})
.map(|(position, id, hint)| (id, InlayProperties::new(position, &hint)))
.collect();
drop(buffer);
self.display_map.update(cx, |display_map, cx| {