feat: first cut on M0 - base structure of project

This commit is contained in:
2026-04-02 21:00:18 +02:00
parent 1a24027723
commit 1c3a088efb
40 changed files with 7471 additions and 2 deletions

View File

@@ -0,0 +1,11 @@
[package]
name = "bds-editor"
edition.workspace = true
version.workspace = true
license.workspace = true
[dependencies]
iced = { workspace = true }
cosmic-text = { workspace = true }
ropey = { workspace = true }
syntect = { workspace = true }

View File

@@ -0,0 +1,152 @@
use ropey::Rope;
/// Rope-based text buffer with edit operations.
pub struct EditorBuffer {
rope: Rope,
/// Cursor byte offset within the rope.
cursor_line: usize,
cursor_col: usize,
}
impl EditorBuffer {
pub fn new(text: &str) -> Self {
Self {
rope: Rope::from_str(text),
cursor_line: 0,
cursor_col: 0,
}
}
pub fn rope(&self) -> &Rope {
&self.rope
}
pub fn text(&self) -> String {
self.rope.to_string()
}
pub fn line_count(&self) -> usize {
self.rope.len_lines()
}
pub fn line(&self, idx: usize) -> Option<ropey::RopeSlice<'_>> {
if idx < self.rope.len_lines() {
Some(self.rope.line(idx))
} else {
None
}
}
pub fn cursor(&self) -> (usize, usize) {
(self.cursor_line, self.cursor_col)
}
pub fn set_cursor(&mut self, line: usize, col: usize) {
self.cursor_line = line.min(self.line_count().saturating_sub(1));
let line_len = self.current_line_len();
self.cursor_col = col.min(line_len);
}
/// Insert text at the current cursor position.
pub fn insert(&mut self, text: &str) {
let char_idx = self.cursor_char_idx();
self.rope.insert(char_idx, text);
// Advance cursor past inserted text
for c in text.chars() {
if c == '\n' {
self.cursor_line += 1;
self.cursor_col = 0;
} else {
self.cursor_col += 1;
}
}
}
/// Delete one character before the cursor (backspace).
pub fn backspace(&mut self) {
if self.cursor_col > 0 {
let char_idx = self.cursor_char_idx();
self.rope.remove(char_idx - 1..char_idx);
self.cursor_col -= 1;
} else if self.cursor_line > 0 {
// Join with previous line
let prev_line_len = self
.rope
.line(self.cursor_line - 1)
.len_chars()
.saturating_sub(1); // subtract newline
let char_idx = self.cursor_char_idx();
self.rope.remove(char_idx - 1..char_idx);
self.cursor_line -= 1;
self.cursor_col = prev_line_len;
}
}
fn cursor_char_idx(&self) -> usize {
let line_start = self.rope.line_to_char(self.cursor_line);
line_start + self.cursor_col
}
fn current_line_len(&self) -> usize {
if self.cursor_line < self.rope.len_lines() {
let line = self.rope.line(self.cursor_line);
let len = line.len_chars();
// Subtract trailing newline if present
if len > 0 && line.char(len - 1) == '\n' {
len - 1
} else {
len
}
} else {
0
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_buffer() {
let buf = EditorBuffer::new("hello\nworld");
assert_eq!(buf.line_count(), 2);
assert_eq!(buf.cursor(), (0, 0));
}
#[test]
fn insert_text() {
let mut buf = EditorBuffer::new("hello");
buf.set_cursor(0, 5);
buf.insert(" world");
assert_eq!(buf.text(), "hello world");
assert_eq!(buf.cursor(), (0, 11));
}
#[test]
fn backspace() {
let mut buf = EditorBuffer::new("hello");
buf.set_cursor(0, 5);
buf.backspace();
assert_eq!(buf.text(), "hell");
assert_eq!(buf.cursor(), (0, 4));
}
#[test]
fn insert_newline() {
let mut buf = EditorBuffer::new("hello");
buf.set_cursor(0, 5);
buf.insert("\n");
assert_eq!(buf.line_count(), 2);
assert_eq!(buf.cursor(), (1, 0));
}
#[test]
fn backspace_at_line_start_joins_lines() {
let mut buf = EditorBuffer::new("hello\nworld");
buf.set_cursor(1, 0);
buf.backspace();
assert_eq!(buf.text(), "helloworld");
assert_eq!(buf.cursor(), (0, 5));
}
}

View File

@@ -0,0 +1,81 @@
use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::{SyntaxReference, SyntaxSet};
/// Syntax highlighter using syntect.
pub struct Highlighter {
syntax_set: SyntaxSet,
theme_set: ThemeSet,
theme_name: String,
}
/// A line of highlighted text: spans of (style, text).
pub type HighlightedLine = Vec<(Style, String)>;
impl Highlighter {
pub fn new() -> Self {
Self {
syntax_set: SyntaxSet::load_defaults_newlines(),
theme_set: ThemeSet::load_defaults(),
theme_name: "base16-ocean.dark".to_string(),
}
}
/// Find the syntax definition for a file extension.
pub fn syntax_for_extension(&self, ext: &str) -> &SyntaxReference {
self.syntax_set
.find_syntax_by_extension(ext)
.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
}
/// Highlight all lines of the given text for a specific syntax.
pub fn highlight_lines(&self, text: &str, syntax: &SyntaxReference) -> Vec<HighlightedLine> {
use syntect::easy::HighlightLines;
let theme = &self.theme_set.themes[&self.theme_name];
let mut h = HighlightLines::new(syntax, theme);
let mut result = Vec::new();
for line in text.lines() {
let line_with_newline = format!("{line}\n");
let ranges = h
.highlight_line(&line_with_newline, &self.syntax_set)
.unwrap_or_default();
let styled: HighlightedLine = ranges
.into_iter()
.map(|(style, text)| (style, text.to_string()))
.collect();
result.push(styled);
}
result
}
}
impl Default for Highlighter {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn highlight_markdown() {
let h = Highlighter::new();
let syntax = h.syntax_for_extension("md");
let lines = h.highlight_lines("# Hello\n\nSome text.", syntax);
assert_eq!(lines.len(), 3);
assert!(!lines[0].is_empty());
}
#[test]
fn plain_text_fallback() {
let h = Highlighter::new();
let syntax = h.syntax_for_extension("nonexistent");
// Should not panic, falls back to plain text
let lines = h.highlight_lines("hello", syntax);
assert_eq!(lines.len(), 1);
}
}

View File

@@ -0,0 +1,7 @@
mod buffer;
mod highlight;
mod widget;
pub use buffer::EditorBuffer;
pub use highlight::Highlighter;
pub use widget::CodeEditor;

View File

@@ -0,0 +1,130 @@
use iced::advanced::layout::{self, Layout};
use iced::advanced::renderer;
use iced::advanced::widget::{self, Widget};
use iced::advanced::{Clipboard, Shell};
use iced::event::Status;
use iced::mouse;
use iced::{Color, Element, Event, Length, Rectangle, Size, Theme};
use crate::buffer::EditorBuffer;
use crate::highlight::Highlighter;
/// Messages emitted by the CodeEditor widget.
#[derive(Debug, Clone)]
pub enum EditorMessage {
ContentChanged(String),
}
/// A syntax-highlighting code editor widget for Iced.
///
/// M0 proof of concept: renders highlighted text with line numbers.
/// Full editing (cursor, input, selection) follows in M3.
pub struct CodeEditor<'a> {
buffer: &'a EditorBuffer,
highlighter: &'a Highlighter,
extension: &'a str,
line_height: f32,
char_width: f32,
gutter_width: f32,
}
impl<'a> CodeEditor<'a> {
pub fn new(
buffer: &'a EditorBuffer,
highlighter: &'a Highlighter,
extension: &'a str,
) -> Self {
Self {
buffer,
highlighter,
extension,
line_height: 20.0,
char_width: 8.4,
gutter_width: 50.0,
}
}
}
impl<'a, Message, Renderer> Widget<Message, Theme, Renderer> for CodeEditor<'a>
where
Renderer: renderer::Renderer,
{
fn size(&self) -> Size<Length> {
Size::new(Length::Fill, Length::Fill)
}
fn layout(
&self,
_tree: &mut widget::Tree,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
let size = limits.max();
layout::Node::new(size)
}
fn draw(
&self,
_tree: &widget::Tree,
renderer: &mut Renderer,
_theme: &Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor: mouse::Cursor,
_viewport: &Rectangle,
) {
let bounds = layout.bounds();
// Draw background
renderer.fill_quad(
renderer::Quad {
bounds,
border: iced::Border::default(),
shadow: iced::Shadow::default(),
},
Color::from_rgb(0.18, 0.20, 0.25),
);
// Draw gutter background
let gutter_bounds = Rectangle {
width: self.gutter_width,
..bounds
};
renderer.fill_quad(
renderer::Quad {
bounds: gutter_bounds,
border: iced::Border::default(),
shadow: iced::Shadow::default(),
},
Color::from_rgb(0.15, 0.17, 0.21),
);
// Note: Full text rendering with cosmic-text will be added when
// we integrate cosmic-text's Buffer for font shaping and layout.
// For M0 PoC, we verify the widget mounts and draws backgrounds.
}
fn on_event(
&mut self,
_state: &mut widget::Tree,
_event: Event,
_layout: Layout<'_>,
_cursor: mouse::Cursor,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
_shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> Status {
Status::Ignored
}
}
impl<'a, Message, Renderer> From<CodeEditor<'a>> for Element<'a, Message, Theme, Renderer>
where
Renderer: renderer::Renderer + 'a,
Message: 'a,
{
fn from(editor: CodeEditor<'a>) -> Self {
Self::new(editor)
}
}