Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lil 76/fix/visual selection highlight #226

Merged
merged 16 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
51 changes: 17 additions & 34 deletions src/view.v
Original file line number Diff line number Diff line change
Expand Up @@ -791,42 +791,25 @@ fn draw_text_line_visual_selection_starts_and_ends_on_same_line(mut ctx draw.Con
line_runes []rune,
original_line_runes []rune
) {
mut x_offset := 0
tab_count := original_line_runes[..selection_start.x].string().count('\t')
selection_x_offset := tab_count * 3
pre_sel := line_runes[..selection_start.x + selection_x_offset]
sel := line_runes[selection_start.x + x_offset + selection_x_offset..selection_end.x +
selection_x_offset]
post_sel := line_runes[selection_end.x + selection_x_offset..]

if pre_sel.len != 0 {
if screen_space_y == cursor_screen_space_y {
ctx.set_bg_color(r: 53, g: 53, b: 53)
}
draw_text_line_as_segments(mut ctx, syntax, screen_space_x + x_offset, screen_space_y,
document_space_y, pre_sel.string())
x_offset += pre_sel.len
}
ctx.set_bg_color(r: 53, g: 53, b: 53)
defer { ctx.reset_bg_color() }
pre_tab_count := original_line_runes[..selection_start.x].string().count('\t')
pre_selection := line_runes[..selection_start.x + (pre_tab_count * 3)]
draw_text_line_as_segments(mut ctx, syntax, screen_space_x, screen_space_y, document_space_y, pre_selection.string())

if sel.len != 0 {
ctx.set_bg_color(
r: selection_highlight_color.r
g: selection_highlight_color.g
b: selection_highlight_color.b
)
ctx.draw_text(screen_space_x + x_offset + 1, screen_space_y + 1, sel.string())
ctx.reset_bg_color()
x_offset += sel.len
}
sel_tab_count := original_line_runes[selection_start.x..selection_end.x].string().count('\t')
within_selection := line_runes[selection_start.x + (pre_tab_count * 3)..selection_end.x + ((pre_tab_count + sel_tab_count) * 3)]
ctx.set_bg_color(
r: selection_highlight_color.r
g: selection_highlight_color.g
b: selection_highlight_color.b
)
ctx.draw_text(screen_space_x + 1 + pre_selection.len, screen_space_y + 1, within_selection.string())
ctx.reset_bg_color()

if post_sel.len != 0 {
if screen_space_y == cursor_screen_space_y {
ctx.set_bg_color(r: 53, g: 53, b: 53)
}
draw_text_line_as_segments(mut ctx, syntax, screen_space_x + x_offset, screen_space_y,
document_space_y, post_sel.string())
x_offset += post_sel.len
}
ctx.set_bg_color(r: 53, g: 53, b: 53)
post_selection := line_runes[selection_end.x + ((pre_tab_count + sel_tab_count) * 3)..]
draw_text_line_as_segments(mut ctx, syntax, screen_space_x + pre_selection.len + within_selection.len, screen_space_y, document_space_y, post_selection.string())
}

fn draw_text_line_visual_selection_starts_on_same_but_ends_after(mut ctx draw.Contextable,
Expand Down
29 changes: 29 additions & 0 deletions src/view_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,35 @@ fn (mockctx MockContextable) clear() {}

fn (mockctx MockContextable) flush() {}

struct DrawnTextRec {
content string
pos Pos
}

fn test_draw_text_line_visual_selection_start_end_on_same_line() {
mut drawn_text := []DrawnTextRec{}
mut drawn_text_ref := &drawn_text

mut m_ctx := MockContextable{
on_draw_cb: fn [mut drawn_text_ref] (x int, y int, text string) {
drawn_text_ref << DrawnTextRec{
content: text,
pos: Pos{ x: x, y: y }
}
}
}

mut m_cursor := Cursor{ pos: Pos{ x: 71, y: 0 }, selection_start_pos: Pos{ x: 44, y: 0 } }
document_line := 'This part of the text is before the selection but this part is within it, and this part is after it'
draw_text_line_within_visual_selection(
mut m_ctx, resolve_test_syntax(),
m_cursor, Color{ r: 10, g: 10, b: 10 },
0, 0, 0, 0, document_line, document_line
)

assert drawn_text.len == 3
}

fn test_draw_text_line_within_visual_selection_start_end_on_same_line_with_tab_prefix() {
mut drawed_text := []string{}
mut drawed_text_ref := &drawed_text
Expand Down
Loading