Skip to content

Commit c47928d

Browse files
committed
ran clippy for cleaner and better code quality
1 parent 68970d2 commit c47928d

File tree

20 files changed

+497
-508
lines changed

20 files changed

+497
-508
lines changed

kaolinite/src/document.rs

Lines changed: 94 additions & 73 deletions
Large diffs are not rendered by default.

kaolinite/src/event.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ impl Event {
3939
#[must_use]
4040
pub fn loc(self) -> Loc {
4141
match self {
42-
Event::Insert(loc, _) => loc,
43-
Event::Delete(loc, _) => loc,
44-
Event::InsertLine(loc, _) => Loc { x: 0, y: loc },
45-
Event::DeleteLine(loc, _) => Loc { x: 0, y: loc },
46-
Event::SplitDown(loc) => loc,
47-
Event::SpliceUp(loc) => loc,
42+
Event::Insert(loc, _)
43+
| Event::Delete(loc, _)
44+
| Event::SplitDown(loc)
45+
| Event::SpliceUp(loc) => loc,
46+
Event::InsertLine(loc, _) | Event::DeleteLine(loc, _) => Loc { x: 0, y: loc },
4847
}
4948
}
5049
}
@@ -96,6 +95,7 @@ pub struct UndoMgmt {
9695
}
9796

