Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,33 @@ use reedline::{Completer, Suggestion};
use tracing::info;

use crate::completer::search_term::SearchTerm;
use crate::completer::CommandCompleter;

#[derive(Clone)]
pub struct FileCompleter {
pub struct InputCompleter {
walker: Walker,
}

impl FileCompleter {
impl InputCompleter {
pub fn new(cwd: PathBuf) -> Self {
let walker = Walker::max_all().cwd(cwd).skip_binary(true);
Self { walker }
}
}

impl Completer for FileCompleter {
impl Completer for InputCompleter {
fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
info!("Completing line: '{}' pos: {}", line, pos);

if line.starts_with("/") {
// if the line starts with '/' it's probably a command, so we delegate to the
// command completer.
let result = CommandCompleter.complete(line, pos);
if !result.is_empty() {
return result;
}
}

if let Some(query) = SearchTerm::new(line, pos).process() {
info!("Search term: {:?}", query);

Expand Down
4 changes: 2 additions & 2 deletions crates/forge_main/src/completer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod command;
mod file;
mod input_completer;
mod search_term;

pub use command::CommandCompleter;
pub use file::FileCompleter;
pub use input_completer::InputCompleter;
37 changes: 5 additions & 32 deletions crates/forge_main/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use reedline::{
KeyCode, KeyModifiers, MenuBuilder, Prompt, Reedline, ReedlineEvent, ReedlineMenu, Signal,
};

use super::completer::{CommandCompleter, FileCompleter};
use super::completer::InputCompleter;

// TODO: Store the last `HISTORY_CAPACITY` commands in the history file
const HISTORY_CAPACITY: usize = 1024;
const FILE_COMPLETION_MENU: &str = "file_completion_menu";
const COMMAND_COMPLETION_MENU: &str = "command_completion_menu";
const COMPLETION_MENU: &str = "completion_menu";

pub struct ForgeEditor {
editor: Reedline,
Expand All @@ -32,19 +31,11 @@ impl ForgeEditor {
KeyModifiers::NONE,
KeyCode::Tab,
ReedlineEvent::UntilFound(vec![
ReedlineEvent::HistoryHintComplete,
ReedlineEvent::Menu(FILE_COMPLETION_MENU.to_string()),
ReedlineEvent::Menu(COMPLETION_MENU.to_string()),
ReedlineEvent::Edit(vec![EditCommand::Complete]),
]),
);

// on SHIFT + '@' press to start the file completion.
keybindings.add_binding(
KeyModifiers::SHIFT,
KeyCode::Char('@'),
ReedlineEvent::Menu(FILE_COMPLETION_MENU.to_owned()),
);

// on CTRL + k press clears the screen
keybindings.add_binding(
KeyModifiers::CONTROL,
Expand All @@ -66,12 +57,6 @@ impl ForgeEditor {
ReedlineEvent::Edit(vec![EditCommand::InsertNewline]),
);

keybindings.add_binding(
KeyModifiers::NONE,
KeyCode::Esc,
ReedlineEvent::Menu(COMMAND_COMPLETION_MENU.to_string()),
);

keybindings
}

Expand All @@ -84,15 +69,7 @@ impl ForgeEditor {
);
let completion_menu = Box::new(
ColumnarMenu::default()
.with_name(FILE_COMPLETION_MENU)
.with_marker("")
.with_text_style(Style::new().bold().fg(Color::Cyan))
.with_selected_text_style(Style::new().on(Color::White).fg(Color::Black)),
);

let commands_menu = Box::new(
ColumnarMenu::default()
.with_name(COMMAND_COMPLETION_MENU)
.with_name(COMPLETION_MENU)
.with_marker("")
.with_text_style(Style::new().bold().fg(Color::Cyan))
.with_selected_text_style(Style::new().on(Color::White).fg(Color::Black)),
Expand All @@ -101,16 +78,12 @@ impl ForgeEditor {
let edit_mode = Box::new(Emacs::new(Self::init()));

let editor = Reedline::create()
.with_completer(Box::new(FileCompleter::new(env.cwd)))
.with_completer(Box::new(InputCompleter::new(env.cwd)))
.with_history(history)
.with_hinter(Box::new(
DefaultHinter::default().with_style(Style::new().fg(Color::DarkGray)),
))
.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_menu(ReedlineMenu::WithCompleter {
menu: commands_menu,
completer: Box::new(CommandCompleter),
})
.with_edit_mode(edit_mode)
.with_quick_completions(true)
.with_partial_completions(true)
Expand Down