diff --git a/Cargo.lock b/Cargo.lock index c7d88a44a4..e831f6bea6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -417,6 +417,7 @@ dependencies = [ "similar", "smallvec", "smol", + "strum", "telemetry_events", "terminal", "terminal_view", diff --git a/assets/prompts/edit_workflow.hbs b/assets/prompts/edit_workflow.hbs index 7abf79d20d..a9cb07a650 100644 --- a/assets/prompts/edit_workflow.hbs +++ b/assets/prompts/edit_workflow.hbs @@ -1,302 +1,656 @@ - -Guide the user through code changes in numbered steps that focus on individual functions, type definitions, etc. -Surround each distinct step in a XML tag. The user will be performing these steps in a code editor -named Zed, which is where they will have entered this prompt and will be seeing the response. + - -- Use the language of the file for code fence blocks unless otherwise specified. -- Include a code or file action in each step. -- Only put code in separate steps if it should either go in separate files, or in different (non-contiguous) places in the same file. -- Provide error handling and input validation where appropriate. -- Adapt explanations based on the user's perceived level of expertise. -- Include comments in code examples to enhance understanding. -- Consider more complex programming scenarios when relevant. -- Avoid using the terminal to perform filesystem operations such as creating, deleting, renaming, and editing files. Assume the user prefers to use Zed for these operations, unless the prompt suggests that the user is expecting terminal commands. -- When creating a new file, Zed will automatically create any necessary directories in that file's path. So when a step will create a new file in Zed, don't mention needing to create the directory that it will go in. -- Assume the user is performing these actions in the Zed code editor, so avoid redundancies like "In your code editor, ..." or "In Zed, ..." -- Be concise without leaving out important information. - +# Code Change Workflow -Examples of user-assistant dialogs with step numbers and user follow-up requests: +Your task is to guide the user through code changes using a series of steps. Each step should describe a high-level change, which can consist of multiple edits to distinct locations in the codebase. + +## Output Example + +Provide output as XML, with the following format: + + +Update the Person struct to store an age + +```rust +struct Person { + // existing fields... + age: u8, + height: f32, + // existing fields... +} + +impl Person { + fn age(&self) -> u8 { + self.age + } +} +``` + + + src/person.rs + insert_before + struct Person height + Add the age field + + + + src/person.rs + append_child + impl Person + Add the age getter + + + +## Output Format + +First, each `` must contain a written description of the change that should be made. The description should begin with a high-level overview, and can contain markdown code blocks as well. The description should be self-contained and actionable. + +Each `` must contain one or more `` tags, each of which refer to a specific range in a source file. Each `` tag must contain the following child tags: + +### `` (required) + +This tag contains the path to the file that will be changed. It can be an existing path, or a path that should be created. + +### `` (optional) + +This tag contains the fully-qualified name of a symbol in the source file, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. If not provided, the new content will be inserted at the top of the file. + +### `` (required) + +This tag contains a single-line description of the edit that should be made at the given location. + +### `` (required) + +This tag indicates what type of change should be made, relative to the given location. It can be one of the following: +- `update`: Rewrites the specified symbol entirely based on the given description. +- `create`: Creates a new file with the given path based on the provided description. +- `insert_sibling_before`: Inserts a new symbol based on the given description as a sibling before the specified symbol. +- `insert_sibling_after`: Inserts a new symbol based on the given description as a sibling after the specified symbol. +- `prepend_child`: Inserts a new symbol as a child of the specified symbol at the start. +- `append_child`: Inserts a new symbol as a child of the specified symbol at the end. +- `delete`: Deletes the specified symbol from the containing file. + + +- There's no need to describe *what* to do, just *where* to do it. +- Only reference locations that actually exist (unless you're creating a file). +- If creating a file, assume any subsequent updates are included at the time of creation. +- Don't create and then update a file. Always create new files in shot. +- Prefer updating symbols lower in the syntax tree if possible. +- Never include edits on a parent symbol and one of its children in the same edit block. +- Never nest an edit with another edit. Never include CDATA. All edits are leaf nodes. +- Descriptions are required for all edits except delete. +- When generating multiple edits, ensure the descriptions are specific to each individual operation. +- Avoid referring to the location in the description. Focus on the change to be made, not the location where it's made. That's implicit with the symbol you provide. +- Don't generate multiple edits at the same location. Instead, combine them together in a single edit with a succinct combined description. +- Always ensure imports are added if you're referencing symbols that are not in scope. To manipulate imports, produce an edit where the `"symbol"` key is set to `"#imports"` + + +Here are some concrete examples. - -Create a Library struct with a vector of Book structs. Include methods to add books and find books by author. Use generics for the book ID. -Certainly! Let's create a Library struct with a vector of Book structs, including methods to add books and find books by author, using generics for the book ID: -Define the Book and Library structs -```rust -use std::fmt::Display; - -struct Book -where - T: Display + Clone -{ - id: T, - title: String, - author: String, + +```rs src/rectangle.rs +struct Rectangle { + width: f64, + height: f64, } -struct Library -where - T: Display + Clone -{ - books: Vec>, -} -``` - -Implement methods for the Library struct -```rust -impl Library -where - T: Display + Clone -{ - fn new() -> Self { - Library { books: Vec::new() } - } - - fn add_book(&mut self, id: T, title: String, author: String) { - let book = Book { id, title, author }; - self.books.push(book); - } - - fn find_books_by_author(&self, author: &str) -> Vec<&Book> { - self.books - .iter() - .filter(|book| book.author.to_lowercase() == author.to_lowercase()) - .collect() +impl Rectangle { + fn new(width: f64, height: f64) -> Self { + Rectangle { width, height } } } ``` - -This code defines: -1. A generic Book struct with an ID of type T, title, and author -2. A Library struct containing a vector of Books -3. Methods to create a new Library, add books, and find books by author -The use of generics allows flexibility in the type of ID used for books. - +We need to add methods to calculate the area and perimeter of the rectangle. Can you help with that? + - -How can I use this Library struct in a main function? -Here's how you can use the Library struct in a main function: + + +Add methods to calculate the area and perimeter of the rectangle -Create a main function to demonstrate Library usage ```rust -fn main() { - // Create a new Library with string IDs - let mut library = Library::new(); +impl Rectangle { + // existing methods... - // Add some books - library.add_book(String::from("001"), String::from("The Rust Programming Language"), String::from("Steve Klabnik")); - library.add_book(String::from("002"), String::from("Programming Rust"), String::from("Jim Blandy")); - library.add_book(String::from("003"), String::from("Rust in Action"), String::from("Tim McNamara")); + fn calculate_area(&self) -> f64 { + self.width * self.height + } - // Find books by author - let author = "Steve Klabnik"; - let books = library.find_books_by_author(author); - - println!("Books by {}:", author); - for book in books { - println!("ID: {}, Title: {}", book.id, book.title); + fn calculate_perimeter(&self) -> f64 { + 2.0 * (self.width + self.height) } } ``` + + + src/rectangle.rs + append_child + impl Rectangle + Add calculate_area and calculate_perimeter methods + -This main function demonstrates: -1. Creating a new Library instance -2. Adding books with string IDs -3. Finding and displaying books by a specific author - + +Implement the Display trait for the Rectangle struct - -Can you add a method to remove a book by ID and handle potential errors? -Certainly! Let's add a method to remove a book by ID and handle potential errors: - -Add a remove_book method to the Library struct ```rust -use std::fmt::Display; -use std::error::Error; use std::fmt; -#[derive(Debug)] -struct BookNotFoundError; +impl fmt::Display for Rectangle { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Rectangle: {}x{}", self.width, self.height) + } +} +``` -impl Error for BookNotFoundError {} + + src/rectangle.rs + insert_sibling_after + impl Rectangle + Implement Display trait for Rectangle + -impl fmt::Display for BookNotFoundError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Book not found") + + src/rectangle.rs + append_child + #imports + Add use statement for fmt + + + + + + + + + +```rs src/user.rs +struct User { + pub name: String, + age: u32, + email: String, +} + +impl User { + fn new(name: String, age: u32, email: String) -> Self { + User { name, age, email } + } + + pub fn print_info(&self) { + todo!() + } +} +``` + +Let's print all the user information and delete the email field. + + + + +Update the 'print_info' method to print user information + +```rust +impl User { + // ... other methods ... + + pub fn print_info(&self) { + println!("Name: {name}, Age: {age}", name = self.name, age = self.age); + } +} +``` + + + src/user.rs + update + impl User pub fn print_info + Print all the user information + + + + +Remove the 'email' field from the User struct + + + src/user.rs + delete + struct User email + + + + src/user.rs + update + impl User fn new + Remove email parameter from new method + + + + + + + + + + +```rs src/vehicle.rs +struct Vehicle { + make: String, + model: String, + year: u32, +} + +impl Vehicle { + fn new(make: String, model: String, year: u32) -> Self { + Vehicle { make, model, year } + } + + fn print_year(&self) { + println!("Year: {}", self.year); + } +} +``` + +Let's add a `start_engine` method for the vehicle and print how long it takes to start it. + + + + +Add a 'use std::time::Instant;' statement at the beginning of the file + + + src/vehicle.rs + prepend_child + #imports + Add use statement for std::time::Instant + + + + +Add a new method 'start_engine' in the Vehicle impl block + +```rust +impl Vehicle { + // ... existing methods ... + + fn start_engine(&self) { + let t0 = Instant::now(); + println!("Starting the engine of {} {} {}. Took: {:?}", + self.year, self.make, self.model, t0.elapsed()); + } +} +``` + + + src/vehicle.rs + append_child + impl Vehicle + Add start_engine method + + + + + + + + + +```rs src/employee.rs +struct Employee { + name: String, + position: String, + salary: u32, + department: String, +} + +impl Employee { + fn new(name: String, position: String, salary: u32, department: String) -> Self { + Employee { name, position, salary, department } + } + + fn print_details(&self) { + println!("Name: {}, Position: {}, Salary: {}, Department: {}", + self.name, self.position, self.salary, self.department); + } + + fn give_raise(&mut self, amount: u32) { + self.salary += amount; + } +} +``` + +Let's turn the salary field into an f32. Also, let's delete the department field. + + + + +Make salary an f32 + +```rust +struct Employee { + name: String, + position: String, + salary: f32, + department: String, +} + +impl Employee { + fn new(name: String, position: String, salary: f32, department: String) -> Self { + Employee { name, position, salary, department } + } + + // ... other methods ... + + fn give_raise(&mut self, amount: f32) { + self.salary += amount; + } +} +``` + + + src/employee.rs + update + struct Employee salary + Change salary type to f32 + + + + src/employee.rs + update + impl Employee fn new + Update new method to use f32 for salary + + + + src/employee.rs + update + impl Employee fn give_raise + Update give_raise method to use f32 for amount + + + + +Remove the 'department' field and update the 'print_details' method + +```rust +struct Employee { + name: String, + position: String, + salary: f32, +} + +impl Employee { + fn new(name: String, position: String, salary: f32) -> Self { + Employee { name, position, salary } + } + + fn print_details(&self) { + println!("Name: {}, Position: {}, Salary: {}", + self.name, self.position, self.salary); + } + + // ... other methods ... +} +``` + + + src/employee.rs + delete + struct Employee department + + + + src/employee.rs + update + impl Employee fn new + Remove department parameter from new method + + + + src/employee.rs + update + impl Employee fn print_details + Remove department from print_details method + + + + + + + + + +```rs src/game.rs +struct Player { + name: String, + health: i32, + pub score: u32, +} + +impl Player { + pub fn new(name: String) -> Self { + Player { name, health: 100, score: 0 } } } -impl Library -where - T: Display + Clone + PartialEq -{ - // ... (previous methods remain the same) +struct Game { + players: Vec, +} - fn remove_book(&mut self, id: &T) -> Result, BookNotFoundError> { - if let Some(index) = self.books.iter().position(|book| &book.id == id) { - Ok(self.books.remove(index)) - } else { - Err(BookNotFoundError) +impl Game { + fn new() -> Self { + Game { players: Vec::new() } + } +} +``` + +Introduce a level field. + + + + +Add a 'level' field to Player and update the 'new' method + +```rust +struct Player { + name: String, + health: i32, + pub score: u32, + level: u8, +} + +impl Player { + pub fn new(name: String) -> Self { + Player { name, health: 100, score: 0, level: 1 } + } +} +``` + + + src/game.rs + insert_sibling_after + struct Player pub score + Add level field to Player struct + + + + src/game.rs + update + impl Player pub fn new + Initialize level in new method + + + + + + + + + +```rs src/config.rs +use std::collections::HashMap; + +struct Config { + settings: HashMap, +} + +impl Config { + fn new() -> Self { + Config { settings: HashMap::new() } + } +} +``` + +I want to load the configuration from a file. + + + + +Add a 'load_from_file' method to Config and import necessary modules + +```rust +use std::collections::HashMap; +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + +struct Config { + settings: HashMap, +} + +impl Config { + fn new() -> Self { + Config { settings: HashMap::new() } + } + + fn load_from_file>(&mut self, path: P) -> io::Result<()> { + let file = File::open(path)?; + for line in io::BufReader::new(file).lines() { + let line = line?; + if let Some((key, value)) = line.split_once('=') { + self.settings.insert(key.trim().to_string(), value.trim().to_string()); + } + } + Ok(()) + } +} +``` + + + src/config.rs + append_child + #imports + Import necessary modules for file operations + + + + src/config.rs + append_child + impl Config + Add load_from_file method + + + + + + + + + +```rs src/database.rs +pub(crate) struct Database { + connection: Connection, +} + +impl Database { + fn new(url: &str) -> Result { + let connection = Connection::connect(url)?; + Ok(Database { connection }) + } + + async fn query(&self, sql: &str) -> Result, Error> { + self.connection.query(sql, &[]) + } +} +``` + +I want to add error handling to this module. + + + + +Add error handling to the 'query' method and create a custom error type + +```rust +use std::fmt; +use std::error::Error as StdError; + +#[derive(Debug)] +pub enum DatabaseError { + ConnectionError(String), + QueryError(String), +} + +impl fmt::Display for DatabaseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + DatabaseError::ConnectionError(msg) => write!(f, "Connection error: {}", msg), + DatabaseError::QueryError(msg) => write!(f, "Query error: {}", msg), } } } -``` - -Update the main function to demonstrate book removal -```rust -fn main() { - // ... (previous code remains the same) - // Attempt to remove a book - let id_to_remove = String::from("002"); - match library.remove_book(&id_to_remove) { - Ok(removed_book) => println!("Removed book: {} by {}", removed_book.title, removed_book.author), - Err(e) => println!("Error removing book: {}", e), +impl StdError for DatabaseError {} + +// ...omitted code... + +impl Database { + fn new(url: &str) -> Result { + let connection = Connection::connect(url) + .map_err(|e| DatabaseError::ConnectionError(e.to_string()))?; + Ok(Database { connection }) } - // Attempt to remove a non-existent book - let non_existent_id = String::from("999"); - match library.remove_book(&non_existent_id) { - Ok(_) => println!("Book removed successfully"), - Err(e) => println!("Error removing book: {}", e), - } - - // Display remaining books - println!("\nRemaining books:"); - for book in &library.books { - println!("ID: {}, Title: {} by {}", book.id, book.title, book.author); + async fn query(&self, sql: &str) -> Result, DatabaseError> { + self.connection.query(sql, &[]) + .await + .map_err(|e| DatabaseError::QueryError(e.to_string())) } } ``` + + + src/database.rs + prepend_child + #imports + Import necessary error handling modules + + + + src/database.rs + insert_sibling_before + pub(crate) struct Database + Define custom DatabaseError enum + + + + src/database.rs + update + impl Database fn new + Update new method to use DatabaseError + + + + src/database.rs + update + impl Database async fn query + Update query method to use DatabaseError + + -This update includes: -1. A new remove_book method that returns a Result type -2. A custom BookNotFoundError for error handling -3. Demonstration of successful book removal and error handling for non-existent books -4. Display of remaining books after removal attempts - -The remove_book method uses the PartialEq trait to compare book IDs, allowing for flexible ID types while maintaining type safety. - - - -Create a Person struct with name, age, and email fields, and a method to greet. Include error handling for age. -Certainly! Let's create a Person struct with the specified fields and a method to greet in Go: +You should think step by step. When possible, produce smaller, coherent logical steps as opposed to one big step that combines lots of heterogeneous edits. -Define the Person struct and NewPerson function with error handling -```go -type Person struct { - Name string - Age int - Email string -} - -func NewPerson(name string, age int, email string) (*Person, error) { - if age < 0 || age > 150 { - return nil, fmt.Errorf("invalid age: %d", age) - } - return &Person{Name: name, Age: age, Email: email}, nil -} -``` - -Implement the Greet method for the Person struct -```go -func (p *Person) Greet() string { - return fmt.Sprintf("Hello, I'm %s, %d years old. You can reach me at %s.", p.Name, p.Age, p.Email) -} -``` - - -This code defines a Person struct, a NewPerson function with age validation, and a Greet method. - - - -How can I use these in a main function? -Here's how to use the Person struct and its methods in a main function: - -Create a main function with error handling -```go -func main() { - // Create a new Person instance - person, err := NewPerson("Alice", 30, "alice@example.com") - if err != nil { - fmt.Println("Error creating person:", err) - return - } - - // Use the Greet method - fmt.Println(person.Greet()) - - // Demonstrate error handling with invalid age - invalidPerson, err := NewPerson("Bob", 200, "bob@example.com") - if err != nil { - fmt.Println("Error creating person with invalid age:", err) - } -} -``` - - -This main function demonstrates: -1. Creating a valid Person instance -2. Using the Greet method -3. Handling errors when creating a Person with an invalid age - - - -Can you add a method to update the email with validation? -Certainly! Let's add a method to update the email with validation: - -Add UpdateEmail method with validation -```go -import ( - "fmt" - "regexp" -) - -func (p *Person) UpdateEmail(newEmail string) error { - // Simple email validation using regex - emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) - if !emailRegex.MatchString(newEmail) { - return fmt.Errorf("invalid email format: %s", newEmail) - } - p.Email = newEmail - return nil -} -``` - -Update the main function to demonstrate email update -```go -func main() { - // ... (previous code remains the same) - - // Demonstrate email update - err = person.UpdateEmail("alice.new@example.com") - if err != nil { - fmt.Println("Error updating email:", err) - } else { - fmt.Println("Updated greeting:", person.Greet()) - } - - // Demonstrate email update with invalid email - err = person.UpdateEmail("invalid-email") - if err != nil { - fmt.Println("Error updating email:", err) - } -} -``` - - -This update includes: -1. An UpdateEmail method with email format validation -2. Demonstration of successful email update in the main function -3. Handling of invalid email update attempt - - - + diff --git a/crates/assistant/Cargo.toml b/crates/assistant/Cargo.toml index 8dc50495fa..08de8ad694 100644 --- a/crates/assistant/Cargo.toml +++ b/crates/assistant/Cargo.toml @@ -73,6 +73,7 @@ settings.workspace = true similar.workspace = true smallvec.workspace = true smol.workspace = true +strum.workspace = true telemetry_events.workspace = true terminal.workspace = true terminal_view.workspace = true diff --git a/crates/assistant/src/assistant.rs b/crates/assistant/src/assistant.rs index c6fa9b2f97..9557eb6e3d 100644 --- a/crates/assistant/src/assistant.rs +++ b/crates/assistant/src/assistant.rs @@ -362,7 +362,7 @@ fn register_slash_commands(prompt_builder: Option>, cx: &mut if let Some(prompt_builder) = prompt_builder { slash_command_registry.register_command( - workflow_command::WorkflowSlashCommand::new(prompt_builder), + workflow_command::WorkflowSlashCommand::new(prompt_builder.clone()), true, ); } diff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rs index f915e221fb..988f2d6bba 100644 --- a/crates/assistant/src/assistant_panel.rs +++ b/crates/assistant/src/assistant_panel.rs @@ -12,13 +12,12 @@ use crate::{ slash_command_picker, terminal_inline_assistant::TerminalInlineAssistant, Assist, CacheStatus, ConfirmCommand, Context, ContextEvent, ContextId, ContextStore, - CycleMessageRole, DeployHistory, DeployPromptLibrary, InlineAssistId, InlineAssistant, - InsertIntoEditor, Message, MessageId, MessageMetadata, MessageStatus, ModelSelector, - PendingSlashCommand, PendingSlashCommandStatus, QuoteSelection, RemoteContextMetadata, - SavedContextMetadata, Split, ToggleFocus, ToggleModelSelector, WorkflowStepResolution, - WorkflowStepView, + ContextStoreEvent, CycleMessageRole, DeployHistory, DeployPromptLibrary, InlineAssistId, + InlineAssistant, InsertIntoEditor, Message, MessageId, MessageMetadata, MessageStatus, + ModelPickerDelegate, ModelSelector, PendingSlashCommand, PendingSlashCommandStatus, + QuoteSelection, RemoteContextMetadata, SavedContextMetadata, Split, ToggleFocus, + ToggleModelSelector, WorkflowStepResolution, }; -use crate::{ContextStoreEvent, ModelPickerDelegate}; use anyhow::{anyhow, Result}; use assistant_slash_command::{SlashCommand, SlashCommandOutputSection}; use client::{proto, Client, Status}; @@ -26,7 +25,7 @@ use collections::{BTreeSet, HashMap, HashSet}; use editor::{ actions::{FoldAt, MoveToEndOfLine, Newline, ShowCompletions, UnfoldAt}, display_map::{ - BlockContext, BlockDisposition, BlockProperties, BlockStyle, Crease, CustomBlockId, + BlockDisposition, BlockId, BlockProperties, BlockStyle, Crease, CustomBlockId, FoldId, RenderBlock, ToDisplayPoint, }, scroll::{Autoscroll, AutoscrollStrategy, ScrollAnchor}, @@ -57,12 +56,7 @@ use search::{buffer_search::DivRegistrar, BufferSearchBar}; use settings::{update_settings_file, Settings}; use smol::stream::StreamExt; use std::{ - borrow::Cow, - cmp, - fmt::Write, - ops::{DerefMut, Range}, - path::PathBuf, - sync::Arc, + borrow::Cow, cmp, collections::hash_map, fmt::Write, ops::Range, path::PathBuf, sync::Arc, time::Duration, }; use terminal_view::{terminal_panel::TerminalPanel, TerminalView}; @@ -70,8 +64,8 @@ use ui::TintColor; use ui::{ prelude::*, utils::{format_distance_from_now, DateTimeType}, - Avatar, AvatarShape, ButtonLike, ContextMenu, Disclosure, ElevationIndex, IconButtonShape, - KeyBinding, ListItem, ListItemSpacing, PopoverMenu, PopoverMenuHandle, Tooltip, + Avatar, AvatarShape, ButtonLike, ContextMenu, Disclosure, ElevationIndex, KeyBinding, ListItem, + ListItemSpacing, PopoverMenu, PopoverMenuHandle, Tooltip, }; use util::ResultExt; use workspace::{ @@ -1326,364 +1320,49 @@ struct ScrollPosition { cursor: Anchor, } -struct WorkflowStep { - range: Range, +struct WorkflowStepViewState { header_block_id: CustomBlockId, - footer_block_id: CustomBlockId, - resolved_step: Option>>, + header_crease_id: CreaseId, + footer_block_id: Option, + footer_crease_id: Option, assist: Option, - auto_apply: bool, + resolution: Option>>, } -impl WorkflowStep { +impl WorkflowStepViewState { fn status(&self, cx: &AppContext) -> WorkflowStepStatus { - match self.resolved_step.as_ref() { - Some(Ok(step)) => { - if step.suggestion_groups.is_empty() { - WorkflowStepStatus::Empty - } else if let Some(assist) = self.assist.as_ref() { - let assistant = InlineAssistant::global(cx); - if assist - .assist_ids - .iter() - .any(|assist_id| assistant.assist_status(*assist_id, cx).is_pending()) - { - WorkflowStepStatus::Pending - } else if assist - .assist_ids - .iter() - .all(|assist_id| assistant.assist_status(*assist_id, cx).is_confirmed()) - { - WorkflowStepStatus::Confirmed - } else if assist - .assist_ids - .iter() - .all(|assist_id| assistant.assist_status(*assist_id, cx).is_done()) - { - WorkflowStepStatus::Done - } else { - WorkflowStepStatus::Idle - } - } else { - WorkflowStepStatus::Idle - } + if let Some(assist) = &self.assist { + match assist.status(cx) { + WorkflowAssistStatus::Idle => WorkflowStepStatus::Idle, + WorkflowAssistStatus::Pending => WorkflowStepStatus::Pending, + WorkflowAssistStatus::Done => WorkflowStepStatus::Done, + WorkflowAssistStatus::Confirmed => WorkflowStepStatus::Confirmed, } - Some(Err(error)) => WorkflowStepStatus::Error(error.clone()), - None => WorkflowStepStatus::Resolving { - auto_apply: self.auto_apply, - }, + } else if let Some(resolution) = self.resolution.as_deref() { + match resolution { + Err(err) => WorkflowStepStatus::Error(err), + Ok(_) => WorkflowStepStatus::Idle, + } + } else { + WorkflowStepStatus::Resolving } } } -#[derive(Clone)] -enum WorkflowStepStatus { - Resolving { auto_apply: bool }, - Error(Arc), - Empty, +#[derive(Clone, Copy)] +enum WorkflowStepStatus<'a> { + Resolving, + Error(&'a anyhow::Error), Idle, Pending, Done, Confirmed, } -impl WorkflowStepStatus { +impl<'a> WorkflowStepStatus<'a> { pub(crate) fn is_confirmed(&self) -> bool { matches!(self, Self::Confirmed) } - - fn render_workflow_step_error( - id: EntityId, - editor: WeakView, - step_range: Range, - error: String, - ) -> AnyElement { - h_flex() - .gap_2() - .child( - div() - .id("step-resolution-failure") - .child( - Label::new("Step Resolution Failed") - .size(LabelSize::Small) - .color(Color::Error), - ) - .tooltip(move |cx| Tooltip::text(error.clone(), cx)), - ) - .child( - Button::new(("transform", id), "Retry") - .icon(IconName::Update) - .icon_position(IconPosition::Start) - .icon_size(IconSize::Small) - .label_size(LabelSize::Small) - .on_click({ - let editor = editor.clone(); - let step_range = step_range.clone(); - move |_, cx| { - editor - .update(cx, |this, cx| { - this.resolve_workflow_step(step_range.clone(), cx) - }) - .ok(); - } - }), - ) - .into_any() - } - - pub(crate) fn into_element( - &self, - step_range: Range, - focus_handle: FocusHandle, - editor: WeakView, - cx: &mut BlockContext<'_, '_>, - ) -> AnyElement { - let id = EntityId::from(cx.block_id); - fn display_keybind_in_tooltip( - step_range: &Range, - editor: &WeakView, - cx: &mut WindowContext<'_>, - ) -> bool { - editor - .update(cx, |this, _| { - this.active_workflow_step - .as_ref() - .map(|step| &step.range == step_range) - }) - .ok() - .flatten() - .unwrap_or_default() - } - match self { - WorkflowStepStatus::Error(error) => Self::render_workflow_step_error( - id, - editor.clone(), - step_range.clone(), - error.to_string(), - ), - WorkflowStepStatus::Empty => Self::render_workflow_step_error( - id, - editor.clone(), - step_range.clone(), - "Model was unable to locate the code to edit".to_string(), - ), - WorkflowStepStatus::Idle | WorkflowStepStatus::Resolving { .. } => { - let status = self.clone(); - Button::new(("transform", id), "Transform") - .icon(IconName::SparkleAlt) - .icon_position(IconPosition::Start) - .icon_size(IconSize::Small) - .label_size(LabelSize::Small) - .style(ButtonStyle::Tinted(TintColor::Accent)) - .tooltip({ - let step_range = step_range.clone(); - let editor = editor.clone(); - move |cx| { - cx.new_view(|cx| { - let tooltip = Tooltip::new("Transform"); - if display_keybind_in_tooltip(&step_range, &editor, cx) { - tooltip.key_binding(KeyBinding::for_action_in( - &Assist, - &focus_handle, - cx, - )) - } else { - tooltip - } - }) - .into() - } - }) - .on_click({ - let editor = editor.clone(); - let step_range = step_range.clone(); - move |_, cx| { - if let WorkflowStepStatus::Idle = &status { - editor - .update(cx, |this, cx| { - this.apply_workflow_step(step_range.clone(), cx) - }) - .ok(); - } else if let WorkflowStepStatus::Resolving { auto_apply: false } = - &status - { - editor - .update(cx, |this, _| { - if let Some(step) = this.workflow_steps.get_mut(&step_range) - { - step.auto_apply = true; - } - }) - .ok(); - } - } - }) - .map(|this| { - if let WorkflowStepStatus::Resolving { auto_apply: true } = &self { - this.with_animation( - ("resolving-suggestion-animation", id), - Animation::new(Duration::from_secs(2)) - .repeat() - .with_easing(pulsating_between(0.4, 0.8)), - |label, delta| label.alpha(delta), - ) - .into_any_element() - } else { - this.into_any_element() - } - }) - } - WorkflowStepStatus::Pending => h_flex() - .items_center() - .gap_2() - .child( - Label::new("Applying...") - .size(LabelSize::Small) - .with_animation( - ("applying-step-transformation-label", id), - Animation::new(Duration::from_secs(2)) - .repeat() - .with_easing(pulsating_between(0.4, 0.8)), - |label, delta| label.alpha(delta), - ), - ) - .child( - IconButton::new(("stop-transformation", id), IconName::Stop) - .icon_size(IconSize::Small) - .icon_color(Color::Error) - .style(ButtonStyle::Subtle) - .tooltip({ - let step_range = step_range.clone(); - let editor = editor.clone(); - move |cx| { - cx.new_view(|cx| { - let tooltip = Tooltip::new("Stop Transformation"); - if display_keybind_in_tooltip(&step_range, &editor, cx) { - tooltip.key_binding(KeyBinding::for_action_in( - &editor::actions::Cancel, - &focus_handle, - cx, - )) - } else { - tooltip - } - }) - .into() - } - }) - .on_click({ - let editor = editor.clone(); - let step_range = step_range.clone(); - move |_, cx| { - editor - .update(cx, |this, cx| { - this.stop_workflow_step(step_range.clone(), cx) - }) - .ok(); - } - }), - ) - .into_any_element(), - WorkflowStepStatus::Done => h_flex() - .gap_1() - .child( - IconButton::new(("stop-transformation", id), IconName::Close) - .icon_size(IconSize::Small) - .style(ButtonStyle::Tinted(TintColor::Negative)) - .tooltip({ - let focus_handle = focus_handle.clone(); - let editor = editor.clone(); - let step_range = step_range.clone(); - move |cx| { - cx.new_view(|cx| { - let tooltip = Tooltip::new("Reject Transformation"); - if display_keybind_in_tooltip(&step_range, &editor, cx) { - tooltip.key_binding(KeyBinding::for_action_in( - &editor::actions::Cancel, - &focus_handle, - cx, - )) - } else { - tooltip - } - }) - .into() - } - }) - .on_click({ - let editor = editor.clone(); - let step_range = step_range.clone(); - move |_, cx| { - editor - .update(cx, |this, cx| { - this.reject_workflow_step(step_range.clone(), cx); - }) - .ok(); - } - }), - ) - .child( - Button::new(("confirm-workflow-step", id), "Accept") - .icon(IconName::Check) - .icon_position(IconPosition::Start) - .icon_size(IconSize::Small) - .label_size(LabelSize::Small) - .style(ButtonStyle::Tinted(TintColor::Positive)) - .tooltip({ - let editor = editor.clone(); - let step_range = step_range.clone(); - move |cx| { - cx.new_view(|cx| { - let tooltip = Tooltip::new("Accept Transformation"); - if display_keybind_in_tooltip(&step_range, &editor, cx) { - tooltip.key_binding(KeyBinding::for_action_in( - &Assist, - &focus_handle, - cx, - )) - } else { - tooltip - } - }) - .into() - } - }) - .on_click({ - let editor = editor.clone(); - let step_range = step_range.clone(); - move |_, cx| { - editor - .update(cx, |this, cx| { - this.confirm_workflow_step(step_range.clone(), cx); - }) - .ok(); - } - }), - ) - .into_any_element(), - WorkflowStepStatus::Confirmed => h_flex() - .child( - Button::new(("revert-workflow-step", id), "Undo") - .style(ButtonStyle::Filled) - .icon(Some(IconName::Undo)) - .icon_position(IconPosition::Start) - .icon_size(IconSize::Small) - .label_size(LabelSize::Small) - .on_click({ - let editor = editor.clone(); - let step_range = step_range.clone(); - move |_, cx| { - editor - .update(cx, |this, cx| { - this.undo_workflow_step(step_range.clone(), cx); - }) - .ok(); - } - }), - ) - .into_any_element(), - } - } } #[derive(Debug, Eq, PartialEq)] @@ -1714,7 +1393,7 @@ pub struct ContextEditor { pending_slash_command_creases: HashMap, CreaseId>, pending_slash_command_blocks: HashMap, CustomBlockId>, _subscriptions: Vec, - workflow_steps: HashMap, WorkflowStep>, + workflow_steps: HashMap, WorkflowStepViewState>, active_workflow_step: Option, assistant_panel: WeakView, error_message: Option, @@ -1763,6 +1442,7 @@ impl ContextEditor { ]; let sections = context.read(cx).slash_command_output_sections().to_vec(); + let edit_step_ranges = context.read(cx).workflow_step_ranges().collect::>(); let mut this = Self { context, editor, @@ -1787,6 +1467,7 @@ impl ContextEditor { this.update_message_headers(cx); this.update_image_blocks(cx); this.insert_slash_command_output_sections(sections, false, cx); + this.workflow_steps_updated(&Vec::new(), &edit_step_ranges, cx); this } @@ -1796,7 +1477,7 @@ impl ContextEditor { editor.insert(&format!("/{command_name}\n\n"), cx) }); let command = self.context.update(cx, |context, cx| { - context.reparse_slash_commands(cx); + context.reparse(cx); context.pending_slash_commands()[0].clone() }); self.run_command( @@ -1858,27 +1539,26 @@ impl ContextEditor { } fn apply_active_workflow_step(&mut self, cx: &mut ViewContext) -> bool { - let Some(step) = self.active_workflow_step() else { + let Some((range, step)) = self.active_workflow_step() else { return false; }; - let range = step.range.clone(); - match step.status(cx) { - WorkflowStepStatus::Resolving { .. } | WorkflowStepStatus::Pending => true, - WorkflowStepStatus::Idle => { - self.apply_workflow_step(range, cx); - true + if let Some(assist) = step.assist.as_ref() { + match assist.status(cx) { + WorkflowAssistStatus::Pending => {} + WorkflowAssistStatus::Confirmed => return false, + WorkflowAssistStatus::Done => self.confirm_workflow_step(range, cx), + WorkflowAssistStatus::Idle => self.apply_workflow_step(range, cx), } - WorkflowStepStatus::Done => { - self.confirm_workflow_step(range, cx); - true + } else { + match step.resolution.as_deref() { + Some(Ok(_)) => self.apply_workflow_step(range, cx), + Some(Err(_)) => self.resolve_workflow_step(range, cx), + None => {} } - WorkflowStepStatus::Error(_) | WorkflowStepStatus::Empty => { - self.resolve_workflow_step(range, cx); - true - } - WorkflowStepStatus::Confirmed => false, } + + true } fn resolve_workflow_step( @@ -1982,14 +1662,14 @@ impl ContextEditor { return; } - if let Some(active_step) = self.active_workflow_step() { + if let Some((range, active_step)) = self.active_workflow_step() { match active_step.status(cx) { WorkflowStepStatus::Pending => { - self.stop_workflow_step(active_step.range.clone(), cx); + self.stop_workflow_step(range, cx); return; } WorkflowStepStatus::Done => { - self.reject_workflow_step(active_step.range.clone(), cx); + self.reject_workflow_step(range, cx); return; } _ => {} @@ -2062,7 +1742,7 @@ impl ContextEditor { let mut commands_by_range = HashMap::default(); let workspace = self.workspace.clone(); self.context.update(cx, |context, cx| { - context.reparse_slash_commands(cx); + context.reparse(cx); for selection in selections.iter() { if let Some(command) = context.pending_command_for_position(selection.head().text_anchor, cx) @@ -2133,14 +1813,6 @@ impl ContextEditor { context.save(Some(Duration::from_millis(500)), self.fs.clone(), cx); }); } - ContextEvent::WorkflowStepsRemoved(removed) => { - self.remove_workflow_steps(removed, cx); - cx.notify(); - } - ContextEvent::WorkflowStepUpdated(updated) => { - self.update_workflow_step(updated.clone(), cx); - cx.notify(); - } ContextEvent::SummaryChanged => { cx.emit(EditorEvent::TitleChanged); self.context.update(cx, |context, cx| { @@ -2161,6 +1833,9 @@ impl ContextEditor { } }); } + ContextEvent::WorkflowStepsUpdated { removed, updated } => { + self.workflow_steps_updated(removed, updated, cx); + } ContextEvent::PendingSlashCommandsUpdated { removed, updated } => { self.editor.update(cx, |editor, cx| { let buffer = editor.buffer().read(cx).snapshot(cx); @@ -2305,7 +1980,7 @@ impl ContextEditor { if *run_commands_in_output { let commands = self.context.update(cx, |context, cx| { - context.reparse_slash_commands(cx); + context.reparse(cx); context .pending_commands_for_range(output_range.clone(), cx) .to_vec() @@ -2331,6 +2006,231 @@ impl ContextEditor { } } + fn workflow_steps_updated( + &mut self, + removed: &Vec>, + updated: &Vec>, + cx: &mut ViewContext, + ) { + let this = cx.view().downgrade(); + let mut removed_crease_ids = Vec::new(); + let mut removed_block_ids = HashSet::default(); + let mut editors_to_close = Vec::new(); + for range in removed { + if let Some(state) = self.workflow_steps.remove(range) { + editors_to_close.extend(self.hide_workflow_step(range.clone(), cx)); + removed_block_ids.insert(state.header_block_id); + removed_crease_ids.push(state.header_crease_id); + removed_block_ids.extend(state.footer_block_id); + removed_crease_ids.extend(state.footer_crease_id); + } + } + + for range in updated { + editors_to_close.extend(self.hide_workflow_step(range.clone(), cx)); + } + + self.editor.update(cx, |editor, cx| { + let snapshot = editor.snapshot(cx); + let multibuffer = &snapshot.buffer_snapshot; + let (&excerpt_id, _, buffer) = multibuffer.as_singleton().unwrap(); + + for range in updated { + let Some(step) = self.context.read(cx).workflow_step_for_range(&range, cx) else { + continue; + }; + + let resolution = step.resolution.clone(); + let header_start = step.range.start; + let header_end = if buffer.contains_str_at(step.leading_tags_end, "\n") { + buffer.anchor_before(step.leading_tags_end.to_offset(&buffer) + 1) + } else { + step.leading_tags_end + }; + let header_range = multibuffer + .anchor_in_excerpt(excerpt_id, header_start) + .unwrap() + ..multibuffer + .anchor_in_excerpt(excerpt_id, header_end) + .unwrap(); + let footer_range = step.trailing_tag_start.map(|start| { + let mut step_range_end = step.range.end.to_offset(&buffer); + if buffer.contains_str_at(step_range_end, "\n") { + // Only include the newline if it belongs to the same message. + let messages = self + .context + .read(cx) + .messages_for_offsets([step_range_end, step_range_end + 1], cx); + if messages.len() == 1 { + step_range_end += 1; + } + } + + let end = buffer.anchor_before(step_range_end); + multibuffer.anchor_in_excerpt(excerpt_id, start).unwrap() + ..multibuffer.anchor_in_excerpt(excerpt_id, end).unwrap() + }); + + let block_ids = editor.insert_blocks( + [BlockProperties { + position: header_range.start, + height: 1, + style: BlockStyle::Flex, + render: Box::new({ + let this = this.clone(); + let range = step.range.clone(); + move |cx| { + let block_id = cx.block_id; + let max_width = cx.max_width; + let gutter_width = cx.gutter_dimensions.full_width(); + this.update(&mut **cx, |this, cx| { + this.render_workflow_step_header( + range.clone(), + max_width, + gutter_width, + block_id, + cx, + ) + }) + .ok() + .flatten() + .unwrap_or_else(|| Empty.into_any()) + } + }), + disposition: BlockDisposition::Above, + priority: 0, + }] + .into_iter() + .chain(footer_range.as_ref().map(|footer_range| { + return BlockProperties { + position: footer_range.end, + height: 1, + style: BlockStyle::Flex, + render: Box::new({ + let this = this.clone(); + let range = step.range.clone(); + move |cx| { + let max_width = cx.max_width; + let gutter_width = cx.gutter_dimensions.full_width(); + this.update(&mut **cx, |this, cx| { + this.render_workflow_step_footer( + range.clone(), + max_width, + gutter_width, + cx, + ) + }) + .ok() + .flatten() + .unwrap_or_else(|| Empty.into_any()) + } + }), + disposition: BlockDisposition::Below, + priority: 0, + }; + })), + None, + cx, + ); + + let header_placeholder = FoldPlaceholder { + render: Arc::new(move |_, _crease_range, _cx| Empty.into_any()), + constrain_width: false, + merge_adjacent: false, + }; + let footer_placeholder = FoldPlaceholder { + render: render_fold_icon_button( + cx.view().downgrade(), + IconName::Code, + "Edits".into(), + ), + constrain_width: false, + merge_adjacent: false, + }; + + let new_crease_ids = editor.insert_creases( + [Crease::new( + header_range.clone(), + header_placeholder.clone(), + fold_toggle("step-header"), + |_, _, _| Empty.into_any_element(), + )] + .into_iter() + .chain(footer_range.clone().map(|footer_range| { + Crease::new( + footer_range, + footer_placeholder.clone(), + |row, is_folded, fold, cx| { + if is_folded { + Empty.into_any_element() + } else { + fold_toggle("step-footer")(row, is_folded, fold, cx) + } + }, + |_, _, _| Empty.into_any_element(), + ) + })), + cx, + ); + + let state = WorkflowStepViewState { + header_block_id: block_ids[0], + header_crease_id: new_crease_ids[0], + footer_block_id: block_ids.get(1).copied(), + footer_crease_id: new_crease_ids.get(1).copied(), + resolution, + assist: None, + }; + + let mut folds_to_insert = [(header_range.clone(), header_placeholder)] + .into_iter() + .chain( + footer_range + .clone() + .map(|range| (range, footer_placeholder)), + ) + .collect::>(); + + match self.workflow_steps.entry(range.clone()) { + hash_map::Entry::Vacant(entry) => { + entry.insert(state); + } + hash_map::Entry::Occupied(mut entry) => { + let entry = entry.get_mut(); + removed_block_ids.insert(entry.header_block_id); + removed_crease_ids.push(entry.header_crease_id); + removed_block_ids.extend(entry.footer_block_id); + removed_crease_ids.extend(entry.footer_crease_id); + folds_to_insert.retain(|(range, _)| snapshot.intersects_fold(range.start)); + *entry = state; + } + } + + editor.unfold_ranges( + [header_range.clone()] + .into_iter() + .chain(footer_range.clone()), + true, + false, + cx, + ); + + if !folds_to_insert.is_empty() { + editor.fold_ranges(folds_to_insert, false, cx); + } + } + + editor.remove_creases(removed_crease_ids, cx); + editor.remove_blocks(removed_block_ids, None, cx); + }); + + for (editor, editor_was_open) in editors_to_close { + self.close_workflow_editor(cx, editor, editor_was_open); + } + + self.update_active_workflow_step(cx); + } + fn insert_slash_command_output_sections( &mut self, sections: impl IntoIterator>, @@ -2354,31 +2254,11 @@ impl ContextEditor { creases.push(Crease::new( start..end, FoldPlaceholder { - render: Arc::new({ - let editor = cx.view().downgrade(); - let icon = section.icon; - let label = section.label.clone(); - move |fold_id, fold_range, _cx| { - let editor = editor.clone(); - ButtonLike::new(fold_id) - .style(ButtonStyle::Filled) - .layer(ElevationIndex::ElevatedSurface) - .child(Icon::new(icon)) - .child(Label::new(label.clone()).single_line()) - .on_click(move |_, cx| { - editor - .update(cx, |editor, cx| { - let buffer_start = fold_range - .start - .to_point(&editor.buffer().read(cx).read(cx)); - let buffer_row = MultiBufferRow(buffer_start.row); - editor.unfold_at(&UnfoldAt { buffer_row }, cx); - }) - .ok(); - }) - .into_any_element() - } - }), + render: render_fold_icon_button( + cx.view().downgrade(), + section.icon, + section.label.clone(), + ), constrain_width: false, merge_adjacent: false, }, @@ -2422,321 +2302,22 @@ impl ContextEditor { cx.emit(event.clone()); } - fn active_workflow_step(&self) -> Option<&WorkflowStep> { + fn active_workflow_step(&self) -> Option<(Range, &WorkflowStepViewState)> { let step = self.active_workflow_step.as_ref()?; - self.workflow_steps.get(&step.range) - } - - fn remove_workflow_steps( - &mut self, - removed_steps: &[Range], - cx: &mut ViewContext, - ) { - let mut blocks_to_remove = HashSet::default(); - for step_range in removed_steps { - self.hide_workflow_step(step_range.clone(), cx); - if let Some(step) = self.workflow_steps.remove(step_range) { - blocks_to_remove.insert(step.header_block_id); - blocks_to_remove.insert(step.footer_block_id); - } - } - self.editor.update(cx, |editor, cx| { - editor.remove_blocks(blocks_to_remove, None, cx) - }); - self.update_active_workflow_step(cx); - } - - fn update_workflow_step( - &mut self, - step_range: Range, - cx: &mut ViewContext, - ) { - let buffer_snapshot = self.editor.read(cx).buffer().read(cx).snapshot(cx); - let (&excerpt_id, _, _) = buffer_snapshot.as_singleton().unwrap(); - - let Some(step) = self - .context - .read(cx) - .workflow_step_for_range(step_range.clone(), cx) - else { - return; - }; - - let resolved_step = step.read(cx).resolution.clone(); - - if let Some(Ok(resolution)) = resolved_step.as_ref() { - for (buffer, _) in resolution.suggestion_groups.iter() { - let step_range = step_range.clone(); - cx.subscribe(buffer, move |this, _, event, cx| match event { - language::Event::Discarded => this.undo_workflow_step(step_range.clone(), cx), - _ => {} - }) - .detach(); - } - } - - if let Some(existing_step) = self.workflow_steps.get_mut(&step_range) { - existing_step.resolved_step = resolved_step; - } else { - let start = buffer_snapshot - .anchor_in_excerpt(excerpt_id, step_range.start) - .unwrap(); - let end = buffer_snapshot - .anchor_in_excerpt(excerpt_id, step_range.end) - .unwrap(); - let weak_self = cx.view().downgrade(); - let block_ids = self.editor.update(cx, |editor, cx| { - let step_range = step_range.clone(); - let editor_focus_handle = editor.focus_handle(cx); - editor.insert_blocks( - vec![ - BlockProperties { - position: start, - height: 1, - style: BlockStyle::Sticky, - render: Box::new({ - let weak_self = weak_self.clone(); - let step_range = step_range.clone(); - move |cx| { - let current_status = weak_self - .update(&mut **cx, |context_editor, cx| { - let step = - context_editor.workflow_steps.get(&step_range)?; - Some(step.status(cx)) - }) - .ok() - .flatten(); - - let theme = cx.theme().status(); - let border_color = if current_status - .as_ref() - .map_or(false, |status| status.is_confirmed()) - { - theme.ignored_border - } else { - theme.info_border - }; - let step_index = weak_self - .update(&mut **cx, |this, cx| { - let snapshot = this - .editor - .read(cx) - .buffer() - .read(cx) - .as_singleton()? - .read(cx) - .text_snapshot(); - let start_offset = - step_range.start.to_offset(&snapshot); - let parent_message = this - .context - .read(cx) - .messages_for_offsets([start_offset], cx); - debug_assert_eq!(parent_message.len(), 1); - let parent_message = parent_message.first()?; - - let index_of_current_step = this - .workflow_steps - .keys() - .filter(|workflow_step_range| { - workflow_step_range - .start - .cmp(&parent_message.anchor, &snapshot) - .is_ge() - && workflow_step_range - .end - .cmp(&step_range.end, &snapshot) - .is_le() - }) - .count(); - Some(index_of_current_step) - }) - .ok() - .flatten(); - - let step_label = if let Some(index) = step_index { - Label::new(format!("Step {index}")).size(LabelSize::Small) - } else { - Label::new("Step").size(LabelSize::Small) - }; - - let step_label = if current_status - .as_ref() - .is_some_and(|status| status.is_confirmed()) - { - h_flex() - .items_center() - .gap_2() - .child( - step_label.strikethrough(true).color(Color::Muted), - ) - .child( - Icon::new(IconName::Check) - .size(IconSize::Small) - .color(Color::Created), - ) - } else { - div().child(step_label) - }; - - let step_label_element = step_label.into_any_element(); - - let step_label = h_flex() - .id("step") - .group("step-label") - .items_center() - .gap_1() - .child(step_label_element) - .child( - IconButton::new("edit-step", IconName::SearchCode) - .size(ButtonSize::Compact) - .icon_size(IconSize::Small) - .shape(IconButtonShape::Square) - .visible_on_hover("step-label") - .tooltip(|cx| Tooltip::text("Open Step View", cx)) - .on_click({ - let this = weak_self.clone(); - let step_range = step_range.clone(); - move |_, cx| { - this.update(cx, |this, cx| { - this.open_workflow_step( - step_range.clone(), - cx, - ); - }) - .ok(); - } - }), - ); - - div() - .w_full() - .px(cx.gutter_dimensions.full_width()) - .child( - h_flex() - .w_full() - .h_8() - .border_b_1() - .border_color(border_color) - .pb_2() - .items_center() - .justify_between() - .gap_2() - .child( - h_flex() - .justify_start() - .gap_2() - .child(step_label), - ) - .children(current_status.as_ref().map(|status| { - h_flex().w_full().justify_end().child( - status.into_element( - step_range.clone(), - editor_focus_handle.clone(), - weak_self.clone(), - cx, - ), - ) - })), - ) - .into_any() - } - }), - disposition: BlockDisposition::Above, - priority: 0, - }, - BlockProperties { - position: end, - height: 0, - style: BlockStyle::Sticky, - render: Box::new(move |cx| { - let current_status = weak_self - .update(&mut **cx, |context_editor, cx| { - let step = - context_editor.workflow_steps.get(&step_range)?; - Some(step.status(cx)) - }) - .ok() - .flatten(); - let theme = cx.theme().status(); - let border_color = if current_status - .as_ref() - .map_or(false, |status| status.is_confirmed()) - { - theme.ignored_border - } else { - theme.info_border - }; - - div() - .w_full() - .px(cx.gutter_dimensions.full_width()) - .child(h_flex().h(px(1.)).bg(border_color)) - .into_any() - }), - disposition: BlockDisposition::Below, - priority: 0, - }, - ], - None, - cx, - ) - }); - self.workflow_steps.insert( - step_range.clone(), - WorkflowStep { - range: step_range.clone(), - header_block_id: block_ids[0], - footer_block_id: block_ids[1], - resolved_step, - assist: None, - auto_apply: false, - }, - ); - } - - self.update_active_workflow_step(cx); - if let Some(step) = self.workflow_steps.get_mut(&step_range) { - if step.auto_apply && matches!(step.status(cx), WorkflowStepStatus::Idle) { - self.apply_workflow_step(step_range, cx); - } - } - } - - fn open_workflow_step( - &mut self, - step_range: Range, - cx: &mut ViewContext, - ) -> Option<()> { - let pane = self - .assistant_panel - .update(cx, |panel, _| panel.pane()) - .ok()??; - let context = self.context.read(cx); - let language_registry = context.language_registry(); - let step = context.workflow_step_for_range(step_range, cx)?; - let context = self.context.clone(); - cx.deref_mut().defer(move |cx| { - pane.update(cx, |pane, cx| { - let existing_item = pane - .items_of_type::() - .find(|item| *item.read(cx).step() == step.downgrade()); - if let Some(item) = existing_item { - if let Some(index) = pane.index_for_item(&item) { - pane.activate_item(index, true, true, cx); - } - } else { - let view = cx - .new_view(|cx| WorkflowStepView::new(context, step, language_registry, cx)); - pane.add_item(Box::new(view), true, true, None, cx); - } - }); - }); - None + Some((step.range.clone(), self.workflow_steps.get(&step.range)?)) } fn update_active_workflow_step(&mut self, cx: &mut ViewContext) { - let new_step = self.active_workflow_step_for_cursor(cx); + let newest_cursor = self.editor.read(cx).selections.newest::(cx).head(); + let context = self.context.read(cx); + + let new_step = context + .workflow_step_containing(newest_cursor, cx) + .map(|step| ActiveWorkflowStep { + resolved: step.resolution.is_some(), + range: step.range.clone(), + }); + if new_step.as_ref() != self.active_workflow_step.as_ref() { let mut old_editor = None; let mut old_editor_was_open = None; @@ -2765,27 +2346,22 @@ impl ContextEditor { step_range: Range, cx: &mut ViewContext, ) -> Option<(View, bool)> { - let Some(step) = self.workflow_steps.get_mut(&step_range) else { - return None; - }; - let Some(assist) = step.assist.as_ref() else { - return None; - }; - let Some(editor) = assist.editor.upgrade() else { - return None; - }; + if let Some(step) = self.workflow_steps.get_mut(&step_range) { + let assist = step.assist.as_ref()?; + let editor = assist.editor.upgrade()?; - if matches!(step.status(cx), WorkflowStepStatus::Idle) { - let assist = step.assist.take().unwrap(); - InlineAssistant::update_global(cx, |assistant, cx| { - for assist_id in assist.assist_ids { - assistant.finish_assist(assist_id, true, cx) - } - }); - return Some((editor, assist.editor_was_open)); + if matches!(step.status(cx), WorkflowStepStatus::Idle) { + let assist = step.assist.take().unwrap(); + InlineAssistant::update_global(cx, |assistant, cx| { + for assist_id in assist.assist_ids { + assistant.finish_assist(assist_id, true, cx) + } + }); + return Some((editor, assist.editor_was_open)); + } } - return None; + None } fn close_workflow_editor( @@ -2799,7 +2375,7 @@ impl ContextEditor { if let Some(pane) = workspace.pane_for(&editor) { pane.update(cx, |pane, cx| { let item_id = editor.entity_id(); - if !editor_was_open && pane.is_active_preview_item(item_id) { + if !editor_was_open && !editor.read(cx).is_focused(cx) { pane.close_item_by_id(item_id, SaveIntent::Skip, cx) .detach_and_log_err(cx); } @@ -2814,18 +2390,17 @@ impl ContextEditor { step_range: Range, cx: &mut ViewContext, ) -> Option> { - let Some(step) = self.workflow_steps.get_mut(&step_range) else { - return None; - }; + let step = self.workflow_steps.get_mut(&step_range)?; + let mut editor_to_return = None; let mut scroll_to_assist_id = None; match step.status(cx) { WorkflowStepStatus::Idle => { if let Some(assist) = step.assist.as_ref() { scroll_to_assist_id = assist.assist_ids.first().copied(); - } else if let Some(Ok(resolved)) = step.resolved_step.as_ref() { + } else if let Some(Ok(resolved)) = step.resolution.clone().as_deref() { step.assist = Self::open_assists_for_step( - resolved, + &resolved, &self.project, &self.assistant_panel, &self.workspace, @@ -2873,7 +2448,7 @@ impl ContextEditor { } } - return editor_to_return; + editor_to_return } fn open_assists_for_step( @@ -3197,7 +2772,7 @@ impl ContextEditor { }; let create_block_properties = |message: &Message| BlockProperties { position: buffer - .anchor_in_excerpt(excerpt_id, message.anchor) + .anchor_in_excerpt(excerpt_id, message.anchor_range.start) .unwrap(), height: 2, style: BlockStyle::Sticky, @@ -3585,6 +3160,396 @@ impl ContextEditor { .unwrap_or_else(|| Cow::Borrowed(DEFAULT_TAB_TITLE)) } + fn render_workflow_step_header( + &self, + range: Range, + max_width: Pixels, + gutter_width: Pixels, + id: BlockId, + cx: &mut ViewContext, + ) -> Option { + let step_state = self.workflow_steps.get(&range)?; + let status = step_state.status(cx); + let this = cx.view().downgrade(); + + let theme = cx.theme().status(); + let is_confirmed = status.is_confirmed(); + let border_color = if is_confirmed { + theme.ignored_border + } else { + theme.info_border + }; + + let editor = self.editor.read(cx); + let focus_handle = editor.focus_handle(cx); + let snapshot = editor + .buffer() + .read(cx) + .as_singleton()? + .read(cx) + .text_snapshot(); + let start_offset = range.start.to_offset(&snapshot); + let parent_message = self + .context + .read(cx) + .messages_for_offsets([start_offset], cx); + debug_assert_eq!(parent_message.len(), 1); + let parent_message = parent_message.first()?; + + let step_index = self + .workflow_steps + .keys() + .filter(|workflow_step_range| { + workflow_step_range + .start + .cmp(&parent_message.anchor_range.start, &snapshot) + .is_ge() + && workflow_step_range.end.cmp(&range.end, &snapshot).is_le() + }) + .count(); + + let step_label = Label::new(format!("Step {step_index}")).size(LabelSize::Small); + + let step_label = if is_confirmed { + h_flex() + .items_center() + .gap_2() + .child(step_label.strikethrough(true).color(Color::Muted)) + .child( + Icon::new(IconName::Check) + .size(IconSize::Small) + .color(Color::Created), + ) + } else { + div().child(step_label) + }; + + Some( + v_flex() + .w(max_width) + .pl(gutter_width) + .child( + h_flex() + .w_full() + .h_8() + .border_b_1() + .border_color(border_color) + .items_center() + .justify_between() + .gap_2() + .child(h_flex().justify_start().gap_2().child(step_label)) + .child(h_flex().w_full().justify_end().child( + Self::render_workflow_step_status( + status, + range.clone(), + focus_handle.clone(), + this.clone(), + id, + ), + )), + ) + // todo!("do we wanna keep this?") + // .children(edit_paths.iter().map(|path| { + // h_flex() + // .gap_1() + // .child(Icon::new(IconName::File)) + // .child(Label::new(path.clone())) + // })) + .into_any(), + ) + } + + fn render_workflow_step_footer( + &self, + step_range: Range, + max_width: Pixels, + gutter_width: Pixels, + cx: &mut ViewContext, + ) -> Option { + let step = self.workflow_steps.get(&step_range)?; + let current_status = step.status(cx); + let theme = cx.theme().status(); + let border_color = if current_status.is_confirmed() { + theme.ignored_border + } else { + theme.info_border + }; + Some( + v_flex() + .w(max_width) + .pt_1() + .pl(gutter_width) + .child(h_flex().h(px(1.)).bg(border_color)) + .into_any(), + ) + } + + fn render_workflow_step_status( + status: WorkflowStepStatus, + step_range: Range, + focus_handle: FocusHandle, + editor: WeakView, + id: BlockId, + ) -> AnyElement { + let id = EntityId::from(id).as_u64(); + fn display_keybind_in_tooltip( + step_range: &Range, + editor: &WeakView, + cx: &mut WindowContext<'_>, + ) -> bool { + editor + .update(cx, |this, _| { + this.active_workflow_step + .as_ref() + .map(|step| &step.range == step_range) + }) + .ok() + .flatten() + .unwrap_or_default() + } + + match status { + WorkflowStepStatus::Error(error) => { + let error = error.to_string(); + h_flex() + .gap_2() + .child( + div() + .id("step-resolution-failure") + .child( + Label::new("Step Resolution Failed") + .size(LabelSize::Small) + .color(Color::Error), + ) + .tooltip(move |cx| Tooltip::text(error.clone(), cx)), + ) + .child( + Button::new(("transform", id), "Retry") + .icon(IconName::Update) + .icon_position(IconPosition::Start) + .icon_size(IconSize::Small) + .label_size(LabelSize::Small) + .on_click({ + let editor = editor.clone(); + let step_range = step_range.clone(); + move |_, cx| { + editor + .update(cx, |this, cx| { + this.resolve_workflow_step(step_range.clone(), cx) + }) + .ok(); + } + }), + ) + .into_any() + } + WorkflowStepStatus::Idle | WorkflowStepStatus::Resolving { .. } => { + Button::new(("transform", id), "Transform") + .icon(IconName::SparkleAlt) + .icon_position(IconPosition::Start) + .icon_size(IconSize::Small) + .label_size(LabelSize::Small) + .style(ButtonStyle::Tinted(TintColor::Accent)) + .tooltip({ + let step_range = step_range.clone(); + let editor = editor.clone(); + move |cx| { + cx.new_view(|cx| { + let tooltip = Tooltip::new("Transform"); + if display_keybind_in_tooltip(&step_range, &editor, cx) { + tooltip.key_binding(KeyBinding::for_action_in( + &Assist, + &focus_handle, + cx, + )) + } else { + tooltip + } + }) + .into() + } + }) + .on_click({ + let editor = editor.clone(); + let step_range = step_range.clone(); + let is_idle = matches!(status, WorkflowStepStatus::Idle); + move |_, cx| { + if is_idle { + editor + .update(cx, |this, cx| { + this.apply_workflow_step(step_range.clone(), cx) + }) + .ok(); + } + } + }) + .map(|this| { + if let WorkflowStepStatus::Resolving = &status { + this.with_animation( + ("resolving-suggestion-animation", id), + Animation::new(Duration::from_secs(2)) + .repeat() + .with_easing(pulsating_between(0.4, 0.8)), + |label, delta| label.alpha(delta), + ) + .into_any_element() + } else { + this.into_any_element() + } + }) + } + WorkflowStepStatus::Pending => h_flex() + .items_center() + .gap_2() + .child( + Label::new("Applying...") + .size(LabelSize::Small) + .with_animation( + ("applying-step-transformation-label", id), + Animation::new(Duration::from_secs(2)) + .repeat() + .with_easing(pulsating_between(0.4, 0.8)), + |label, delta| label.alpha(delta), + ), + ) + .child( + IconButton::new(("stop-transformation", id), IconName::Stop) + .icon_size(IconSize::Small) + .icon_color(Color::Error) + .style(ButtonStyle::Subtle) + .tooltip({ + let step_range = step_range.clone(); + let editor = editor.clone(); + move |cx| { + cx.new_view(|cx| { + let tooltip = Tooltip::new("Stop Transformation"); + if display_keybind_in_tooltip(&step_range, &editor, cx) { + tooltip.key_binding(KeyBinding::for_action_in( + &editor::actions::Cancel, + &focus_handle, + cx, + )) + } else { + tooltip + } + }) + .into() + } + }) + .on_click({ + let editor = editor.clone(); + let step_range = step_range.clone(); + move |_, cx| { + editor + .update(cx, |this, cx| { + this.stop_workflow_step(step_range.clone(), cx) + }) + .ok(); + } + }), + ) + .into_any_element(), + WorkflowStepStatus::Done => h_flex() + .gap_1() + .child( + IconButton::new(("stop-transformation", id), IconName::Close) + .icon_size(IconSize::Small) + .style(ButtonStyle::Tinted(TintColor::Negative)) + .tooltip({ + let focus_handle = focus_handle.clone(); + let editor = editor.clone(); + let step_range = step_range.clone(); + move |cx| { + cx.new_view(|cx| { + let tooltip = Tooltip::new("Reject Transformation"); + if display_keybind_in_tooltip(&step_range, &editor, cx) { + tooltip.key_binding(KeyBinding::for_action_in( + &editor::actions::Cancel, + &focus_handle, + cx, + )) + } else { + tooltip + } + }) + .into() + } + }) + .on_click({ + let editor = editor.clone(); + let step_range = step_range.clone(); + move |_, cx| { + editor + .update(cx, |this, cx| { + this.reject_workflow_step(step_range.clone(), cx); + }) + .ok(); + } + }), + ) + .child( + Button::new(("confirm-workflow-step", id), "Accept") + .icon(IconName::Check) + .icon_position(IconPosition::Start) + .icon_size(IconSize::Small) + .label_size(LabelSize::Small) + .style(ButtonStyle::Tinted(TintColor::Positive)) + .tooltip({ + let editor = editor.clone(); + let step_range = step_range.clone(); + move |cx| { + cx.new_view(|cx| { + let tooltip = Tooltip::new("Accept Transformation"); + if display_keybind_in_tooltip(&step_range, &editor, cx) { + tooltip.key_binding(KeyBinding::for_action_in( + &Assist, + &focus_handle, + cx, + )) + } else { + tooltip + } + }) + .into() + } + }) + .on_click({ + let editor = editor.clone(); + let step_range = step_range.clone(); + move |_, cx| { + editor + .update(cx, |this, cx| { + this.confirm_workflow_step(step_range.clone(), cx); + }) + .ok(); + } + }), + ) + .into_any_element(), + WorkflowStepStatus::Confirmed => h_flex() + .child( + Button::new(("revert-workflow-step", id), "Undo") + .style(ButtonStyle::Filled) + .icon(Some(IconName::Undo)) + .icon_position(IconPosition::Start) + .icon_size(IconSize::Small) + .label_size(LabelSize::Small) + .on_click({ + let editor = editor.clone(); + let step_range = step_range.clone(); + move |_, cx| { + editor + .update(cx, |this, cx| { + this.undo_workflow_step(step_range.clone(), cx); + }) + .ok(); + } + }), + ) + .into_any_element(), + } + } + fn render_notice(&self, cx: &mut ViewContext) -> Option { use feature_flags::FeatureFlagAppExt; let nudge = self.assistant_panel.upgrade().map(|assistant_panel| { @@ -3670,14 +3635,10 @@ impl ContextEditor { fn render_send_button(&self, cx: &mut ViewContext) -> impl IntoElement { let focus_handle = self.focus_handle(cx).clone(); - let mut should_pulsate = false; let button_text = match self.active_workflow_step() { - Some(step) => match step.status(cx) { - WorkflowStepStatus::Empty | WorkflowStepStatus::Error(_) => "Retry Step Resolution", - WorkflowStepStatus::Resolving { auto_apply } => { - should_pulsate = auto_apply; - "Transform" - } + Some((_, step)) => match step.status(cx) { + WorkflowStepStatus::Error(_) => "Retry Step Resolution", + WorkflowStepStatus::Resolving => "Transform", WorkflowStepStatus::Idle => "Transform", WorkflowStepStatus::Pending => "Applying...", WorkflowStepStatus::Done => "Accept", @@ -3724,20 +3685,7 @@ impl ContextEditor { button.tooltip(move |_| tooltip.clone()) }) .layer(ElevationIndex::ModalSurface) - .child(Label::new(button_text).map(|this| { - if should_pulsate { - this.with_animation( - "resolving-suggestion-send-button-animation", - Animation::new(Duration::from_secs(2)) - .repeat() - .with_easing(pulsating_between(0.4, 0.8)), - |label, delta| label.alpha(delta), - ) - .into_any_element() - } else { - this.into_any_element() - } - })) + .child(Label::new(button_text)) .children( KeyBinding::for_action_in(&Assist, &focus_handle, cx) .map(|binding| binding.into_any_element()), @@ -3746,16 +3694,33 @@ impl ContextEditor { focus_handle.dispatch_action(&Assist, cx); }) } +} - fn active_workflow_step_for_cursor(&self, cx: &AppContext) -> Option { - let newest_cursor = self.editor.read(cx).selections.newest::(cx).head(); - let context = self.context.read(cx); - let (range, step) = context.workflow_step_containing(newest_cursor, cx)?; - Some(ActiveWorkflowStep { - resolved: step.read(cx).resolution.is_some(), - range, - }) - } +fn render_fold_icon_button( + editor: WeakView, + icon: IconName, + label: SharedString, +) -> Arc, &mut WindowContext) -> AnyElement> { + Arc::new(move |fold_id, fold_range, _cx| { + let editor = editor.clone(); + ButtonLike::new(fold_id) + .style(ButtonStyle::Filled) + .layer(ElevationIndex::ElevatedSurface) + .child(Icon::new(icon)) + .child(Label::new(label.clone()).single_line()) + .on_click(move |_, cx| { + editor + .update(cx, |editor, cx| { + let buffer_start = fold_range + .start + .to_point(&editor.buffer().read(cx).read(cx)); + let buffer_row = MultiBufferRow(buffer_start.row); + editor.unfold_at(&UnfoldAt { buffer_row }, cx); + }) + .ok(); + }) + .into_any_element() + }) } impl EventEmitter for ContextEditor {} @@ -4455,6 +4420,41 @@ impl ContextHistory { } } +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum WorkflowAssistStatus { + Pending, + Confirmed, + Done, + Idle, +} + +impl WorkflowAssist { + pub fn status(&self, cx: &AppContext) -> WorkflowAssistStatus { + let assistant = InlineAssistant::global(cx); + if self + .assist_ids + .iter() + .any(|assist_id| assistant.assist_status(*assist_id, cx).is_pending()) + { + WorkflowAssistStatus::Pending + } else if self + .assist_ids + .iter() + .all(|assist_id| assistant.assist_status(*assist_id, cx).is_confirmed()) + { + WorkflowAssistStatus::Confirmed + } else if self + .assist_ids + .iter() + .all(|assist_id| assistant.assist_status(*assist_id, cx).is_done()) + { + WorkflowAssistStatus::Done + } else { + WorkflowAssistStatus::Idle + } + } +} + impl Render for ContextHistory { fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { div().size_full().child(self.picker.clone()) @@ -4684,6 +4684,22 @@ fn render_slash_command_output_toggle( .into_any_element() } +fn fold_toggle( + name: &'static str, +) -> impl Fn( + MultiBufferRow, + bool, + Arc) + Send + Sync>, + &mut WindowContext<'_>, +) -> AnyElement { + move |row, is_folded, fold, _cx| { + Disclosure::new((name, row.0 as u64), !is_folded) + .selected(is_folded) + .on_click(move |_e, cx| fold(!is_folded, cx)) + .into_any_element() + } +} + fn quote_selection_fold_placeholder(title: String, editor: WeakView) -> FoldPlaceholder { FoldPlaceholder { render: Arc::new({ diff --git a/crates/assistant/src/context.rs b/crates/assistant/src/context.rs index 6a810dd988..b2f0c705d6 100644 --- a/crates/assistant/src/context.rs +++ b/crates/assistant/src/context.rs @@ -2,8 +2,8 @@ mod context_tests; use crate::{ - prompts::PromptBuilder, slash_command::SlashCommandLine, workflow::WorkflowStep, MessageId, - MessageStatus, + prompts::PromptBuilder, slash_command::SlashCommandLine, MessageId, MessageStatus, + WorkflowStep, WorkflowStepEdit, WorkflowStepResolution, WorkflowSuggestionGroup, }; use anyhow::{anyhow, Context as _, Result}; use assistant_slash_command::{ @@ -13,10 +13,14 @@ use client::{self, proto, telemetry::Telemetry}; use clock::ReplicaId; use collections::{HashMap, HashSet}; use fs::{Fs, RemoveOptions}; -use futures::{future::Shared, stream::FuturesUnordered, FutureExt, StreamExt}; +use futures::{ + future::{self, Shared}, + stream::FuturesUnordered, + FutureExt, StreamExt, +}; use gpui::{ - AppContext, Context as _, EventEmitter, Image, Model, ModelContext, RenderImage, SharedString, - Subscription, Task, + AppContext, AsyncAppContext, Context as _, EventEmitter, Image, Model, ModelContext, + RenderImage, SharedString, Subscription, Task, }; use language::{AnchorRangeExt, Bias, Buffer, LanguageRegistry, OffsetRangeExt, Point, ToOffset}; @@ -30,12 +34,13 @@ use project::Project; use serde::{Deserialize, Serialize}; use smallvec::SmallVec; use std::{ - cmp::{max, Ordering}, + cmp::{self, max, Ordering}, collections::hash_map, fmt::Debug, iter, mem, ops::Range, path::{Path, PathBuf}, + str::FromStr as _, sync::Arc, time::{Duration, Instant}, }; @@ -283,9 +288,11 @@ pub enum ContextEvent { ShowAssistError(SharedString), MessagesEdited, SummaryChanged, - WorkflowStepsRemoved(Vec>), - WorkflowStepUpdated(Range), StreamedCompletion, + WorkflowStepsUpdated { + removed: Vec>, + updated: Vec>, + }, PendingSlashCommandsUpdated { removed: Vec>, updated: Vec, @@ -381,8 +388,8 @@ pub struct Message { pub image_offsets: SmallVec<[(usize, MessageImage); 1]>, pub offset_range: Range, pub index_range: Range, + pub anchor_range: Range, pub id: MessageId, - pub anchor: language::Anchor, pub role: Role, pub status: MessageStatus, pub cache: Option, @@ -452,9 +459,23 @@ struct PendingCompletion { #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] pub struct SlashCommandId(clock::Lamport); -struct WorkflowStepEntry { - range: Range, - step: Model, +#[derive(Clone, Debug)] +pub struct XmlTag { + pub kind: XmlTagKind, + pub range: Range, + pub is_open_tag: bool, +} + +#[derive(Copy, Clone, Debug, strum::EnumString, PartialEq, Eq, strum::AsRefStr)] +#[strum(serialize_all = "snake_case")] +pub enum XmlTagKind { + Step, + Edit, + Path, + Symbol, + Within, + Operation, + Description, } pub struct Context { @@ -465,7 +486,7 @@ pub struct Context { operations: Vec, buffer: Model, pending_slash_commands: Vec, - edits_since_last_slash_command_parse: language::Subscription, + edits_since_last_parse: language::Subscription, finished_slash_commands: HashSet, slash_command_output_sections: Vec>, message_anchors: Vec, @@ -484,12 +505,34 @@ pub struct Context { _subscriptions: Vec, telemetry: Option>, language_registry: Arc, - workflow_steps: Vec, - edits_since_last_workflow_step_prune: language::Subscription, + workflow_steps: Vec, + xml_tags: Vec, project: Option>, prompt_builder: Arc, } +trait ContextAnnotation { + fn range(&self) -> &Range; +} + +impl ContextAnnotation for PendingSlashCommand { + fn range(&self) -> &Range { + &self.source_range + } +} + +impl ContextAnnotation for WorkflowStep { + fn range(&self) -> &Range { + &self.range + } +} + +impl ContextAnnotation for XmlTag { + fn range(&self) -> &Range { + &self.range + } +} + impl EventEmitter for Context {} impl Context { @@ -535,8 +578,6 @@ impl Context { }); let edits_since_last_slash_command_parse = buffer.update(cx, |buffer, _| buffer.subscribe()); - let edits_since_last_workflow_step_prune = - buffer.update(cx, |buffer, _| buffer.subscribe()); let mut this = Self { id, timestamp: clock::Lamport::new(replica_id), @@ -550,7 +591,7 @@ impl Context { pending_slash_commands: Vec::new(), finished_slash_commands: HashSet::default(), slash_command_output_sections: Vec::new(), - edits_since_last_slash_command_parse, + edits_since_last_parse: edits_since_last_slash_command_parse, summary: None, pending_summary: Task::ready(None), completion_count: Default::default(), @@ -566,7 +607,7 @@ impl Context { project, language_registry, workflow_steps: Vec::new(), - edits_since_last_workflow_step_prune, + xml_tags: Vec::new(), prompt_builder, }; @@ -753,7 +794,7 @@ impl Context { } fn flush_ops(&mut self, cx: &mut ModelContext) { - let mut messages_changed = false; + let mut changed_messages = HashSet::default(); let mut summary_changed = false; self.pending_ops.sort_unstable_by_key(|op| op.timestamp()); @@ -771,8 +812,8 @@ impl Context { if self.messages_metadata.contains_key(&anchor.id) { // We already applied this operation. } else { + changed_messages.insert(anchor.id); self.insert_message(anchor, metadata, cx); - messages_changed = true; } } ContextOperation::UpdateMessage { @@ -783,7 +824,7 @@ impl Context { let metadata = self.messages_metadata.get_mut(&message_id).unwrap(); if new_metadata.timestamp > metadata.timestamp { *metadata = new_metadata; - messages_changed = true; + changed_messages.insert(message_id); } } ContextOperation::UpdateSummary { @@ -827,7 +868,8 @@ impl Context { self.operations.push(op); } - if messages_changed { + if !changed_messages.is_empty() { + self.message_roles_updated(changed_messages, cx); cx.emit(ContextEvent::MessagesEdited); cx.notify(); } @@ -908,11 +950,11 @@ impl Context { self.summary.as_ref() } - pub fn workflow_step_containing( + pub(crate) fn workflow_step_containing( &self, offset: usize, cx: &AppContext, - ) -> Option<(Range, Model)> { + ) -> Option<&WorkflowStep> { let buffer = self.buffer.read(cx); let index = self .workflow_steps @@ -927,21 +969,24 @@ impl Context { } }) .ok()?; - let step = &self.workflow_steps[index]; - Some((step.range.clone(), step.step.clone())) + Some(&self.workflow_steps[index]) } - pub fn workflow_step_for_range( + pub fn workflow_step_ranges(&self) -> impl Iterator> + '_ { + self.workflow_steps.iter().map(|step| step.range.clone()) + } + + pub(crate) fn workflow_step_for_range( &self, - range: Range, + range: &Range, cx: &AppContext, - ) -> Option> { + ) -> Option<&WorkflowStep> { let buffer = self.buffer.read(cx); - let index = self.workflow_step_index_for_range(&range, buffer).ok()?; - Some(self.workflow_steps[index].step.clone()) + let index = self.workflow_step_index_for_range(range, buffer).ok()?; + Some(&self.workflow_steps[index]) } - pub fn workflow_step_index_for_range( + fn workflow_step_index_for_range( &self, tagged_range: &Range, buffer: &text::BufferSnapshot, @@ -982,10 +1027,9 @@ impl Context { )), language::Event::Edited => { self.count_remaining_tokens(cx); - self.reparse_slash_commands(cx); + self.reparse(cx); // Use `inclusive = true` to invalidate a step when an edit occurs // at the start/end of a parsed step. - self.prune_invalid_workflow_steps(true, cx); cx.emit(ContextEvent::MessagesEdited); } _ => {} @@ -1199,10 +1243,10 @@ impl Context { cx.notify(); } - pub fn reparse_slash_commands(&mut self, cx: &mut ModelContext) { - let buffer = self.buffer.read(cx); + pub fn reparse(&mut self, cx: &mut ModelContext) { + let buffer = self.buffer.read(cx).text_snapshot(); let mut row_ranges = self - .edits_since_last_slash_command_parse + .edits_since_last_parse .consume() .into_iter() .map(|edit| { @@ -1212,8 +1256,10 @@ impl Context { }) .peekable(); - let mut removed = Vec::new(); - let mut updated = Vec::new(); + let mut removed_slash_command_ranges = Vec::new(); + let mut updated_slash_commands = Vec::new(); + let mut removed_steps = Vec::new(); + let mut updated_steps = Vec::new(); while let Some(mut row_range) = row_ranges.next() { while let Some(next_row_range) = row_ranges.peek() { if row_range.end >= next_row_range.start { @@ -1230,217 +1276,457 @@ impl Context { buffer.line_len(row_range.end - 1), )); - let old_range = self.pending_command_indices_for_range(start..end, cx); + self.reparse_slash_commands_in_range( + start..end, + &buffer, + &mut updated_slash_commands, + &mut removed_slash_command_ranges, + cx, + ); + self.reparse_workflow_steps_in_range( + start..end, + &buffer, + &mut updated_steps, + &mut removed_steps, + cx, + ); + } - let mut new_commands = Vec::new(); - let mut lines = buffer.text_for_range(start..end).lines(); - let mut offset = lines.offset(); - while let Some(line) = lines.next() { - if let Some(command_line) = SlashCommandLine::parse(line) { - let name = &line[command_line.name.clone()]; - let arguments = command_line - .arguments - .iter() - .filter_map(|argument_range| { - if argument_range.is_empty() { - None - } else { - line.get(argument_range.clone()) + if !updated_slash_commands.is_empty() || !removed_slash_command_ranges.is_empty() { + cx.emit(ContextEvent::PendingSlashCommandsUpdated { + removed: removed_slash_command_ranges, + updated: updated_slash_commands, + }); + } + + if !updated_steps.is_empty() || !removed_steps.is_empty() { + cx.emit(ContextEvent::WorkflowStepsUpdated { + removed: removed_steps, + updated: updated_steps, + }); + } + } + + fn reparse_slash_commands_in_range( + &mut self, + range: Range, + buffer: &BufferSnapshot, + updated: &mut Vec, + removed: &mut Vec>, + cx: &AppContext, + ) { + let old_range = self.pending_command_indices_for_range(range.clone(), cx); + + let mut new_commands = Vec::new(); + let mut lines = buffer.text_for_range(range).lines(); + let mut offset = lines.offset(); + while let Some(line) = lines.next() { + if let Some(command_line) = SlashCommandLine::parse(line) { + let name = &line[command_line.name.clone()]; + let arguments = command_line + .arguments + .iter() + .filter_map(|argument_range| { + if argument_range.is_empty() { + None + } else { + line.get(argument_range.clone()) + } + }) + .map(ToOwned::to_owned) + .collect::>(); + if let Some(command) = SlashCommandRegistry::global(cx).command(name) { + if !command.requires_argument() || !arguments.is_empty() { + let start_ix = offset + command_line.name.start - 1; + let end_ix = offset + + command_line + .arguments + .last() + .map_or(command_line.name.end, |argument| argument.end); + let source_range = + buffer.anchor_after(start_ix)..buffer.anchor_after(end_ix); + let pending_command = PendingSlashCommand { + name: name.to_string(), + arguments, + source_range, + status: PendingSlashCommandStatus::Idle, + }; + updated.push(pending_command.clone()); + new_commands.push(pending_command); + } + } + } + + offset = lines.offset(); + } + + let removed_commands = self.pending_slash_commands.splice(old_range, new_commands); + removed.extend(removed_commands.map(|command| command.source_range)); + } + + fn reparse_workflow_steps_in_range( + &mut self, + range: Range, + buffer: &BufferSnapshot, + updated: &mut Vec>, + removed: &mut Vec>, + cx: &mut ModelContext, + ) { + // Rebuild the XML tags in the edited range. + let intersecting_tags_range = + self.indices_intersecting_buffer_range(&self.xml_tags, range.clone(), cx); + let new_tags = self.parse_xml_tags_in_range(buffer, range.clone(), cx); + self.xml_tags + .splice(intersecting_tags_range.clone(), new_tags); + + // Find which steps intersect the changed range. + let intersecting_steps_range = + self.indices_intersecting_buffer_range(&self.workflow_steps, range.clone(), cx); + + // Reparse all tags after the last unchanged step before the change. + let mut tags_start_ix = 0; + if let Some(preceding_unchanged_step) = + self.workflow_steps[..intersecting_steps_range.start].last() + { + tags_start_ix = match self.xml_tags.binary_search_by(|tag| { + tag.range + .start + .cmp(&preceding_unchanged_step.range.end, buffer) + .then(Ordering::Less) + }) { + Ok(ix) | Err(ix) => ix, + }; + } + + // Rebuild the edit suggestions in the range. + let mut new_steps = self.parse_steps(tags_start_ix, range.end, buffer); + + if let Some(project) = self.project() { + for step in &mut new_steps { + Self::resolve_workflow_step_internal(step, &project, cx); + } + } + + updated.extend(new_steps.iter().map(|step| step.range.clone())); + let removed_steps = self + .workflow_steps + .splice(intersecting_steps_range, new_steps); + removed.extend( + removed_steps + .map(|step| step.range) + .filter(|range| !updated.contains(&range)), + ); + } + + fn parse_xml_tags_in_range( + &self, + buffer: &BufferSnapshot, + range: Range, + cx: &AppContext, + ) -> Vec { + let mut messages = self.messages(cx).peekable(); + + let mut tags = Vec::new(); + let mut lines = buffer.text_for_range(range).lines(); + let mut offset = lines.offset(); + + while let Some(line) = lines.next() { + while let Some(message) = messages.peek() { + if offset < message.offset_range.end { + break; + } else { + messages.next(); + } + } + + let is_assistant_message = messages + .peek() + .map_or(false, |message| message.role == Role::Assistant); + if is_assistant_message { + for (start_ix, _) in line.match_indices('<') { + let mut name_start_ix = start_ix + 1; + let closing_bracket_ix = line[start_ix..].find('>').map(|i| start_ix + i); + if let Some(closing_bracket_ix) = closing_bracket_ix { + let end_ix = closing_bracket_ix + 1; + let mut is_open_tag = true; + if line[name_start_ix..closing_bracket_ix].starts_with('/') { + name_start_ix += 1; + is_open_tag = false; + } + let tag_inner = &line[name_start_ix..closing_bracket_ix]; + let tag_name_len = tag_inner + .find(|c: char| c.is_whitespace()) + .unwrap_or(tag_inner.len()); + if let Ok(kind) = XmlTagKind::from_str(&tag_inner[..tag_name_len]) { + tags.push(XmlTag { + range: buffer.anchor_after(offset + start_ix) + ..buffer.anchor_before(offset + end_ix), + is_open_tag, + kind, + }); + }; + } + } + } + + offset = lines.offset(); + } + tags + } + + fn parse_steps( + &mut self, + tags_start_ix: usize, + buffer_end: text::Anchor, + buffer: &BufferSnapshot, + ) -> Vec { + let mut new_steps = Vec::new(); + let mut pending_step = None; + let mut edit_step_depth = 0; + let mut tags = self.xml_tags[tags_start_ix..].iter().peekable(); + 'tags: while let Some(tag) = tags.next() { + if tag.range.start.cmp(&buffer_end, buffer).is_gt() && edit_step_depth == 0 { + break; + } + + if tag.kind == XmlTagKind::Step && tag.is_open_tag { + edit_step_depth += 1; + let edit_start = tag.range.start; + let mut edits = Vec::new(); + let mut step = WorkflowStep { + range: edit_start..edit_start, + leading_tags_end: tag.range.end, + trailing_tag_start: None, + edits: Default::default(), + resolution: None, + resolution_task: None, + }; + + while let Some(tag) = tags.next() { + step.trailing_tag_start.get_or_insert(tag.range.start); + + if tag.kind == XmlTagKind::Step && !tag.is_open_tag { + // step.trailing_tag_start = Some(tag.range.start); + edit_step_depth -= 1; + if edit_step_depth == 0 { + step.range.end = tag.range.end; + step.edits = edits.into(); + new_steps.push(step); + continue 'tags; + } + } + + if tag.kind == XmlTagKind::Edit && tag.is_open_tag { + let mut path = None; + let mut symbol = None; + let mut operation = None; + let mut description = None; + + while let Some(tag) = tags.next() { + if tag.kind == XmlTagKind::Edit && !tag.is_open_tag { + edits.push(WorkflowStepEdit::new( + path, + operation, + symbol, + description, + )); + break; + } + + if tag.is_open_tag + && [ + XmlTagKind::Path, + XmlTagKind::Symbol, + XmlTagKind::Operation, + XmlTagKind::Description, + ] + .contains(&tag.kind) + { + let kind = tag.kind; + let content_start = tag.range.end; + if let Some(tag) = tags.peek() { + if tag.kind == kind && !tag.is_open_tag { + let tag = tags.next().unwrap(); + let content_end = tag.range.start; + let mut content = buffer + .text_for_range(content_start..content_end) + .collect::(); + content.truncate(content.trim_end().len()); + match kind { + XmlTagKind::Path => path = Some(content), + XmlTagKind::Operation => operation = Some(content), + XmlTagKind::Symbol => { + symbol = Some(content).filter(|s| !s.is_empty()) + } + XmlTagKind::Description => { + description = + Some(content).filter(|s| !s.is_empty()) + } + _ => {} + } + } + } } - }) - .map(ToOwned::to_owned) - .collect::>(); - if let Some(command) = SlashCommandRegistry::global(cx).command(name) { - if !command.requires_argument() || !arguments.is_empty() { - let start_ix = offset + command_line.name.start - 1; - let end_ix = offset - + command_line - .arguments - .last() - .map_or(command_line.name.end, |argument| argument.end); - let source_range = - buffer.anchor_after(start_ix)..buffer.anchor_after(end_ix); - let pending_command = PendingSlashCommand { - name: name.to_string(), - arguments, - source_range, - status: PendingSlashCommandStatus::Idle, - }; - updated.push(pending_command.clone()); - new_commands.push(pending_command); } } } - offset = lines.offset(); + pending_step = Some(step); } - - let removed_commands = self.pending_slash_commands.splice(old_range, new_commands); - removed.extend(removed_commands.map(|command| command.source_range)); } - if !updated.is_empty() || !removed.is_empty() { - cx.emit(ContextEvent::PendingSlashCommandsUpdated { removed, updated }); - } - } - - fn prune_invalid_workflow_steps(&mut self, inclusive: bool, cx: &mut ModelContext) { - let mut removed = Vec::new(); - - for edit_range in self.edits_since_last_workflow_step_prune.consume() { - let intersecting_range = self.find_intersecting_steps(edit_range.new, inclusive, cx); - removed.extend( - self.workflow_steps - .drain(intersecting_range) - .map(|step| step.range), - ); + if let Some(mut pending_step) = pending_step { + pending_step.range.end = text::Anchor::MAX; + new_steps.push(pending_step); } - if !removed.is_empty() { - cx.emit(ContextEvent::WorkflowStepsRemoved(removed)); - cx.notify(); - } - } - - fn find_intersecting_steps( - &self, - range: Range, - inclusive: bool, - cx: &AppContext, - ) -> Range { - let buffer = self.buffer.read(cx); - let start_ix = match self.workflow_steps.binary_search_by(|probe| { - probe - .range - .end - .to_offset(buffer) - .cmp(&range.start) - .then(if inclusive { - Ordering::Greater - } else { - Ordering::Less - }) - }) { - Ok(ix) | Err(ix) => ix, - }; - let end_ix = match self.workflow_steps.binary_search_by(|probe| { - probe - .range - .start - .to_offset(buffer) - .cmp(&range.end) - .then(if inclusive { - Ordering::Less - } else { - Ordering::Greater - }) - }) { - Ok(ix) | Err(ix) => ix, - }; - start_ix..end_ix - } - - fn parse_workflow_steps_in_range(&mut self, range: Range, cx: &mut ModelContext) { - let weak_self = cx.weak_model(); - let mut new_edit_steps = Vec::new(); - let mut edits = Vec::new(); - - let buffer = self.buffer.read(cx).snapshot(); - let mut message_lines = buffer.as_rope().chunks_in_range(range).lines(); - let mut in_step = false; - let mut step_open_tag_start_ix = 0; - let mut line_start_offset = message_lines.offset(); - - while let Some(line) = message_lines.next() { - if let Some(step_start_index) = line.find("") { - if !in_step { - in_step = true; - step_open_tag_start_ix = line_start_offset + step_start_index; - } - } - - if let Some(step_end_index) = line.find("") { - if in_step { - let mut step_open_tag_end_ix = step_open_tag_start_ix + "".len(); - if buffer.chars_at(step_open_tag_end_ix).next() == Some('\n') { - step_open_tag_end_ix += 1; - } - let mut step_end_tag_start_ix = line_start_offset + step_end_index; - let step_end_tag_end_ix = step_end_tag_start_ix + "".len(); - if buffer.reversed_chars_at(step_end_tag_start_ix).next() == Some('\n') { - step_end_tag_start_ix -= 1; - } - edits.push((step_open_tag_start_ix..step_open_tag_end_ix, "")); - edits.push((step_end_tag_start_ix..step_end_tag_end_ix, "")); - let tagged_range = buffer.anchor_after(step_open_tag_end_ix) - ..buffer.anchor_before(step_end_tag_start_ix); - - // Check if a step with the same range already exists - let existing_step_index = - self.workflow_step_index_for_range(&tagged_range, &buffer); - - if let Err(ix) = existing_step_index { - new_edit_steps.push(( - ix, - WorkflowStepEntry { - step: cx.new_model(|_| { - WorkflowStep::new(tagged_range.clone(), weak_self.clone()) - }), - range: tagged_range, - }, - )); - } - - in_step = false; - } - } - - line_start_offset = message_lines.offset(); - } - - let mut updated = Vec::new(); - for (index, step) in new_edit_steps.into_iter().rev() { - let step_range = step.range.clone(); - updated.push(step_range.clone()); - self.workflow_steps.insert(index, step); - self.resolve_workflow_step(step_range, cx); - } - - // Delete tags, making sure we don't accidentally invalidate - // the step we just parsed. - self.buffer - .update(cx, |buffer, cx| buffer.edit(edits, None, cx)); - self.edits_since_last_workflow_step_prune.consume(); + new_steps } pub fn resolve_workflow_step( &mut self, - tagged_range: Range, + tagged_range: Range, cx: &mut ModelContext, - ) { - let Ok(step_index) = self - .workflow_steps - .binary_search_by(|step| step.range.cmp(&tagged_range, self.buffer.read(cx))) - else { - return; - }; - - cx.emit(ContextEvent::WorkflowStepUpdated(tagged_range.clone())); - cx.notify(); - - let resolution = self.workflow_steps[step_index].step.clone(); - cx.defer(move |cx| { - resolution.update(cx, |resolution, cx| resolution.resolve(cx)); - }); + ) -> Option<()> { + let index = self + .workflow_step_index_for_range(&tagged_range, self.buffer.read(cx)) + .ok()?; + let step = &mut self.workflow_steps[index]; + let project = self.project.as_ref()?; + step.resolution.take(); + Self::resolve_workflow_step_internal(step, project, cx); + None } - pub fn workflow_step_updated( - &mut self, - range: Range, - cx: &mut ModelContext, + fn resolve_workflow_step_internal( + step: &mut WorkflowStep, + project: &Model, + cx: &mut ModelContext<'_, Context>, ) { - cx.emit(ContextEvent::WorkflowStepUpdated(range)); - cx.notify(); + step.resolution_task = Some(cx.spawn({ + let range = step.range.clone(); + let edits = step.edits.clone(); + let project = project.clone(); + |this, mut cx| async move { + let suggestion_groups = + Self::compute_step_resolution(project, edits, &mut cx).await; + + this.update(&mut cx, |this, cx| { + let buffer = this.buffer.read(cx).text_snapshot(); + let ix = this.workflow_step_index_for_range(&range, &buffer).ok(); + if let Some(ix) = ix { + let step = &mut this.workflow_steps[ix]; + + let resolution = suggestion_groups.map(|suggestion_groups| { + let mut title = String::new(); + for mut chunk in buffer.text_for_range( + step.leading_tags_end + ..step.trailing_tag_start.unwrap_or(step.range.end), + ) { + if title.is_empty() { + chunk = chunk.trim_start(); + } + if let Some((prefix, _)) = chunk.split_once('\n') { + title.push_str(prefix); + break; + } else { + title.push_str(chunk); + } + } + + WorkflowStepResolution { + title, + suggestion_groups, + } + }); + + step.resolution = Some(Arc::new(resolution)); + cx.emit(ContextEvent::WorkflowStepsUpdated { + removed: vec![], + updated: vec![range], + }) + } + }) + .ok(); + } + })); + } + + async fn compute_step_resolution( + project: Model, + edits: Arc<[Result]>, + cx: &mut AsyncAppContext, + ) -> Result, Vec>> { + let mut suggestion_tasks = Vec::new(); + for edit in edits.iter() { + let edit = edit.as_ref().map_err(|e| anyhow!("{e}"))?; + suggestion_tasks.push(edit.resolve(project.clone(), cx.clone())); + } + + // Expand the context ranges of each suggestion and group suggestions with overlapping context ranges. + let suggestions = future::try_join_all(suggestion_tasks).await?; + + let mut suggestions_by_buffer = HashMap::default(); + for (buffer, suggestion) in suggestions { + suggestions_by_buffer + .entry(buffer) + .or_insert_with(Vec::new) + .push(suggestion); + } + + let mut suggestion_groups_by_buffer = HashMap::default(); + for (buffer, mut suggestions) in suggestions_by_buffer { + let mut suggestion_groups = Vec::::new(); + let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot())?; + // Sort suggestions by their range so that earlier, larger ranges come first + suggestions.sort_by(|a, b| a.range().cmp(&b.range(), &snapshot)); + + // Merge overlapping suggestions + suggestions.dedup_by(|a, b| b.try_merge(a, &snapshot)); + + // Create context ranges for each suggestion + for suggestion in suggestions { + let context_range = { + let suggestion_point_range = suggestion.range().to_point(&snapshot); + let start_row = suggestion_point_range.start.row.saturating_sub(5); + let end_row = + cmp::min(suggestion_point_range.end.row + 5, snapshot.max_point().row); + let start = snapshot.anchor_before(Point::new(start_row, 0)); + let end = + snapshot.anchor_after(Point::new(end_row, snapshot.line_len(end_row))); + start..end + }; + + if let Some(last_group) = suggestion_groups.last_mut() { + if last_group + .context_range + .end + .cmp(&context_range.start, &snapshot) + .is_ge() + { + // Merge with the previous group if context ranges overlap + last_group.context_range.end = context_range.end; + last_group.suggestions.push(suggestion); + } else { + // Create a new group + suggestion_groups.push(WorkflowSuggestionGroup { + context_range, + suggestions: vec![suggestion], + }); + } + } else { + // Create the first group + suggestion_groups.push(WorkflowSuggestionGroup { + context_range, + suggestions: vec![suggestion], + }); + } + } + + suggestion_groups_by_buffer.insert(buffer, suggestion_groups); + } + + Ok(suggestion_groups_by_buffer) } pub fn pending_command_for_position( @@ -1480,17 +1766,24 @@ impl Context { &self, range: Range, cx: &AppContext, + ) -> Range { + self.indices_intersecting_buffer_range(&self.pending_slash_commands, range, cx) + } + + fn indices_intersecting_buffer_range( + &self, + all_annotations: &[T], + range: Range, + cx: &AppContext, ) -> Range { let buffer = self.buffer.read(cx); - let start_ix = match self - .pending_slash_commands - .binary_search_by(|probe| probe.source_range.end.cmp(&range.start, &buffer)) + let start_ix = match all_annotations + .binary_search_by(|probe| probe.range().end.cmp(&range.start, &buffer)) { Ok(ix) | Err(ix) => ix, }; - let end_ix = match self - .pending_slash_commands - .binary_search_by(|probe| probe.source_range.start.cmp(&range.end, &buffer)) + let end_ix = match all_annotations + .binary_search_by(|probe| probe.range().start.cmp(&range.end, &buffer)) { Ok(ix) => ix + 1, Err(ix) => ix, @@ -1506,7 +1799,7 @@ impl Context { expand_result: bool, cx: &mut ModelContext, ) { - self.reparse_slash_commands(cx); + self.reparse(cx); let insert_output_task = cx.spawn(|this, mut cx| { let command_range = command_range.clone(); @@ -1670,27 +1963,21 @@ impl Context { .message_anchors .iter() .position(|message| message.id == assistant_message_id)?; - let message_range = this.buffer.update(cx, |buffer, cx| { - let message_start_offset = - this.message_anchors[message_ix].start.to_offset(buffer); + this.buffer.update(cx, |buffer, cx| { let message_old_end_offset = this.message_anchors[message_ix + 1..] .iter() .find(|message| message.start.is_valid(buffer)) .map_or(buffer.len(), |message| { message.start.to_offset(buffer).saturating_sub(1) }); - let message_new_end_offset = message_old_end_offset + chunk.len(); buffer.edit( [(message_old_end_offset..message_old_end_offset, chunk)], None, cx, ); - message_start_offset..message_new_end_offset }); // Use `inclusive = false` as edits might occur at the end of a parsed step. - this.prune_invalid_workflow_steps(false, cx); - this.parse_workflow_steps_in_range(message_range, cx); cx.emit(ContextEvent::StreamedCompletion); Some(()) @@ -1781,12 +2068,34 @@ impl Context { } pub fn cycle_message_roles(&mut self, ids: HashSet, cx: &mut ModelContext) { - for id in ids { - if let Some(metadata) = self.messages_metadata.get(&id) { + for id in &ids { + if let Some(metadata) = self.messages_metadata.get(id) { let role = metadata.role.cycle(); - self.update_metadata(id, cx, |metadata| metadata.role = role); + self.update_metadata(*id, cx, |metadata| metadata.role = role); } } + + self.message_roles_updated(ids, cx); + } + + fn message_roles_updated(&mut self, ids: HashSet, cx: &mut ModelContext) { + let mut ranges = Vec::new(); + for message in self.messages(cx) { + if ids.contains(&message.id) { + ranges.push(message.anchor_range.clone()); + } + } + + let buffer = self.buffer.read(cx).text_snapshot(); + let mut updated = Vec::new(); + let mut removed = Vec::new(); + for range in ranges { + self.reparse_workflow_steps_in_range(range, &buffer, &mut updated, &mut removed, cx); + } + + if !updated.is_empty() || !removed.is_empty() { + cx.emit(ContextEvent::WorkflowStepsUpdated { removed, updated }) + } } pub fn update_metadata( @@ -2249,8 +2558,8 @@ impl Context { return Some(Message { index_range: start_ix..end_ix, offset_range: message_start..message_end, + anchor_range: message_anchor.start..message_end_anchor, id: message_anchor.id, - anchor: message_anchor.start, role: metadata.role, status: metadata.status.clone(), cache: metadata.cache.clone(), diff --git a/crates/assistant/src/context/context_tests.rs b/crates/assistant/src/context/context_tests.rs index 35764822d7..c80e105a44 100644 --- a/crates/assistant/src/context/context_tests.rs +++ b/crates/assistant/src/context/context_tests.rs @@ -1,6 +1,8 @@ +use super::{MessageCacheMetadata, WorkflowStepEdit}; use crate::{ - assistant_panel, prompt_library, slash_command::file_command, workflow::tool, CacheStatus, - Context, ContextEvent, ContextId, ContextOperation, MessageId, MessageStatus, PromptBuilder, + assistant_panel, prompt_library, slash_command::file_command, CacheStatus, Context, + ContextEvent, ContextId, ContextOperation, MessageId, MessageStatus, PromptBuilder, + WorkflowStepEditKind, }; use anyhow::Result; use assistant_slash_command::{ @@ -8,15 +10,13 @@ use assistant_slash_command::{ SlashCommandRegistry, }; use collections::HashSet; -use fs::{FakeFs, Fs as _}; +use fs::FakeFs; use gpui::{AppContext, Model, SharedString, Task, TestAppContext, WeakView}; -use indoc::indoc; use language::{Buffer, LanguageRegistry, LspAdapterDelegate}; use language_model::{LanguageModelCacheConfiguration, LanguageModelRegistry, Role}; use parking_lot::Mutex; use project::Project; use rand::prelude::*; -use rope::Point; use serde_json::json; use settings::SettingsStore; use std::{ @@ -27,14 +27,15 @@ use std::{ rc::Rc, sync::{atomic::AtomicBool, Arc}, }; -use text::{network::Network, OffsetRangeExt as _, ReplicaId, ToPoint as _}; +use text::{network::Network, OffsetRangeExt as _, ReplicaId}; use ui::{Context as _, WindowContext}; use unindent::Unindent; -use util::{test::marked_text_ranges, RandomCharIter}; +use util::{ + test::{generate_marked_text, marked_text_ranges}, + RandomCharIter, +}; use workspace::Workspace; -use super::MessageCacheMetadata; - #[gpui::test] fn test_inserting_and_removing_messages(cx: &mut AppContext) { let settings_store = SettingsStore::test(cx); @@ -479,28 +480,12 @@ async fn test_workflow_step_parsing(cx: &mut TestAppContext) { cx.update(prompt_library::init); let settings_store = cx.update(SettingsStore::test); cx.set_global(settings_store); + cx.update(language::init); cx.update(Project::init_settings); let fs = FakeFs::new(cx.executor()); - fs.as_fake() - .insert_tree( - "/root", - json!({ - "hello.rs": r#" - fn hello() { - println!("Hello, World!"); - } - "#.unindent() - }), - ) - .await; let project = Project::test(fs, [Path::new("/root")], cx).await; cx.update(LanguageModelRegistry::test); - let model = cx.read(|cx| { - LanguageModelRegistry::read_global(cx) - .active_model() - .unwrap() - }); cx.update(assistant_panel::init); let registry = Arc::new(LanguageRegistry::test(cx.executor())); @@ -515,151 +500,382 @@ async fn test_workflow_step_parsing(cx: &mut TestAppContext) { cx, ) }); - let buffer = context.read_with(cx, |context, _| context.buffer.clone()); - // Simulate user input - let user_message = indoc! {r#" - Please add unnecessary complexity to this code: - - ```hello.rs - fn main() { - println!("Hello, World!"); - } - ``` - "#}; - buffer.update(cx, |buffer, cx| { - buffer.edit([(0..0, user_message)], None, cx); + // Insert an assistant message to simulate a response. + let assistant_message_id = context.update(cx, |context, cx| { + let user_message_id = context.messages(cx).next().unwrap().id; + context + .insert_message_after(user_message_id, Role::Assistant, MessageStatus::Done, cx) + .unwrap() + .id }); - // Simulate LLM response with edit steps - let llm_response = indoc! {r#" - Sure, I can help you with that. Here's a step-by-step process: + // No edit tags + edit( + &context, + " - - First, let's extract the greeting into a separate function: + «one + two + »", + cx, + ); + expect_steps( + &context, + " + + one + two + ", + &[], + cx, + ); + + // Partial edit step tag is added + edit( + &context, + " + + one + two + « + + Add a second function ```rust - fn greet() { - println!("Hello, World!"); - } - - fn main() { - greet(); - } + fn two() {} ``` - - - Now, let's make the greeting customizable: + »", + cx, + ); + expect_steps( + &context, + " + + one + two + + « + Add a second function ```rust - fn greet(name: &str) { - println!("Hello, {}!", name); - } - - fn main() { - greet("World"); - } + fn two() {} ``` + + »", + &[&[]], + cx, + ); + + // The full suggestion is added + edit( + &context, + " + + one + two + + + Add a second function + + ```rust + fn two() {} + ``` + + « + src/lib.rs + insert_sibling_after + fn one + add a `two` function + - These changes make the code more modular and flexible. - "#}; + also,»", + cx, + ); + expect_steps( + &context, + " - // Simulate the assist method to trigger the LLM response - context.update(cx, |context, cx| context.assist(cx)); - cx.run_until_parked(); + one + two - // Retrieve the assistant response message's start from the context - let response_start_row = context.read_with(cx, |context, cx| { - let buffer = context.buffer.read(cx); - context.message_anchors[1].start.to_point(buffer).row + « + Add a second function + + ```rust + fn two() {} + ``` + + + src/lib.rs + insert_sibling_after + fn one + add a `two` function + + » + + also,", + &[&[WorkflowStepEdit { + path: "src/lib.rs".into(), + kind: WorkflowStepEditKind::InsertSiblingAfter { + symbol: "fn one".into(), + description: "add a `two` function".into(), + }, + }]], + cx, + ); + + // The step is manually edited. + edit( + &context, + " + + one + two + + + Add a second function + + ```rust + fn two() {} + ``` + + + src/lib.rs + insert_sibling_after + «fn zero» + add a `two` function + + + + also,", + cx, + ); + expect_steps( + &context, + " + + one + two + + « + Add a second function + + ```rust + fn two() {} + ``` + + + src/lib.rs + insert_sibling_after + fn zero + add a `two` function + + » + + also,", + &[&[WorkflowStepEdit { + path: "src/lib.rs".into(), + kind: WorkflowStepEditKind::InsertSiblingAfter { + symbol: "fn zero".into(), + description: "add a `two` function".into(), + }, + }]], + cx, + ); + + // When setting the message role to User, the steps are cleared. + context.update(cx, |context, cx| { + context.cycle_message_roles(HashSet::from_iter([assistant_message_id]), cx); + context.cycle_message_roles(HashSet::from_iter([assistant_message_id]), cx); }); + expect_steps( + &context, + " - // Simulate the LLM completion - model - .as_fake() - .stream_last_completion_response(llm_response.to_string()); - model.as_fake().end_last_completion_stream(); + one + two - // Wait for the completion to be processed - cx.run_until_parked(); + + Add a second function - // Verify that the edit steps were parsed correctly - context.read_with(cx, |context, cx| { - assert_eq!( - workflow_steps(context, cx), - vec![ - ( - Point::new(response_start_row + 2, 0)..Point::new(response_start_row + 12, 3), - WorkflowStepTestStatus::Pending - ), - ( - Point::new(response_start_row + 14, 0)..Point::new(response_start_row + 24, 3), - WorkflowStepTestStatus::Pending - ), - ] - ); + ```rust + fn two() {} + ``` + + + src/lib.rs + insert_sibling_after + fn zero + add a `two` function + + + + also,", + &[], + cx, + ); + + // When setting the message role back to Assistant, the steps are reparsed. + context.update(cx, |context, cx| { + context.cycle_message_roles(HashSet::from_iter([assistant_message_id]), cx); }); + expect_steps( + &context, + " - model - .as_fake() - .respond_to_last_tool_use(tool::WorkflowStepResolutionTool { - step_title: "Title".into(), - suggestions: vec![tool::WorkflowSuggestionTool { - path: "/root/hello.rs".into(), - // Simulate a symbol name that's slightly different than our outline query - kind: tool::WorkflowSuggestionToolKind::Update { - symbol: "fn main()".into(), - description: "Extract a greeting function".into(), - }, - }], + one + two + + « + Add a second function + + ```rust + fn two() {} + ``` + + + src/lib.rs + insert_sibling_after + fn zero + add a `two` function + + » + + also,", + &[&[WorkflowStepEdit { + path: "src/lib.rs".into(), + kind: WorkflowStepEditKind::InsertSiblingAfter { + symbol: "fn zero".into(), + description: "add a `two` function".into(), + }, + }]], + cx, + ); + + // Ensure steps are re-parsed when deserializing. + let serialized_context = context.read_with(cx, |context, cx| context.serialize(cx)); + let deserialized_context = cx.new_model(|cx| { + Context::deserialize( + serialized_context, + Default::default(), + registry.clone(), + prompt_builder.clone(), + None, + None, + cx, + ) + }); + expect_steps( + &deserialized_context, + " + + one + two + + « + Add a second function + + ```rust + fn two() {} + ``` + + + src/lib.rs + insert_sibling_after + fn zero + add a `two` function + + » + + also,", + &[&[WorkflowStepEdit { + path: "src/lib.rs".into(), + kind: WorkflowStepEditKind::InsertSiblingAfter { + symbol: "fn zero".into(), + description: "add a `two` function".into(), + }, + }]], + cx, + ); + + fn edit(context: &Model, new_text_marked_with_edits: &str, cx: &mut TestAppContext) { + context.update(cx, |context, cx| { + context.buffer.update(cx, |buffer, cx| { + buffer.edit_via_marked_text(&new_text_marked_with_edits.unindent(), None, cx); + }); }); - - // Wait for tool use to be processed. - cx.run_until_parked(); - - // Verify that the first edit step is not pending anymore. - context.read_with(cx, |context, cx| { - assert_eq!( - workflow_steps(context, cx), - vec![ - ( - Point::new(response_start_row + 2, 0)..Point::new(response_start_row + 12, 3), - WorkflowStepTestStatus::Resolved - ), - ( - Point::new(response_start_row + 14, 0)..Point::new(response_start_row + 24, 3), - WorkflowStepTestStatus::Pending - ), - ] - ); - }); - - #[derive(Copy, Clone, Debug, Eq, PartialEq)] - enum WorkflowStepTestStatus { - Pending, - Resolved, - Error, + cx.executor().run_until_parked(); } - fn workflow_steps( - context: &Context, - cx: &AppContext, - ) -> Vec<(Range, WorkflowStepTestStatus)> { - context - .workflow_steps - .iter() - .map(|step| { - let buffer = context.buffer.read(cx); - let status = match &step.step.read(cx).resolution { - None => WorkflowStepTestStatus::Pending, - Some(Ok(_)) => WorkflowStepTestStatus::Resolved, - Some(Err(_)) => WorkflowStepTestStatus::Error, - }; - (step.range.to_point(buffer), status) - }) - .collect() + fn expect_steps( + context: &Model, + expected_marked_text: &str, + expected_suggestions: &[&[WorkflowStepEdit]], + cx: &mut TestAppContext, + ) { + context.update(cx, |context, cx| { + let expected_marked_text = expected_marked_text.unindent(); + let (expected_text, expected_ranges) = marked_text_ranges(&expected_marked_text, false); + context.buffer.read_with(cx, |buffer, _| { + assert_eq!(buffer.text(), expected_text); + let ranges = context + .workflow_steps + .iter() + .map(|entry| entry.range.to_offset(buffer)) + .collect::>(); + let marked = generate_marked_text(&expected_text, &ranges, false); + assert_eq!( + marked, + expected_marked_text, + "unexpected suggestion ranges. actual: {ranges:?}, expected: {expected_ranges:?}" + ); + let suggestions = context + .workflow_steps + .iter() + .map(|step| { + step.edits + .iter() + .map(|edit| { + let edit = edit.as_ref().unwrap(); + WorkflowStepEdit { + path: edit.path.clone(), + kind: edit.kind.clone(), + } + }) + .collect::>() + }) + .collect::>(); + + assert_eq!(suggestions, expected_suggestions); + }); + }); } } diff --git a/crates/assistant/src/inline_assistant.rs b/crates/assistant/src/inline_assistant.rs index fc7f06f578..deea00f78d 100644 --- a/crates/assistant/src/inline_assistant.rs +++ b/crates/assistant/src/inline_assistant.rs @@ -1200,9 +1200,11 @@ impl InlineAssistStatus { pub(crate) fn is_pending(&self) -> bool { matches!(self, Self::Pending) } + pub(crate) fn is_confirmed(&self) -> bool { matches!(self, Self::Confirmed) } + pub(crate) fn is_done(&self) -> bool { matches!(self, Self::Done) } diff --git a/crates/assistant/src/prompts.rs b/crates/assistant/src/prompts.rs index ef49c5ced6..87ccb92971 100644 --- a/crates/assistant/src/prompts.rs +++ b/crates/assistant/src/prompts.rs @@ -297,11 +297,4 @@ impl PromptBuilder { pub fn generate_workflow_prompt(&self) -> Result { self.handlebars.lock().render("edit_workflow", &()) } - - pub fn generate_step_resolution_prompt( - &self, - context: &StepResolutionContext, - ) -> Result { - self.handlebars.lock().render("step_resolution", context) - } } diff --git a/crates/assistant/src/workflow.rs b/crates/assistant/src/workflow.rs index cb7e2402dc..a99eff366c 100644 --- a/crates/assistant/src/workflow.rs +++ b/crates/assistant/src/workflow.rs @@ -1,40 +1,37 @@ -mod step_view; - -use crate::{ - prompts::StepResolutionContext, AssistantPanel, Context, InlineAssistId, InlineAssistant, -}; -use anyhow::{anyhow, Error, Result}; +use crate::{AssistantPanel, InlineAssistId, InlineAssistant}; +use anyhow::{anyhow, Context as _, Result}; use collections::HashMap; use editor::Editor; -use futures::future; -use gpui::{ - Model, ModelContext, Task, UpdateGlobal as _, View, WeakModel, WeakView, WindowContext, -}; -use language::{Anchor, Buffer, BufferSnapshot, SymbolPath}; -use language_model::{LanguageModelRegistry, LanguageModelRequestMessage, Role}; -use project::Project; +use gpui::AsyncAppContext; +use gpui::{Model, Task, UpdateGlobal as _, View, WeakView, WindowContext}; +use language::{Anchor, Buffer, BufferSnapshot, Outline, OutlineItem, ParseStatus, SymbolPath}; +use project::{Project, ProjectPath}; use rope::Point; +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use smol::stream::StreamExt; -use std::{cmp, fmt::Write, ops::Range, sync::Arc}; -use text::{AnchorRangeExt as _, OffsetRangeExt as _}; -use util::ResultExt as _; +use std::{ops::Range, path::Path, sync::Arc}; use workspace::Workspace; -pub use step_view::WorkflowStepView; - const IMPORTS_SYMBOL: &str = "#imports"; -pub struct WorkflowStep { - context: WeakModel, - context_buffer_range: Range, - tool_output: String, - resolve_task: Option>, - pub resolution: Option>>, +#[derive(Debug)] +pub(crate) struct WorkflowStep { + pub range: Range, + pub leading_tags_end: text::Anchor, + pub trailing_tag_start: Option, + pub edits: Arc<[Result]>, + pub resolution_task: Option>, + pub resolution: Option>>, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct WorkflowStepEdit { + pub path: String, + pub kind: WorkflowStepEditKind, } #[derive(Clone, Debug, Eq, PartialEq)] -pub struct WorkflowStepResolution { +pub(crate) struct WorkflowStepResolution { pub title: String, pub suggestion_groups: HashMap, Vec>, } @@ -81,194 +78,6 @@ pub enum WorkflowSuggestion { }, } -impl WorkflowStep { - pub fn new(range: Range, context: WeakModel) -> Self { - Self { - context_buffer_range: range, - tool_output: String::new(), - context, - resolution: None, - resolve_task: None, - } - } - - pub fn resolve(&mut self, cx: &mut ModelContext) -> Option<()> { - let range = self.context_buffer_range.clone(); - let context = self.context.upgrade()?; - let context = context.read(cx); - let project = context.project()?; - let prompt_builder = context.prompt_builder(); - let mut request = context.to_completion_request(cx); - let model = LanguageModelRegistry::read_global(cx).active_model(); - let context_buffer = context.buffer(); - let step_text = context_buffer - .read(cx) - .text_for_range(range.clone()) - .collect::(); - - let mut workflow_context = String::new(); - for message in context.messages(cx) { - write!(&mut workflow_context, "", message.role).unwrap(); - for chunk in context_buffer.read(cx).text_for_range(message.offset_range) { - write!(&mut workflow_context, "{chunk}").unwrap(); - } - write!(&mut workflow_context, "").unwrap(); - } - - self.resolve_task = Some(cx.spawn(|this, mut cx| async move { - let result = async { - let Some(model) = model else { - return Err(anyhow!("no model selected")); - }; - - this.update(&mut cx, |this, cx| { - this.tool_output.clear(); - this.resolution = None; - this.result_updated(cx); - cx.notify(); - })?; - - let resolution_context = StepResolutionContext { - workflow_context, - step_to_resolve: step_text.clone(), - }; - let mut prompt = - prompt_builder.generate_step_resolution_prompt(&resolution_context)?; - prompt.push_str(&step_text); - request.messages.push(LanguageModelRequestMessage { - role: Role::User, - content: vec![prompt.into()], - cache: false, - }); - - // Invoke the model to get its edit suggestions for this workflow step. - let mut stream = model - .use_tool_stream::(request, &cx) - .await?; - while let Some(chunk) = stream.next().await { - let chunk = chunk?; - this.update(&mut cx, |this, cx| { - this.tool_output.push_str(&chunk); - cx.notify(); - })?; - } - - let resolution = this.update(&mut cx, |this, _| { - serde_json::from_str::(&this.tool_output) - })??; - - this.update(&mut cx, |this, cx| { - this.tool_output = serde_json::to_string_pretty(&resolution).unwrap(); - cx.notify(); - })?; - - // Translate the parsed suggestions to our internal types, which anchor the suggestions to locations in the code. - let suggestion_tasks: Vec<_> = resolution - .suggestions - .iter() - .map(|suggestion| suggestion.resolve(project.clone(), cx.clone())) - .collect(); - - // Expand the context ranges of each suggestion and group suggestions with overlapping context ranges. - let suggestions = future::join_all(suggestion_tasks) - .await - .into_iter() - .filter_map(|task| task.log_err()) - .collect::>(); - - let mut suggestions_by_buffer = HashMap::default(); - for (buffer, suggestion) in suggestions { - suggestions_by_buffer - .entry(buffer) - .or_insert_with(Vec::new) - .push(suggestion); - } - - let mut suggestion_groups_by_buffer = HashMap::default(); - for (buffer, mut suggestions) in suggestions_by_buffer { - let mut suggestion_groups = Vec::::new(); - let snapshot = buffer.update(&mut cx, |buffer, _| buffer.snapshot())?; - // Sort suggestions by their range so that earlier, larger ranges come first - suggestions.sort_by(|a, b| a.range().cmp(&b.range(), &snapshot)); - - // Merge overlapping suggestions - suggestions.dedup_by(|a, b| b.try_merge(a, &snapshot)); - - // Create context ranges for each suggestion - for suggestion in suggestions { - let context_range = { - let suggestion_point_range = suggestion.range().to_point(&snapshot); - let start_row = suggestion_point_range.start.row.saturating_sub(5); - let end_row = cmp::min( - suggestion_point_range.end.row + 5, - snapshot.max_point().row, - ); - let start = snapshot.anchor_before(Point::new(start_row, 0)); - let end = snapshot - .anchor_after(Point::new(end_row, snapshot.line_len(end_row))); - start..end - }; - - if let Some(last_group) = suggestion_groups.last_mut() { - if last_group - .context_range - .end - .cmp(&context_range.start, &snapshot) - .is_ge() - { - // Merge with the previous group if context ranges overlap - last_group.context_range.end = context_range.end; - last_group.suggestions.push(suggestion); - } else { - // Create a new group - suggestion_groups.push(WorkflowSuggestionGroup { - context_range, - suggestions: vec![suggestion], - }); - } - } else { - // Create the first group - suggestion_groups.push(WorkflowSuggestionGroup { - context_range, - suggestions: vec![suggestion], - }); - } - } - - suggestion_groups_by_buffer.insert(buffer, suggestion_groups); - } - - Ok((resolution.step_title, suggestion_groups_by_buffer)) - }; - - let result = result.await; - this.update(&mut cx, |this, cx| { - this.resolution = Some(match result { - Ok((title, suggestion_groups)) => Ok(WorkflowStepResolution { - title, - suggestion_groups, - }), - Err(error) => Err(Arc::new(error)), - }); - this.context - .update(cx, |context, cx| context.workflow_step_updated(range, cx)) - .ok(); - cx.notify(); - }) - .ok(); - })); - None - } - - fn result_updated(&mut self, cx: &mut ModelContext) { - self.context - .update(cx, |context, cx| { - context.workflow_step_updated(self.context_buffer_range.clone(), cx) - }) - .ok(); - } -} - impl WorkflowSuggestion { pub fn range(&self) -> Range { match self { @@ -306,31 +115,7 @@ impl WorkflowSuggestion { } } - fn symbol_path(&self) -> Option<&SymbolPath> { - match self { - Self::Update { symbol_path, .. } => Some(symbol_path), - Self::InsertSiblingBefore { symbol_path, .. } => Some(symbol_path), - Self::InsertSiblingAfter { symbol_path, .. } => Some(symbol_path), - Self::PrependChild { symbol_path, .. } => symbol_path.as_ref(), - Self::AppendChild { symbol_path, .. } => symbol_path.as_ref(), - Self::Delete { symbol_path, .. } => Some(symbol_path), - Self::CreateFile { .. } => None, - } - } - - fn kind(&self) -> &str { - match self { - Self::Update { .. } => "Update", - Self::CreateFile { .. } => "CreateFile", - Self::InsertSiblingBefore { .. } => "InsertSiblingBefore", - Self::InsertSiblingAfter { .. } => "InsertSiblingAfter", - Self::PrependChild { .. } => "PrependChild", - Self::AppendChild { .. } => "AppendChild", - Self::Delete { .. } => "Delete", - } - } - - fn try_merge(&mut self, other: &Self, buffer: &BufferSnapshot) -> bool { + pub fn try_merge(&mut self, other: &Self, buffer: &BufferSnapshot) -> bool { let range = self.range(); let other_range = other.range(); @@ -465,339 +250,323 @@ impl WorkflowSuggestion { } } -pub mod tool { - use super::*; - use anyhow::Context as _; - use gpui::AsyncAppContext; - use language::{Outline, OutlineItem, ParseStatus}; - use language_model::LanguageModelTool; - use project::ProjectPath; - use schemars::JsonSchema; - use std::path::Path; +impl WorkflowStepEdit { + pub fn new( + path: Option, + operation: Option, + symbol: Option, + description: Option, + ) -> Result { + let path = path.ok_or_else(|| anyhow!("missing path"))?; + let operation = operation.ok_or_else(|| anyhow!("missing operation"))?; - #[derive(Debug, Serialize, Deserialize, JsonSchema)] - pub struct WorkflowStepResolutionTool { - /// An extremely short title for the edit step represented by these operations. - pub step_title: String, - /// A sequence of operations to apply to the codebase. - /// When multiple operations are required for a step, be sure to include multiple operations in this list. - pub suggestions: Vec, - } - - impl LanguageModelTool for WorkflowStepResolutionTool { - fn name() -> String { - "edit".into() - } - - fn description() -> String { - "suggest edits to one or more locations in the codebase".into() - } - } - - /// A description of an operation to apply to one location in the codebase. - /// - /// This object represents a single edit operation that can be performed on a specific file - /// in the codebase. It encapsulates both the location (file path) and the nature of the - /// edit to be made. - /// - /// # Fields - /// - /// * `path`: A string representing the file path where the edit operation should be applied. - /// This path is relative to the root of the project or repository. - /// - /// * `kind`: An enum representing the specific type of edit operation to be performed. - /// - /// # Usage - /// - /// `EditOperation` is used within a code editor to represent and apply - /// programmatic changes to source code. It provides a structured way to describe - /// edits for features like refactoring tools or AI-assisted coding suggestions. - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] - pub struct WorkflowSuggestionTool { - /// The path to the file containing the relevant operation - pub path: String, - #[serde(flatten)] - pub kind: WorkflowSuggestionToolKind, - } - - impl WorkflowSuggestionTool { - pub(super) async fn resolve( - &self, - project: Model, - mut cx: AsyncAppContext, - ) -> Result<(Model, super::WorkflowSuggestion)> { - let path = self.path.clone(); - let kind = self.kind.clone(); - let buffer = project - .update(&mut cx, |project, cx| { - let project_path = project - .find_project_path(Path::new(&path), cx) - .or_else(|| { - // If we couldn't find a project path for it, put it in the active worktree - // so that when we create the buffer, it can be saved. - let worktree = project - .active_entry() - .and_then(|entry_id| project.worktree_for_entry(entry_id, cx)) - .or_else(|| project.worktrees(cx).next())?; - let worktree = worktree.read(cx); - - Some(ProjectPath { - worktree_id: worktree.id(), - path: Arc::from(Path::new(&path)), - }) - }) - .with_context(|| format!("worktree not found for {:?}", path))?; - anyhow::Ok(project.open_buffer(project_path, cx)) - })?? - .await?; - - let mut parse_status = buffer.read_with(&cx, |buffer, _cx| buffer.parse_status())?; - while *parse_status.borrow() != ParseStatus::Idle { - parse_status.changed().await?; - } - - let snapshot = buffer.update(&mut cx, |buffer, _| buffer.snapshot())?; - let outline = snapshot.outline(None).context("no outline for buffer")?; - - let suggestion = match kind { - WorkflowSuggestionToolKind::Update { - symbol, - description, - } => { - let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; - let start = symbol - .annotation_range - .map_or(symbol.range.start, |range| range.start); - let start = Point::new(start.row, 0); - let end = Point::new( - symbol.range.end.row, - snapshot.line_len(symbol.range.end.row), - ); - let range = snapshot.anchor_before(start)..snapshot.anchor_after(end); - WorkflowSuggestion::Update { - range, - description, - symbol_path, - } - } - WorkflowSuggestionToolKind::Create { description } => { - WorkflowSuggestion::CreateFile { description } - } - WorkflowSuggestionToolKind::InsertSiblingBefore { - symbol, - description, - } => { - let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; - let position = snapshot.anchor_before( - symbol - .annotation_range - .map_or(symbol.range.start, |annotation_range| { - annotation_range.start - }), - ); - WorkflowSuggestion::InsertSiblingBefore { - position, - description, - symbol_path, - } - } - WorkflowSuggestionToolKind::InsertSiblingAfter { - symbol, - description, - } => { - let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; - let position = snapshot.anchor_after(symbol.range.end); - WorkflowSuggestion::InsertSiblingAfter { - position, - description, - symbol_path, - } - } - WorkflowSuggestionToolKind::PrependChild { - symbol, - description, - } => { - if let Some(symbol) = symbol { - let (symbol_path, symbol) = - Self::resolve_symbol(&snapshot, &outline, &symbol)?; - - let position = snapshot.anchor_after( - symbol - .body_range - .map_or(symbol.range.start, |body_range| body_range.start), - ); - WorkflowSuggestion::PrependChild { - position, - description, - symbol_path: Some(symbol_path), - } - } else { - WorkflowSuggestion::PrependChild { - position: language::Anchor::MIN, - description, - symbol_path: None, - } - } - } - WorkflowSuggestionToolKind::AppendChild { - symbol, - description, - } => { - if let Some(symbol) = symbol { - let (symbol_path, symbol) = - Self::resolve_symbol(&snapshot, &outline, &symbol)?; - - let position = snapshot.anchor_before( - symbol - .body_range - .map_or(symbol.range.end, |body_range| body_range.end), - ); - WorkflowSuggestion::AppendChild { - position, - description, - symbol_path: Some(symbol_path), - } - } else { - WorkflowSuggestion::PrependChild { - position: language::Anchor::MAX, - description, - symbol_path: None, - } - } - } - WorkflowSuggestionToolKind::Delete { symbol } => { - let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; - let start = symbol - .annotation_range - .map_or(symbol.range.start, |range| range.start); - let start = Point::new(start.row, 0); - let end = Point::new( - symbol.range.end.row, - snapshot.line_len(symbol.range.end.row), - ); - let range = snapshot.anchor_before(start)..snapshot.anchor_after(end); - WorkflowSuggestion::Delete { range, symbol_path } - } - }; - - Ok((buffer, suggestion)) - } - - fn resolve_symbol( - snapshot: &BufferSnapshot, - outline: &Outline, - symbol: &str, - ) -> Result<(SymbolPath, OutlineItem)> { - if symbol == IMPORTS_SYMBOL { - let target_row = find_first_non_comment_line(snapshot); - Ok(( - SymbolPath(IMPORTS_SYMBOL.to_string()), - OutlineItem { - range: Point::new(target_row, 0)..Point::new(target_row + 1, 0), - ..Default::default() - }, - )) - } else { - let (symbol_path, symbol) = outline - .find_most_similar(symbol) - .with_context(|| format!("symbol not found: {symbol}"))?; - Ok((symbol_path, symbol.to_point(snapshot))) - } - } - } - - fn find_first_non_comment_line(snapshot: &BufferSnapshot) -> u32 { - let Some(language) = snapshot.language() else { - return 0; + let kind = match operation.as_str() { + "update" => WorkflowStepEditKind::Update { + symbol: symbol.ok_or_else(|| anyhow!("missing symbol"))?, + description: description.ok_or_else(|| anyhow!("missing description"))?, + }, + "insert_sibling_before" => WorkflowStepEditKind::InsertSiblingBefore { + symbol: symbol.ok_or_else(|| anyhow!("missing symbol"))?, + description: description.ok_or_else(|| anyhow!("missing description"))?, + }, + "insert_sibling_after" => WorkflowStepEditKind::InsertSiblingAfter { + symbol: symbol.ok_or_else(|| anyhow!("missing symbol"))?, + description: description.ok_or_else(|| anyhow!("missing description"))?, + }, + "prepend_child" => WorkflowStepEditKind::PrependChild { + symbol, + description: description.ok_or_else(|| anyhow!("missing description"))?, + }, + "append_child" => WorkflowStepEditKind::AppendChild { + symbol, + description: description.ok_or_else(|| anyhow!("missing description"))?, + }, + "delete" => WorkflowStepEditKind::Delete { + symbol: symbol.ok_or_else(|| anyhow!("missing symbol"))?, + }, + "create" => WorkflowStepEditKind::Create { + description: description.ok_or_else(|| anyhow!("missing description"))?, + }, + _ => Err(anyhow!("unknown operation {operation:?}"))?, }; - let scope = language.default_scope(); - let comment_prefixes = scope.line_comment_prefixes(); - - let mut chunks = snapshot.as_rope().chunks(); - let mut target_row = 0; - loop { - let starts_with_comment = chunks - .peek() - .map(|chunk| { - comment_prefixes - .iter() - .any(|s| chunk.starts_with(s.as_ref().trim_end())) - }) - .unwrap_or(false); - - if !starts_with_comment { - break; - } - - target_row += 1; - if !chunks.next_line() { - break; - } - } - target_row + Ok(Self { path, kind }) } - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] - #[serde(tag = "kind")] - pub enum WorkflowSuggestionToolKind { - /// Rewrites the specified symbol entirely based on the given description. - /// This operation completely replaces the existing symbol with new content. - Update { - /// A fully-qualified reference to the symbol, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. - /// The path should uniquely identify the symbol within the containing file. - symbol: String, - /// A brief description of the transformation to apply to the symbol. - description: String, - }, - /// Creates a new file with the given path based on the provided description. - /// This operation adds a new file to the codebase. - Create { - /// A brief description of the file to be created. - description: String, - }, - /// Inserts a new symbol based on the given description before the specified symbol. - /// This operation adds new content immediately preceding an existing symbol. - InsertSiblingBefore { - /// A fully-qualified reference to the symbol, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. - /// The new content will be inserted immediately before this symbol. - symbol: String, - /// A brief description of the new symbol to be inserted. - description: String, - }, - /// Inserts a new symbol based on the given description after the specified symbol. - /// This operation adds new content immediately following an existing symbol. - InsertSiblingAfter { - /// A fully-qualified reference to the symbol, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. - /// The new content will be inserted immediately after this symbol. - symbol: String, - /// A brief description of the new symbol to be inserted. - description: String, - }, - /// Inserts a new symbol as a child of the specified symbol at the start. - /// This operation adds new content as the first child of an existing symbol (or file if no symbol is provided). - PrependChild { - /// An optional fully-qualified reference to the symbol after the code you want to insert, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. - /// If provided, the new content will be inserted as the first child of this symbol. - /// If not provided, the new content will be inserted at the top of the file. - symbol: Option, - /// A brief description of the new symbol to be inserted. - description: String, - }, - /// Inserts a new symbol as a child of the specified symbol at the end. - /// This operation adds new content as the last child of an existing symbol (or file if no symbol is provided). - AppendChild { - /// An optional fully-qualified reference to the symbol before the code you want to insert, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. - /// If provided, the new content will be inserted as the last child of this symbol. - /// If not provided, the new content will be applied at the bottom of the file. - symbol: Option, - /// A brief description of the new symbol to be inserted. - description: String, - }, - /// Deletes the specified symbol from the containing file. - Delete { - /// An fully-qualified reference to the symbol to be deleted, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. - symbol: String, - }, + pub async fn resolve( + &self, + project: Model, + mut cx: AsyncAppContext, + ) -> Result<(Model, super::WorkflowSuggestion)> { + let path = self.path.clone(); + let kind = self.kind.clone(); + let buffer = project + .update(&mut cx, |project, cx| { + let project_path = project + .find_project_path(Path::new(&path), cx) + .or_else(|| { + // If we couldn't find a project path for it, put it in the active worktree + // so that when we create the buffer, it can be saved. + let worktree = project + .active_entry() + .and_then(|entry_id| project.worktree_for_entry(entry_id, cx)) + .or_else(|| project.worktrees(cx).next())?; + let worktree = worktree.read(cx); + + Some(ProjectPath { + worktree_id: worktree.id(), + path: Arc::from(Path::new(&path)), + }) + }) + .with_context(|| format!("worktree not found for {:?}", path))?; + anyhow::Ok(project.open_buffer(project_path, cx)) + })?? + .await?; + + let mut parse_status = buffer.read_with(&cx, |buffer, _cx| buffer.parse_status())?; + while *parse_status.borrow() != ParseStatus::Idle { + parse_status.changed().await?; + } + + let snapshot = buffer.update(&mut cx, |buffer, _| buffer.snapshot())?; + let outline = snapshot.outline(None).context("no outline for buffer")?; + + let suggestion = match kind { + WorkflowStepEditKind::Update { + symbol, + description, + } => { + let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; + let start = symbol + .annotation_range + .map_or(symbol.range.start, |range| range.start); + let start = Point::new(start.row, 0); + let end = Point::new( + symbol.range.end.row, + snapshot.line_len(symbol.range.end.row), + ); + let range = snapshot.anchor_before(start)..snapshot.anchor_after(end); + WorkflowSuggestion::Update { + range, + description, + symbol_path, + } + } + WorkflowStepEditKind::Create { description } => { + WorkflowSuggestion::CreateFile { description } + } + WorkflowStepEditKind::InsertSiblingBefore { + symbol, + description, + } => { + let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; + let position = snapshot.anchor_before( + symbol + .annotation_range + .map_or(symbol.range.start, |annotation_range| { + annotation_range.start + }), + ); + WorkflowSuggestion::InsertSiblingBefore { + position, + description, + symbol_path, + } + } + WorkflowStepEditKind::InsertSiblingAfter { + symbol, + description, + } => { + let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; + let position = snapshot.anchor_after(symbol.range.end); + WorkflowSuggestion::InsertSiblingAfter { + position, + description, + symbol_path, + } + } + WorkflowStepEditKind::PrependChild { + symbol, + description, + } => { + if let Some(symbol) = symbol { + let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; + + let position = snapshot.anchor_after( + symbol + .body_range + .map_or(symbol.range.start, |body_range| body_range.start), + ); + WorkflowSuggestion::PrependChild { + position, + description, + symbol_path: Some(symbol_path), + } + } else { + WorkflowSuggestion::PrependChild { + position: language::Anchor::MIN, + description, + symbol_path: None, + } + } + } + WorkflowStepEditKind::AppendChild { + symbol, + description, + } => { + if let Some(symbol) = symbol { + let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; + + let position = snapshot.anchor_before( + symbol + .body_range + .map_or(symbol.range.end, |body_range| body_range.end), + ); + WorkflowSuggestion::AppendChild { + position, + description, + symbol_path: Some(symbol_path), + } + } else { + WorkflowSuggestion::PrependChild { + position: language::Anchor::MAX, + description, + symbol_path: None, + } + } + } + WorkflowStepEditKind::Delete { symbol } => { + let (symbol_path, symbol) = Self::resolve_symbol(&snapshot, &outline, &symbol)?; + let start = symbol + .annotation_range + .map_or(symbol.range.start, |range| range.start); + let start = Point::new(start.row, 0); + let end = Point::new( + symbol.range.end.row, + snapshot.line_len(symbol.range.end.row), + ); + let range = snapshot.anchor_before(start)..snapshot.anchor_after(end); + WorkflowSuggestion::Delete { range, symbol_path } + } + }; + + Ok((buffer, suggestion)) + } + + fn resolve_symbol( + snapshot: &BufferSnapshot, + outline: &Outline, + symbol: &str, + ) -> Result<(SymbolPath, OutlineItem)> { + if symbol == IMPORTS_SYMBOL { + let target_row = find_first_non_comment_line(snapshot); + Ok(( + SymbolPath(IMPORTS_SYMBOL.to_string()), + OutlineItem { + range: Point::new(target_row, 0)..Point::new(target_row + 1, 0), + ..Default::default() + }, + )) + } else { + let (symbol_path, symbol) = outline + .find_most_similar(symbol) + .with_context(|| format!("symbol not found: {symbol}"))?; + Ok((symbol_path, symbol.to_point(snapshot))) + } } } + +fn find_first_non_comment_line(snapshot: &BufferSnapshot) -> u32 { + let Some(language) = snapshot.language() else { + return 0; + }; + + let scope = language.default_scope(); + let comment_prefixes = scope.line_comment_prefixes(); + + let mut chunks = snapshot.as_rope().chunks(); + let mut target_row = 0; + loop { + let starts_with_comment = chunks + .peek() + .map(|chunk| { + comment_prefixes + .iter() + .any(|s| chunk.starts_with(s.as_ref().trim_end())) + }) + .unwrap_or(false); + + if !starts_with_comment { + break; + } + + target_row += 1; + if !chunks.next_line() { + break; + } + } + target_row +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[serde(tag = "operation")] +pub enum WorkflowStepEditKind { + /// Rewrites the specified symbol entirely based on the given description. + /// This operation completely replaces the existing symbol with new content. + Update { + /// A fully-qualified reference to the symbol, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. + /// The path should uniquely identify the symbol within the containing file. + symbol: String, + /// A brief description of the transformation to apply to the symbol. + description: String, + }, + /// Creates a new file with the given path based on the provided description. + /// This operation adds a new file to the codebase. + Create { + /// A brief description of the file to be created. + description: String, + }, + /// Inserts a new symbol based on the given description before the specified symbol. + /// This operation adds new content immediately preceding an existing symbol. + InsertSiblingBefore { + /// A fully-qualified reference to the symbol, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. + /// The new content will be inserted immediately before this symbol. + symbol: String, + /// A brief description of the new symbol to be inserted. + description: String, + }, + /// Inserts a new symbol based on the given description after the specified symbol. + /// This operation adds new content immediately following an existing symbol. + InsertSiblingAfter { + /// A fully-qualified reference to the symbol, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. + /// The new content will be inserted immediately after this symbol. + symbol: String, + /// A brief description of the new symbol to be inserted. + description: String, + }, + /// Inserts a new symbol as a child of the specified symbol at the start. + /// This operation adds new content as the first child of an existing symbol (or file if no symbol is provided). + PrependChild { + /// An optional fully-qualified reference to the symbol after the code you want to insert, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. + /// If provided, the new content will be inserted as the first child of this symbol. + /// If not provided, the new content will be inserted at the top of the file. + symbol: Option, + /// A brief description of the new symbol to be inserted. + description: String, + }, + /// Inserts a new symbol as a child of the specified symbol at the end. + /// This operation adds new content as the last child of an existing symbol (or file if no symbol is provided). + AppendChild { + /// An optional fully-qualified reference to the symbol before the code you want to insert, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. + /// If provided, the new content will be inserted as the last child of this symbol. + /// If not provided, the new content will be applied at the bottom of the file. + symbol: Option, + /// A brief description of the new symbol to be inserted. + description: String, + }, + /// Deletes the specified symbol from the containing file. + Delete { + /// An fully-qualified reference to the symbol to be deleted, e.g. `mod foo impl Bar pub fn baz` instead of just `fn baz`. + symbol: String, + }, +} diff --git a/crates/assistant/src/workflow/step_view.rs b/crates/assistant/src/workflow/step_view.rs deleted file mode 100644 index 07c0cb9e6e..0000000000 --- a/crates/assistant/src/workflow/step_view.rs +++ /dev/null @@ -1,315 +0,0 @@ -use super::WorkflowStep; -use crate::{Assist, Context}; -use editor::{ - display_map::{BlockDisposition, BlockProperties, BlockStyle}, - Editor, EditorEvent, ExcerptRange, MultiBuffer, -}; -use gpui::{ - div, AnyElement, AppContext, Context as _, Empty, EventEmitter, FocusableView, IntoElement, - Model, ParentElement as _, Render, SharedString, Styled as _, View, ViewContext, - VisualContext as _, WeakModel, WindowContext, -}; -use language::{language_settings::SoftWrap, Anchor, Buffer, LanguageRegistry}; -use std::{ops::DerefMut, sync::Arc}; -use text::OffsetRangeExt; -use theme::ActiveTheme as _; -use ui::{ - h_flex, v_flex, ButtonCommon as _, ButtonLike, ButtonStyle, Color, Icon, IconName, - InteractiveElement as _, Label, LabelCommon as _, -}; -use workspace::{ - item::{self, Item}, - pane, - searchable::SearchableItemHandle, -}; - -pub struct WorkflowStepView { - step: WeakModel, - tool_output_buffer: Model, - editor: View, -} - -impl WorkflowStepView { - pub fn new( - context: Model, - step: Model, - language_registry: Arc, - cx: &mut ViewContext, - ) -> Self { - let tool_output_buffer = - cx.new_model(|cx| Buffer::local(step.read(cx).tool_output.clone(), cx)); - let buffer = cx.new_model(|cx| { - let mut buffer = MultiBuffer::without_headers(0, language::Capability::ReadWrite); - buffer.push_excerpts( - context.read(cx).buffer().clone(), - [ExcerptRange { - context: step.read(cx).context_buffer_range.clone(), - primary: None, - }], - cx, - ); - buffer.push_excerpts( - tool_output_buffer.clone(), - [ExcerptRange { - context: Anchor::MIN..Anchor::MAX, - primary: None, - }], - cx, - ); - buffer - }); - - let buffer_snapshot = buffer.read(cx).snapshot(cx); - let output_excerpt = buffer_snapshot.excerpts().skip(1).next().unwrap().0; - let input_start_anchor = multi_buffer::Anchor::min(); - let output_start_anchor = buffer_snapshot - .anchor_in_excerpt(output_excerpt, Anchor::MIN) - .unwrap(); - let output_end_anchor = multi_buffer::Anchor::max(); - - let handle = cx.view().downgrade(); - let editor = cx.new_view(|cx| { - let mut editor = Editor::for_multibuffer(buffer.clone(), None, false, cx); - editor.set_soft_wrap_mode(SoftWrap::EditorWidth, cx); - editor.set_show_line_numbers(false, cx); - editor.set_show_git_diff_gutter(false, cx); - editor.set_show_code_actions(false, cx); - editor.set_show_runnables(false, cx); - editor.set_show_wrap_guides(false, cx); - editor.set_show_indent_guides(false, cx); - editor.set_read_only(true); - editor.set_show_inline_completions(Some(false), cx); - editor.insert_blocks( - [ - BlockProperties { - position: input_start_anchor, - height: 1, - style: BlockStyle::Fixed, - render: Box::new(|cx| section_header("Step Input", cx)), - disposition: BlockDisposition::Above, - priority: 0, - }, - BlockProperties { - position: output_start_anchor, - height: 1, - style: BlockStyle::Fixed, - render: Box::new(|cx| section_header("Tool Output", cx)), - disposition: BlockDisposition::Above, - priority: 0, - }, - BlockProperties { - position: output_end_anchor, - height: 1, - style: BlockStyle::Fixed, - render: Box::new(move |cx| { - if let Some(result) = handle.upgrade().and_then(|this| { - this.update(cx.deref_mut(), |this, cx| this.render_result(cx)) - }) { - v_flex() - .child(section_header("Output", cx)) - .child( - div().pl(cx.gutter_dimensions.full_width()).child(result), - ) - .into_any_element() - } else { - Empty.into_any_element() - } - }), - disposition: BlockDisposition::Below, - priority: 0, - }, - ], - None, - cx, - ); - editor - }); - - cx.observe(&step, Self::step_updated).detach(); - cx.observe_release(&step, Self::step_released).detach(); - - cx.spawn(|this, mut cx| async move { - if let Ok(language) = language_registry.language_for_name("JSON").await { - this.update(&mut cx, |this, cx| { - this.tool_output_buffer.update(cx, |buffer, cx| { - buffer.set_language(Some(language), cx); - }); - }) - .ok(); - } - }) - .detach(); - - Self { - tool_output_buffer, - step: step.downgrade(), - editor, - } - } - - pub fn step(&self) -> &WeakModel { - &self.step - } - - fn render_result(&mut self, cx: &mut ViewContext) -> Option { - let step = self.step.upgrade()?; - let result = step.read(cx).resolution.as_ref()?; - match result { - Ok(result) => { - Some( - v_flex() - .child(result.title.clone()) - .children(result.suggestion_groups.iter().filter_map( - |(buffer, suggestion_groups)| { - let buffer = buffer.read(cx); - let path = buffer.file().map(|f| f.path()); - let snapshot = buffer.snapshot(); - v_flex() - .mb_2() - .border_b_1() - .children(path.map(|path| format!("path: {}", path.display()))) - .children(suggestion_groups.iter().map(|group| { - v_flex().pt_2().pl_2().children( - group.suggestions.iter().map(|suggestion| { - let range = suggestion.range().to_point(&snapshot); - v_flex() - .children( - suggestion.description().map(|desc| { - format!("description: {desc}") - }), - ) - .child(format!("kind: {}", suggestion.kind())) - .children(suggestion.symbol_path().map( - |path| format!("symbol path: {}", path.0), - )) - .child(format!( - "lines: {} - {}", - range.start.row + 1, - range.end.row + 1 - )) - }), - ) - })) - .into() - }, - )) - .into_any_element(), - ) - } - Err(error) => Some(format!("{:?}", error).into_any_element()), - } - } - - fn step_updated(&mut self, step: Model, cx: &mut ViewContext) { - self.tool_output_buffer.update(cx, |buffer, cx| { - let text = step.read(cx).tool_output.clone(); - buffer.set_text(text, cx); - }); - cx.notify(); - } - - fn step_released(&mut self, _: &mut WorkflowStep, cx: &mut ViewContext) { - cx.emit(EditorEvent::Closed); - } - - fn resolve(&mut self, _: &Assist, cx: &mut ViewContext) { - self.step - .update(cx, |step, cx| { - step.resolve(cx); - }) - .ok(); - } -} - -fn section_header( - name: &'static str, - cx: &mut editor::display_map::BlockContext, -) -> gpui::AnyElement { - h_flex() - .pl(cx.gutter_dimensions.full_width()) - .h_11() - .w_full() - .relative() - .gap_1() - .child( - ButtonLike::new("role") - .style(ButtonStyle::Filled) - .child(Label::new(name).color(Color::Default)), - ) - .into_any_element() -} - -impl Render for WorkflowStepView { - fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { - div() - .key_context("ContextEditor") - .on_action(cx.listener(Self::resolve)) - .flex_grow() - .bg(cx.theme().colors().editor_background) - .child(self.editor.clone()) - } -} - -impl EventEmitter for WorkflowStepView {} - -impl FocusableView for WorkflowStepView { - fn focus_handle(&self, cx: &gpui::AppContext) -> gpui::FocusHandle { - self.editor.read(cx).focus_handle(cx) - } -} - -impl Item for WorkflowStepView { - type Event = EditorEvent; - - fn tab_content_text(&self, cx: &WindowContext) -> Option { - let step = self.step.upgrade()?.read(cx); - let context = step.context.upgrade()?.read(cx); - let buffer = context.buffer().read(cx); - let index = context - .workflow_step_index_for_range(&step.context_buffer_range, buffer) - .ok()? - + 1; - Some(format!("Step {index}").into()) - } - - fn tab_icon(&self, _cx: &WindowContext) -> Option { - Some(Icon::new(IconName::SearchCode)) - } - - fn to_item_events(event: &Self::Event, mut f: impl FnMut(item::ItemEvent)) { - match event { - EditorEvent::Edited { .. } => { - f(item::ItemEvent::Edit); - } - EditorEvent::TitleChanged => { - f(item::ItemEvent::UpdateTab); - } - EditorEvent::Closed => f(item::ItemEvent::CloseItem), - _ => {} - } - } - - fn tab_tooltip_text(&self, _cx: &AppContext) -> Option { - None - } - - fn as_searchable(&self, _handle: &View) -> Option> { - None - } - - fn set_nav_history(&mut self, nav_history: pane::ItemNavHistory, cx: &mut ViewContext) { - self.editor.update(cx, |editor, cx| { - Item::set_nav_history(editor, nav_history, cx) - }) - } - - fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { - self.editor - .update(cx, |editor, cx| Item::navigate(editor, data, cx)) - } - - fn deactivated(&mut self, cx: &mut ViewContext) { - self.editor - .update(cx, |editor, cx| Item::deactivated(editor, cx)) - } -} diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index ef69c08546..056c47dfbd 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -105,7 +105,7 @@ pub struct DisplayMap { inlay_highlights: InlayHighlights, /// A container for explicitly foldable ranges, which supersede indentation based fold range suggestions. crease_map: CreaseMap, - fold_placeholder: FoldPlaceholder, + pub(crate) fold_placeholder: FoldPlaceholder, pub clip_at_line_ends: bool, pub(crate) masked: bool, } diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 71fe31474b..ce6349b203 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -10388,6 +10388,10 @@ impl Editor { } } + pub fn default_fold_placeholder(&self, cx: &AppContext) -> FoldPlaceholder { + self.display_map.read(cx).fold_placeholder.clone() + } + pub fn set_gutter_hovered(&mut self, hovered: bool, cx: &mut ViewContext) { if hovered != self.gutter_hovered { self.gutter_hovered = hovered; diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 98a215c9b5..ea0378853a 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -41,8 +41,12 @@ use gpui::{ ViewContext, WeakView, WindowContext, }; use itertools::Itertools; -use language::language_settings::{ - IndentGuideBackgroundColoring, IndentGuideColoring, IndentGuideSettings, ShowWhitespaceSetting, +use language::{ + language_settings::{ + IndentGuideBackgroundColoring, IndentGuideColoring, IndentGuideSettings, + ShowWhitespaceSetting, + }, + ChunkRendererContext, }; use lsp::DiagnosticSeverity; use multi_buffer::{Anchor, MultiBufferPoint, MultiBufferRow}; @@ -1872,6 +1876,7 @@ impl EditorElement { line_number_layouts: &[Option], snapshot: &EditorSnapshot, style: &EditorStyle, + editor_width: Pixels, cx: &mut WindowContext, ) -> Vec { if rows.start >= rows.end { @@ -1922,6 +1927,7 @@ impl EditorElement { rows.len(), line_number_layouts, snapshot.mode, + editor_width, cx, ) } @@ -1966,6 +1972,7 @@ impl EditorElement { line_height: Pixels, em_width: Pixels, text_hitbox: &Hitbox, + editor_width: Pixels, scroll_width: &mut Pixels, resized_blocks: &mut HashMap, cx: &mut WindowContext, @@ -1981,7 +1988,7 @@ impl EditorElement { line_layouts[align_to.row().minus(rows.start) as usize] .x_for_index(align_to.column() as usize) } else { - layout_line(align_to.row(), snapshot, &self.style, cx) + layout_line(align_to.row(), snapshot, &self.style, editor_width, cx) .x_for_index(align_to.column() as usize) }; @@ -2418,6 +2425,7 @@ impl EditorElement { snapshot: &EditorSnapshot, hitbox: &Hitbox, text_hitbox: &Hitbox, + editor_width: Pixels, scroll_width: &mut Pixels, gutter_dimensions: &GutterDimensions, em_width: Pixels, @@ -2457,6 +2465,7 @@ impl EditorElement { line_height, em_width, text_hitbox, + editor_width, scroll_width, &mut resized_blocks, cx, @@ -2500,6 +2509,7 @@ impl EditorElement { line_height, em_width, text_hitbox, + editor_width, scroll_width, &mut resized_blocks, cx, @@ -2544,6 +2554,7 @@ impl EditorElement { line_height, em_width, text_hitbox, + editor_width, scroll_width, &mut resized_blocks, cx, @@ -4353,6 +4364,7 @@ impl fmt::Debug for LineFragment { } impl LineWithInvisibles { + #[allow(clippy::too_many_arguments)] fn from_chunks<'a>( chunks: impl Iterator>, text_style: &TextStyle, @@ -4360,6 +4372,7 @@ impl LineWithInvisibles { max_line_count: usize, line_number_layouts: &[Option], editor_mode: EditorMode, + text_width: Pixels, cx: &mut WindowContext, ) -> Vec { let mut layouts = Vec::with_capacity(max_line_count); @@ -4414,7 +4427,10 @@ impl LineWithInvisibles { AvailableSpace::MinContent }; - let mut element = (renderer.render)(cx); + let mut element = (renderer.render)(&mut ChunkRendererContext { + context: cx, + max_width: text_width, + }); let line_height = text_style.line_height_in_pixels(cx.rem_size()); let size = element.layout_as_root( size(available_width, AvailableSpace::Definite(line_height)), @@ -4871,6 +4887,7 @@ impl Element for EditorElement { &[], &editor_snapshot, &style, + px(f32::MAX), cx, ) .pop() @@ -4983,6 +5000,9 @@ impl Element for EditorElement { }; let overscroll = size(em_width + right_margin, px(0.)); + let editor_width = + text_width - gutter_dimensions.margin - overscroll.width - em_width; + snapshot = self.editor.update(cx, |editor, cx| { editor.last_bounds = Some(bounds); editor.gutter_dimensions = gutter_dimensions; @@ -4991,8 +5011,6 @@ impl Element for EditorElement { if matches!(editor.mode, EditorMode::AutoHeight { .. }) { snapshot } else { - let editor_width = - text_width - gutter_dimensions.margin - overscroll.width - em_width; let wrap_width = match editor.soft_wrap_mode(cx) { SoftWrap::None => None, SoftWrap::PreferLine => { @@ -5162,6 +5180,7 @@ impl Element for EditorElement { &line_numbers, &snapshot, &self.style, + editor_width, cx, ); for line_with_invisibles in &line_layouts { @@ -5171,7 +5190,8 @@ impl Element for EditorElement { } let longest_line_width = - layout_line(snapshot.longest_row(), &snapshot, &style, cx).width; + layout_line(snapshot.longest_row(), &snapshot, &style, editor_width, cx) + .width; let mut scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.width; @@ -5181,6 +5201,7 @@ impl Element for EditorElement { &snapshot, &hitbox, &text_hitbox, + editor_width, &mut scroll_width, &gutter_dimensions, em_width, @@ -5952,12 +5973,22 @@ fn layout_line( row: DisplayRow, snapshot: &EditorSnapshot, style: &EditorStyle, + text_width: Pixels, cx: &mut WindowContext, ) -> LineWithInvisibles { let chunks = snapshot.highlighted_chunks(row..row + DisplayRow(1), true, style); - LineWithInvisibles::from_chunks(chunks, &style.text, MAX_LINE_LEN, 1, &[], snapshot.mode, cx) - .pop() - .unwrap() + LineWithInvisibles::from_chunks( + chunks, + &style.text, + MAX_LINE_LEN, + 1, + &[], + snapshot.mode, + text_width, + cx, + ) + .pop() + .unwrap() } #[derive(Debug)] diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index ae8b2a957f..9d11633c10 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -21,7 +21,7 @@ use async_watch as watch; pub use clock::ReplicaId; use futures::channel::oneshot; use gpui::{ - AnyElement, AppContext, EventEmitter, HighlightStyle, ModelContext, Task, TaskLabel, + AnyElement, AppContext, EventEmitter, HighlightStyle, ModelContext, Pixels, Task, TaskLabel, WindowContext, }; use lsp::LanguageServerId; @@ -40,7 +40,7 @@ use std::{ future::Future, iter::{self, Iterator, Peekable}, mem, - ops::{Deref, Range}, + ops::{Deref, DerefMut, Range}, path::{Path, PathBuf}, str, sync::{Arc, LazyLock}, @@ -486,11 +486,16 @@ pub struct Chunk<'a> { #[derive(Clone)] pub struct ChunkRenderer { /// creates a custom element to represent this chunk. - pub render: Arc AnyElement>, + pub render: Arc AnyElement>, /// If true, the element is constrained to the shaped width of the text. pub constrain_width: bool, } +pub struct ChunkRendererContext<'a, 'b> { + pub context: &'a mut WindowContext<'b>, + pub max_width: Pixels, +} + impl fmt::Debug for ChunkRenderer { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("ChunkRenderer") @@ -499,6 +504,20 @@ impl fmt::Debug for ChunkRenderer { } } +impl<'a, 'b> Deref for ChunkRendererContext<'a, 'b> { + type Target = WindowContext<'b>; + + fn deref(&self) -> &Self::Target { + self.context + } +} + +impl<'a, 'b> DerefMut for ChunkRendererContext<'a, 'b> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.context + } +} + /// A set of edits to a given version of a buffer, computed asynchronously. #[derive(Debug)] pub struct Diff { diff --git a/crates/languages/src/rust/outline.scm b/crates/languages/src/rust/outline.scm index 3012995e2a..98892ce93c 100644 --- a/crates/languages/src/rust/outline.scm +++ b/crates/languages/src/rust/outline.scm @@ -4,7 +4,8 @@ (struct_item (visibility_modifier)? @context "struct" @context - name: (_) @name) @item + name: (_) @name + body: (_ "{" @open (_)* "}" @close)) @item (enum_item (visibility_modifier)? @context