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

frontend: indirect Buffer to enable different buffer types with different behaviors #130

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
43ffb87
frontend: rename file tree related models to reflect a specialisation
aserowy Dec 4, 2024
54f7796
buffer: rename buffer to text buffer
aserowy Dec 4, 2024
65816d7
frontend: PART1 add indirection for buffer to enable enumeration of d…
aserowy Dec 5, 2024
12f125f
frontend: 118 diagnostics left
aserowy Dec 5, 2024
e1aa9fb
frontend: 67 diagnostics left
aserowy Dec 6, 2024
5dafd6b
frontend: 0 diagnostics left
aserowy Dec 6, 2024
02e25e5
Merge branch 'main' into feature/split-buffer
aserowy Dec 6, 2024
016b1a8
frontend: add state and app abstractions for easier passing
aserowy Dec 6, 2024
2c33ee6
frontend: remove unnecessary refs
aserowy Dec 6, 2024
35bfaa2
frontend: remove unnecessary muts
aserowy Dec 6, 2024
f50d9fc
frontend: box buffer contents
aserowy Dec 6, 2024
1b7d316
frontend: refactor naming and references for better visibility of tre…
aserowy Dec 7, 2024
cc7b935
frontend: NOT BUILDABLE introduce buffer list and window to reflect ui
aserowy Dec 11, 2024
e2d361d
frontend: NOT BUILDABLE change buffer specific actions to vec buffer
aserowy Dec 16, 2024
3e9ba5d
frontend: NOT BUILDABLE change qfix, path, search, marks to new struc…
aserowy Dec 18, 2024
b760935
frontend: NOT BUILDABLE rework commands, settings, signs
aserowy Dec 19, 2024
5377dbe
frontend: reworked cursor, enumeration, and mode
aserowy Dec 20, 2024
ebd8849
frontend: add buffer handling, rework parts of view
aserowy Dec 26, 2024
7cbbc34
frontend: render statusline and commandline
aserowy Dec 27, 2024
cf4341c
frontend: NOT BUILDABLE rework file tree vp and cursor lifecycle for …
aserowy Dec 28, 2024
5f011f7
frontend: NOT BUILDABLE rework file tree vp and cursor lifecycle for …
aserowy Dec 28, 2024
ed8b74b
frontend: NOT BUILDABLE rework file tree vp and cursor lifecycle for …
aserowy Dec 29, 2024
24264f2
frontend: NOT BUILDABLE rework file tree vp and cursor lifecycle for …
aserowy Dec 30, 2024
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
38 changes: 36 additions & 2 deletions yeet-buffer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
use message::BufferMessage;
use model::{viewport::ViewPort, BufferResult, Cursor, Mode, TextBuffer};
use ratatui::Frame;

pub mod message;
pub mod model;
pub mod update;
pub mod view;
mod update;
mod view;

pub fn update(
viewport: Option<&ViewPort>,
cursor: Option<&Cursor>,
mode: &Mode,
buffer: &mut TextBuffer,
messages: Vec<&BufferMessage>,
) -> (Option<ViewPort>, Option<Cursor>, Vec<BufferResult>) {
update::update(viewport, cursor, mode, buffer, messages)
}

pub fn view(
viewport: &ViewPort,
cursor: Option<&Cursor>,
mode: &Mode,
buffer: &TextBuffer,
frame: &mut Frame,
horizontal_offset: u16,
vertical_offset: u16,
) {
view::view(
viewport,
cursor,
mode,
buffer,
frame,
horizontal_offset,
vertical_offset,
)
}
4 changes: 2 additions & 2 deletions yeet-buffer/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ pub enum SearchDirection {
}

