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

added partial solution #12

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
24 changes: 15 additions & 9 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use std::fmt::{Display, Write as _};
use std::io::{self, BufRead, BufReader, ErrorKind, Read, Seek, Write};
use std::iter::{self, repeat, successors};
use std::{fs::File, path::Path, process::Command, thread, time::Instant};
use std::{cell::RefCell, fs::File, path::Path, process::Command, thread, time::Instant};

use unicode_width::UnicodeWidthStr;

use crate::row::{HlState, Row};
use crate::{ansi_escape::*, syntax::Conf as SyntaxConf, sys, terminal, Config, Error};
Expand All @@ -28,9 +30,9 @@

/// `set_status!` sets a formatted status message for the editor.
/// Example usage: `set_status!(editor, "{} written to {}", file_size, file_name)`
macro_rules! set_status {
($editor:expr, $($arg:expr),*) => ($editor.status_msg = Some(StatusMessage::new(format!($($arg),*))))
}
macro_rules! set_status { ($editor:expr, $($arg:expr),*) => ($editor.status_msg = Some(StatusMessage::new(format!($($arg),*)))) }
// `width!` returns the display width of a string, plus one for the cursor
fn dsp_width(msg: &String) -> usize { UnicodeWidthStr::width(msg.as_str()) + 1 }

Check failure on line 35 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, x86_64-unknown-linux-gnu)

[clippy-x86_64-unknown-linux-gnu] reported by reviewdog 🐶 <pre><code>error: writing `&String` instead of `&str` involves a new object where a slice will do --> src/editor.rs:35:19 | 35 | fn dsp_width(msg: &String) -> usize { UnicodeWidthStr::width(msg.as_str()) + 1 } | ^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg = note: `-D clippy::ptr-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::ptr_arg)]` help: change this to | 35 | fn dsp_width(msg: &str) -> usize { UnicodeWidthStr::width(msg) + 1 } | ~~~~ ~~~ </code></pre> Raw Output: src/editor.rs:35:19:e: <pre><code>error: writing `&String` instead of `&str` involves a new object where a slice will do --> src/editor.rs:35:19 | 35 | fn dsp_width(msg: &String) -> usize { UnicodeWidthStr::width(msg.as_str()) + 1 } | ^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg = note: `-D clippy::ptr-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::ptr_arg)]` help: change this to | 35 | fn dsp_width(msg: &str) -> usize { UnicodeWidthStr::width(msg) + 1 } | ~~~~ ~~~ </code></pre> __END__

Check failure on line 35 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (macos-latest, x86_64-apple-darwin)

[clippy-x86_64-apple-darwin] reported by reviewdog 🐶 <pre><code>error: writing `&String` instead of `&str` involves a new object where a slice will do --> src/editor.rs:35:19 | 35 | fn dsp_width(msg: &String) -> usize { UnicodeWidthStr::width(msg.as_str()) + 1 } | ^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg = note: `-D clippy::ptr-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::ptr_arg)]` help: change this to | 35 | fn dsp_width(msg: &str) -> usize { UnicodeWidthStr::width(msg) + 1 } | ~~~~ ~~~ </code></pre> Raw Output: src/editor.rs:35:19:e: <pre><code>error: writing `&String` instead of `&str` involves a new object where a slice will do --> src/editor.rs:35:19 | 35 | fn dsp_width(msg: &String) -> usize { UnicodeWidthStr::width(msg.as_str()) + 1 } | ^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg = note: `-D clippy::ptr-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::ptr_arg)]` help: change this to | 35 | fn dsp_width(msg: &str) -> usize { UnicodeWidthStr::width(msg) + 1 } | ~~~~ ~~~ </code></pre> __END__

Check failure on line 35 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, wasm32-wasi)

[clippy-wasm32-wasi] reported by reviewdog 🐶 <pre><code>error: writing `&String` instead of `&str` involves a new object where a slice will do --> src/editor.rs:35:19 | 35 | fn dsp_width(msg: &String) -> usize { UnicodeWidthStr::width(msg.as_str()) + 1 } | ^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg = note: `-D clippy::ptr-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::ptr_arg)]` help: change this to | 35 | fn dsp_width(msg: &str) -> usize { UnicodeWidthStr::width(msg) + 1 } | ~~~~ ~~~ </code></pre> Raw Output: src/editor.rs:35:19:e: <pre><code>error: writing `&String` instead of `&str` involves a new object where a slice will do --> src/editor.rs:35:19 | 35 | fn dsp_width(msg: &String) -> usize { UnicodeWidthStr::width(msg.as_str()) + 1 } | ^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg = note: `-D clippy::ptr-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::ptr_arg)]` help: change this to | 35 | fn dsp_width(msg: &str) -> usize { UnicodeWidthStr::width(msg) + 1 } | ~~~~ ~~~ </code></pre> __END__

