Skip to content

Commit

Permalink
Refactor model::listing::layout into model::listing::line and model::…
Browse files Browse the repository at this point in the history
…listing::window
  • Loading branch information
misson20000 committed Jul 27, 2024
1 parent 715f4fd commit a4d5b80
Show file tree
Hide file tree
Showing 8 changed files with 562 additions and 560 deletions.
9 changes: 5 additions & 4 deletions src/bin/layoutplayground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ use std::sync;
use std::vec;

use charm::model::document;
use charm::model::listing::layout;
use charm::model::listing::line;
use charm::model::listing::token;
use charm::model::listing::token::TokenKind;
use charm::model::listing::window;

struct Line {
indent: usize,
tokens: vec::Vec<token::Token>
}

impl layout::LineView for Line {
fn from_line(line: layout::Line) -> Self {
impl window::LineView for Line {
fn from_line(line: line::Line) -> Self {
let tokens: vec::Vec<token::Token> = line.to_tokens().collect();

Line {
Expand All @@ -38,7 +39,7 @@ fn main() {
let xml_path = args.next().expect("expected path to xml");

let document = sync::Arc::new(document::Document::load_from_testing_structure(xml_path).unwrap());
let mut window = layout::Window::<Line>::new(document);
let mut window = window::Window::<Line>::new(document);

window.resize(150);

Expand Down
70 changes: 67 additions & 3 deletions src/logic/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
use std::sync;

use crate::model::addr;
use crate::model::listing::token;
use crate::model::listing::token::TokenKind;
use crate::model::document::structure;
use crate::model::document::change;
use crate::model::document::structure;
use crate::model::document;
use crate::model::listing::token::TokenKind;
use crate::model::listing::token;

use tracing::instrument;

Expand Down Expand Up @@ -66,6 +67,23 @@ pub struct TokenizerStackEntry {
node_addr: addr::Address,
}

/* This lets us provide an alternate, simpler implementation to
* certain unit tests to help isolate bugs to either Tokenizer logic
* or Window/Line logic. */
pub trait AbstractTokenizer: Clone {
fn at_beginning(root: sync::Arc<structure::Node>) -> Self;
fn at_path(root: sync::Arc<structure::Node>, path: &structure::Path, offset: addr::Address) -> Self;
fn port_change(&mut self, new_doc: &sync::Arc<document::Document>, change: &document::change::Change);
fn hit_top(&self) -> bool;
fn hit_bottom(&self) -> bool;
fn gen_token(&self) -> TokenGenerationResult;
fn move_prev(&mut self) -> bool;
fn move_next(&mut self) -> bool;
fn next_postincrement(&mut self) -> Option<token::Token>;
fn prev(&mut self) -> Option<token::Token>;
fn in_summary(&self) -> bool;
}

#[derive(Clone)]
pub struct Tokenizer {
/* invariants:
Expand Down Expand Up @@ -1661,6 +1679,52 @@ pub mod xml {
}
}

impl AbstractTokenizer for Tokenizer {
fn at_beginning(root: sync::Arc<structure::Node>) -> Self {
Tokenizer::at_beginning(root)
}

fn at_path(root: sync::Arc<structure::Node>, path: &structure::Path, offset: addr::Address) -> Self {
Tokenizer::at_path(root, path, offset)
}

fn port_change(&mut self, new_doc: &sync::Arc<document::Document>, change: &document::change::Change) {
Tokenizer::port_change(self, &new_doc.root, change, &mut PortOptions::default());
}

fn hit_top(&self) -> bool {
Tokenizer::hit_top(self)
}

fn hit_bottom(&self) -> bool {
Tokenizer::hit_bottom(self)
}

fn gen_token(&self) -> TokenGenerationResult {
Tokenizer::gen_token(self)
}

fn move_prev(&mut self) -> bool {
Tokenizer::move_prev(self)
}

fn move_next(&mut self) -> bool {
Tokenizer::move_next(self)
}

fn next_postincrement(&mut self) -> Option<token::Token> {
Tokenizer::next_postincrement(self)
}

fn prev(&mut self) -> Option<token::Token> {
Tokenizer::prev(self)
}

fn in_summary(&self) -> bool {
Tokenizer::in_summary(self)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit a4d5b80

Please sign in to comment.