Wire up rename editor

This commit is contained in:
Antonio Scandurra
2023-11-14 18:50:02 +01:00
parent 5ae96e4eb6
commit 428be43710
2 changed files with 177 additions and 168 deletions
+167 -165
View File
@@ -40,7 +40,7 @@ use fuzzy::{StringMatch, StringMatchCandidate};
use git::diff_hunk_to_display; use git::diff_hunk_to_display;
use gpui::{ use gpui::{
action, actions, div, point, px, relative, rems, size, uniform_list, AnyElement, AppContext, action, actions, div, point, px, relative, rems, size, uniform_list, AnyElement, AppContext,
AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Component, Context, AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Component, Context, Entity,
EventEmitter, FocusHandle, FontFeatures, FontStyle, FontWeight, HighlightStyle, Hsla, EventEmitter, FocusHandle, FontFeatures, FontStyle, FontWeight, HighlightStyle, Hsla,
InputHandler, KeyContext, Model, MouseButton, ParentElement, Pixels, Render, InputHandler, KeyContext, Model, MouseButton, ParentElement, Pixels, Render,
StatefulInteractive, StatelessInteractive, Styled, Subscription, Task, TextStyle, StatefulInteractive, StatelessInteractive, Styled, Subscription, Task, TextStyle,
@@ -100,7 +100,9 @@ use theme::{
use ui::{v_stack, HighlightedLabel, IconButton, StyledExt, TextTooltip}; use ui::{v_stack, HighlightedLabel, IconButton, StyledExt, TextTooltip};
use util::{post_inc, RangeExt, ResultExt, TryFutureExt}; use util::{post_inc, RangeExt, ResultExt, TryFutureExt};
use workspace::{ use workspace::{
item::ItemEvent, searchable::SearchEvent, ItemNavHistory, SplitDirection, ViewId, Workspace, item::{ItemEvent, ItemHandle},
searchable::SearchEvent,
ItemNavHistory, SplitDirection, ViewId, Workspace,
}; };
const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500); const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
@@ -7690,183 +7692,183 @@ impl Editor {
} }
} }
// pub fn rename(&mut self, _: &Rename, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> { pub fn rename(&mut self, _: &Rename, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
// use language::ToOffset as _; use language::ToOffset as _;
// let project = self.project.clone()?; let project = self.project.clone()?;
// let selection = self.selections.newest_anchor().clone(); let selection = self.selections.newest_anchor().clone();
// let (cursor_buffer, cursor_buffer_position) = self let (cursor_buffer, cursor_buffer_position) = self
// .buffer .buffer
// .read(cx) .read(cx)
// .text_anchor_for_position(selection.head(), cx)?; .text_anchor_for_position(selection.head(), cx)?;
// let (tail_buffer, _) = self let (tail_buffer, _) = self
// .buffer .buffer
// .read(cx) .read(cx)
// .text_anchor_for_position(selection.tail(), cx)?; .text_anchor_for_position(selection.tail(), cx)?;
// if tail_buffer != cursor_buffer { if tail_buffer != cursor_buffer {
// return None; return None;
// } }
// let snapshot = cursor_buffer.read(cx).snapshot(); let snapshot = cursor_buffer.read(cx).snapshot();
// let cursor_buffer_offset = cursor_buffer_position.to_offset(&snapshot); let cursor_buffer_offset = cursor_buffer_position.to_offset(&snapshot);
// let prepare_rename = project.update(cx, |project, cx| { let prepare_rename = project.update(cx, |project, cx| {
// project.prepare_rename(cursor_buffer, cursor_buffer_offset, cx) project.prepare_rename(cursor_buffer, cursor_buffer_offset, cx)
// }); });
// Some(cx.spawn(|this, mut cx| async move { Some(cx.spawn(|this, mut cx| async move {
// let rename_range = if let Some(range) = prepare_rename.await? { let rename_range = if let Some(range) = prepare_rename.await? {
// Some(range) Some(range)
// } else { } else {
// this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
// let buffer = this.buffer.read(cx).snapshot(cx); let buffer = this.buffer.read(cx).snapshot(cx);
// let mut buffer_highlights = this let mut buffer_highlights = this
// .document_highlights_for_position(selection.head(), &buffer) .document_highlights_for_position(selection.head(), &buffer)
// .filter(|highlight| { .filter(|highlight| {
// highlight.start.excerpt_id == selection.head().excerpt_id highlight.start.excerpt_id == selection.head().excerpt_id
// && highlight.end.excerpt_id == selection.head().excerpt_id && highlight.end.excerpt_id == selection.head().excerpt_id
// }); });
// buffer_highlights buffer_highlights
// .next() .next()
// .map(|highlight| highlight.start.text_anchor..highlight.end.text_anchor) .map(|highlight| highlight.start.text_anchor..highlight.end.text_anchor)
// })? })?
// }; };
// if let Some(rename_range) = rename_range { if let Some(rename_range) = rename_range {
// let rename_buffer_range = rename_range.to_offset(&snapshot); let rename_buffer_range = rename_range.to_offset(&snapshot);
// let cursor_offset_in_rename_range = let cursor_offset_in_rename_range =
// cursor_buffer_offset.saturating_sub(rename_buffer_range.start); cursor_buffer_offset.saturating_sub(rename_buffer_range.start);
// this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
// this.take_rename(false, cx); this.take_rename(false, cx);
// let buffer = this.buffer.read(cx).read(cx); let buffer = this.buffer.read(cx).read(cx);
// let cursor_offset = selection.head().to_offset(&buffer); let cursor_offset = selection.head().to_offset(&buffer);
// let rename_start = cursor_offset.saturating_sub(cursor_offset_in_rename_range); let rename_start = cursor_offset.saturating_sub(cursor_offset_in_rename_range);
// let rename_end = rename_start + rename_buffer_range.len(); let rename_end = rename_start + rename_buffer_range.len();
// let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end); let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end);
// let mut old_highlight_id = None; let mut old_highlight_id = None;
// let old_name: Arc<str> = buffer let old_name: Arc<str> = buffer
// .chunks(rename_start..rename_end, true) .chunks(rename_start..rename_end, true)
// .map(|chunk| { .map(|chunk| {
// if old_highlight_id.is_none() { if old_highlight_id.is_none() {
// old_highlight_id = chunk.syntax_highlight_id; old_highlight_id = chunk.syntax_highlight_id;
// } }
// chunk.text chunk.text
// }) })
// .collect::<String>() .collect::<String>()
// .into(); .into();
// drop(buffer); drop(buffer);
// // Position the selection in the rename editor so that it matches the current selection. // Position the selection in the rename editor so that it matches the current selection.
// this.show_local_selections = false; this.show_local_selections = false;
// let rename_editor = cx.build_view(|cx| { let rename_editor = cx.build_view(|cx| {
// let mut editor = Editor::single_line(cx); let mut editor = Editor::single_line(cx);
// if let Some(old_highlight_id) = old_highlight_id { editor.buffer.update(cx, |buffer, cx| {
// editor.override_text_style = buffer.edit([(0..0, old_name.clone())], None, cx)
// Some(Box::new(move |style| old_highlight_id.style(&style.syntax))); });
// } editor.select_all(&SelectAll, cx);
// editor.buffer.update(cx, |buffer, cx| { editor
// buffer.edit([(0..0, old_name.clone())], None, cx) });
// });
// editor.select_all(&SelectAll, cx);
// editor
// });
// let ranges = this let ranges = this
// .clear_background_highlights::<DocumentHighlightWrite>(cx) .clear_background_highlights::<DocumentHighlightWrite>(cx)
// .into_iter() .into_iter()
// .flat_map(|(_, ranges)| ranges.into_iter()) .flat_map(|(_, ranges)| ranges.into_iter())
// .chain( .chain(
// this.clear_background_highlights::<DocumentHighlightRead>(cx) this.clear_background_highlights::<DocumentHighlightRead>(cx)
// .into_iter() .into_iter()
// .flat_map(|(_, ranges)| ranges.into_iter()), .flat_map(|(_, ranges)| ranges.into_iter()),
// ) )
// .collect(); .collect();
// this.highlight_text::<Rename>( this.highlight_text::<Rename>(
// ranges, ranges,
// HighlightStyle { HighlightStyle {
// fade_out: Some(style.rename_fade), fade_out: Some(0.6),
// ..Default::default() ..Default::default()
// }, },
// cx, cx,
// ); );
// cx.focus(&rename_editor); let rename_focus_handle = rename_editor.focus_handle(cx);
// let block_id = this.insert_blocks( cx.focus(&rename_focus_handle);
// [BlockProperties { let block_id = this.insert_blocks(
// style: BlockStyle::Flex, [BlockProperties {
// position: range.start.clone(), style: BlockStyle::Flex,
// height: 1, position: range.start.clone(),
// render: Arc::new({ height: 1,
// let editor = rename_editor.clone(); render: Arc::new({
// move |cx: &mut BlockContext| { let editor = rename_editor.clone();
// ChildView::new(&editor, cx) move |cx: &mut BlockContext| {
// .contained() div().pl(cx.anchor_x).child(editor.clone()).render()
// .with_padding_left(cx.anchor_x) }
// .into_any() }),
// } disposition: BlockDisposition::Below,
// }), }],
// disposition: BlockDisposition::Below, Some(Autoscroll::fit()),
// }], cx,
// Some(Autoscroll::fit()), )[0];
// cx, this.pending_rename = Some(RenameState {
// )[0]; range,
// this.pending_rename = Some(RenameState { old_name,
// range, editor: rename_editor,
// old_name, block_id,
// editor: rename_editor, });
// block_id, })?;
// }); }
// })?;
// }
// Ok(()) Ok(())
// })) }))
// } }
// pub fn confirm_rename( pub fn confirm_rename(
// workspace: &mut Workspace, &mut self,
// _: &ConfirmRename, _: &ConfirmRename,
// cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
// let editor = workspace.active_item(cx)?.act_as::<Editor>(cx)?; let rename = self.take_rename(false, cx)?;
let workspace = self.workspace()?;
let (start_buffer, start) = self
.buffer
.read(cx)
.text_anchor_for_position(rename.range.start.clone(), cx)?;
let (end_buffer, end) = self
.buffer
.read(cx)
.text_anchor_for_position(rename.range.end.clone(), cx)?;
if start_buffer != end_buffer {
return None;
}
// let (buffer, range, old_name, new_name) = editor.update(cx, |editor, cx| { let buffer = start_buffer;
// let rename = editor.take_rename(false, cx)?; let range = start..end;
// let buffer = editor.buffer.read(cx); let old_name = rename.old_name;
// let (start_buffer, start) = let new_name = rename.editor.read(cx).text(cx);
// buffer.text_anchor_for_position(rename.range.start.clone(), cx)?;
// let (end_buffer, end) =
// buffer.text_anchor_for_position(rename.range.end.clone(), cx)?;
// if start_buffer == end_buffer {
// let new_name = rename.editor.read(cx).text(cx);
// Some((start_buffer, start..end, rename.old_name, new_name))
// } else {
// None
// }
// })?;
// let rename = workspace.project().clone().update(cx, |project, cx| { let rename = workspace
// project.perform_rename(buffer.clone(), range.start, new_name.clone(), true, cx) .read(cx)
// }); .project()
.clone()
.update(cx, |project, cx| {
project.perform_rename(buffer.clone(), range.start, new_name.clone(), true, cx)
});
let workspace = workspace.downgrade();
// let editor = editor.downgrade(); Some(cx.spawn(|editor, mut cx| async move {
// Some(cx.spawn(|workspace, mut cx| async move { let project_transaction = rename.await?;
// let project_transaction = rename.await?; Self::open_project_transaction(
// Self::open_project_transaction( &editor,
// &editor, workspace,
// workspace, project_transaction,
// project_transaction, format!("Rename: {} → {}", old_name, new_name),
// format!("Rename: {} → {}", old_name, new_name), cx.clone(),
// cx.clone(), )
// ) .await?;
// .await?;
// editor.update(&mut cx, |editor, cx| { editor.update(&mut cx, |editor, cx| {
// editor.refresh_document_highlights(cx); editor.refresh_document_highlights(cx);
// })?; })?;
// Ok(()) Ok(())
// })) }))
// } }
fn take_rename( fn take_rename(
&mut self, &mut self,
+10 -3
View File
@@ -2007,7 +2007,6 @@ impl EditorElement {
anchor_x, anchor_x,
gutter_padding, gutter_padding,
line_height, line_height,
// scroll_x,
gutter_width, gutter_width,
em_width, em_width,
block_id, block_id,
@@ -2569,8 +2568,16 @@ impl Element<Editor> for EditorElement {
.confirm_code_action(action, cx) .confirm_code_action(action, cx)
.map(|task| task.detach_and_log_err(cx)); .map(|task| task.detach_and_log_err(cx));
}); });
// on_action(cx, Editor::rename); todo!() register_action(cx, |editor, action, cx| {
// on_action(cx, Editor::confirm_rename); todo!() editor
.rename(action, cx)
.map(|task| task.detach_and_log_err(cx));
});
register_action(cx, |editor, action, cx| {
editor
.confirm_rename(action, cx)
.map(|task| task.detach_and_log_err(cx));
});
register_action(cx, |editor, action, cx| { register_action(cx, |editor, action, cx| {
editor editor
.find_all_references(action, cx) .find_all_references(action, cx)