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>
This commit is contained in:
Thorsten Ball 2024-09-19 14:43:56 +02:00 committed by GitHub
parent 1723713dc2
commit 23e1faa485
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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();