Skip to content

Commit

Permalink
define new visual line mode (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
tauraamui authored Jun 20, 2024
1 parent 6d8d925 commit 1b46936
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/mode.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import lib.draw
enum Mode as u8 {
normal
visual
visual_line
insert
command
search
Expand Down Expand Up @@ -47,6 +48,7 @@ fn (mode Mode) color() Color {
return match mode {
.normal { status_green }
.visual { status_lilac }
.visual_line { status_lilac }
.insert { status_orange }
.command { status_cyan }
.search { status_purple }
Expand All @@ -59,7 +61,8 @@ fn (mode Mode) color() Color {
fn (mode Mode) str() string {
return match mode {
.normal { "NORMAL" }
.visual { "VISUAL" }
.visual { "VISUAL" }
.visual_line { "VISUAL LINE" }
.insert { "INSERT" }
.command { "COMMAND" }
.search { "SEARCH" }
Expand Down
8 changes: 4 additions & 4 deletions src/view.v
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ fn (mut view View) draw_document(mut ctx draw.Contextable) {

mut cursor_screen_space_y := view.cursor.pos.y - view.from
// draw cursor line
if view.mode != .visual {
if view.mode != .visual_line {
if cursor_screen_space_y > view.code_view_height() - 1 { cursor_screen_space_y = view.code_view_height() - 1 }
ctx.draw_rect(view.x+1, cursor_screen_space_y+1, ctx.window_width(), cursor_screen_space_y+1)
}
Expand All @@ -576,7 +576,7 @@ fn (mut view View) draw_document(mut ctx draw.Contextable) {

document_space_y := view.from + y
match view.mode {
.visual {
.visual_line {
within_selection = view.cursor.line_is_within_selection(document_space_y)
if within_selection { ctx.set_bg_color(r: color.r, g: color.g, b: color.b) }
}
Expand Down Expand Up @@ -1127,8 +1127,8 @@ fn (mut view View) i() {
view.buffer.snapshot()
}

fn (mut view View) v() {
view.mode = .visual
fn (mut view View) shift_v() {
view.mode = .visual_line
view.cursor.selection_start = view.cursor.pos
}

Expand Down
5 changes: 3 additions & 2 deletions src/view_keybinds.v
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn (mut view View) on_key_down(e draw.Event, mut root Root) {
.j { view.exec(view.chord.j()) }
.k { view.exec(view.chord.k()) }
.i { view.exec(view.chord.i()) }
.v { if e.modifiers == .shift { view.v() } }
.v { if e.modifiers == .shift { view.shift_v() } }
.e { view.exec(view.chord.e()) }
.w { view.exec(view.chord.w()) }
.b { view.exec(view.chord.b()) }
Expand Down Expand Up @@ -53,7 +53,8 @@ fn (mut view View) on_key_down(e draw.Event, mut root Root) {
else {}
}
}
.visual {
.visual {}
.visual_line {
match e.code {
.escape { view.escape() }
.h { view.h() }
Expand Down
20 changes: 10 additions & 10 deletions src/view_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn test_resolve_whitespace_prefix_on_line_with_no_text() {
assert resolve_whitespace_prefix(test_line_with_just_4_spaces).len == 4
}

fn test_v_toggles_visual_mode_and_starts_selection() {
fn test_shift_v_toggles_visual_line_mode_and_starts_selection() {
mut clip := clipboard.new()
mut fake_view := View{ log: unsafe { nil }, mode: .normal, clipboard: mut clip }
// manually set the "document" contents
Expand All @@ -233,9 +233,9 @@ fn test_v_toggles_visual_mode_and_starts_selection() {
fake_view.cursor.pos.x = 6

// invoke the 'v' command
fake_view.v()
fake_view.shift_v()

assert fake_view.mode == .visual
assert fake_view.mode == .visual_line
assert fake_view.cursor.selection_active()
assert fake_view.cursor.selection_start == Pos{ 6, 0 }
}
Expand Down Expand Up @@ -541,7 +541,7 @@ fn test_visual_indent_indents_highlighted_lines() {

fake_view.cursor.pos.y = 1

fake_view.v()
fake_view.shift_v()
fake_view.j()
fake_view.j()
fake_view.j()
Expand All @@ -559,7 +559,7 @@ fn test_visual_indent_indents_highlighted_lines() {
}

fn test_visual_unindent_unindents_highlighted_lines() {
mut fake_view := View{ log: unsafe { nil }, mode: .visual, clipboard: clipboard.new(), config: workspace.Config{ insert_tabs_not_spaces: true } }
mut fake_view := View{ log: unsafe { nil }, mode: .visual_line, clipboard: clipboard.new(), config: workspace.Config{ insert_tabs_not_spaces: true } }

fake_view.buffer.lines = [
"1. first line",
Expand All @@ -572,7 +572,7 @@ fn test_visual_unindent_unindents_highlighted_lines() {

fake_view.cursor.pos.y = 1

fake_view.v()
fake_view.shift_v()
fake_view.j()
fake_view.j()
fake_view.j()
Expand All @@ -599,7 +599,7 @@ fn test_visual_insert_mode_and_delete_in_place() {
fake_view.cursor.pos.x = 0
fake_view.cursor.pos.y = 1

fake_view.v()
fake_view.shift_v()
fake_view.visual_d(true)

assert fake_view.mode == .normal
Expand All @@ -616,7 +616,7 @@ fn test_visual_insert_mode_selection_move_down_once_and_delete() {
fake_view.cursor.pos.x = 0
fake_view.cursor.pos.y = 1

fake_view.v()
fake_view.shift_v()
fake_view.j()
fake_view.visual_d(true)

Expand All @@ -641,7 +641,7 @@ fn test_visual_selection_copy() {
fake_view.cursor.pos.x = 0
fake_view.cursor.pos.y = 1

fake_view.v()
fake_view.shift_v()
fake_view.j()
fake_view.visual_y()

Expand Down Expand Up @@ -707,7 +707,7 @@ fn test_visual_paste() {
// ensure cursor is set to sit on second line
fake_view.cursor.pos.x = 0
fake_view.cursor.pos.y = 1
fake_view.v()
fake_view.shift_v()
fake_view.j()

fake_view.visual_p()
Expand Down

0 comments on commit 1b46936

Please sign in to comment.