Repository: alacritty/alacritty
File: alacritty_terminal/src/term/mod.rs (Rust)

Code before:
        }
    }

    #[must_use]
    pub fn damage(&mut self, selection: Option<SelectionRange>) -> TermDamage<'_> {
        // Ensure the entire terminal is damaged after entering insert mode.
        // Leaving is handled in the ansi handler.
        if self.mode.contains(TermMode::INSERT) {
            self.mark_fully_damaged();
        }

        // Update tracking of cursor, selection, and vi mode cursor.

        let previous_cursor = mem::replace(&mut self.damage.last_cursor, self.grid.cursor.point);
        let previous_selection = mem::replace(&mut self.damage.last_selection, selection);

        let display_offset = self.grid().display_offset();
        let vi_cursor_point = if self.mode.contains(TermMode::VI) {
            let vi_cursor_point =
                point_to_viewport(display_offset, self.vi_mode_cursor.point).unwrap();
            Some(vi_cursor_point)
        } else {
            None
        };

        let previous_vi_cursor_point =
            mem::replace(&mut self.damage.last_vi_cursor_point, vi_cursor_point);

        // Early return if the entire terminal is damaged.
        if self.damage.is_fully_damaged {
            return TermDamage::Full;
        }

        // Add information about old cursor position and new one if they are not the same, so we
        // cover everything that was produced by `Term::input`.
        if self.damage.last_cursor != previous_cursor {
            // Cursor cooridanates are always inside viewport even if you have `display_offset`.
            let point = Point::new(previous_cursor.line.0 as usize, previous_cursor.column);
            self.damage.damage_point(point);
        }

        // Always damage current cursor.
        self.damage_cursor();

        // Vi mode doesn't update the terminal content, thus only last vi cursor position and the
        // new one should be damaged.
        if let Some(previous_vi_cursor_point) = previous_vi_cursor_point {
            self.damage.damage_point(previous_vi_cursor_point)
        }

        // Damage Vi cursor if it's present.

Review comment (suggestion):
```suggestion
        let display_offset = self.grid().display_offset();
        let vi_cursor_point = if self.mode.contains(TermMode::VI) {
            let vi_cursor_point =
                point_to_viewport(display_offset, self.vi_mode_cursor.point).unwrap();
            Some(vi_cursor_point)
        } else {
            None
        };
        
        let previous_cursor = mem::replace(&mut self.damage.last_cursor, self.grid.cursor.point);
        let previous_selection = mem::replace(&mut self.damage.last_selection, selection);
        let previous_vi_cursor_point =
            mem::replace(&mut self.damage.last_vi_cursor_point, vi_cursor_point);
```

I think this way around would be pretty cool, this way the mem replace stuff is all together.

Code after:
        }
    }

    #[must_use]
    pub fn damage(&mut self, selection: Option<SelectionRange>) -> TermDamage<'_> {
        // Ensure the entire terminal is damaged after entering insert mode.
        // Leaving is handled in the ansi handler.
        if self.mode.contains(TermMode::INSERT) {
            self.mark_fully_damaged();
        }

        // Update tracking of cursor, selection, and vi mode cursor.

        let display_offset = self.grid().display_offset();
        let vi_cursor_point = if self.mode.contains(TermMode::VI) {
            point_to_viewport(display_offset, self.vi_mode_cursor.point)
        } else {
            None
        };

        let previous_cursor = mem::replace(&mut self.damage.last_cursor, self.grid.cursor.point);
        let previous_selection = mem::replace(&mut self.damage.last_selection, selection);
        let previous_vi_cursor_point =
            mem::replace(&mut self.damage.last_vi_cursor_point, vi_cursor_point);

        // Early return if the entire terminal is damaged.
        if self.damage.is_fully_damaged {
            return TermDamage::Full;
        }

        // Add information about old cursor position and new one if they are not the same, so we
        // cover everything that was produced by `Term::input`.
        if self.damage.last_cursor != previous_cursor {
            // Cursor cooridanates are always inside viewport even if you have `display_offset`.
            let point = Point::new(previous_cursor.line.0 as usize, previous_cursor.column);
            self.damage.damage_point(point);
        }

        // Always damage current cursor.
        self.damage_cursor();

        // Vi mode doesn't update the terminal content, thus only last vi cursor position and the
        // new one should be damaged.
        if let Some(previous_vi_cursor_point) = previous_vi_cursor_point {
            self.damage.damage_point(previous_vi_cursor_point)
        }

        // Damage Vi cursor if it's present.
        if let Some(vi_cursor_point) = self.damage.last_vi_cursor_point {
            self.damage.damage_point(vi_cursor_point);
        }