feat: implement basic callback functions for prompt operarions

pull/12902/head
Nikita Revenco 2025-02-17 23:08:17 +00:00
parent 7dc631de9a
commit 2eef82e4df
4 changed files with 47 additions and 11 deletions

View File

@ -15,6 +15,7 @@ pub enum EventResult {
Consumed(Option<Callback>), Consumed(Option<Callback>),
} }
use crate::commands;
use crate::job::Jobs; use crate::job::Jobs;
use crate::ui::picker; use crate::ui::picker;
use helix_view::Editor; use helix_view::Editor;

View File

@ -285,6 +285,40 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
type FileExplorer = Picker<(PathBuf, bool), (PathBuf, Style)>; type FileExplorer = Picker<(PathBuf, bool), (PathBuf, Style)>;
fn create_file_operation_prompt(
prompt: &'static str,
cx: &mut Context,
path: &Path,
callback: fn(&Path, &str),
) {
cx.editor.path_editing = Some(path.to_path_buf());
let callback = Box::pin(async move {
let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| {
// let path = path.clone();
let mut prompt = Prompt::new(
prompt.into(),
None,
crate::ui::completers::none,
move |_cx, input: &str, event: PromptEvent| {
if event != PromptEvent::Validate {
return;
};
callback(path, input);
},
);
if let Some(path_editing) = &editor.path_editing {
prompt.set_line_no_recalculate(path_editing.display().to_string());
}
compositor.push(Box::new(prompt));
}));
Ok(call)
});
cx.jobs.callback(callback);
}
pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std::io::Error> { pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std::io::Error> {
let directory_style = editor.theme.get("ui.text.directory"); let directory_style = editor.theme.get("ui.text.directory");
let directory_content = directory_content(&root)?; let directory_content = directory_content(&root)?;
@ -336,26 +370,19 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|cx, (path, _is_dir): &(PathBuf, bool)|, |cx, (path, _is_dir): &(PathBuf, bool)|,
// create // create
alt!('c') => { alt!('c') => {
// TODO: ask user for name of file to be created create_file_operation_prompt("create:", cx, path, |path, input| ())
// Fill in with the picker's current directory
log::error!("create file");
}, },
// move // move
alt!('m') => { alt!('m') => {
// TODO: ask the user for new name of file create_file_operation_prompt("move:", cx, path, |path, input| ())
// on enter move the file to the new location
log::error!("rename file");
}, },
// delete // delete
alt!('d') => { alt!('d') => {
// TODO: ask user for confirmation, while showing which file create_file_operation_prompt("delete? (y/n):", cx, path, |path, input| ())
// will be deleted
log::error!("delete file");
}, },
// copy // copy
alt!('y') => { alt!('y') => {
// TODO: ask the user for new name of file create_file_operation_prompt("copy-to:", cx, path, |path, input| ())
log::error!("copy file");
}, },
}); });

View File

@ -122,6 +122,12 @@ impl Prompt {
self.recalculate_completion(editor); self.recalculate_completion(editor);
} }
pub fn set_line_no_recalculate(&mut self, line: String) {
let cursor = line.len();
self.line = line;
self.cursor = cursor;
}
pub fn with_language( pub fn with_language(
mut self, mut self,
language: &'static str, language: &'static str,

View File

@ -1035,6 +1035,7 @@ pub struct Editor {
pub tree: Tree, pub tree: Tree,
pub next_document_id: DocumentId, pub next_document_id: DocumentId,
pub documents: BTreeMap<DocumentId, Document>, pub documents: BTreeMap<DocumentId, Document>,
pub path_editing: Option<PathBuf>,
// We Flatten<> to resolve the inner DocumentSavedEventFuture. For that we need a stream of streams, hence the Once<>. // We Flatten<> to resolve the inner DocumentSavedEventFuture. For that we need a stream of streams, hence the Once<>.
// https://stackoverflow.com/a/66875668 // https://stackoverflow.com/a/66875668
@ -1223,6 +1224,7 @@ impl Editor {
handlers, handlers,
mouse_down_range: None, mouse_down_range: None,
cursor_cache: CursorCache::default(), cursor_cache: CursorCache::default(),
path_editing: None,
} }
} }