Skip to content

Commit

Permalink
Feature: Add basic Vim marks feature
Browse files Browse the repository at this point in the history
Adds CreateMark and GoToMark funcitonality, but only local version:
e.g. within current buffer. (no global marks yet).
  • Loading branch information
UltraArtem committed May 25, 2023
1 parent e7f9261 commit ede8843
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features/Changes

- [#1964](https://github.com/lapce/lapce/pull/1964): Add option to open files at line/column
- [#2403](https://github.com/lapce/lapce/pull/2403): Add basic Vim marks feature

### Bug Fixes

Expand Down
10 changes: 10 additions & 0 deletions defaults/keymaps-common.toml
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ key = "d"
command = "delete_forward"
mode = "v"

[[keymaps]]
key = "m"
command = "create_mark"
mode = "nv"

[[keymaps]]
key = "'"
command = "go_to_mark"
mode = "nv"

[[keymaps]]
key = "f"
command = "inline_find_right"
Expand Down
4 changes: 4 additions & 0 deletions lapce-core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ pub enum FocusCommand {
InlineFindRight,
#[strum(serialize = "inline_find_left")]
InlineFindLeft,
#[strum(serialize = "create_mark")]
CreateMark,
#[strum(serialize = "go_to_mark")]
GoToMark,
#[strum(serialize = "repeat_last_inline_find")]
RepeatLastInlineFind,
#[strum(message = "Save")]
Expand Down
28 changes: 19 additions & 9 deletions lapce-data/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
collections::{HashMap, HashSet},
env,
fmt::Display,
hash::Hash,
io::{BufReader, Read, Write},
path::{Path, PathBuf},
rc::Rc,
Expand Down Expand Up @@ -994,10 +995,10 @@ impl LapceTabData {
}
}

/// Get information about the specific editor, with various data so that it can provide useful
/// utility functions for the editor buffer.
/// Get information about the specific editor, with various data so that it can provide useful
/// utility functions for the editor buffer.
/// Note that if you edit the editor buffer or related fields, then you'll have to 'give it
/// back' to [`LapceTabData`] so that it can update the internals.
/// back' to [`LapceTabData`] so that it can update the internals.
/// ```rust,ignore
/// // Get the editor before it may be modified by the `editor_data`
/// let editor = data.main_split.editors.get(&view_id).unwrap().clone();
Expand Down Expand Up @@ -1088,7 +1089,7 @@ impl LapceTabData {
}
}

/// Update the stored information with the changed editor buffer data.
/// Update the stored information with the changed editor buffer data.
/// ```rust,ignore
/// // Get the editor before it may be modified by the `editor_data`
/// let editor = data.main_split.editors.get(&view_id).unwrap().clone();
Expand Down Expand Up @@ -3007,9 +3008,9 @@ impl LapceMainSplitData {
return Arc::make_mut(self.editors.get_mut(&new_editor.view_id).unwrap());
}

/// If the supplied `editor_view_id` is some, then this simply returns the editor data for it.
/// If the supplied `editor_view_id` is some, then this simply returns the editor data for it.
/// Otherwise, we check the active tab (and friends if `same_tab` is false) to see if there is
/// an existing editor that matches the parameters. If not, we create a new editor.
/// an existing editor that matches the parameters. If not, we create a new editor.
/// Note that this does not load the file into the editor. See
/// [`LapceMainSplitData::jump_to_location`] or [`LapceMainSplitData::go_to_location`] for
/// creating the editor and loading the file.
Expand Down Expand Up @@ -3217,7 +3218,7 @@ impl LapceMainSplitData {
)
}

/// Jump to a specific location, getting/creating the editor as needed.
/// Jump to a specific location, getting/creating the editor as needed.
/// This version allows a callback which will be called once the buffer is loaded.
pub fn jump_to_location_cb<
P: EditorPosition + Send + 'static,
Expand Down Expand Up @@ -4367,6 +4368,13 @@ pub enum InlineFindDirection {
Right,
}

#[derive(Clone, Debug)]

Check warning on line 4371 in lapce-data/src/data.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/data.rs#L4371

Added line #L4371 was not covered by tests
pub enum ModalCommand {
InlineFind(InlineFindDirection),
CreateMark,
GoToMark,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EditorTabChild {
Editor(WidgetId, WidgetId, Option<(WidgetId, WidgetId)>),
Expand Down Expand Up @@ -4509,8 +4517,9 @@ pub struct LapceEditorData {
pub snippet: Option<Vec<(usize, (usize, usize))>>,
pub last_movement_new: Movement,
pub last_inline_find: Option<(InlineFindDirection, String)>,
pub inline_find: Option<InlineFindDirection>,
pub motion_mode: Option<MotionMode>,
pub next_modal_command: Option<ModalCommand>,
pub marks: HashMap<String, Position>,
}

impl LapceEditorData {
Expand Down Expand Up @@ -4552,9 +4561,10 @@ impl LapceEditorData {
window_origin: Rc::new(RefCell::new(Point::ZERO)),
snippet: None,
last_movement_new: Movement::Left,
inline_find: None,
last_inline_find: None,
motion_mode: None,
next_modal_command: None,
marks: HashMap::new(),

Check warning on line 4567 in lapce-data/src/data.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/data.rs#L4566-L4567

Added lines #L4566 - L4567 were not covered by tests
}
}

Expand Down
65 changes: 53 additions & 12 deletions lapce-data/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::{
config::LapceConfig,
data::{
EditorDiagnostic, EditorView, FocusArea, InlineFindDirection,
LapceEditorData, LapceMainSplitData, SplitContent,
LapceEditorData, LapceMainSplitData, ModalCommand, SplitContent,
},
document::{BufferContent, Document, LocalBufferKind},
find::Find,
Expand Down Expand Up @@ -179,7 +179,7 @@ impl EditorPosition for Position {
}
}

/// Used to specify a location with some path, and potentially position information.
/// Used to specify a location with some path, and potentially position information.
/// This is generic so that you can jump to utf8 offsets, line+column offsets, just the line,
/// or even utf16 offsets (such as those given by the LSP)
#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -241,6 +241,28 @@ impl LapceEditorBufferData {
}
}