/// Enum of input keys
enum Key {
Expand Down Expand Up @@ -593,7 +595,7 @@
(self.rx() - self.cursor.coff + 1 + self.ln_pad, self.cursor.y - self.cursor.roff + 1)
} else {
// If in prompt mode, position the cursor on the prompt line at the end of the line.
(self.status_msg.as_ref().map_or(0, |sm| sm.msg.len() + 1), self.screen_rows + 2)
(self.status_msg.as_ref().map_or(0, |s| dsp_width(&s.msg)), self.screen_rows + 2)
};
// Finally, print `buffer` and move the cursor
print!("{buffer}\x1b[{cursor_y};{cursor_x}H{SHOW_CURSOR}");
Expand Down Expand Up @@ -678,7 +680,7 @@
// will be updated in self.cursor.scroll() so that the result is visible
(self.cursor.x, self.cursor.y, self.cursor.coff) = (cx, current, 0);
let rx = row.cx2rx[cx];
row.match_segment = Some(rx..rx + query.len());
row.match_segment = Some(rx..rx + UnicodeWidthStr::width(query));
return Some(current);
}
}
Expand Down Expand Up @@ -837,18 +839,22 @@
Cancelled,
}

thread_local! (static CHARACTER: RefCell<Vec<u8>> = {let cache = Vec::new(); RefCell::new(cache)});

Check failure on line 842 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, x86_64-unknown-linux-gnu)

[clippy-x86_64-unknown-linux-gnu] reported by reviewdog 🐶 <pre><code>error: initializer for `thread_local` value can be made `const` --> src/editor.rs:842:53 | 842 | thread_local! (static CHARACTER: RefCell<Vec<u8>> = {let cache = Vec::new(); RefCell::new(cache)}); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { {let cache = Vec::new(); RefCell::new(cache)} }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#thread_local_initializer_can_be_made_const = note: `-D clippy::thread-local-initializer-can-be-made-const` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::thread_local_initializer_can_be_made_const)]` </code></pre> Raw Output: src/editor.rs:842:53:e: <pre><code>error: initializer for `thread_local` value can be made `const` --> src/editor.rs:842:53 | 842 | thread_local! (static CHARACTER: RefCell<Vec<u8>> = {let cache = Vec::new(); RefCell::new(cache)}); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { {let cache = Vec::new(); RefCell::new(cache)} }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#thread_local_initializer_can_be_made_const = note: `-D clippy::thread-local-initializer-can-be-made-const` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::thread_local_initializer_can_be_made_const)]` </code></pre> __END__

Check failure on line 842 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (macos-latest, x86_64-apple-darwin)

[clippy-x86_64-apple-darwin] reported by reviewdog 🐶 <pre><code>error: initializer for `thread_local` value can be made `const` --> src/editor.rs:842:53 | 842 | thread_local! (static CHARACTER: RefCell<Vec<u8>> = {let cache = Vec::new(); RefCell::new(cache)}); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { {let cache = Vec::new(); RefCell::new(cache)} }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#thread_local_initializer_can_be_made_const = note: `-D clippy::thread-local-initializer-can-be-made-const` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::thread_local_initializer_can_be_made_const)]` </code></pre> Raw Output: src/editor.rs:842:53:e: <pre><code>error: initializer for `thread_local` value can be made `const` --> src/editor.rs:842:53 | 842 | thread_local! (static CHARACTER: RefCell<Vec<u8>> = {let cache = Vec::new(); RefCell::new(cache)}); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { {let cache = Vec::new(); RefCell::new(cache)} }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#thread_local_initializer_can_be_made_const = note: `-D clippy::thread-local-initializer-can-be-made-const` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::thread_local_initializer_can_be_made_const)]` </code></pre> __END__

Check failure on line 842 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, wasm32-wasi)

[clippy-wasm32-wasi] reported by reviewdog 🐶 <pre><code>error: initializer for `thread_local` value can be made `const` --> src/editor.rs:842:53 | 842 | thread_local! (static CHARACTER: RefCell<Vec<u8>> = {let cache = Vec::new(); RefCell::new(cache)}); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { {let cache = Vec::new(); RefCell::new(cache)} }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#thread_local_initializer_can_be_made_const = note: `-D clippy::thread-local-initializer-can-be-made-const` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::thread_local_initializer_can_be_made_const)]` </code></pre> Raw Output: src/editor.rs:842:53:e: <pre><code>error: initializer for `thread_local` value can be made `const` --> src/editor.rs:842:53 | 842 | thread_local! (static CHARACTER: RefCell<Vec<u8>> = {let cache = Vec::new(); RefCell::new(cache)}); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { {let cache = Vec::new(); RefCell::new(cache)} }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#thread_local_initializer_can_be_made_const = note: `-D clippy::thread-local-initializer-can-be-made-const` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::thread_local_initializer_can_be_made_const)]` </code></pre> __END__
/// Process a prompt keypress event and return the new state for the prompt.
fn process_prompt_keypress(mut buffer: String, key: &Key) -> PromptState {
match key {
Key::Char(b'\r') => return PromptState::Completed(buffer),
Key::Escape | Key::Char(EXIT) => return PromptState::Cancelled,
Key::Char(BACKSPACE | DELETE_BIS) => {
buffer.pop();
}
Key::Char(BACKSPACE | DELETE_BIS) => _ = buffer.pop(),
Key::Char(c @ 0..=126) if !c.is_ascii_control() => buffer.push(*c as char),
Key::Char(c @ 128..=255) => CHARACTER.with(|cache| cache.borrow_mut().push(*c)),
// No-op
_ => (),
}
let character = CHARACTER.with(|cache| String::from_utf8(cache.borrow_mut().clone()));
character.clone().map_or((), |c| buffer.push_str(c.as_str()));

Check failure on line 855 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, x86_64-unknown-linux-gnu)