#[derive(Default)]
pub struct Buffer {
pub struct TextBuffer {
pub last_find: Option<CursorDirection>,
pub lines: Vec<BufferLine>,
pub undo: Undo,
}

impl std::fmt::Debug for Buffer {
impl std::fmt::Debug for TextBuffer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Buffer")
.field("last_find", &self.last_find)
Expand Down
11 changes: 6 additions & 5 deletions yeet-buffer/src/model/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ pub struct WindowSettings {
pub sign_column_width: usize,
}

#[derive(Debug, Default)]
#[derive(Clone, Debug, Default)]
pub struct ViewPort {
pub height: usize,
pub height: u16,
pub hidden_sign_ids: HashSet<SignIdentifier>,
pub horizontal_index: usize,
pub line_number: LineNumber,
pub line_number_width: usize,
pub show_border: bool,
pub sign_column_width: usize,
pub vertical_index: usize,
pub width: usize,
pub width: u16,
}

// TODO: enable with settings
Expand All @@ -32,10 +33,10 @@ impl ViewPort {

pub fn get_content_width(&self, line: &BufferLine) -> usize {
let offset = self.get_offset_width(line);
if self.width < offset {
if usize::from(self.width) < offset {
0
} else {
self.width - offset
usize::from(self.width) - offset
}
}

Expand Down
12 changes: 8 additions & 4 deletions yeet-buffer/src/update/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::cmp::Ordering;

use crate::{
message::{CursorDirection, Search},
model::{Buffer, BufferLine, BufferResult, Cursor, CursorPosition, Mode},
model::{BufferLine, BufferResult, Cursor, CursorPosition, Mode, TextBuffer},
};

use super::{find, word};

// TODO: refactor
pub fn update_cursor_by_direction(
pub fn update_by_direction(
cursor: &mut Cursor,
mode: &Mode,
buffer: &mut Buffer,
buffer: &mut TextBuffer,
count: &usize,
direction: &CursorDirection,
) -> Vec<BufferResult> {
Expand Down Expand Up @@ -222,7 +222,11 @@ pub fn update_cursor_by_direction(
results
}

pub fn set_outbound_cursor_to_inbound_position(cursor: &mut Cursor, mode: &Mode, model: &Buffer) {
pub fn set_to_inbound_position(
cursor: &mut Cursor,
mode: &Mode,
model: &TextBuffer,
) {
let position = if model.lines.is_empty() {
get_position(mode, &0, &cursor.horizontal_index)
} else {
Expand Down
4 changes: 2 additions & 2 deletions yeet-buffer/src/update/find.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
message::CursorDirection,
model::{Buffer, BufferLine, Cursor, CursorPosition},
model::{BufferLine, Cursor, CursorPosition, TextBuffer},
};

use super::cursor;

pub fn char(cursor: &mut Cursor, direction: &CursorDirection, model: &Buffer) {
pub fn char(cursor: &mut Cursor, direction: &CursorDirection, model: &TextBuffer) {
match direction {
CursorDirection::FindBackward(find) => {
if let Some(found) = find_char_backward(find, &model.lines, cursor) {
Expand Down
114 changes: 82 additions & 32 deletions yeet-buffer/src/update/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
use crate::{
message::{BufferMessage, CursorDirection},
model::{viewport::ViewPort, Buffer, BufferResult, Cursor, CursorPosition, Mode},
update::cursor::{set_outbound_cursor_to_inbound_position, update_cursor_by_direction},
model::{viewport::ViewPort, BufferResult, Cursor, CursorPosition, Mode, TextBuffer},
};

mod cursor;
mod find;
mod modification;
mod viewport;
pub mod viewport;
mod word;

pub fn update_buffer(
viewport: &mut ViewPort,
pub fn update(
viewport: Option<&ViewPort>,
cursor: Option<&Cursor>,
mode: &Mode,
buffer: &mut TextBuffer,
messages: Vec<&BufferMessage>,
) -> (Option<ViewPort>, Option<Cursor>, Vec<BufferResult>) {
let mut actions = Vec::new();

let mut viewport = match viewport {
Some(it) => Some(it.clone()),
None => None,
};

let mut cursor = match cursor {
Some(it) => Some(it.clone()),
None => None,
};

for message in messages.iter() {
actions.push(update_buffer(
&mut viewport,
&mut cursor,
mode,
buffer,
message,
));
}

(viewport, cursor, actions.into_iter().flatten().collect())
}

fn update_buffer(
viewport: &mut Option<ViewPort>,
cursor: &mut Option<Cursor>,
mode: &Mode,
buffer: &mut Buffer,
buffer: &mut TextBuffer,
message: &BufferMessage,
) -> Vec<BufferResult> {
tracing::debug!("handling buffer message: {:?}", message);
Expand All @@ -27,14 +58,21 @@ pub fn update_buffer(
buffer.undo.close_transaction();

if let Some(cursor) = cursor {
update_cursor_by_direction(cursor, mode, buffer, &1, &CursorDirection::Left);
cursor::update_by_direction(cursor, mode, buffer, &1, &CursorDirection::Left);
if let Some(viewport) = viewport {
viewport::update_by_cursor(viewport, cursor, buffer);
}
}
}
Vec::new()
}
BufferMessage::Modification(count, modification) => {
if let Some(cursor) = cursor {
let changes = modification::update(cursor, mode, buffer, count, modification);
if let Some(viewport) = viewport {
viewport::update_by_cursor(viewport, cursor, buffer);
}

if let Some(changes) = changes {
buffer.undo.add(mode, changes);
}
Expand All @@ -43,29 +81,43 @@ pub fn update_buffer(
}
BufferMessage::MoveCursor(count, direction) => {
if let Some(cursor) = cursor {
update_cursor_by_direction(cursor, mode, buffer, count, direction)
let result = cursor::update_by_direction(cursor, mode, buffer, count, direction);
if let Some(viewport) = viewport {
viewport::update_by_cursor(viewport, cursor, buffer);
}

result
} else {
Vec::new()
}
// TODO: history::add_history_entry(&mut model.history, selected.as_path());
}
BufferMessage::MoveViewPort(direction) => {
let viewport = match viewport {
Some(it) => it,
None => return Vec::new(),
};

viewport::update_by_direction(viewport, cursor, buffer, direction);
if let Some(cursor) = cursor {
viewport::update_by_cursor(viewport, cursor, buffer);
}

Vec::new()
}
BufferMessage::RemoveLine(index) => {
buffer.lines.remove(*index);

if let Some(cursor) = cursor {
set_outbound_cursor_to_inbound_position(cursor, mode, buffer);
cursor::set_to_inbound_position(cursor, mode, buffer);
if let Some(viewport) = viewport {
viewport::update_by_cursor(viewport, cursor, buffer);
}
}

Vec::new()
}
BufferMessage::ResetCursor => {
viewport.horizontal_index = 0;
viewport.vertical_index = 0;

if let Some(cursor) = cursor {
cursor.vertical_index = 0;

Expand All @@ -79,6 +131,12 @@ pub fn update_buffer(
},
CursorPosition::End => CursorPosition::End,
CursorPosition::None => CursorPosition::None,
};
if let Some(viewport) = viewport {
viewport.horizontal_index = 0;
viewport.vertical_index = 0;

viewport::update_by_cursor(viewport, cursor, buffer);
}
}

Expand All @@ -93,7 +151,10 @@ pub fn update_buffer(
buffer.lines = content.to_vec();

if let Some(cursor) = cursor {
set_outbound_cursor_to_inbound_position(cursor, mode, buffer);
cursor::set_to_inbound_position(cursor, mode, buffer);
if let Some(viewport) = viewport {
viewport::update_by_cursor(viewport, cursor, buffer);
}
}

Vec::new()
Expand All @@ -114,8 +175,10 @@ pub fn update_buffer(
cursor.vertical_index = index;
cursor.hide_cursor_line = false;

set_outbound_cursor_to_inbound_position(cursor, mode, buffer);
viewport::update_by_cursor(viewport, cursor, buffer);
cursor::set_to_inbound_position(cursor, mode, buffer);
if let Some(viewport) = viewport {
viewport::update_by_cursor(viewport, cursor, buffer);
}

vec![BufferResult::CursorPositionChanged]
} else {
Expand All @@ -126,28 +189,15 @@ pub fn update_buffer(
// TODO: cursor should stay on current selection
buffer.lines.sort_unstable_by(sort);
if let Some(cursor) = cursor {
set_outbound_cursor_to_inbound_position(cursor, mode, buffer);
cursor::set_to_inbound_position(cursor, mode, buffer);
if let Some(viewport) = viewport {
viewport::update_by_cursor(viewport, cursor, buffer);
}
}
Vec::new()
}
BufferMessage::UpdateViewPortByCursor => Vec::new(),
};

if let Some(cursor) = cursor {
viewport::update_by_cursor(viewport, cursor, buffer);
}

result
}

pub fn focus_buffer(cursor: &mut Option<Cursor>) {
if let Some(cursor) = cursor {
cursor.hide_cursor = false;
}
}

pub fn unfocus_buffer(cursor: &mut Option<Cursor>) {
if let Some(cursor) = cursor {
cursor.hide_cursor = true;
}
}
14 changes: 8 additions & 6 deletions yeet-buffer/src/update/modification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use std::mem;

use crate::{
message::{CursorDirection, LineDirection, TextModification},
model::{ansi::Ansi, undo::BufferChanged, Buffer, BufferLine, Cursor, CursorPosition, Mode},
model::{
ansi::Ansi, undo::BufferChanged, BufferLine, Cursor, CursorPosition, Mode, TextBuffer,
},
};

use super::cursor;

pub fn update(
cursor: &mut Cursor,
mode: &Mode,
buffer: &mut Buffer,
buffer: &mut TextBuffer,
count: &usize,
modification: &TextModification,
) -> Option<Vec<BufferChanged>> {
Expand Down Expand Up @@ -39,14 +41,14 @@ pub fn update(
changes.push(BufferChanged::LineRemoved(line_index, line.content));
}

cursor::set_outbound_cursor_to_inbound_position(cursor, mode, buffer);
cursor::set_to_inbound_position(cursor, mode, buffer);

Some(changes)
}
TextModification::DeleteMotion(delete_count, motion) => {
let pre_motion_cursor = cursor.clone();
for _ in 0..*count {
cursor::update_cursor_by_direction(cursor, mode, buffer, delete_count, motion);
cursor::update_by_direction(cursor, mode, buffer, delete_count, motion);
}

let mut changes = Vec::new();
Expand Down Expand Up @@ -110,7 +112,7 @@ pub fn update(
changes.push(changed);
}

cursor::set_outbound_cursor_to_inbound_position(cursor, mode, buffer);
cursor::set_to_inbound_position(cursor, mode, buffer);

Some(changes)
}
Expand Down Expand Up @@ -271,7 +273,7 @@ fn is_line_delete(motion: &CursorDirection) -> bool {

fn get_line_or_create_on_empty<'a>(
cursor: &'a mut Cursor,
model: &'a mut Buffer,
model: &'a mut TextBuffer,
) -> Option<(&'a mut Cursor, &'a mut BufferLine)> {
if cursor.horizontal_index == CursorPosition::None {
return None;
Expand Down
Loading
Loading