Skip to content

Commit

Permalink
chore: updating clippy lints in ad
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Sep 11, 2024
1 parent 83ae1f2 commit f594121
Show file tree
Hide file tree
Showing 27 changed files with 256 additions and 118 deletions.
60 changes: 0 additions & 60 deletions examples/fs_demo.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/buffer/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Buffers {
if let Some(idx) = self.inner.iter().position(|b| b.id == bufid) {
self.inner.swap(0, idx);
self.inner[0].dot = cur.into();
self.inner[0].view_port(ViewPort::Center, screen_rows, screen_cols);
self.inner[0].set_view_port(ViewPort::Center, screen_rows, screen_cols);
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/buffer/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@ impl GapBuffer {
let mut v = Vec::with_capacity(self.len());
v.extend(&self.data[..self.gap_start]);
v.extend(&self.data[self.gap_end..]);

v
}

/// Iterate over the lines of the buffer
pub fn iter_lines(&self) -> impl Iterator<Item = Slice<'_>> {
let mut line_idx = 0;

Expand All @@ -211,10 +213,12 @@ impl GapBuffer {
}
let slice = self.line(line_idx);
line_idx += 1;

Some(slice)
})
}

/// The number of lines within the buffer
#[inline]
pub fn len_lines(&self) -> usize {
match self.line_endings.last_key_value() {
Expand All @@ -232,15 +236,23 @@ impl GapBuffer {
n
}
}

None => 1,
}
}

/// The number of characters in the buffer
#[inline]
pub fn len_chars(&self) -> usize {
self.n_chars
}