fn create_mark(&mut self, mark_name: &str) {
let cursor_position = self
.doc
.buffer()
.offset_to_position(self.editor.cursor.offset());

let editor = Arc::make_mut(&mut self.editor);
editor.marks.insert(mark_name.to_owned(), cursor_position);
}

Check warning on line 252 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L244-L252

Added lines #L244 - L252 were not covered by tests

fn go_to_mark(&mut self, ctx: &mut EventCtx, mark_name: &str) {
if let Some(marked_position) = self.editor.marks.get(mark_name) {
let offset = self.doc.buffer().offset_of_position(marked_position);
self.run_move_command(
ctx,
&lapce_core::movement::Movement::Offset(offset),
None,
Modifiers::empty(),
);
}
}

Check warning on line 264 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L255-L264

Added lines #L255 - L264 were not covered by tests

/// Jump to the next/previous column on the line which matches the given text
fn inline_find(
&mut self,
Expand Down Expand Up @@ -359,7 +381,7 @@ impl LapceEditorBufferData {
}
}

/// Update the positions of cursors in other editors which are editing the same document
/// Update the positions of cursors in other editors which are editing the same document
/// Ex: You type at the start of the document, the cursor in the other editor (like a split)
/// should be moved forward.
fn inactive_apply_delta(&mut self, delta: &RopeDelta) {
Expand Down Expand Up @@ -2414,13 +2436,21 @@ impl LapceEditorBufferData {
));
}
}
CreateMark => {
Arc::make_mut(&mut self.editor).next_modal_command =
Some(ModalCommand::CreateMark);
}
GoToMark => {
Arc::make_mut(&mut self.editor).next_modal_command =
Some(ModalCommand::GoToMark);
}

Check warning on line 2446 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L2439-L2446

Added lines #L2439 - L2446 were not covered by tests
InlineFindLeft => {
Arc::make_mut(&mut self.editor).inline_find =
Some(InlineFindDirection::Left);
Arc::make_mut(&mut self.editor).next_modal_command =
Some(ModalCommand::InlineFind(InlineFindDirection::Left));

Check warning on line 2449 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L2448-L2449

Added lines #L2448 - L2449 were not covered by tests
}
InlineFindRight => {
Arc::make_mut(&mut self.editor).inline_find =
Some(InlineFindDirection::Right);
Arc::make_mut(&mut self.editor).next_modal_command =
Some(ModalCommand::InlineFind(InlineFindDirection::Right));

Check warning on line 2453 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L2452-L2453

Added lines #L2452 - L2453 were not covered by tests
}
RepeatLastInlineFind => {
if let Some((direction, c)) = self.editor.last_inline_find.clone() {
Expand Down Expand Up @@ -2631,7 +2661,7 @@ impl KeyPressFocus for LapceEditorBufferData {
}

fn expect_char(&self) -> bool {
self.editor.inline_find.is_some()
self.editor.next_modal_command.is_some()

Check warning on line 2664 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L2664

Added line #L2664 was not covered by tests
}

fn check_condition(&self, condition: &str) -> bool {
Expand Down Expand Up @@ -2694,11 +2724,22 @@ impl KeyPressFocus for LapceEditorBufferData {
}
self.cancel_hover();
self.apply_deltas(&deltas);
} else if let Some(direction) = self.editor.inline_find.clone() {
self.inline_find(ctx, direction.clone(), c);
} else if let Some(modal_command) = self.editor.next_modal_command.clone() {
match modal_command {
ModalCommand::CreateMark => {
self.create_mark(c);
}
ModalCommand::GoToMark => {
self.go_to_mark(ctx, c);
}
ModalCommand::InlineFind(direction) => {
self.inline_find(ctx, direction.clone(), c);
let editor = Arc::make_mut(&mut self.editor);
editor.last_inline_find = Some((direction, c.to_string()));
}

Check warning on line 2739 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L2727-L2739

Added lines #L2727 - L2739 were not covered by tests
}
let editor = Arc::make_mut(&mut self.editor);
editor.last_inline_find = Some((direction, c.to_string()));
editor.inline_find = None;
editor.next_modal_command = None;

Check warning on line 2742 in lapce-data/src/editor.rs

View check run for this annotation

Codecov / codecov/patch

lapce-data/src/editor.rs#L2742

Added line #L2742 was not covered by tests
}
}

Expand Down

0 comments on commit ede8843

Please sign in to comment.