Fix clippy style and remove unecessary stuff (#192)

- remove unused members, found after update to Rust 1.57
- semicolon termination in void blocks
- other clippy pedantic prompted fixes (e.g. docstrings, unnecessary borrows)
This commit is contained in:
Stefan Holderbach 2021-12-02 23:22:12 +01:00 committed by GitHub
parent 2d34d100bd
commit a44a590b25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 79 additions and 92 deletions

View File

@ -13,6 +13,9 @@ pub struct Span {
impl Span {
/// Creates a new `Span` from start and end inputs.
/// The end parameter must be greater than or equal to the start parameter.
///
/// # Panics
/// If `end < start`
pub fn new(start: usize, end: usize) -> Span {
assert!(
end >= start,

View File

@ -29,7 +29,6 @@ use crate::{Completer, Span};
#[derive(Debug, Clone)]
pub struct DefaultCompleter {
root: CompletionNode,
inclusions: Rc<BTreeSet<char>>,
min_word_len: usize,
}
@ -37,8 +36,7 @@ impl Default for DefaultCompleter {
fn default() -> Self {
let inclusions = Rc::new(BTreeSet::new());
Self {
root: CompletionNode::new(inclusions.clone()),
inclusions,
root: CompletionNode::new(inclusions),
min_word_len: 2,
}
}
@ -131,11 +129,11 @@ impl DefaultCompleter {
dc
}
/// Insert external_commands list in the object root
/// Insert `external_commands` list in the object root
///
/// # Arguments
///
/// * `line` A vector of String containing the external commands
/// * `line` A vector of `String` containing the external commands
///
/// # Example
/// ```
@ -158,8 +156,8 @@ impl DefaultCompleter {
}
}
/// Create a new DefaultCompleter with provided non alphabet characters whitelisted.
/// The default DefaultCompleter will only parse alphabet characters (a-z, A-Z). Use this to
/// Create a new `DefaultCompleter` with provided non alphabet characters whitelisted.
/// The default `DefaultCompleter` will only parse alphabet characters (a-z, A-Z). Use this to
/// introduce additional accepted special characters.
///
/// # Arguments
@ -187,13 +185,10 @@ impl DefaultCompleter {
/// ```
pub fn with_inclusions(incl: &[char]) -> Self {
let mut set = BTreeSet::new();
incl.iter().for_each(|c| {
set.insert(*c);
});
set.extend(incl.iter());
let inclusions = Rc::new(set);
Self {
root: CompletionNode::new(inclusions.clone()),
inclusions,
root: CompletionNode::new(inclusions),
..Self::default()
}
}
@ -293,7 +288,7 @@ impl CompletionNode {
}
fn word_count(&self) -> u32 {
let mut count = self.subnodes.values().map(|n| n.word_count()).sum();
let mut count = self.subnodes.values().map(CompletionNode::word_count).sum();
if self.leaf {
count += 1;
}
@ -303,7 +298,7 @@ impl CompletionNode {
fn subnode_count(&self) -> u32 {
self.subnodes
.values()
.map(|n| n.subnode_count())
.map(CompletionNode::subnode_count)
.sum::<u32>()
+ 1
}
@ -333,21 +328,21 @@ impl CompletionNode {
None
}
} else {
Some(self.collect("".to_string()))
Some(self.collect(""))
}
}
fn collect(&self, partial: String) -> Vec<String> {
fn collect(&self, partial: &str) -> Vec<String> {
let mut completions = vec![];
if self.leaf {
completions.push(partial.clone());
completions.push(partial.to_string());
}
if !self.subnodes.is_empty() {
for (c, node) in &self.subnodes {
let mut partial = partial.clone();
let mut partial = partial.to_string();
partial.push(*c);
completions.append(&mut node.collect(partial));
completions.append(&mut node.collect(&partial));
}
}
completions

View File

@ -33,19 +33,19 @@ impl Editor {
}
pub fn move_to_start(&mut self) {
self.line_buffer.move_to_start()
self.line_buffer.move_to_start();
}
pub fn move_to_end(&mut self) {
self.line_buffer.move_to_end()
self.line_buffer.move_to_end();
}
pub fn move_left(&mut self) {
self.line_buffer.move_left()
self.line_buffer.move_left();
}
pub fn move_right(&mut self) {
self.line_buffer.move_right()
self.line_buffer.move_right();
}
pub fn move_word_left(&mut self) {
@ -65,7 +65,7 @@ impl Editor {
}
pub fn insert_char(&mut self, c: char) {
self.line_buffer.insert_char(c)
self.line_buffer.insert_char(c);
}
pub fn backspace(&mut self) {
@ -109,7 +109,7 @@ impl Editor {
}
pub fn set_insertion_point(&mut self, pos: usize) {
self.line_buffer.set_insertion_point(pos)
self.line_buffer.set_insertion_point(pos);
}
pub fn get_buffer(&self) -> &str {
@ -117,22 +117,22 @@ impl Editor {
}
pub fn set_buffer(&mut self, buffer: String) {
self.line_buffer.set_buffer(buffer)
self.line_buffer.set_buffer(buffer);
}
pub fn clear_to_end(&mut self) {
self.line_buffer.clear_to_end()
self.line_buffer.clear_to_end();
}
pub fn clear_to_insertion_point(&mut self) {
self.line_buffer.clear_to_insertion_point()
self.line_buffer.clear_to_insertion_point();
}
pub fn clear_range<R>(&mut self, range: R)
where
R: std::ops::RangeBounds<usize>,
{
self.line_buffer.clear_range(range)
self.line_buffer.clear_range(range);
}
pub fn offset(&self) -> usize {

View File

@ -306,7 +306,7 @@ impl LineBuffer {
let insertion_offset = self.insertion_point().offset;
if left_index < insertion_offset {
self.clear_range(left_index..insertion_offset);
self.insertion_point.offset = left_index
self.insertion_point.offset = left_index;
}
}
@ -353,9 +353,9 @@ impl LineBuffer {
let initial_offset = self.insertion_point().offset;
if initial_offset == 0 {
self.move_right()
self.move_right();
} else if initial_offset == self.get_buffer().len() {
self.move_left()
self.move_left();
}
let updated_offset = self.insertion_point().offset;
@ -399,7 +399,7 @@ impl LineBuffer {
self.set_insertion_point(position);
// Move right from this position to the column we were at
while &buffer[position..(position + 1)] != "\n" && num_of_move_lefts > 0 {
while &buffer[position..=position] != "\n" && num_of_move_lefts > 0 {
self.move_right();
position = self.offset();
num_of_move_lefts -= 1;
@ -436,7 +436,7 @@ impl LineBuffer {
// Move right from this position to the column we were at
while position < buffer.len()
&& &buffer[position..(position + 1)] != "\n"
&& &buffer[position..=position] != "\n"
&& num_of_move_lefts > 0
{
self.move_right();

View File

@ -7,7 +7,7 @@ use crate::{enums::ReedlineEvent, PromptEditMode};
/// - Emacs
/// - Vi
pub trait EditMode {
/// Translate the given user input event into what the LineEditor understands
/// Translate the given user input event into what the `LineEditor` understands
fn parse_event(&mut self, event: Event) -> ReedlineEvent;
/// What to display in the prompt indicator

View File

@ -1,3 +1,5 @@
use std::borrow::Borrow;
use {
crate::{
completion::{CircularCompletionHandler, CompletionActionHandler},
@ -33,20 +35,12 @@ enum InputMode {
HistoryTraversal,
}
#[derive(Default)]
struct PromptWidget {
offset: (u16, u16),
origin: (u16, u16),
}
impl Default for PromptWidget {
fn default() -> Self {
PromptWidget {
offset: Default::default(),
origin: Default::default(),
}
}
}
impl PromptWidget {
fn offset_columns(&self) -> u16 {
self.offset.0
@ -121,7 +115,7 @@ impl Reedline {
let terminal_size = terminal::size()?;
// Note: this is started with a garbage value
let prompt_widget = Default::default();
let prompt_widget = PromptWidget::default();
let edit_mode = Box::new(Emacs::default());
@ -505,7 +499,7 @@ impl Reedline {
// Note: you can't just subtract the offset from the origin,
// as we could be shrinking so fast that the offset we read back from
// crossterm is past where it would have been.
self.set_prompt_origin((current_origin.0, height - 2))
self.set_prompt_origin((current_origin.0, height - 2));
} else if prev_terminal_size.1 < height {
// Terminal is growing down, so move the prompt down the same amount to make space
// for history that's on the screen
@ -514,7 +508,7 @@ impl Reedline {
self.set_prompt_origin((
current_origin.0,
current_origin.1 + (height - prev_terminal_size.1),
))
));
}
let prompt_offset = self.full_repaint(prompt, self.prompt_widget.origin)?;
@ -562,8 +556,8 @@ impl Reedline {
) -> io::Result<Option<Signal>> {
match event {
ReedlineEvent::HandleTab => {
let mut line_buffer = self.editor.line_buffer();
self.tab_handler.handle(&mut line_buffer);
let line_buffer = self.editor.line_buffer();
self.tab_handler.handle(line_buffer);
let (prompt_origin_column, prompt_origin_row) = self.prompt_widget.origin;
@ -729,7 +723,7 @@ impl Reedline {
self.history
.set_navigation(HistoryNavigationQuery::SubstringSearch(String::from(
*c,
)))
)));
}
self.history.back();
}
@ -743,7 +737,7 @@ impl Reedline {
.set_navigation(HistoryNavigationQuery::SubstringSearch(
new_substring.to_string(),
));
self.history.back()
self.history.back();
}
}
_ => {
@ -762,10 +756,10 @@ impl Reedline {
HistoryNavigationQuery::Normal(original) => {
if let Some(buffer_to_paint) = self.history.string_at_cursor() {
self.editor.set_buffer(buffer_to_paint.clone());
self.set_offset(buffer_to_paint.len())
self.set_offset(buffer_to_paint.len());
} else {
// Hack
self.editor.set_line_buffer(original)
self.editor.set_line_buffer(original);
}
}
HistoryNavigationQuery::PrefixSearch(prefix) => {
@ -793,7 +787,7 @@ impl Reedline {
HistoryNavigationQuery::Normal(_)
) {
if let Some(string) = self.history.string_at_cursor() {
self.editor.set_buffer(string)
self.editor.set_buffer(string);
}
}
self.input_mode = InputMode::Regular;
@ -857,7 +851,7 @@ impl Reedline {
/// Set the cursor position as understood by the underlying [`LineBuffer`] for the current line
fn set_offset(&mut self, pos: usize) {
self.editor.set_insertion_point(pos)
self.editor.set_insertion_point(pos);
}
fn terminal_columns(&self) -> u16 {
@ -884,7 +878,7 @@ impl Reedline {
// If we're at the top, move to previous history
self.next_history();
} else {
self.editor.move_line_down()
self.editor.move_line_down();
}
}
@ -1031,7 +1025,7 @@ impl Reedline {
.highlight(buffer_to_paint)
.render_around_insertion_point(
cursor_position_in_buffer,
prompt.render_prompt_multiline_indicator(),
prompt.render_prompt_multiline_indicator().borrow(),
);
let hint: String = self.hinter.handle(
buffer_to_paint,
@ -1064,7 +1058,7 @@ impl Reedline {
.highlight(buffer_to_paint)
.render_around_insertion_point(
cursor_position_in_buffer,
prompt.render_prompt_multiline_indicator(),
prompt.render_prompt_multiline_indicator().borrow(),
);
let hint: String = self.hinter.handle(
buffer_to_paint,

View File

@ -7,9 +7,9 @@ pub static DEFAULT_BUFFER_NEUTRAL_COLOR: Color = Color::White;
pub static DEFAULT_BUFFER_NOTMATCH_COLOR: Color = Color::Red;
/// The syntax highlighting trait. Implementers of this trait will take in the current string and then
/// return a StyledText object, which represents the contents of the original line as styled strings
/// return a `StyledText` object, which represents the contents of the original line as styled strings
pub trait Highlighter {
/// The action that will handle the current buffer as a line and return the corresponding StyleText for the buffer
/// The action that will handle the current buffer as a line and return the corresponding `StyledText` for the buffer
fn highlight(&self, line: &str) -> StyledText;
}

View File

@ -59,7 +59,7 @@ impl History for FileBackedHistory {
}
self.entries.push_back(entry);
}
self.reset_cursor()
self.reset_cursor();
}
fn iter_chronologic(&self) -> Iter<'_, String> {
@ -74,10 +74,10 @@ impl History for FileBackedHistory {
}
}
HistoryNavigationQuery::PrefixSearch(prefix) => {
self.back_with_criteria(&|entry| entry.starts_with(&prefix))
self.back_with_criteria(&|entry| entry.starts_with(&prefix));
}
HistoryNavigationQuery::SubstringSearch(substring) => {
self.back_with_criteria(&|entry| entry.contains(&substring))
self.back_with_criteria(&|entry| entry.contains(&substring));
}
}
}
@ -90,10 +90,10 @@ impl History for FileBackedHistory {
}
}
HistoryNavigationQuery::PrefixSearch(prefix) => {
self.forward_with_criteria(&|entry| entry.starts_with(&prefix))
self.forward_with_criteria(&|entry| entry.starts_with(&prefix));
}
HistoryNavigationQuery::SubstringSearch(substring) => {
self.forward_with_criteria(&|entry| entry.contains(&substring))
self.forward_with_criteria(&|entry| entry.contains(&substring));
}
}
}
@ -114,6 +114,10 @@ impl History for FileBackedHistory {
impl FileBackedHistory {
/// Creates a new in-memory history that remembers `n <= capacity` elements
///
/// # Panics
///
/// If `capacity == usize::MAX`
pub fn new(capacity: usize) -> Self {
if capacity == usize::MAX {
panic!("History capacity too large to be addressed safely");
@ -199,7 +203,7 @@ impl FileBackedHistory {
.find(|(_, entry)| criteria(entry) && previous_match != Some(entry))
{
// set to entry
self.cursor = next_cursor
self.cursor = next_cursor;
}
}
}
@ -214,9 +218,9 @@ impl FileBackedHistory {
.find(|(_, entry)| criteria(entry) && previous_match != Some(entry))
{
// set to entry
self.cursor = next_cursor
self.cursor = next_cursor;
} else {
self.reset_cursor()
self.reset_cursor();
}
}
@ -262,7 +266,7 @@ impl FileBackedHistory {
impl Drop for FileBackedHistory {
/// On drop the content of the [`History`] will be written to the file if specified via [`FileBackedHistory::with_file()`].
fn drop(&mut self) {
let _ = self.flush();
let _res = self.flush();
}
}

View File

@ -98,14 +98,15 @@ impl Prompt for DefaultPrompt {
fn render_prompt_indicator(&self, edit_mode: PromptEditMode) -> Cow<str> {
match edit_mode {
PromptEditMode::Default => DEFAULT_PROMPT_INDICATOR.into(),
PromptEditMode::Emacs => DEFAULT_PROMPT_INDICATOR.into(),
PromptEditMode::Default | PromptEditMode::Emacs => DEFAULT_PROMPT_INDICATOR.into(),
PromptEditMode::Vi(vi_mode) => match vi_mode {
PromptViMode::Normal => DEFAULT_PROMPT_INDICATOR.into(),
PromptViMode::Insert => DEFAULT_VI_INSERT_PROMPT_INDICATOR.into(),
PromptViMode::Visual => DEFAULT_VI_VISUAL_PROMPT_INDICATOR.into(),
},
PromptEditMode::Custom(str) => self.default_wrapped_custom_string(str).into(),
PromptEditMode::Custom(str) => {
DefaultPrompt::default_wrapped_custom_string(&str).into()
}
}
}
@ -178,7 +179,7 @@ impl DefaultPrompt {
Cow::Owned(prompt_str)
}
fn default_wrapped_custom_string(&self, str: String) -> String {
fn default_wrapped_custom_string(str: &str) -> String {
format!("({})", str)
}
}

View File

@ -1,5 +1,3 @@
use std::borrow::{Borrow, Cow};
use nu_ansi_term::{Color, Style};
/// A representation of a buffer with styling, used for doing syntax highlighting
@ -14,7 +12,7 @@ impl Default for StyledText {
}
impl StyledText {
/// Construct a new StyledText
/// Construct a new `StyledText`
pub fn new() -> Self {
Self { buffer: vec![] }
}
@ -32,7 +30,7 @@ impl StyledText {
pub fn render_around_insertion_point(
&self,
insertion_point: usize,
multiline_prompt: Cow<str>,
multiline_prompt: &str,
) -> (String, String) {
let mut current_idx = 0;
let mut left_string = String::new();
@ -40,17 +38,9 @@ impl StyledText {
let prompt_style = Style::new().fg(Color::LightBlue);
for pair in &self.buffer {
if current_idx >= insertion_point {
right_string.push_str(&render_as_string(
pair,
&prompt_style,
multiline_prompt.borrow(),
));
right_string.push_str(&render_as_string(pair, &prompt_style, multiline_prompt));
} else if pair.1.len() + current_idx <= insertion_point {
left_string.push_str(&render_as_string(
pair,
&prompt_style,
multiline_prompt.borrow(),
));
left_string.push_str(&render_as_string(pair, &prompt_style, multiline_prompt));
} else if pair.1.len() + current_idx > insertion_point {
let offset = insertion_point - current_idx;
@ -60,12 +50,12 @@ impl StyledText {
left_string.push_str(&render_as_string(
&(pair.0, left_side),
&prompt_style,
multiline_prompt.borrow(),
multiline_prompt,
));
right_string.push_str(&render_as_string(
&(pair.0, right_side),
&prompt_style,
multiline_prompt.borrow(),
multiline_prompt,
));
}
current_idx += pair.1.len();

View File

@ -32,11 +32,11 @@ fn incomplete_brackets(line: &str) -> bool {
for c in line.chars() {
if c == '{' {
balance.push('}')
balance.push('}');
} else if c == '[' {
balance.push(']')
balance.push(']');
} else if c == '(' {
balance.push(')')
balance.push(')');
} else if ['}', ']', ')'].contains(&c) {
if let Some(last) = balance.last() {
if last == &c {