Skip to content

Commit

Permalink
Refactor helper functions to crate level
Browse files Browse the repository at this point in the history
  • Loading branch information
stfacc committed Nov 1, 2023
1 parent 80f07da commit 7cd131d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
23 changes: 2 additions & 21 deletions src/hinter/cwd_aware.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::{
hinter::get_first_token,
history::SearchQuery,
result::{ReedlineError, ReedlineErrorVariants::HistoryFeatureUnsupported},
Hinter, History,
};
use nu_ansi_term::{Color, Style};
use unicode_segmentation::UnicodeSegmentation;

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

/// A hinter that uses the completions or the history to show a hint to the user
///
Expand Down Expand Up @@ -68,22 +64,7 @@ impl Hinter for CwdAwareHinter {
}

fn next_hint_token(&self) -> String {
let mut reached_content = false;
let result: String = self
.current_hint
.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
get_first_token(&self.current_hint)

Check warning on line 67 in src/hinter/cwd_aware.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/cwd_aware.rs#L67

Added line #L67 was not covered by tests
}
}

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

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

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

fn next_hint_token(&self) -> String {
let mut reached_content = false;
let result: String = self
.current_hint
.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
get_first_token(&self.current_hint)

Check warning on line 50 in src/hinter/default.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/default.rs#L50

Added line #L50 was not covered by tests
}
}

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)
}

Check warning on line 10 in src/hinter/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/mod.rs#L8-L10

Added lines #L8 - L10 were not covered by tests

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,

Check warning on line 18 in src/hinter/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/mod.rs#L12-L18

Added lines #L12 - L18 were not covered by tests
(false, false) => {
reached_content = true;
true

Check warning on line 21 in src/hinter/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/mod.rs#L20-L21

Added lines #L20 - L21 were not covered by tests
}
})
.collect::<Vec<&str>>()
.join("")
.to_string();
result
}

Check warning on line 28 in src/hinter/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/mod.rs#L23-L28

Added lines #L23 - L28 were not covered by tests

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

0 comments on commit 7cd131d

Please sign in to comment.