Skip to content

Commit

Permalink
Removing unnecessary comment struct
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 3, 2024
1 parent 41d873a commit 6abe09c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
12 changes: 8 additions & 4 deletions notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
*** Line-by-line ignore
*** Block ignore
** Tests should compile to same pdf
** Documentation
** Tidy up code
** Find todos
** Clippy
** New source files: write.rs, parse.rs, print.rs
** Check unused regexes
** Check for collect() not needed
** Tests output better
* Bugs
** Better errors including line numbers in source file
** Handle list items properly
* Structure
** Perform indenting
** While long lines are present
*** Trim long lines
*** Perform indenting
32 changes: 10 additions & 22 deletions src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#[derive(Debug)]
pub struct Comment {
// index where the comment starts
pub idx: usize,
}

pub fn find_comment(line: &str) -> Option<Comment> {
pub fn find_comment_index(line: &str) -> Option<usize> {
// no percent means no comment
if !line.contains('%') {
return None;
Expand All @@ -20,9 +14,7 @@ pub fn find_comment(line: &str) -> Option<Comment> {
// check the first character
let mut prev_c: char = line.chars().next().unwrap();
if prev_c == '%' {
return Some(Comment {
idx: 0,
});
return Some(0);
}

// single-character line
Expand All @@ -35,30 +27,26 @@ pub fn find_comment(line: &str) -> Option<Comment> {
let c = line.chars().nth(i).unwrap();
if c == '%' {
if prev_c == ' ' {
return Some(Comment {
idx: i,
});
return Some(i);
} else if prev_c != '\\' {
return Some(Comment {
idx: i,
});
return Some(i);
}
}
prev_c = c;
}
None
}

pub fn remove_comment<'a>(line: &'a str, comm: &Option<Comment>) -> &'a str {
match comm {
Some(c) => &line[0..c.idx],
pub fn remove_comment<'a>(line: &'a str, comment: Option<usize>) -> &'a str {
match comment {
Some(c) => &line[0..c],
None => line,
}
}

pub fn get_comment<'a>(line: &'a str, comm: &Option<Comment>) -> &'a str {
match comm {
Some(c) => &line[c.idx..line.len()],
pub fn get_comment<'a>(line: &'a str, comment: Option<usize>) -> &'a str {
match comment {
Some(c) => &line[c..line.len()],
None => "",
}
}
6 changes: 2 additions & 4 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const CLOSES: [char; 3] = [')', ']', '}'];

#[derive(Debug)]
pub struct Indent {
/// actual running indentation count at end of current line
pub actual: i8,
/// visual indentation of current line
pub visual: i8,
}

Expand Down Expand Up @@ -114,8 +112,8 @@ pub fn apply_indent(file: &str, debug: bool) -> String {

for (i, line) in lines.iter().enumerate() {
// calculate indent
let comm = find_comment(line);
let line_strip = remove_comment(line, &comm);
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
indent = get_indent(line_strip, indent);
if !debug {
assert!(indent.actual >= 0, "line {}: {}", i, line);
Expand Down
10 changes: 5 additions & 5 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ pub fn remove_trailing_spaces(file: &str) -> String {

pub fn begin_end_environments_new_line(file: &str) -> String {
let mut new_file = "".to_string();
let lines: Vec<&str> = file.lines().collect();
for line in lines.iter() {
let lines = file.lines();
for line in lines {
if RE_ENV_BEGIN.is_match(line) || RE_ENV_END.is_match(line) {
let comm = find_comment(line);
let comment = get_comment(line, &comm);
let text = remove_comment(line, &comm);
let comment_index = find_comment_index(line);
let comment = get_comment(line, comment_index);
let text = remove_comment(line, comment_index);
let text = &RE_ENV_BEGIN_SHARED_LINE
.replace_all(text, "$prev\n$env")
.to_string();
Expand Down
10 changes: 5 additions & 5 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ pub fn wrap_line(line: &str) -> String {
let mut can_wrap = true;
while line_needs_wrap(&remaining_line) && can_wrap {
let wrap_point = find_wrap_point(&remaining_line);
let comm = find_comment(&remaining_line);
let comment_index = find_comment_index(&remaining_line);
match wrap_point {
Some(p) => {
let line_start = match comm {
Some(ref c) => {
if p > c.idx {
let line_start = match comment_index {
Some(c) => {
if p > c {
"%"
}
else {
Expand All @@ -64,7 +64,7 @@ pub fn wrap_line(line: &str) -> String {

pub fn wrap(file: &str) -> String {
let mut new_file = "".to_string();
let lines: Vec<&str> = file.lines().collect();
let lines = file.lines();
for line in lines {
if line_needs_wrap(line) {
let new_line = wrap_line(line);
Expand Down

0 comments on commit 6abe09c

Please sign in to comment.