@@ 385,14 385,15 @@ impl View for EditorView {
(self.scrollbase.start_line + self.scrollbase.view_height)
.min(self.buf.text().len_lines());
- // Cache the line strings.
- for line_idx in line_idc.clone() {
- if let None = self.lines.get(&line_idx) {
- self.lines.insert(line_idx,
+ // Automatically allocate and cache the line strings.
+ fn get_line<'a, 'b>(line_idx: usize, buf: &'b Buffer, lines: &'a mut BTreeMap<usize, String>) -> &'a str {
+ if let None = lines.get(&line_idx) {
+ lines.insert(line_idx,
// allocate owned string
- self.buf.text().line(line_idx).to_string()
+ buf.text().line(line_idx).to_string()
);
}
+ lines.get(&line_idx).unwrap().as_str()
}
{ // Cache the line styles.
@@ 406,18 407,15 @@ impl View for EditorView {
.unwrap_or_else(|| (0, self.highlighting.start()));
for line_idx in start_line_idx..line_idx {
- highlight.parse(
- self.lines.get(&line_idx).expect("line not allocated").as_str()
- );
+ highlight.parse(get_line(line_idx, &self.buf, &mut self.lines));
}
highlight
});
- let line = self.lines.get(&line_idx).expect("line not allocated").as_str();
self.spans.insert(line_idx,
// highlight syntax
- highlight.highlight(line)
+ highlight.highlight(get_line(line_idx, &self.buf, &mut self.lines))
);
current_highlight = Some(highlight);
@@ 539,7 537,7 @@ impl View for EditorView {
let mut style: Style = ColorStyle::primary().into(); // fallback
// find style at span under cursor
if let Some(spans) = self.spans.get(&line_idx) {
- let line = self.lines.get(&line_idx).expect("line not cached");
+ let line = get_line(line_idx, &self.buf, &mut self.lines);
let mut x_acc = 0;
for span in spans.iter() {