Don't include start of a line when selection ends at start of line

This commit is contained in:
Antonio Scandurra 2023-10-10 19:00:05 +02:00
parent 5cf92980f0
commit b366592878
3 changed files with 30 additions and 32 deletions

View file

@ -17,7 +17,7 @@ use editor::{
BlockContext, BlockDisposition, BlockId, BlockProperties, BlockStyle, ToDisplayPoint, BlockContext, BlockDisposition, BlockId, BlockProperties, BlockStyle, ToDisplayPoint,
}, },
scroll::autoscroll::{Autoscroll, AutoscrollStrategy}, scroll::autoscroll::{Autoscroll, AutoscrollStrategy},
Anchor, Editor, MoveDown, MoveUp, MultiBufferSnapshot, ToOffset, Anchor, Editor, MoveDown, MoveUp, MultiBufferSnapshot, ToOffset, ToPoint,
}; };
use fs::Fs; use fs::Fs;
use futures::StreamExt; use futures::StreamExt;
@ -278,22 +278,36 @@ impl AssistantPanel {
if selection.start.excerpt_id() != selection.end.excerpt_id() { if selection.start.excerpt_id() != selection.end.excerpt_id() {
return; return;
} }
let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
// Extend the selection to the start and the end of the line.
let mut point_selection = selection.map(|selection| selection.to_point(&snapshot));
if point_selection.end > point_selection.start {
point_selection.start.column = 0;
// If the selection ends at the start of the line, we don't want to include it.
if point_selection.end.column == 0 {
point_selection.end.row -= 1;
}
point_selection.end.column = snapshot.line_len(point_selection.end.row);
}
let codegen_kind = if point_selection.start == point_selection.end {
CodegenKind::Generate {
position: snapshot.anchor_after(point_selection.start),
}
} else {
CodegenKind::Transform {
range: snapshot.anchor_before(point_selection.start)
..snapshot.anchor_after(point_selection.end),
}
};
let inline_assist_id = post_inc(&mut self.next_inline_assist_id); let inline_assist_id = post_inc(&mut self.next_inline_assist_id);
let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
let provider = Arc::new(OpenAICompletionProvider::new( let provider = Arc::new(OpenAICompletionProvider::new(
api_key, api_key,
cx.background().clone(), cx.background().clone(),
)); ));
let codegen_kind = if editor.read(cx).selections.newest::<usize>(cx).is_empty() {
CodegenKind::Generate {
position: selection.start,
}
} else {
CodegenKind::Transform {
range: selection.start..selection.end,
}
};
let codegen = cx.add_model(|cx| { let codegen = cx.add_model(|cx| {
Codegen::new(editor.read(cx).buffer().clone(), codegen_kind, provider, cx) Codegen::new(editor.read(cx).buffer().clone(), codegen_kind, provider, cx)
}); });
@ -319,7 +333,7 @@ impl AssistantPanel {
editor.insert_blocks( editor.insert_blocks(
[BlockProperties { [BlockProperties {
style: BlockStyle::Flex, style: BlockStyle::Flex,
position: selection.head().bias_left(&snapshot), position: snapshot.anchor_before(point_selection.head()),
height: 2, height: 2,
render: Arc::new({ render: Arc::new({
let inline_assistant = inline_assistant.clone(); let inline_assistant = inline_assistant.clone();

View file

@ -1,9 +1,7 @@
use crate::streaming_diff::{Hunk, StreamingDiff}; use crate::streaming_diff::{Hunk, StreamingDiff};
use ai::completion::{CompletionProvider, OpenAIRequest}; use ai::completion::{CompletionProvider, OpenAIRequest};
use anyhow::Result; use anyhow::Result;
use editor::{ use editor::{multi_buffer, Anchor, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint};
multi_buffer, Anchor, AnchorRangeExt, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint,
};
use futures::{channel::mpsc, SinkExt, Stream, StreamExt}; use futures::{channel::mpsc, SinkExt, Stream, StreamExt};
use gpui::{Entity, ModelContext, ModelHandle, Task}; use gpui::{Entity, ModelContext, ModelHandle, Task};
use language::{Rope, TransactionId}; use language::{Rope, TransactionId};
@ -40,26 +38,11 @@ impl Entity for Codegen {
impl Codegen { impl Codegen {
pub fn new( pub fn new(
buffer: ModelHandle<MultiBuffer>, buffer: ModelHandle<MultiBuffer>,
mut kind: CodegenKind, kind: CodegenKind,
provider: Arc<dyn CompletionProvider>, provider: Arc<dyn CompletionProvider>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Self { ) -> Self {
let snapshot = buffer.read(cx).snapshot(cx); let snapshot = buffer.read(cx).snapshot(cx);
match &mut kind {
CodegenKind::Transform { range } => {
let mut point_range = range.to_point(&snapshot);
point_range.start.column = 0;
if point_range.end.column > 0 || point_range.start.row == point_range.end.row {
point_range.end.column = snapshot.line_len(point_range.end.row);
}
range.start = snapshot.anchor_before(point_range.start);
range.end = snapshot.anchor_after(point_range.end);
}
CodegenKind::Generate { position } => {
*position = position.bias_right(&snapshot);
}
}
Self { Self {
provider, provider,
buffer: buffer.clone(), buffer: buffer.clone(),
@ -386,7 +369,7 @@ mod tests {
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
let range = buffer.read_with(cx, |buffer, cx| { let range = buffer.read_with(cx, |buffer, cx| {
let snapshot = buffer.snapshot(cx); let snapshot = buffer.snapshot(cx);
snapshot.anchor_before(Point::new(1, 4))..snapshot.anchor_after(Point::new(4, 4)) snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(4, 5))
}); });
let provider = Arc::new(TestCompletionProvider::new()); let provider = Arc::new(TestCompletionProvider::new());
let codegen = cx.add_model(|cx| { let codegen = cx.add_model(|cx| {

View file

@ -4,6 +4,7 @@ use std::cmp::{self, Reverse};
use std::fmt::Write; use std::fmt::Write;
use std::ops::Range; use std::ops::Range;
#[allow(dead_code)]
fn summarize(buffer: &BufferSnapshot, selected_range: Range<impl ToOffset>) -> String { fn summarize(buffer: &BufferSnapshot, selected_range: Range<impl ToOffset>) -> String {
#[derive(Debug)] #[derive(Debug)]
struct Match { struct Match {