on master help sign in
simplify.
commita924ad288560cff871a4e653e000396c78805252 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 5 Feb 2021, 16:06 (6 years ago) parentc70080d
3 files changed+30−17
helix-core/src/selection.rs+1 helix-term/src/ui/editor.rs+23−17 helix-view/src/tree.rs+6
helix-core/src/selection.rs+1
@@ -184,6 +184,7 @@ impl Selection {
184184 Self::single(pos, pos)
185185 }
186186
187+ // TODO: consume an iterator or a vec to reduce allocations?
187188 #[must_use]
188189 pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
189190 fn normalize(mut ranges: SmallVec<[Range; 1]>, mut primary_index: usize) -> Selection {
helix-term/src/ui/editor.rs+23−17
@@ -69,15 +69,15 @@ impl EditorView {
6969 is_focused: bool,
7070 ) {
7171 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
72− let source_code = view.doc.text().to_string();
72+ let text = view.doc.text();
73+ let source_code = text.to_string();
7374
7475 let last_line = view.last_line();
7576
7677 let range = {
7778 // calculate viewport byte ranges
78− let start = view.doc.text().line_to_byte(view.first_line);
79− let end = view.doc.text().line_to_byte(last_line)
80− + view.doc.text().line(last_line).len_bytes();
79+ let start = text.line_to_byte(view.first_line);
80+ let end = text.line_to_byte(last_line) + text.line(last_line).len_bytes();
8181
8282 start..end
8383 };
@@ -126,10 +126,12 @@ impl EditorView {
126126 HighlightEvent::Source { start, end } => {
127127 // TODO: filter out spans out of viewport for now..
128128
129− let start = view.doc.text().byte_to_char(start);
130− let end = view.doc.text().byte_to_char(end); // <-- index 744, len 743
129+ // TODO: do these before iterating
130+ let text = view.doc.text();
131+ let start = text.byte_to_char(start);
132+ let end = text.byte_to_char(end);
131133
132− let text = view.doc.text().slice(start..end);
134+ let text = text.slice(start..end);
133135
134136 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
135137
@@ -141,6 +143,7 @@ impl EditorView {
141143 // TODO: we could render the text to a surface, then cache that, that
142144 // way if only the selection/cursor changes we can copy from cache
143145 // and paint the new cursor.
146+ // We could keep a single resizable surface on the View for that.
144147
145148 let mut char_index = start;
146149
@@ -159,6 +162,13 @@ impl EditorView {
159162 } else if grapheme == "\t" {
160163 visual_x += (TAB_WIDTH as u16);
161164 } else {
165+ if visual_x >= viewport.width {
166+ // if we're offscreen just keep going until we hit a new line
167+ // TODO: will need tweaking when we also take into account
168+ // horizontal scrolling
169+ continue;
170+ }
171+
162172 // Cow will prevent allocations if span contained in a single slice
163173 // which should really be the majority case
164174 let grapheme = Cow::from(grapheme);
@@ -198,16 +208,12 @@ impl EditorView {
198208
199209 // TODO: paint cursor heads except primary
200210
201− // HAXX: we don't render the char if it's offscreen. This should be
202− // skipped in a better way much earlier
203− if visual_x < viewport.width {
204− surface.set_string(
205− viewport.x + visual_x,
206− viewport.y + line,
207− grapheme,
208− style,
209− );
210− }
211+ surface.set_string(
212+ viewport.x + visual_x,
213+ viewport.y + line,
214+ grapheme,
215+ style,
216+ );
211217
212218 visual_x += width;
213219 }
helix-view/src/tree.rs+6
@@ -57,6 +57,12 @@ impl Container {
5757 }
5858}
5959
60+impl Default for Container {
61+ fn default() -> Self {
62+ Self::new()
63+ }
64+}
65+
6066impl Tree {
6167 pub fn new(area: Rect) -> Self {
6268 let root = Node::container();