Skip to content

Commit 6c3a7f6

Browse files
gpui: Fix text wrapping for URLs (#35724)
Close #35715 Release Notes: - Fixed to wrap long URLs in editor. <img width="836" height="740" alt="image" src="https://github.com/user-attachments/assets/635ce792-5f19-4c76-b131-0d270d09b103" /> I remember when I was working on CJK line wrapping support in the early days, I considered making `\` a line wrapping character, but for some reason it was on the list of characters that were not allowed to wrap. In reference to VS Code, it looks like `&`, `/`, `?` should wrap, so I removed all of them. --------- Co-authored-by: Mikayla Maki <[email protected]>
1 parent e67065f commit 6c3a7f6

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

crates/gpui/examples/text_wrapper.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ struct HelloWorld {}
77

88
impl Render for HelloWorld {
99
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
10-
let text = "The longest word 你好世界这段是中文,こんにちはこの段落は日本語です in any of the major English language dictionaries is pneumonoultramicroscopicsilicovolcanoconiosis, a word that refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano; medically, it is the same as silicosis.";
10+
let text = "The longest word 你好世界这段是中文,こんにちはこの段落は日本語です in any of the major \
11+
English language dictionaries is pneumonoultramicroscopicsilicovolcanoconiosis, a word that \
12+
refers to a lung disease contracted from the inhalation of very fine silica particles, \
13+
a url https://github.com/zed-industries/zed/pull/35724?query=foo&bar=2, \
14+
specifically from a volcano; medically, it is the same as silicosis.";
1115
div()
1216
.id("page")
1317
.size_full()

crates/gpui/src/text_system/line_wrapper.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ impl LineWrapper {
163163
line
164164
}
165165

166+
/// Any character in this list should be treated as a word character,
167+
/// meaning it can be part of a word that should not be wrapped.
166168
pub(crate) fn is_word_char(c: char) -> bool {
167169
// ASCII alphanumeric characters, for English, numbers: `Hello123`, etc.
168170
c.is_ascii_alphanumeric() ||
@@ -180,10 +182,9 @@ impl LineWrapper {
180182
// https://en.wikipedia.org/wiki/Cyrillic_script_in_Unicode
181183
matches!(c, '\u{0400}'..='\u{04FF}') ||
182184
// Some other known special characters that should be treated as word characters,
183-
// e.g. `a-b`, `var_name`, `I'm`, '@mention`, `#hashtag`, `100%`, `3.1415`, `2^3`, `a~b`, etc.
184-
matches!(c, '-' | '_' | '.' | '\'' | '$' | '%' | '@' | '#' | '^' | '~' | ',' | '!' | ';' | '*') ||
185-
// Characters that used in URL, e.g. `https://github.com/zed-industries/zed?a=1&b=2` for better wrapping a long URL.
186-
matches!(c, '/' | ':' | '?' | '&' | '=') ||
185+
// e.g. `a-b`, `var_name`, `I'm`, '@mention`, `#hashtag`, `100%`, `3.1415`,
186+
// `2^3`, `a~b`, `a=1`, `Self::new`, etc.
187+
matches!(c, '-' | '_' | '.' | '\'' | '$' | '%' | '@' | '#' | '^' | '~' | ',' | '=' | ':') ||
187188
// `⋯` character is special used in Zed, to keep this at the end of the line.
188189
matches!(c, '⋯')
189190
}
@@ -644,15 +645,19 @@ mod tests {
644645
assert_word("@mention");
645646
assert_word("#hashtag");
646647
assert_word("$variable");
648+
assert_word("a=1");
649+
assert_word("Self::is_word_char");
647650
assert_word("more⋯");
648651

649652
// Space
650653
assert_not_word("foo bar");
651654

652655
// URL case
653-
assert_word("https://github.com/zed-industries/zed/");
654656
assert_word("github.com");
655-
assert_word("a=1&b=2");
657+
assert_not_word("zed-industries/zed");
658+
assert_not_word("zed-industries\\zed");
659+
assert_not_word("a=1&b=2");
660+
assert_not_word("foo?b=2");
656661

657662
// Latin-1 Supplement
658663
assert_word("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ");

0 commit comments

Comments
 (0)