[clippy-x86_64-unknown-linux-gnu] reported by reviewdog 🐶 <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:855:5 | 855 | character.clone().map_or((), |c| buffer.push_str(c.as_str())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead = note: `-D unused-must-use` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_must_use)]` help: use `let _ = ...` to ignore the resulting value | 855 | let _ = character.clone().map_or((), |c| buffer.push_str(c.as_str())); | +++++++ </code></pre> Raw Output: src/editor.rs:855:5:e: <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:855:5 | 855 | character.clone().map_or((), |c| buffer.push_str(c.as_str())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead = note: `-D unused-must-use` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_must_use)]` help: use `let _ = ...` to ignore the resulting value | 855 | let _ = character.clone().map_or((), |c| buffer.push_str(c.as_str())); | +++++++ </code></pre> __END__

Check failure on line 855 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (macos-latest, x86_64-apple-darwin)

[clippy-x86_64-apple-darwin] reported by reviewdog 🐶 <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:855:5 | 855 | character.clone().map_or((), |c| buffer.push_str(c.as_str())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead = note: `-D unused-must-use` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_must_use)]` help: use `let _ = ...` to ignore the resulting value | 855 | let _ = character.clone().map_or((), |c| buffer.push_str(c.as_str())); | +++++++ </code></pre> Raw Output: src/editor.rs:855:5:e: <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:855:5 | 855 | character.clone().map_or((), |c| buffer.push_str(c.as_str())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead = note: `-D unused-must-use` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_must_use)]` help: use `let _ = ...` to ignore the resulting value | 855 | let _ = character.clone().map_or((), |c| buffer.push_str(c.as_str())); | +++++++ </code></pre> __END__

Check failure on line 855 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, wasm32-wasi)

[clippy-wasm32-wasi] reported by reviewdog 🐶 <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:855:5 | 855 | character.clone().map_or((), |c| buffer.push_str(c.as_str())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead = note: `-D unused-must-use` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_must_use)]` help: use `let _ = ...` to ignore the resulting value | 855 | let _ = character.clone().map_or((), |c| buffer.push_str(c.as_str())); | +++++++ </code></pre> Raw Output: src/editor.rs:855:5:e: <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:855:5 | 855 | character.clone().map_or((), |c| buffer.push_str(c.as_str())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead = note: `-D unused-must-use` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_must_use)]` help: use `let _ = ...` to ignore the resulting value | 855 | let _ = character.clone().map_or((), |c| buffer.push_str(c.as_str())); | +++++++ </code></pre> __END__
character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear()));

Check failure on line 856 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, x86_64-unknown-linux-gnu)

[clippy-x86_64-unknown-linux-gnu] reported by reviewdog 🐶 <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:856:5 | 856 | character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead help: use `let _ = ...` to ignore the resulting value | 856 | let _ = character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | +++++++ </code></pre> Raw Output: src/editor.rs:856:5:e: <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:856:5 | 856 | character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead help: use `let _ = ...` to ignore the resulting value | 856 | let _ = character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | +++++++ </code></pre> __END__

Check failure on line 856 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (macos-latest, x86_64-apple-darwin)

[clippy-x86_64-apple-darwin] reported by reviewdog 🐶 <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:856:5 | 856 | character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead help: use `let _ = ...` to ignore the resulting value | 856 | let _ = character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | +++++++ </code></pre> Raw Output: src/editor.rs:856:5:e: <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:856:5 | 856 | character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead help: use `let _ = ...` to ignore the resulting value | 856 | let _ = character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | +++++++ </code></pre> __END__

Check failure on line 856 in src/editor.rs

View workflow job for this annotation

GitHub Actions / static_checks (ubuntu-latest, wasm32-wasi)

[clippy-wasm32-wasi] reported by reviewdog 🐶 <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:856:5 | 856 | character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead help: use `let _ = ...` to ignore the resulting value | 856 | let _ = character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | +++++++ </code></pre> Raw Output: src/editor.rs:856:5:e: <pre><code>error: unused return value of `std::result::Result::<T, E>::map_or` that must be used --> src/editor.rs:856:5 | 856 | character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if you don't need the returned value, use `if let` instead help: use `let _ = ...` to ignore the resulting value | 856 | let _ = character.map_or((), |_| CHARACTER.with(|cache| cache.borrow_mut().clear())); | +++++++ </code></pre> __END__

PromptState::Active(buffer)
}

Expand Down
Loading