Skip to content

Commit 068735c

Browse files
author
Noam Lewis
committed
Revert "maintain chunk index adjacent to each line"
This reverts commit 07008b8.
1 parent 07008b8 commit 068735c

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

src/virtual_file.rs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ impl LoadStore for FileLoadStore {
3737
}
3838
}
3939

40-
struct LoadedLine {
41-
chunk_index: ChunkIndex,
42-
line: EditLine,
43-
}
44-
4540
pub struct VirtualFile {
4641
// configuration
4742
chunk_size: u64,
@@ -53,7 +48,7 @@ pub struct VirtualFile {
5348
loaded_chunks: Range<ChunkIndex>,
5449

5550
/// lines loaded from memstore (disk)
56-
chunk_lines: Vec<LoadedLine>,
51+
chunk_lines: Vec<EditLine>,
5752

5853
memstore: Memstore<FileLoadStore>,
5954
}
@@ -85,22 +80,21 @@ impl VirtualFile {
8580
Chunk::Loaded {
8681
data,
8782
need_store: _,
88-
} => Self::parse_chunk(&index, data),
83+
} => Self::parse_chunk(data),
8984
Chunk::Empty => vec![],
9085
};
9186
self.update_chunk_lines(index, new_chunk_lines);
9287
}
9388

94-
fn update_chunk_lines(&mut self, new_index: ChunkIndex, mut new_chunk_lines: Vec<LoadedLine>) {
89+
fn update_chunk_lines(&mut self, new_index: ChunkIndex, mut new_chunk_lines: Vec<EditLine>) {
9590
if new_index == self.loaded_chunks.end && !self.loaded_chunks.is_empty() {
9691
self.loaded_chunks.end = new_index.next();
9792
// append new lines to existing lines
9893
// line_index is relative to the range start which stays unchanged.
9994
self.chunk_lines
10095
.last_mut()
10196
.unwrap()
102-
.line
103-
.extend(new_chunk_lines.remove(0).line);
97+
.extend(new_chunk_lines.remove(0));
10498
self.chunk_lines.append(&mut new_chunk_lines);
10599
} else if new_index.next() == self.loaded_chunks.start && !self.loaded_chunks.is_empty() {
106100
self.loaded_chunks.start = new_index;
@@ -111,8 +105,7 @@ impl VirtualFile {
111105
self.chunk_lines
112106
.last_mut()
113107
.unwrap()
114-
.line
115-
.extend(new_chunk_lines.remove(0).line);
108+
.extend(new_chunk_lines.remove(0));
116109
self.chunk_lines.append(&mut new_chunk_lines);
117110
} else {
118111
// replace existing lines
@@ -160,44 +153,31 @@ impl VirtualFile {
160153
self.line_index -= 1;
161154
} else if self.chunk_lines.len() == 0 {
162155
// that was the only line left, add one back to avoid empty
163-
self.chunk_lines.push(LoadedLine {
164-
chunk_index: ChunkIndex::from_offset(0, self.chunk_size),
165-
line: EditLine::empty(),
166-
});
156+
self.chunk_lines.push(EditLine::empty());
167157
}
168-
return removed_line.line;
158+
return removed_line;
169159
}
170160

171161
pub fn get_index(&self) -> usize {
172162
self.line_index
173163
}
174164

175165
pub fn insert_after(&mut self, new_line: EditLine) {
176-
let line = self.chunk_lines.get(self.line_index).unwrap();
177-
self.chunk_lines.insert(
178-
self.line_index + 1,
179-
LoadedLine {
180-
chunk_index: line.chunk_index.clone(),
181-
line: new_line,
182-
},
183-
);
166+
self.chunk_lines.insert(self.line_index + 1, new_line);
184167
}
185168

186169
pub fn get(&self) -> &EditLine {
187-
&self.chunk_lines.get(self.line_index).unwrap().line
170+
self.chunk_lines.get(self.line_index).unwrap()
188171
}
189172

190173
pub fn get_mut(&mut self) -> &mut EditLine {
191-
&mut self.chunk_lines.get_mut(self.line_index).unwrap().line
174+
self.chunk_lines.get_mut(self.line_index).unwrap()
192175
}
193176

194-
fn parse_chunk(chunk_index: &ChunkIndex, data: &Vec<u8>) -> Vec<LoadedLine> {
177+
fn parse_chunk(data: &Vec<u8>) -> Vec<EditLine> {
195178
String::from_utf8_lossy(data)
196179
.split(|c: char| c == '\n')
197-
.map(|s| LoadedLine {
198-
chunk_index: chunk_index.clone(),
199-
line: EditLine::new(s.to_string()),
200-
})
180+
.map(|s| EditLine::new(s.to_string()))
201181
.collect()
202182
}
203183

@@ -228,7 +208,7 @@ impl VirtualFile {
228208
// ... and now go back to where line_index was before
229209
self.line_index = (self.line_index as i32 - clobber).try_into().unwrap();
230210

231-
self.chunk_lines.iter().skip(start_index).map(|l| &l.line)
211+
self.chunk_lines.iter().skip(start_index)
232212
}
233213
}
234214

0 commit comments

Comments
 (0)