Skip to content

Commit de29b0a

Browse files
committed
refactor: remove unused code and fields from typing_core
- Remove unused fields: * text_original: never accessed after initialization * line_starts: calculate_line_starts() method also removed * mistake_positions: statistics tracking not implemented - Remove unused methods: * text_original(): getter for removed field * line_starts(): getter for removed field * mistake_positions(): getter for removed field * current_char_to_display(): never called externally * reset_position(): never called externally * calculate_line_starts(): helper for removed field - Simplify record_mistake() to only track current mistake position - Clean code structure while maintaining all functionality
1 parent 67ccd84 commit de29b0a

File tree

1 file changed

+8
-59
lines changed

1 file changed

+8
-59
lines changed

src/game/typing_core.rs

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ use crate::models::Challenge;
22

33
#[derive(Debug, Clone)]
44
pub struct TypingCore {
5-
// Original source code with comments and formatting
6-
text_original: String,
7-
85
// Text for typing logic (comments and empty lines removed)
96
text_to_type: String,
107
current_position_to_type: usize,
@@ -17,11 +14,9 @@ pub struct TypingCore {
1714

1815
// Metadata
1916
comment_ranges: Vec<(usize, usize)>,
20-
line_starts: Vec<usize>,
2117

2218
// Mistake tracking
2319
mistakes: usize,
24-
mistake_positions: Vec<usize>, // typing positions for statistics
2520
current_mistake_position: Option<usize>, // display position for highlighting
2621
}
2722

@@ -44,6 +39,14 @@ impl Default for ProcessingOptions {
4439
}
4540
}
4641

42+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43+
pub enum InputResult {
44+
Correct, // Input was correct, continue
45+
Incorrect, // Input was incorrect (mistake)
46+
Completed, // Input was correct and typing is complete
47+
NoAction, // No input accepted (already completed)
48+
}
49+
4750
impl TypingCore {
4851
pub fn new(
4952
original_text: &str,
@@ -56,8 +59,6 @@ impl TypingCore {
5659
let (text_to_display, text_mapping_to_display) =
5760
Self::create_display_text(original_text, comment_ranges, &options);
5861

59-
let line_starts = Self::calculate_line_starts(&text_to_display);
60-
6162
let initial_position_to_type = text_to_type
6263
.char_indices()
6364
.find(|(_, ch)| !ch.is_whitespace() || *ch == '\n')
@@ -76,17 +77,14 @@ impl TypingCore {
7677
};
7778

7879
Self {
79-
text_original: original_text.to_string(),
8080
text_to_type,
8181
current_position_to_type: initial_position_to_type,
8282
mapping_to_type: text_mapping_to_type,
8383
text_to_display,
8484
current_position_to_display: initial_position_to_display,
8585
mapping_to_display: text_mapping_to_display,
8686
comment_ranges: comment_ranges.to_vec(),
87-
line_starts,
8887
mistakes: 0,
89-
mistake_positions: Vec::new(),
9088
current_mistake_position: None,
9189
}
9290
}
@@ -96,11 +94,6 @@ impl TypingCore {
9694
Self::new(&challenge.code_content, &challenge.comment_ranges, options)
9795
}
9896

99-
// Getters for the three text representations
100-
pub fn text_original(&self) -> &str {
101-
&self.text_original
102-
}
103-
10497
// text_to_type
10598
pub fn text_to_type(&self) -> &str {
10699
&self.text_to_type
@@ -123,12 +116,6 @@ impl TypingCore {
123116
self.current_position_to_display
124117
}
125118

126-
pub fn current_char_to_display(&self) -> Option<char> {
127-
self.text_to_display
128-
.chars()
129-
.nth(self.current_position_to_display)
130-
}
131-
132119
pub fn current_line_to_display(&self) -> usize {
133120
// Count newlines up to current position to determine line number
134121
self.text_to_display
@@ -138,20 +125,11 @@ impl TypingCore {
138125
.count()
139126
}
140127

141-
// Others
142-
pub fn line_starts(&self) -> &[usize] {
143-
&self.line_starts
144-
}
145-
146128
// Mistake tracking
147129
pub fn mistakes(&self) -> usize {
148130
self.mistakes
149131
}
150132

151-
pub fn mistake_positions(&self) -> &[usize] {
152-
&self.mistake_positions
153-
}
154-
155133
pub fn current_mistake_position(&self) -> Option<usize> {
156134
self.current_mistake_position
157135
}
@@ -349,16 +327,6 @@ impl TypingCore {
349327
(display_text, position_mapping)
350328
}
351329

352-
fn calculate_line_starts(text: &str) -> Vec<usize> {
353-
let mut line_starts = vec![0];
354-
for (i, ch) in text.chars().enumerate() {
355-
if ch == '\n' && i + 1 < text.len() {
356-
line_starts.push(i + 1);
357-
}
358-
}
359-
line_starts
360-
}
361-
362330
fn update_display_position(&mut self) {
363331
// Use the mapping arrays to find the correct display position
364332
if self.current_position_to_type < self.mapping_to_type.len() {
@@ -409,16 +377,6 @@ impl TypingCore {
409377
self.update_display_position();
410378
}
411379

412-
pub fn reset_position(&mut self) {
413-
self.current_position_to_type = self
414-
.text_to_type
415-
.char_indices()
416-
.find(|(_, ch)| !ch.is_whitespace() || *ch == '\n')
417-
.map(|(pos, _)| pos)
418-
.unwrap_or(0);
419-
self.update_display_position();
420-
}
421-
422380
// Helper methods for typing logic
423381
pub fn is_completed(&self) -> bool {
424382
self.current_position_to_type >= self.text_to_type.len()
@@ -451,7 +409,6 @@ impl TypingCore {
451409

452410
fn record_mistake(&mut self) {
453411
self.mistakes += 1;
454-
self.mistake_positions.push(self.current_position_to_type);
455412
self.current_mistake_position = Some(self.current_position_to_display);
456413
}
457414

@@ -517,11 +474,3 @@ impl TypingCore {
517474
}
518475
}
519476
}
520-
521-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
522-
pub enum InputResult {
523-
Correct, // Input was correct, continue
524-
Incorrect, // Input was incorrect (mistake)
525-
Completed, // Input was correct and typing is complete
526-
NoAction, // No input accepted (already completed)
527-
}

0 commit comments

Comments
 (0)