9897
impl Document {
98+
#[must_use]
9999
pub fn take_snapshot(&self) -> Snapshot {
100100
Snapshot {
101101
content: self.file.clone(),
@@ -153,10 +153,11 @@ impl UndoMgmt {
153153

154154
/// On file save, mark where the document is to match it on the disk
155155
pub fn saved(&mut self) {
156-
self.on_disk = self.undo.len()
156+
self.on_disk = self.undo.len();
157157
}
158158

159159
/// Determine if the state of the document is currently that of what is on the disk
160+
#[must_use]
160161
pub fn at_file(&self) -> bool {
161162
self.undo.len() == self.on_disk
162163
}

kaolinite/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! # Kaolinite
22
//! > Kaolinite is an advanced library that handles the backend of a terminal text editor. You can
3-
//! feel free to make your own terminal text editor using kaolinite, or see the reference
4-
//! implementation found under the directory `examples/cactus`.
3+
//! > feel free to make your own terminal text editor using kaolinite, or see the reference
4+
//! > implementation found under the directory `examples/cactus`.
55
//!
66
//! It'll handle things like
77
//! - Opening and saving files

kaolinite/src/utils.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Loc {
3030
/// Shorthand to produce a location
3131
#[must_use]
3232
pub fn at(x: usize, y: usize) -> Self {
33-
Self { x, y }
33+
Self { y, x }
3434
}
3535
}
3636

@@ -56,21 +56,21 @@ impl Size {
5656
pub fn trim(string: &str, start: usize, length: usize, tab_width: usize) -> String {
5757
let string = string.replace('\t', &" ".repeat(tab_width));
5858
if start >= string.width() {
59-
return "".to_string();
59+
return String::new();
6060
}
6161
let desired_length = string.width().saturating_sub(start);
6262
let mut chars: String = string;
6363
while chars.width() > desired_length {
6464
chars = chars.chars().skip(1).collect();
6565
}
6666
if chars.width() < desired_length {
67-
chars = format!(" {}", chars);
67+
chars = format!(" {chars}");
6868
}
6969
while chars.width() > length {
7070
chars.pop();
7171
}
7272
if chars.width() < length && desired_length > length {
73-
chars = format!("{} ", chars);
73+
chars = format!("{chars} ");
7474
}
7575
chars
7676
}
@@ -151,7 +151,7 @@ pub fn get_file_name(path: &str) -> Option<String> {
151151
let p = Path::new(path);
152152
p.file_name()
153153
.and_then(|name| name.to_str())
154-
.map(|s| s.to_string())
154+
.map(std::string::ToString::to_string)
155155
}
156156

157157
/// Will get the file name from a file
@@ -160,7 +160,7 @@ pub fn get_file_ext(path: &str) -> Option<String> {
160160
let p = Path::new(path);
161161
p.extension()
162162
.and_then(|name| name.to_str())
163-
.map(|s| s.to_string())
163+
.map(std::string::ToString::to_string)
164164
}
165165

166166
/// Determine the filetype from the extension
@@ -298,14 +298,14 @@ pub fn icon(language: &str) -> String {
298298
"Assembly" => " ",
299299
"Batch" => "󰆍 ",
300300
"Brainfuck" => " ",
301-
"C" => " ",
302-
"CMake" => " ",
301+
"C" | "C Header" => " ",
302+
"CMake" | "Makefile" => " ",
303303
"Java" => " ",
304304
"Clojure" => " ",
305305
"CoffeeScript" => " ",
306306
"Crystal" => " ",
307307
"Cuda" => " ",
308-
"C++" => " ",
308+
"C++" | "C++ Header" => " ",
309309
"C#" => " ",
310310
"CSS" => " ",
311311
"CSV" => " ",
@@ -325,11 +325,9 @@ pub fn icon(language: &str) -> String {
325325
"Gnuplot" => " ",
326326
"Go" => "",
327327
"Groovy" => " ",
328-
"C Header" => " ",
329328
"Haml" => "",
330329
"Handlebars" => "󰅩 ",
331330
"Haskell" => " ",
332-
"C++ Header" => " ",
333331
"HTML" => " ",
334332
"INI" => " ",
335333
"Arduino" => " ",
@@ -346,24 +344,21 @@ pub fn icon(language: &str) -> String {
346344
"Matlab" => " ",
347345
"Objective-C" => " ",
348346
"OCaml" => " ",
349-
"Makefile" => " ",
350347
"Markdown" => " ",
351348
"Nix" => " ",
352349
"NumPy" => "󰘨 ",
353350
"PHP" => "󰌟 ",
354351
"Perl" => " ",
355352
"PowerShell" => "󰨊 ",
356353
"Prolog" => " ",
357-
"Python" => " ",
358-
"Cython" => " ",
354+
"Python" | "Cython" => " ",
359355
"R" => " ",
360356
"reStructuredText" => "󰊄",
361357
"Ruby" => " ",
362358
"Rust" => " ",
363-
"Shell" => " ",
364-
"SCSS" => " ",
359+
"Shell" | "Zsh" => " ",
360+
"SCSS" | "Sass" => " ",
365361
"SQL" => " ",
366-
"Sass" => " ",
367362
"Scala" => "",
368363
"Scheme" => "",
369364
"Swift" => " ",
@@ -375,7 +370,6 @@ pub fn icon(language: &str) -> String {
375370
"Visual Basic" => "󰯁 ",
376371
"Vue" => " ",
377372
"XML" => "󰗀 ",
378-
"Zsh" => " ",
379373
_ => "󰈙 ",
380374
}
381375
.to_string()

kaolinite/tests/test.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ fn document_disks() {
307307
let mut doc = Document::new(Size::is(100, 10));
308308
assert!(doc.save().is_err());
309309
let mut doc = Document::new(Size::is(100, 10));
310-
doc.read_only = true;
310+
doc.info.read_only = true;
311311
assert!(doc.save().is_err());
312312
// Save as
313313
assert!(doc.save_as("tests/data/ghost.txt").is_err());
314-
doc.read_only = false;
314+
doc.info.read_only = false;
315315
assert!(doc.save_as("tests/data/ghost.txt").is_ok());
316316
// Clean up and verify ghost exists
317317
let result = std::fs::read_to_string("tests/data/ghost.txt").unwrap();
@@ -368,19 +368,19 @@ fn document_undo_redo() {
368368
doc.load_to(100);
369369
assert!(doc.undo_mgmt.undo(doc.take_snapshot()).is_none());
370370
assert!(doc.redo().is_ok());
371-
assert!(!doc.modified);
371+
assert!(!doc.info.modified);
372372
doc.exe(Event::InsertLine(0, st!("hello你bye好hello")));
373373
doc.exe(Event::Delete(Loc { x: 0, y: 2 }, st!("\t")));
374374
doc.exe(Event::Insert(Loc { x: 3, y: 2 }, st!("a")));
375375
doc.commit();
376-
assert!(doc.modified);
376+
assert!(doc.info.modified);
377377
assert!(doc.undo().is_ok());
378-
assert!(!doc.modified);
378+
assert!(!doc.info.modified);
379379
assert_eq!(doc.line(0), Some(st!(" 你好")));
380380
assert_eq!(doc.line(1), Some(st!("\thello")));
381381
assert_eq!(doc.line(2), Some(st!(" hello")));
382382
assert!(doc.redo().is_ok());
383-
assert!(doc.modified);
383+
assert!(doc.info.modified);
384384
assert_eq!(doc.line(0), Some(st!("hello你bye好hello")));
385385
assert_eq!(doc.line(2), Some(st!("helalo")));
386386
}
@@ -401,7 +401,7 @@ fn document_moving() {
401401
(1 + loaded).saturating_sub(9)
402402
}
403403
);
404-
assert!(doc.loaded_to >= loaded);
404+
assert!(doc.info.loaded_to >= loaded);
405405
}
406406
assert_eq!(doc.move_down(), Status::EndOfFile);
407407
// Check moving up
@@ -507,7 +507,7 @@ fn document_moving() {
507507
}
508508
);
509509
assert_eq!(doc.old_cursor, 0);
510-
assert_eq!(doc.loaded_to, doc.len_lines() + 1);
510+
assert_eq!(doc.info.loaded_to, doc.len_lines() + 1);
511511
doc.move_top();
512512
assert_eq!(doc.char_loc(), Loc { x: 0, y: 0 });
513513
assert_eq!(doc.old_cursor, 0);
@@ -520,7 +520,7 @@ fn document_moving() {
520520
}
521521
);
522522
assert_eq!(doc.old_cursor, 0);
523-
assert_eq!(doc.loaded_to, doc.len_lines() + 1);
523+
assert_eq!(doc.info.loaded_to, doc.len_lines() + 1);
524524
doc.select_top();
525525
assert_eq!(doc.char_loc(), Loc { x: 0, y: 0 });
526526
assert_eq!(doc.old_cursor, 0);
@@ -606,12 +606,12 @@ fn document_scrolling() {
606606
assert_eq!(doc.offset.y, 0);
607607
doc.scroll_down();
608608
assert_eq!(doc.offset.y, 1);
609-
assert_eq!(doc.loaded_to, 11);
609+
assert_eq!(doc.info.loaded_to, 11);
610610
// Scrolling up
611611
assert_eq!(doc.offset.y, 1);
612612
doc.scroll_up();
613613
assert_eq!(doc.offset.y, 0);
614-
assert_eq!(doc.loaded_to, 11);
614+
assert_eq!(doc.info.loaded_to, 11);
615615
}
616616

617617
#[test]
@@ -725,7 +725,7 @@ fn document_searching() {
725725
text: st!("world")
726726
})
727727
);
728-
assert_eq!(doc.loaded_to, 5);
728+
assert_eq!(doc.info.loaded_to, 5);
729729
doc.move_to(&Loc { x: 2, y: 2 });
730730
assert_eq!(
731731
doc.next_match("hello", 0),

src/cli.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,24 @@ EXAMPLES:
3030
";
3131

3232
/// Read from the standard input
33-
pub fn get_stdin() -> Option<String> {
34-
let input = io::stdin()
35-
.lock()
36-
.lines()
37-
.fold("".to_string(), |acc, line| {
38-
acc + &line.unwrap_or_else(|_| "".to_string()) + "\n"
39-
});
40-
41-
return Some(input);
33+
pub fn get_stdin() -> String {
34+
io::stdin().lock().lines().fold(String::new(), |acc, line| {
35+
acc + &line.unwrap_or_else(|_| String::new()) + "\n"
36+
})
4237
}
4338

44-
/// Struct to help with starting ox
45-
pub struct CommandLineInterface {
39+
/// Flags for command line interface
40+
#[allow(clippy::struct_excessive_bools)]
41+
pub struct CommandLineInterfaceFlags {
4642
pub help: bool,
4743
pub version: bool,
4844
pub read_only: bool,
4945
pub stdin: bool,
46+
}
47+
48+
/// Struct to help with starting ox
49+
pub struct CommandLineInterface {
50+
pub flags: CommandLineInterfaceFlags,
5051
pub file_type: Option<String>,
5152
pub config_path: String,
5253
pub to_open: Vec<String>,
@@ -63,10 +64,12 @@ impl CommandLineInterface {
6364
let config: Key = ["-c", "--config"].into();
6465

6566
Self {
66-
help: j.contains(["-h", "--help"]),
67-
version: j.contains(["-v", "--version"]),
68-
read_only: j.contains(["-r", "--readonly"]),
69-
stdin: j.contains("--stdin"),
67+
flags: CommandLineInterfaceFlags {
68+
help: j.contains(["-h", "--help"]),
69+
version: j.contains(["-v", "--version"]),
70+
read_only: j.contains(["-r", "--readonly"]),
71+
stdin: j.contains("--stdin"),
72+
},
7073
file_type: j.option_arg::<String, Key>(filetype.clone()),
7174
config_path: j
7275
.option_arg::<String, Key>(config.clone())
@@ -77,11 +80,11 @@ impl CommandLineInterface {
7780

7881
/// Handle options that won't need to start the editor
7982
pub fn basic_options(&self) {
80-
if self.help {
81-
println!("{}", HELP);
83+
if self.flags.help {
84+
println!("{HELP}");
8285
std::process::exit(0);
83-
} else if self.version {
84-
println!("{}", VERSION);
86+
} else if self.flags.version {
87+
println!("{VERSION}");
8588
std::process::exit(0);
8689
}
8790
}

0 commit comments

Comments
 (0)