/// Clear the contents of the buffer.
///
/// # Note
/// This does not actually zero out the data currently within the buffer or truncate the
/// allocation in any way. It simply resets internal state so that it behaves like an empty
/// initial buffer.
pub fn clear(&mut self) {
self.move_gap_to(0);
self.gap_end = self.cap;
Expand All @@ -251,6 +263,10 @@ impl GapBuffer {
assert_line_endings!(self);
}

/// The character at the specified character index.
///
/// # Panics
/// This method will panic if the given character index is out of bounds
#[inline]
pub fn char(&self, char_idx: usize) -> char {
let byte_idx = self.char_to_raw_byte(char_idx);
Expand All @@ -259,6 +275,7 @@ impl GapBuffer {
unsafe { decode_char_at(byte_idx, &self.data) }
}

/// The character at the specified character index.
#[inline]
pub fn get_char(&self, char_idx: usize) -> Option<char> {
if char_idx < self.n_chars {
Expand All @@ -274,6 +291,10 @@ impl GapBuffer {
unsafe { decode_char_at(byte_idx, &self.data) }.len_utf8()
}

/// The requested line as a [Slice].
///
/// # Panics
/// This method will panic if the given line index is out of bounds
#[inline]
pub fn line(&self, line_idx: usize) -> Slice<'_> {
if line_idx >= self.len_lines() {
Expand All @@ -297,6 +318,10 @@ impl GapBuffer {
Slice::from_raw_offsets(from, to, self)
}

/// The number of characters in the requested line.
///
/// # Panics
/// This method will panic if the given line index is out of bounds
#[inline]
pub fn line_len_chars(&self, line_idx: usize) -> usize {
if line_idx >= self.len_lines() {
Expand Down Expand Up @@ -338,10 +363,15 @@ impl GapBuffer {
}
}

/// Convert a byte index to a character index
pub fn byte_to_char(&self, byte_idx: usize) -> usize {
self.chars_in_raw_range(0, byte_idx)
}

/// Convert a character index to the index of the line containing it
///
/// # Panics
/// This method will panic if the given char index is out of bounds
pub fn char_to_line(&self, char_idx: usize) -> usize {
match self.try_char_to_line(char_idx) {
Some(line_idx) => line_idx,
Expand All @@ -352,6 +382,7 @@ impl GapBuffer {
}
}

/// Convert a character index to the index of the line containing it
pub fn try_char_to_line(&self, char_idx: usize) -> Option<usize> {
match char_idx.cmp(&self.n_chars) {
Ordering::Less => {
Expand All @@ -370,6 +401,10 @@ impl GapBuffer {
}
}

/// Convert a line index to the character index of its first character
///
/// # Panics
/// This method will panic if the given char index is out of bounds
pub fn line_to_char(&self, line_idx: usize) -> usize {
match self.try_line_to_char(line_idx) {
Some(char_idx) => char_idx,
Expand All @@ -380,6 +415,7 @@ impl GapBuffer {
}
}

/// Convert a line index to the character index of its first character
pub fn try_line_to_char(&self, line_idx: usize) -> Option<usize> {
if line_idx > self.len_lines() - 1 {
return None;
Expand Down Expand Up @@ -703,6 +739,7 @@ impl<'a> Slice<'a> {
}
}

/// The two sides of this slice as &str references
pub fn as_strs(&self) -> (&str, &str) {
// SAFETY: we know that we have valid utf8 data internally
unsafe {
Expand All @@ -713,10 +750,12 @@ impl<'a> Slice<'a> {
}
}

/// Iterate over the characters in this slice
pub fn chars(self) -> Chars<'a> {
Chars { s: self, cur: 0 }
}

/// Iterate over the characters in this slice with their corresponding character indices
pub fn indexed_chars(self, from: usize, rev: bool) -> IdxChars<'a> {
let (cur, idx) = if rev {
(
Expand Down Expand Up @@ -757,6 +796,7 @@ impl<'a> fmt::Display for Slice<'a> {
}
}

/// An iterator of characters from a [Slice]
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct Chars<'a> {
s: Slice<'a>,
Expand All @@ -780,6 +820,7 @@ impl<'a> Iterator for Chars<'a> {
}
}

/// An iterator of characters and their indices from a [Slice]
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct IdxChars<'a> {
s: Slice<'a>,
Expand Down
28 changes: 27 additions & 1 deletion src/buffer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! A [Buffer] represents a single file or in memory text buffer open within the editor.
use crate::{
config,
config::ColorScheme,
Expand Down Expand Up @@ -65,6 +66,7 @@ impl BufferKind {
}
}

/// Internal state for a text buffer backed by a file on disk
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Buffer {
pub(crate) id: usize,
Expand Down Expand Up @@ -207,6 +209,7 @@ impl Buffer {
}
}

/// Create a new unnamed buffer with the given content
pub fn new_unnamed(id: usize, content: &str) -> Self {
Self {
id,
Expand All @@ -225,6 +228,7 @@ impl Buffer {
}
}

/// Create a new virtual buffer with the given name and content
pub fn new_virtual(id: usize, name: String, mut content: String) -> Self {
if content.ends_with('\n') {
content.pop();
Expand Down Expand Up @@ -264,23 +268,28 @@ impl Buffer {
}
}

/// The directory containing the file backing this buffer so long as it has kind `File`.
pub fn dir(&self) -> Option<&Path> {
match &self.kind {
BufferKind::File(p) => p.parent(),
_ => None,
}
}

/// Check whether or not this is an unnamed buffer
pub fn is_unnamed(&self) -> bool {
self.kind == BufferKind::Unnamed
}

/// The raw binary contents of this buffer
pub fn contents(&self) -> Vec<u8> {
let mut contents: Vec<u8> = self.txt.bytes();
contents.push(b'\n');

contents
}

/// The utf-8 string contents of this buffer
pub fn str_contents(&self) -> String {
let mut s = self.txt.to_string();
s.push('\n');
Expand All @@ -300,27 +309,41 @@ impl Buffer {
.collect()
}

/// The contents of the current [Dot].
pub fn dot_contents(&self) -> String {
self.dot.content(self)
}

/// The address of the current [Dot].
pub fn addr(&self) -> String {
self.dot.addr(self)
}

/// The contents of the current xdot.
///
/// This is a virtual dot that is only made use of through the filesystem interface.
pub fn xdot_contents(&self) -> String {
self.xdot.content(self)
}

/// The address of the current xdot.
///
/// This is a virtual dot that is only made use of through the filesystem interface.
pub fn xaddr(&self) -> String {
self.xdot.addr(self)
}

/// The number of lines currently held in the buffer.
#[inline]
pub fn len_lines(&self) -> usize {
self.txt.len_lines()
}

/// Whether or not the buffer is empty.
///
/// # Note
/// This does not always imply that the underlying buffer is zero sized, only that the visible
/// contents are empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.txt.len_chars() == 0
Expand All @@ -330,6 +353,7 @@ impl Buffer {
self.edit_log.debug_edits(self).into_iter().collect()
}

/// Clamp the current viewport to include the [Dot].
pub fn clamp_scroll(&mut self, screen_rows: usize, screen_cols: usize) {
let (y, x) = self.dot.active_cur().as_yx(self);
self.rx = self.rx_from_x(y, x);
Expand All @@ -351,7 +375,8 @@ impl Buffer {
}
}

pub fn view_port(&mut self, vp: ViewPort, screen_rows: usize, screen_cols: usize) {
/// Set the current [ViewPort] while accounting for screen size.
pub fn set_view_port(&mut self, vp: ViewPort, screen_rows: usize, screen_cols: usize) {
let (y, _) = self.dot.active_cur().as_yx(self);

self.row_off = match vp {
Expand Down Expand Up @@ -409,6 +434,7 @@ impl Buffer {
cx
}

/// The line at the requested index returned as a [Slice].
pub fn line(&self, y: usize) -> Option<Slice<'_>> {
if y >= self.len_lines() {
None
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{key::Key, mode::normal_mode, term::Color};
use std::{collections::BTreeMap, env, fs, io};

/// Editor level configuration
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
pub(crate) tabstop: usize,
Expand Down Expand Up @@ -29,6 +30,7 @@ impl Default for Config {
}
}

/// A colorscheme for the terminal UI
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ColorScheme {
// ui
Expand Down Expand Up @@ -73,6 +75,7 @@ impl Default for ColorScheme {
}

impl Config {
/// Attempt to load a config file from the default location
pub fn try_load() -> Result<Self, String> {
let home = env::var("HOME").unwrap();

Expand Down
Loading

0 comments on commit f594121

Please sign in to comment.