From 1c44ba0f04a0997617d517111d0a08245f0dacac Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 21 Dec 2018 20:11:27 +0100 Subject: simplify newline check with macro --- crates/ra_editor/src/line_index_utils.rs | 34 +++++++++++++------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index e62c5089d..2dd7fd708 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -143,35 +143,33 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex let mut lines: u32 = 0; + macro_rules! test_newline { + ($x:ident) => { + if offset < $x { + return lines; + } else { + lines += 1; + } + }; + } + for &orig_newline in line_index.newlines() { loop { let translated_newline = state.translate(orig_newline); match state.next_newlines(translated_newline) { NextNewlines::Use => { - if offset < translated_newline { - return lines; - } else { - lines += 1; - } + test_newline!(translated_newline); break; } NextNewlines::ReplaceMany(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } break; } NextNewlines::AddMany(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } } } @@ -183,11 +181,7 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex None => break, Some(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } } } -- cgit v1.2.3