1830 lines · 57.3 KB · Rust
RawDownload
mod change;mod convert;mod delete;mod increment;pub(crate) mod mark;mod paste;pub(crate) mod repeat;mod scroll;pub(crate) mod search;pub mod substitute;mod toggle_comments;pub(crate) mod yank; use std::collections::HashMap;use std::sync::Arc; use crate::{    Vim,    indent::IndentDirection,    motion::{self, Motion, first_non_whitespace, next_line_end, right},    object::Object,    state::{Mark, Mode, Operator},    surrounds::SurroundsType,};use collections::BTreeSet;use convert::ConvertTarget;use editor::Anchor;use editor::Bias;use editor::Editor;use editor::scroll::Autoscroll;use editor::{display_map::ToDisplayPoint, movement};use gpui::{Context, Window, actions};use language::{Point, SelectionGoal, ToPoint};use log::error;use multi_buffer::MultiBufferRow; actions!(    vim,    [        InsertAfter,        InsertBefore,        InsertFirstNonWhitespace,        InsertEndOfLine,        InsertLineAbove,        InsertLineBelow,        InsertEmptyLineAbove,        InsertEmptyLineBelow,        InsertAtPrevious,        JoinLines,        JoinLinesNoWhitespace,        DeleteLeft,        DeleteRight,        HelixDelete,        ChangeToEndOfLine,        DeleteToEndOfLine,        Yank,        YankLine,        ChangeCase,        ConvertToUpperCase,        ConvertToLowerCase,        ConvertToRot13,        ConvertToRot47,        ToggleComments,        ShowLocation,        Undo,        Redo,    ]); pub(crate) fn register(editor: &mut Editor, cx: &mut Context<Vim>) {    Vim::action(editor, cx, Vim::insert_after);    Vim::action(editor, cx, Vim::insert_before);    Vim::action(editor, cx, Vim::insert_first_non_whitespace);    Vim::action(editor, cx, Vim::insert_end_of_line);    Vim::action(editor, cx, Vim::insert_line_above);    Vim::action(editor, cx, Vim::insert_line_below);    Vim::action(editor, cx, Vim::insert_empty_line_above);    Vim::action(editor, cx, Vim::insert_empty_line_below);    Vim::action(editor, cx, Vim::insert_at_previous);    Vim::action(editor, cx, Vim::change_case);    Vim::action(editor, cx, Vim::convert_to_upper_case);    Vim::action(editor, cx, Vim::convert_to_lower_case);    Vim::action(editor, cx, Vim::convert_to_rot13);    Vim::action(editor, cx, Vim::convert_to_rot47);    Vim::action(editor, cx, Vim::yank_line);    Vim::action(editor, cx, Vim::toggle_comments);    Vim::action(editor, cx, Vim::paste);    Vim::action(editor, cx, Vim::show_location);     Vim::action(editor, cx, |vim, _: &DeleteLeft, window, cx| {        vim.record_current_action(cx);        let times = Vim::take_count(cx);        let forced_motion = Vim::take_forced_motion(cx);        vim.delete_motion(Motion::Left, times, forced_motion, window, cx);    });    Vim::action(editor, cx, |vim, _: &DeleteRight, window, cx| {        vim.record_current_action(cx);        let times = Vim::take_count(cx);        let forced_motion = Vim::take_forced_motion(cx);        vim.delete_motion(Motion::Right, times, forced_motion, window, cx);    });     Vim::action(editor, cx, |vim, _: &HelixDelete, window, cx| {        vim.record_current_action(cx);        vim.update_editor(window, cx, |_, editor, window, cx| {            editor.change_selections(None, window, cx, |s| {                s.move_with(|map, selection| {                    if selection.is_empty() {                        selection.end = movement::right(map, selection.end)                    }                })            })        });        vim.visual_delete(false, window, cx);    });     Vim::action(editor, cx, |vim, _: &ChangeToEndOfLine, window, cx| {        vim.start_recording(cx);        let times = Vim::take_count(cx);        let forced_motion = Vim::take_forced_motion(cx);        vim.change_motion(            Motion::EndOfLine {                display_lines: false,            },            times,            forced_motion,            window,            cx,        );    });    Vim::action(editor, cx, |vim, _: &DeleteToEndOfLine, window, cx| {        vim.record_current_action(cx);        let times = Vim::take_count(cx);        let forced_motion = Vim::take_forced_motion(cx);        vim.delete_motion(            Motion::EndOfLine {                display_lines: false,            },            times,            forced_motion,            window,            cx,        );    });    Vim::action(editor, cx, |vim, _: &JoinLines, window, cx| {        vim.join_lines_impl(true, window, cx);    });     Vim::action(editor, cx, |vim, _: &JoinLinesNoWhitespace, window, cx| {        vim.join_lines_impl(false, window, cx);    });     Vim::action(editor, cx, |vim, _: &Undo, window, cx| {        let times = Vim::take_count(cx);        Vim::take_forced_motion(cx);        vim.update_editor(window, cx, |_, editor, window, cx| {            for _ in 0..times.unwrap_or(1) {                editor.undo(&editor::actions::Undo, window, cx);            }        });    });    Vim::action(editor, cx, |vim, _: &Redo, window, cx| {        let times = Vim::take_count(cx);        Vim::take_forced_motion(cx);        vim.update_editor(window, cx, |_, editor, window, cx| {            for _ in 0..times.unwrap_or(1) {                editor.redo(&editor::actions::Redo, window, cx);            }        });    });     repeat::register(editor, cx);    scroll::register(editor, cx);    search::register(editor, cx);    substitute::register(editor, cx);    increment::register(editor, cx);} impl Vim {    pub fn normal_motion(        &mut self,        motion: Motion,        operator: Option<Operator>,        times: Option<usize>,        forced_motion: bool,        window: &mut Window,        cx: &mut Context<Self>,    ) {        match operator {            None => self.move_cursor(motion, times, window, cx),            Some(Operator::Change) => self.change_motion(motion, times, forced_motion, window, cx),            Some(Operator::Delete) => self.delete_motion(motion, times, forced_motion, window, cx),            Some(Operator::Yank) => self.yank_motion(motion, times, forced_motion, window, cx),            Some(Operator::AddSurrounds { target: None }) => {}            Some(Operator::Indent) => self.indent_motion(                motion,                times,                forced_motion,                IndentDirection::In,                window,                cx,            ),            Some(Operator::Rewrap) => self.rewrap_motion(motion, times, forced_motion, window, cx),            Some(Operator::Outdent) => self.indent_motion(                motion,                times,                forced_motion,                IndentDirection::Out,                window,                cx,            ),            Some(Operator::AutoIndent) => self.indent_motion(                motion,                times,                forced_motion,                IndentDirection::Auto,                window,                cx,            ),            Some(Operator::ShellCommand) => {                self.shell_command_motion(motion, times, forced_motion, window, cx)            }            Some(Operator::Lowercase) => self.convert_motion(                motion,                times,                forced_motion,                ConvertTarget::LowerCase,                window,                cx,            ),            Some(Operator::Uppercase) => self.convert_motion(                motion,                times,                forced_motion,                ConvertTarget::UpperCase,                window,                cx,            ),            Some(Operator::OppositeCase) => self.convert_motion(                motion,                times,                forced_motion,                ConvertTarget::OppositeCase,                window,                cx,            ),            Some(Operator::Rot13) => self.convert_motion(                motion,                times,                forced_motion,                ConvertTarget::Rot13,                window,                cx,            ),            Some(Operator::Rot47) => self.convert_motion(                motion,                times,                forced_motion,                ConvertTarget::Rot47,                window,                cx,            ),            Some(Operator::ToggleComments) => {                self.toggle_comments_motion(motion, times, forced_motion, window, cx)            }            Some(Operator::ReplaceWithRegister) => {                self.replace_with_register_motion(motion, times, forced_motion, window, cx)            }            Some(Operator::Exchange) => {                self.exchange_motion(motion, times, forced_motion, window, cx)            }            Some(operator) => {                // Can't do anything for text objects, Ignoring                error!("Unexpected normal mode motion operator: {:?}", operator)            }        }        // Exit temporary normal mode (if active).        self.exit_temporary_normal(window, cx);    }     pub fn normal_object(&mut self, object: Object, window: &mut Window, cx: &mut Context<Self>) {        let mut waiting_operator: Option<Operator> = None;        match self.maybe_pop_operator() {            Some(Operator::Object { around }) => match self.maybe_pop_operator() {                Some(Operator::Change) => self.change_object(object, around, window, cx),                Some(Operator::Delete) => self.delete_object(object, around, window, cx),                Some(Operator::Yank) => self.yank_object(object, around, window, cx),                Some(Operator::Indent) => {                    self.indent_object(object, around, IndentDirection::In, window, cx)                }                Some(Operator::Outdent) => {                    self.indent_object(object, around, IndentDirection::Out, window, cx)                }                Some(Operator::AutoIndent) => {                    self.indent_object(object, around, IndentDirection::Auto, window, cx)                }                Some(Operator::ShellCommand) => {                    self.shell_command_object(object, around, window, cx);                }                Some(Operator::Rewrap) => self.rewrap_object(object, around, window, cx),                Some(Operator::Lowercase) => {                    self.convert_object(object, around, ConvertTarget::LowerCase, window, cx)                }                Some(Operator::Uppercase) => {                    self.convert_object(object, around, ConvertTarget::UpperCase, window, cx)                }                Some(Operator::OppositeCase) => {                    self.convert_object(object, around, ConvertTarget::OppositeCase, window, cx)                }                Some(Operator::Rot13) => {                    self.convert_object(object, around, ConvertTarget::Rot13, window, cx)                }                Some(Operator::Rot47) => {                    self.convert_object(object, around, ConvertTarget::Rot47, window, cx)                }                Some(Operator::AddSurrounds { target: None }) => {                    waiting_operator = Some(Operator::AddSurrounds {                        target: Some(SurroundsType::Object(object, around)),                    });                }                Some(Operator::ToggleComments) => {                    self.toggle_comments_object(object, around, window, cx)                }                Some(Operator::ReplaceWithRegister) => {                    self.replace_with_register_object(object, around, window, cx)                }                Some(Operator::Exchange) => self.exchange_object(object, around, window, cx),                _ => {                    // Can't do anything for namespace operators. Ignoring                }            },            Some(Operator::DeleteSurrounds) => {                waiting_operator = Some(Operator::DeleteSurrounds);            }            Some(Operator::ChangeSurrounds { target: None }) => {                if self.check_and_move_to_valid_bracket_pair(object, window, cx) {                    waiting_operator = Some(Operator::ChangeSurrounds {                        target: Some(object),                    });                }            }            _ => {                // Can't do anything with change/delete/yank/surrounds and text objects. Ignoring            }        }        self.clear_operator(window, cx);        if let Some(operator) = waiting_operator {            self.push_operator(operator, window, cx);        }    }     pub(crate) fn move_cursor(        &mut self,        motion: Motion,        times: Option<usize>,        window: &mut Window,        cx: &mut Context<Self>,    ) {        self.update_editor(window, cx, |_, editor, window, cx| {            let text_layout_details = editor.text_layout_details(window);            editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {                s.move_cursors_with(|map, cursor, goal| {                    motion                        .move_point(map, cursor, goal, times, &text_layout_details)                        .unwrap_or((cursor, goal))                })            })        });    }     fn insert_after(&mut self, _: &InsertAfter, window: &mut Window, cx: &mut Context<Self>) {        self.start_recording(cx);        self.switch_mode(Mode::Insert, false, window, cx);        self.update_editor(window, cx, |_, editor, window, cx| {            editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {                s.move_cursors_with(|map, cursor, _| (right(map, cursor, 1), SelectionGoal::None));            });        });    }     fn insert_before(&mut self, _: &InsertBefore, window: &mut Window, cx: &mut Context<Self>) {        self.start_recording(cx);        if self.mode.is_visual() {            let current_mode = self.mode;            self.update_editor(window, cx, |_, editor, window, cx| {                editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {                    s.move_with(|map, selection| {                        if current_mode == Mode::VisualLine {                            let start_of_line = motion::start_of_line(map, false, selection.start);                            selection.collapse_to(start_of_line, SelectionGoal::None)                        } else {                            selection.collapse_to(selection.start, SelectionGoal::None)                        }                    });                });            });        }        self.switch_mode(Mode::Insert, false, window, cx);    }     fn insert_first_non_whitespace(        &mut self,        _: &InsertFirstNonWhitespace,        window: &mut Window,        cx: &mut Context<Self>,    ) {        self.start_recording(cx);        self.switch_mode(Mode::Insert, false, window, cx);        self.update_editor(window, cx, |_, editor, window, cx| {            editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {                s.move_cursors_with(|map, cursor, _| {                    (                        first_non_whitespace(map, false, cursor),                        SelectionGoal::None,                    )                });            });        });    }     fn insert_end_of_line(        &mut self,        _: &InsertEndOfLine,        window: &mut Window,        cx: &mut Context<Self>,    ) {        self.start_recording(cx);        self.switch_mode(Mode::Insert, false, window, cx);        self.update_editor(window, cx, |_, editor, window, cx| {            editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {                s.move_cursors_with(|map, cursor, _| {                    (next_line_end(map, cursor, 1), SelectionGoal::None)                });            });        });    }     fn insert_at_previous(        &mut self,        _: &InsertAtPrevious,        window: &mut Window,        cx: &mut Context<Self>,    ) {        self.start_recording(cx);        self.switch_mode(Mode::Insert, false, window, cx);        self.update_editor(window, cx, |vim, editor, window, cx| {            let Some(Mark::Local(marks)) = vim.get_mark("^", editor, window, cx) else {                return;            };             editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {                s.select_anchor_ranges(marks.iter().map(|mark| *mark..*mark))            });        });    }     fn insert_line_above(        &mut self,        _: &InsertLineAbove,        window: &mut Window,        cx: &mut Context<Self>,    ) {        self.start_recording(cx);        self.switch_mode(Mode::Insert, false, window, cx);        self.update_editor(window, cx, |_, editor, window, cx| {            editor.transact(window, cx, |editor, window, cx| {                let selections = editor.selections.all::<Point>(cx);                let snapshot = editor.buffer().read(cx).snapshot(cx);                 let selection_start_rows: BTreeSet<u32> = selections                    .into_iter()                    .map(|selection| selection.start.row)                    .collect();                let edits = selection_start_rows                    .into_iter()                    .map(|row| {                        let indent = snapshot                            .indent_and_comment_for_line(MultiBufferRow(row), cx)                            .chars()                            .collect::<String>();                         let start_of_line = Point::new(row, 0);                        (start_of_line..start_of_line, indent + "\n")                    })                    .collect::<Vec<_>>();                editor.edit_with_autoindent(edits, cx);                editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {                    s.move_cursors_with(|map, cursor, _| {                        let previous_line = motion::start_of_relative_buffer_row(map, cursor, -1);                        let insert_point = motion::end_of_line(map, false, previous_line, 1);                        (insert_point, SelectionGoal::None)                    });                });            });        });    }     fn insert_line_below(        &mut self,        _: &InsertLineBelow,

Lines 1–500 of 1830.