Skip to content

Commit

Permalink
fix: don't #![allow(unused)] anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Apr 19, 2024
1 parent 9218e14 commit 87340c7
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 87 deletions.
5 changes: 1 addition & 4 deletions src/engine/doc_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ use super::doc::Doc;
use super::Settings;
use crate::language::Storage;
use crate::pretty_doc::DocRef;
use crate::util::SynlessBug;
use partial_pretty_printer as ppp;
use partial_pretty_printer::pane;
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};

type DocIndex = usize;
use std::path::PathBuf;

/// Label for documents that might be displayed on the screen. Not every document will have such a
/// label, and multiple labels may refer to the same document.
Expand Down
16 changes: 5 additions & 11 deletions src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ use super::command::Command;
use super::doc::Doc;
use super::doc_set::{DocDisplayLabel, DocName, DocSet};
use super::Settings;
use crate::language::{Language, LanguageError, LanguageSpec, NotationSetSpec, Storage};
use crate::language::{LanguageSpec, NotationSetSpec, Storage};
use crate::parsing::{Parse, ParseError};
use crate::pretty_doc::{DocRef, PrettyDocError};
use crate::pretty_doc::DocRef;
use crate::tree::Node;
use crate::util::{bug_assert, error, SynlessBug, SynlessError};
use crate::util::{error, SynlessBug, SynlessError};
use partial_pretty_printer as ppp;
use partial_pretty_printer::pane;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::path::Path;

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -138,14 +137,9 @@ impl Engine {
* Parsers *
***********/

pub fn add_parser(
&mut self,
language_name: &str,
parser: impl Parse + 'static,
) -> Result<(), SynlessError> {
pub fn add_parser(&mut self, language_name: &str, parser: impl Parse + 'static) {
self.parsers
.insert(language_name.to_owned(), Box::new(parser));
Ok(())
}

/******************
Expand Down Expand Up @@ -204,7 +198,7 @@ impl Engine {

pub fn load_doc_from_sexpr(
&self,
doc_name: DocName,
_doc_name: DocName,
_source: &str,
) -> Result<(), SynlessError> {
todo!()
Expand Down
4 changes: 2 additions & 2 deletions src/frontends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ mod frontend;
mod screen_buf;
mod terminal;

pub use frontend::{Event, Frontend, Key, KeyCode, KeyModifiers, MouseButton, MouseEvent};
pub use terminal::{Terminal, TerminalError};
pub use frontend::{Event, Frontend, Key};
pub use terminal::Terminal;

use crate::util::{error, SynlessError};
use partial_pretty_printer::pane;
Expand Down
20 changes: 10 additions & 10 deletions src/frontends/screen_buf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::style::ConcreteStyle;
use partial_pretty_printer::{Height, Pos, Size, Width};
use partial_pretty_printer::{Pos, Size, Width};
use std::mem;

/// The width of a single character. Either 1 ("half-width") or 2 ("full-width").
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<'a> Iterator for ScreenBufIter<'a> {
mod screen_buf_tests {
use super::*;
use crate::style::{ConcreteStyle, Rgb};
use partial_pretty_printer::{Pos, Size};
use partial_pretty_printer::{Height, Pos, Size};

const STYLE_DEFAULT: ConcreteStyle = ConcreteStyle {
fg_color: Rgb {
Expand Down Expand Up @@ -517,10 +517,10 @@ mod screen_buf_tests {
]
);

buf.display_char('一', Pos::zero(), STYLE_RED, 2);
buf.display_char('二', Pos { col: 2, row: 0 }, STYLE_RED, 2);
buf.display_char('*', Pos { col: 4, row: 0 }, STYLE_RED, 1);
buf.display_char('三', Pos { col: 5, row: 0 }, STYLE_RED, 2);
assert!(buf.display_char('一', Pos::zero(), STYLE_RED, 2));
assert!(buf.display_char('二', Pos { col: 2, row: 0 }, STYLE_RED, 2));
assert!(buf.display_char('*', Pos { col: 4, row: 0 }, STYLE_RED, 1));
assert!(buf.display_char('三', Pos { col: 5, row: 0 }, STYLE_RED, 2));
let actual_ops = buf.drain_changes().collect::<Vec<_>>();
assert_eq!(
actual_ops,
Expand All @@ -536,8 +536,8 @@ mod screen_buf_tests {
]
);

buf.display_char('3', Pos { col: 3, row: 0 }, STYLE_RED, 1);
buf.display_char('5', Pos { col: 5, row: 0 }, STYLE_RED, 1);
assert!(buf.display_char('3', Pos { col: 3, row: 0 }, STYLE_RED, 1));
assert!(buf.display_char('5', Pos { col: 5, row: 0 }, STYLE_RED, 1));
let actual_ops = buf.drain_changes().collect::<Vec<_>>();
assert_eq!(
actual_ops,
Expand Down Expand Up @@ -565,9 +565,9 @@ mod screen_buf_tests {
#[test]
fn test_replace_full_width_with_space() {
let mut buf = new_buf(2, 1);
buf.display_char('一', Pos::zero(), STYLE_DEFAULT, 2);
assert!(buf.display_char('一', Pos::zero(), STYLE_DEFAULT, 2));
buf.drain_changes();
buf.display_char(' ', Pos::zero(), STYLE_DEFAULT, 1);
assert!(buf.display_char(' ', Pos::zero(), STYLE_DEFAULT, 1));
let actual_ops = buf.drain_changes().collect::<Vec<_>>();
assert_eq!(
actual_ops,
Expand Down
4 changes: 1 addition & 3 deletions src/keymap/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
use crate::frontends::Key;
use crate::language::Storage;
use crate::tree::Node;
use crate::util::{bug, bug_assert, error, OrderedMap, SynlessBug};
use std::borrow::Borrow;
use std::collections::HashMap;
use crate::util::{bug_assert, error, OrderedMap, SynlessBug};

const KEYHINTS_LANGUAGE_NAME: &str = "keyhints";

Expand Down
5 changes: 2 additions & 3 deletions src/keymap/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::keymap::{KeyProg, Keymap};
use super::menu::{Menu, MenuName, MenuSelectionCmd};
use crate::engine::DocName;
use crate::frontends::Key;
use crate::keymap;
use crate::language::Storage;
use crate::tree::Mode;
use crate::tree::Node;
Expand Down Expand Up @@ -138,7 +137,7 @@ impl LayerManager {
doc_name: &DocName,
layer_name: &str,
) -> Result<(), SynlessError> {
let mut local_layers = self.local_layers.entry(doc_name.to_owned()).or_default();
let local_layers = self.local_layers.entry(doc_name.to_owned()).or_default();
add_layer(&self.layers, local_layers, layer_name)?;
self.cached_composite_layers.clear();
Ok(())
Expand All @@ -159,7 +158,7 @@ impl LayerManager {
doc_name: &DocName,
layer_name: &str,
) -> Result<(), SynlessError> {
let mut local_layers = self.local_layers.entry(doc_name.to_owned()).or_default();
let local_layers = self.local_layers.entry(doc_name.to_owned()).or_default();
remove_layer(&self.layers, local_layers, layer_name)?;
self.cached_composite_layers.clear();
Ok(())
Expand Down
32 changes: 14 additions & 18 deletions src/language/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ pub fn compile_language(language_spec: LanguageSpec) -> Result<LanguageCompiled,

let notation_set = compile_notation_set(language_spec.default_display_notation, &grammar)?;
let mut notation_sets = IndexedMap::new();
notation_sets
.insert(notation_set.name.to_owned(), notation_set)
.bug();
notation_sets.insert(notation_set.name.to_owned(), notation_set);

Ok(LanguageCompiled {
name: language_spec.name,
Expand Down Expand Up @@ -185,9 +183,10 @@ impl GrammarCompiler {
));
}

self.constructs
.insert(construct.name.clone(), construct)
.map_err(LanguageError::DuplicateConstruct)?;
if self.constructs.contains_name(&construct.name) {
return Err(LanguageError::DuplicateConstruct(construct.name));
}
self.constructs.insert(construct.name.clone(), construct);
Ok(())
}

Expand Down Expand Up @@ -319,18 +318,15 @@ impl GrammarCompiler {
}

assert_eq!(construct_id, grammar.constructs.len());
grammar
.constructs
.insert(
construct.name.clone(),
ConstructCompiled {
name: construct.name.clone(),
arity,
is_comment_or_ws: construct.is_comment_or_ws,
key: construct.key,
},
)
.bug();
grammar.constructs.insert(
construct.name.clone(),
ConstructCompiled {
name: construct.name.clone(),
arity,
is_comment_or_ws: construct.is_comment_or_ws,
key: construct.key,
},
);
Ok(())
}
}
12 changes: 8 additions & 4 deletions src/language/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ impl Language {
notation_set: NotationSetSpec,
) -> Result<(), LanguageError> {
let notation_set = compile_notation_set(notation_set, grammar(s, self.language))?;
s.languages[self.language]
.notation_sets
.insert(notation_set.name.clone(), notation_set)
.map_err(|name| LanguageError::DuplicateNotationSet(self.name(s).to_owned(), name))?;
let notation_sets = &mut s.languages[self.language].notation_sets;
if notation_sets.contains_name(&notation_set.name) {
return Err(LanguageError::DuplicateNotationSet(
self.name(s).to_owned(),
notation_set.name,
));
}
notation_sets.insert(notation_set.name.clone(), notation_set);
Ok(())
}

Expand Down
8 changes: 4 additions & 4 deletions src/language/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ impl Storage {
pub fn add_language(&mut self, language_spec: LanguageSpec) -> Result<(), LanguageError> {
let language = compile_language(language_spec)?;
let extensions = language.file_extensions.clone();
let id = self
.languages
.insert(language.name.clone(), language)
.map_err(LanguageError::DuplicateLanguage)?;
if self.languages.contains_name(&language.name) {
return Err(LanguageError::DuplicateLanguage(language.name));
}
let (id, _) = self.languages.insert(language.name.clone(), language);
for ext in extensions {
self.file_extensions.insert(ext, Language::from_id(id));
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: temporary
#![allow(unused)]
// TODO: temporary #[allow(dead_code)]
#![allow(dead_code)]

mod engine;
mod frontends;
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod json_parser;

use crate::language::{LanguageError, Storage};
use crate::language::Storage;
use crate::tree::Node;
use crate::util::{error, SynlessError};
use partial_pretty_printer as ppp;
Expand Down
7 changes: 1 addition & 6 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use crate::engine::{DocDisplayLabel, DocName, Engine, Settings};
use crate::frontends::{Event, Frontend, Key, MouseEvent};
use crate::frontends::{Event, Frontend, Key};
use crate::keymap::{KeyProg, Keymap, Layer, LayerManager};
use crate::language::Construct;
use crate::style::Style;
use crate::tree::{Mode, Node};
use crate::util::{error, log, SynlessBug, SynlessError};
use partial_pretty_printer as ppp;
use partial_pretty_printer::pane;
use std::cell::RefCell;
use std::collections::HashMap;
use std::error::Error;
use std::rc::Rc;
use std::time::Duration;

Expand Down Expand Up @@ -306,7 +302,6 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {

fn list_files_and_dirs(dir: &str) -> Result<rhai::Map, SynlessError> {
use std::fs::read_dir;
use std::path::{Path, PathBuf};

let entries = read_dir(dir).map_err(|err| {
error!(
Expand Down
14 changes: 7 additions & 7 deletions src/tree/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,33 +649,33 @@ mod forest_tests {
let elder = forest.first_child(parent).unwrap();
let younger = forest.next(elder).unwrap();

forest.swap(elder, elder);
assert!(forest.swap(elder, elder));
assert_eq!(
verify_and_print(&forest),
"(parent (elderSister) (youngerSister))"
);

forest.swap(elder, younger);
assert!(forest.swap(elder, younger));
assert_eq!(
verify_and_print(&forest),
"(parent (youngerSister) (elderSister))"
);

forest.swap(elder, younger);
assert!(forest.swap(elder, younger));
assert_eq!(
verify_and_print(&forest),
"(parent (elderSister) (youngerSister))"
);

let middle = forest.new_node("middleSister");
assert!(forest.insert_after(elder, middle));
forest.swap(elder, middle);
assert!(forest.swap(elder, middle));
assert_eq!(
verify_and_print(&forest),
"(parent (middleSister) (elderSister) (youngerSister))"
);

forest.swap(middle, younger);
assert!(forest.swap(middle, younger));
assert_eq!(
verify_and_print(&forest),
"(parent (youngerSister) (elderSister) (middleSister))"
Expand Down Expand Up @@ -748,8 +748,8 @@ mod forest_tests {
"(kid (gramp) (papa (ogramp) (ogram)))(mama (gram))"
);

f.swap(kid, mama);
f.swap(gramp, gram);
assert!(f.swap(kid, mama));
assert!(f.swap(gramp, gram));
assert_eq!(
verify_and_print(&f),
"(kid (gram) (papa (ogramp) (ogram)))(mama (gramp))"
Expand Down
1 change: 0 additions & 1 deletion src/util/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::error::Error;
use std::fmt;

pub use crate::error;
Expand Down
21 changes: 12 additions & 9 deletions src/util/indexed_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::mem;
use std::ops::{Index, IndexMut};

/// A map from `String` to `T`, that also associates a `usize` with each element for faster
Expand All @@ -17,16 +18,18 @@ impl<T> IndexedMap<T> {
}
}

/// Inserts name->value into this map.
/// Returns `Err(name)` if already present or `Ok(new_id)` if not.
pub fn insert(&mut self, name: String, value: T) -> Result<usize, String> {
if self.map.contains_key(&name) {
return Err(name);
/// Inserts name->value into this map, replacing the binding if the name was already present.
/// Returns `(new_id, Option<old_value>)`.
pub fn insert(&mut self, name: String, value: T) -> (usize, Option<T>) {
if let Some(old_id) = self.id(&name) {
let old_value = mem::replace(&mut self.values[old_id], value);
(old_id, Some(old_value))
} else {
let new_id = self.values.len();
self.values.push(value);
self.map.insert(name, new_id);
(new_id, None)
}
let new_id = self.values.len();
self.values.push(value);
self.map.insert(name, new_id);
Ok(new_id)
}

pub fn contains_name(&self, name: &str) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/util/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Log {
}

#[doc(hidden)]
pub fn with_log<R>(mut callback: impl FnOnce(&mut Log) -> R) -> R {
pub fn with_log<R>(callback: impl FnOnce(&mut Log) -> R) -> R {
let log_mutex: &'static Mutex<Log> = LOG.get_or_init(|| Mutex::new(Log::new()));
let mut log_guard: MutexGuard<Log> = log_mutex.lock().bug();
callback(&mut log_guard)
Expand Down
2 changes: 1 addition & 1 deletion tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_json() {
let language_name = engine
.load_language_ron(Path::new(JSON_PATH), &json_lang_ron)
.unwrap();
engine.add_parser(&language_name, JsonParser).unwrap();
engine.add_parser(&language_name, JsonParser);
engine
.set_source_notation(&language_name, JSON_NOTATION_NAME)
.unwrap();
Expand Down

0 comments on commit 87340c7

Please sign in to comment.