Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Start on a refactoring assistant
Antonio Scandurra3ad7f528cb0e
diff --git a/crates/ai/src/ai.rs b/crates/ai/src/ai.rsindex 7cc5f08f7c1..7874bb46a58 100644--- a/crates/ai/src/ai.rs+++ b/crates/ai/src/ai.rs@@ -1,5 +1,6 @@pub mod assistant;mod assistant_settings;+mod refactor;use anyhow::Result;pub use assistant::AssistantPanel;diff --git a/crates/ai/src/refactor.rs b/crates/ai/src/refactor.rsnew file mode 100644index 00000000000..e1b57680eec--- /dev/null+++ b/crates/ai/src/refactor.rs@@ -0,0 +1,88 @@+use collections::HashMap;+use editor::Editor;+use gpui::{+ actions, elements::*, AnyViewHandle, AppContext, Entity, Task, View, ViewContext, ViewHandle,+};+use std::sync::Arc;+use workspace::{Modal, Workspace};++actions!(assistant, [Refactor]);++fn init(cx: &mut AppContext) {+ cx.set_global(RefactoringAssistant::new());+ cx.add_action(RefactoringModal::deploy);+}++pub struct RefactoringAssistant {+ pending_edits_by_editor: HashMap<usize, Task<Option<()>>>,+}++impl RefactoringAssistant {+ fn new() -> Self {+ Self {+ pending_edits_by_editor: Default::default(),+ }+ }++ fn refactor(&mut self, editor: &ViewHandle<Editor>, prompt: &str, cx: &mut AppContext) {}+}++struct RefactoringModal {+ prompt_editor: ViewHandle<Editor>,+ has_focus: bool,+}++impl Entity for RefactoringModal {+ type Event = ();+}++impl View for RefactoringModal {+ fn ui_name() -> &'static str {+ "RefactoringModal"+ }++ fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {+ todo!()+ }++ fn focus_in(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {+ self.has_focus = true;+ }++ fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {+ self.has_focus = false;+ }+}++impl Modal for RefactoringModal {+ fn has_focus(&self) -> bool {+ self.has_focus+ }++ fn dismiss_on_event(event: &Self::Event) -> bool {+ todo!()+ }+}++impl RefactoringModal {+ fn deploy(workspace: &mut Workspace, _: &Refactor, cx: &mut ViewContext<Workspace>) {+ workspace.toggle_modal(cx, |_, cx| {+ let prompt_editor = cx.add_view(|cx| {+ Editor::auto_height(+ 4,+ Some(Arc::new(|theme| theme.search.editor.input.clone())),+ cx,+ )+ });+ cx.add_view(|_| RefactoringModal {+ prompt_editor,+ has_focus: false,+ })+ });+ }+}++// ABCDEFG+// XCDEFG+//+//diff --git a/prompt.md b/prompt.mdnew file mode 100644index 00000000000..33213a58596--- /dev/null+++ b/prompt.md@@ -0,0 +1,11 @@+Given a snippet as the input, you must produce an array of edits. An edit has the following structure:++{ skip: "skip", delete: "delete", insert: "insert" }++`skip` is a string in the input that should be left unchanged. `delete` is a string in the input located right after the skipped text that should be deleted. `insert` is a new string that should be inserted after the end of the text in `skip`. It's crucial that a string in the input can only be skipped or deleted once and only once.++Your task is to produce an array of edits. `delete` and `insert` can be empty if nothing changed. When `skip`, `delete` or `insert` are longer than 20 characters, split them into multiple edits.++Check your reasoning by concatenating all the strings in `skip` and `delete`. If the text is the same as the input snippet then the edits are valid.++It's crucial that you reply only with edits. No prose or remarks.