From a59a3b578eb6624d7eb9d21c55e560fce1f951c4 Mon Sep 17 00:00:00 2001 From: Felix Rech Date: Tue, 23 Mar 2021 15:08:18 +0100 Subject: [PATCH] Fix pasting multiline code in single-lined text area (#1348) When pasting multiline code in a single-lined text area, only the first line will be inserted. --- CHANGELOG.md | 8 ++++++- .../ensogl/lib/text/src/component/area.rs | 22 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58a8f9a493..51051b023c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,9 +40,12 @@ - [Disable area selection][1318]. The area selection was visible despite being non-functional. To avoid confusion, area selection has been disabled until it is [correctly implemented][479]. -- [Handle syntax errors in custom-defined visualizations][#1341]. The IDE is now +- [Handle syntax errors in custom-defined visualizations][1341]. The IDE is now able to run properly, even if some of the custom-defined visualisations inside a project contain syntax errors. +- [Fix issues with pasting multi-line text into single-line text fields][1348]. + The first copied line will be inserted and all additional lines will be + ignored. - [Users can opt out of anonymous data gathering.][1328] This can be done with the `--no-data-gathering` command-line flag during the startup of the IDE. - [You can now start the IDE service without window again.][1353] The command @@ -66,6 +69,9 @@ you can find their release notes [1064]: https://github.com/enso-org/ide/pull/1064 [1316]: https://github.com/enso-org/ide/pull/1316 [1318]: https://github.com/enso-org/ide/pull/1318 +[1328]: https://github.com/enso-org/ide/pull/1328 +[1341]: https://github.com/enso-org/ide/pull/1341 +[1348]: https://github.com/enso-org/ide/pull/1348 [1353]: https://github.com/enso-org/ide/pull/1353
diff --git a/src/rust/ensogl/lib/text/src/component/area.rs b/src/rust/ensogl/lib/text/src/component/area.rs index 9a454e0465..53f6f1bc5a 100644 --- a/src/rust/ensogl/lib/text/src/component/area.rs +++ b/src/rust/ensogl/lib/text/src/component/area.rs @@ -569,7 +569,7 @@ impl Area { eval_ sels_cut (m.buffer.frp.delete_left()); eval_ input.paste (m.paste()); - eval input.paste_string ((s) m.buffer.frp.paste(m.decode_paste(s))); + eval input.paste_string((s) m.paste_string(s)); eval_ m.buffer.frp.text_change (m.redraw(true)); @@ -954,10 +954,30 @@ impl AreaModel { clipboard::read_text(move |t| paste_string.emit(t)); } + /// Paste new text in the place of current selections / cursors. In case of pasting multiple + /// chunks (e.g. after copying multiple selections), the chunks will be pasted into subsequent + /// selections. In case there are more chunks than selections, end chunks will be dropped. In + /// case there is more selections than chunks, end selections will be replaced with empty + /// strings. I `self.single_line` is set to true then each chunk will be truncated to its first + /// line. + fn paste_string(&self, s: &str) { + let mut chunks = self.decode_paste(s); + if self.single_line.get() { + for f in &mut chunks { + Self::drop_all_but_first_line(f); + } + } + self.buffer.frp.paste(chunks); + } + fn decode_paste(&self, encoded:&str) -> Vec { encoded.split(RECORD_SEPARATOR).map(|s|s.into()).collect() } + fn drop_all_but_first_line(s: &mut String) { + *s = s.lines().nth(0).unwrap_or("").to_string(); + } + fn key_to_string(&self, key:&Key) -> Option { match key { Key::Character(s) => Some(s.clone()),