Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

assistant panel: Fix copying code when trailing newline is missing (#18067)

Thorsten Ball23e1faa48524
diff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rs
index 094d187df29..da176ebeee6 100644
--- a/crates/assistant/src/assistant_panel.rs
+++ b/crates/assistant/src/assistant_panel.rs
@@ -3110,6 +3110,8 @@ impl ContextEditor {
context_editor_view: &View<ContextEditor>,
cx: &mut ViewContext<Workspace>,
) -> Option<(String, bool)> {
+ const CODE_FENCE_DELIMITER: &'static str = "```";
+
let context_editor = context_editor_view.read(cx).editor.read(cx);
if context_editor.selections.newest::<Point>(cx).is_empty() {
@@ -3120,10 +3122,17 @@ impl ContextEditor {
let offset = snapshot.point_to_offset(head);
let surrounding_code_block_range = find_surrounding_code_block(snapshot, offset)?;
- let text = snapshot
+ let mut text = snapshot
.text_for_range(surrounding_code_block_range)
.collect::<String>();
+ // If there is no newline trailing the closing three-backticks, then
+ // tree-sitter-md extends the range of the content node to include
+ // the backticks.
+ if text.ends_with(CODE_FENCE_DELIMITER) {
+ text.drain((text.len() - CODE_FENCE_DELIMITER.len())..);
+ }
+
(!text.is_empty()).then_some((text, true))
} else {
let anchor = context_editor.selections.newest_anchor();