Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split hinter tokens at Unicode word boundaries #650

Merged
merged 2 commits into from
Nov 1, 2023
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
17 changes: 2 additions & 15 deletions src/hinter/cwd_aware.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
hinter::get_first_token,
history::SearchQuery,
result::{ReedlineError, ReedlineErrorVariants::HistoryFeatureUnsupported},
Hinter, History,
Expand Down Expand Up @@ -63,21 +64,7 @@ impl Hinter for CwdAwareHinter {
}

fn next_hint_token(&self) -> String {
let mut reached_content = false;
let result: String = self
.current_hint
.chars()
.take_while(|c| match (c.is_whitespace(), reached_content) {
(true, true) => false,
(true, false) => true,
(false, true) => true,
(false, false) => {
reached_content = true;
true
}
})
.collect();
result
get_first_token(&self.current_hint)
}
}

Expand Down
18 changes: 2 additions & 16 deletions src/hinter/default.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{history::SearchQuery, Hinter, History};
use crate::{hinter::get_first_token, history::SearchQuery, Hinter, History};
use nu_ansi_term::{Color, Style};

/// A hinter that uses the completions or the history to show a hint to the user
Expand Down Expand Up @@ -47,21 +47,7 @@ impl Hinter for DefaultHinter {
}

fn next_hint_token(&self) -> String {
let mut reached_content = false;
let result: String = self
.current_hint
.chars()
.take_while(|c| match (c.is_whitespace(), reached_content) {
(true, true) => false,
(true, false) => true,
(false, true) => true,
(false, false) => {
reached_content = true;
true
}
})
.collect();
result
get_first_token(&self.current_hint)
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/hinter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ mod default;
pub use cwd_aware::CwdAwareHinter;
pub use default::DefaultHinter;

use unicode_segmentation::UnicodeSegmentation;

pub fn is_whitespace_str(s: &str) -> bool {
s.chars().all(char::is_whitespace)
}

pub fn get_first_token(string: &str) -> String {
let mut reached_content = false;
let result = string
.split_word_bounds()
.take_while(|word| match (is_whitespace_str(word), reached_content) {
(_, true) => false,
(true, false) => true,
(false, false) => {
reached_content = true;
true
}
})
.collect::<Vec<&str>>()
.join("")
.to_string();
result
}

use crate::History;
/// A trait that's responsible for returning the hint for the current line and position
/// Hints are often shown in-line as part of the buffer, showing the user text they can accept or ignore
Expand Down