Skip to content

Commit 23e1faa

Browse files
mrnuggetbennetbo
andauthored
assistant panel: Fix copying code when trailing newline is missing (#18067)
Follow-up to #17853. Apparently tree-sitter-md extends the range of the content node to include the backticks when there is no newline. Release Notes: - N/A Co-authored-by: Bennet <bennet@zed.dev>
1 parent 1723713 commit 23e1faa

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/assistant/src/assistant_panel.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3110,6 +3110,8 @@ impl ContextEditor {
31103110
context_editor_view: &View<ContextEditor>,
31113111
cx: &mut ViewContext<Workspace>,
31123112
) -> Option<(String, bool)> {
3113+
const CODE_FENCE_DELIMITER: &'static str = "```";
3114+
31133115
let context_editor = context_editor_view.read(cx).editor.read(cx);
31143116

31153117
if context_editor.selections.newest::<Point>(cx).is_empty() {
@@ -3120,10 +3122,17 @@ impl ContextEditor {
31203122
let offset = snapshot.point_to_offset(head);
31213123

31223124
let surrounding_code_block_range = find_surrounding_code_block(snapshot, offset)?;
3123-
let text = snapshot
3125+
let mut text = snapshot
31243126
.text_for_range(surrounding_code_block_range)
31253127
.collect::<String>();
31263128

3129+
// If there is no newline trailing the closing three-backticks, then
3130+
// tree-sitter-md extends the range of the content node to include
3131+
// the backticks.
3132+
if text.ends_with(CODE_FENCE_DELIMITER) {
3133+
text.drain((text.len() - CODE_FENCE_DELIMITER.len())..);
3134+
}
3135+
31273136
(!text.is_empty()).then_some((text, true))
31283137
} else {
31293138
let anchor = context_editor.selections.newest_anchor();

0 commit comments

Comments
 (0)