Skip to content

Commit dce8023

Browse files
committed
feat: statusline rendering through lua and better lua api
1 parent c3ef856 commit dce8023

File tree

29 files changed

+638
-118
lines changed

29 files changed

+638
-118
lines changed

Cargo.lock

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glyph-config/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ impl<'cfg> Config<'cfg> {
164164
let gutter = runtime.from_value::<GutterConfig>(config.get::<Value>("gutter")?)?;
165165
let statusline = StatuslineConfig::from_lua(config.get::<Value>("statusline")?, runtime)?;
166166

167-
println!("{statusline:#?}");
168-
169167
Ok(Config {
170168
cursor,
171169
gutter,

glyph-core/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ thiserror.workspace = true
99
parking_lot.workspace = true
1010

1111
ropey = "1.6.1"
12-
slotmap = "1.0.7"
1312
tree-sitter = "0.24.4"
1413
tree-sitter-rust = "0.23.2"
1514
streaming-iterator = "0.1.9"

glyph-core/src/command.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use crate::editor::Editor;
99
use crate::window::WindowId;
1010

1111
#[derive(Debug)]
12-
pub struct Context<'ctx> {
12+
pub struct Context {
1313
pub editor: Arc<RwLock<Editor>>,
14-
pub cursors: &'ctx mut BTreeMap<WindowId, Cursor>,
14+
pub cursors: Arc<RwLock<BTreeMap<WindowId, Cursor>>>,
1515
}
1616

1717
pub enum MappableCommand {
@@ -68,15 +68,17 @@ fn move_left(ctx: &mut Context) {
6868
let tab = editor.focused_tab();
6969
let window = tab.tree.focus();
7070
let window = tab.tree.window(window);
71-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
71+
let mut cursors = ctx.cursors.write();
72+
let cursor = cursors.get_mut(&window.id).unwrap();
7273
cursor.move_left();
7374
}
7475

7576
let mut editor = ctx.editor.write();
7677
let tab = editor.focused_tab_mut();
7778
let window = tab.tree.focus();
7879
let window = tab.tree.window_mut(window);
79-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
80+
let mut cursors = ctx.cursors.write();
81+
let cursor = cursors.get_mut(&window.id).unwrap();
8082

8183
if cursor.x().checked_sub(window.scroll().0).is_none() {
8284
window.scroll_left();
@@ -90,15 +92,17 @@ fn move_down(ctx: &mut Context) {
9092
let window = tab.tree.focus();
9193
let window = tab.tree.window(window);
9294
let document = editor.document(&window.document);
93-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
95+
let mut cursors = ctx.cursors.write();
96+
let cursor = cursors.get_mut(&window.id).unwrap();
9497
cursor.move_down(document);
9598
}
9699

97100
let mut editor = ctx.editor.write();
98101
let tab = editor.focused_tab_mut();
99102
let window = tab.tree.focus();
100103
let window = tab.tree.window_mut(window);
101-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
104+
let mut cursors = ctx.cursors.write();
105+
let cursor = cursors.get_mut(&window.id).unwrap();
102106

103107
if cursor.y() - window.scroll().1 >= window.area.height.into() {
104108
window.scroll_down();
@@ -111,15 +115,17 @@ fn move_up(ctx: &mut Context) {
111115
let tab = editor.focused_tab();
112116
let window = tab.tree.focus();
113117
let window = tab.tree.window(window);
114-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
118+
let mut cursors = ctx.cursors.write();
119+
let cursor = cursors.get_mut(&window.id).unwrap();
115120
cursor.move_up();
116121
}
117122

118123
let mut editor = ctx.editor.write();
119124
let tab = editor.focused_tab_mut();
120125
let window = tab.tree.focus();
121126
let window = tab.tree.window_mut(window);
122-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
127+
let mut cursors = ctx.cursors.write();
128+
let cursor = cursors.get_mut(&window.id).unwrap();
123129

124130
if cursor.y().checked_sub(window.scroll().1).is_none() {
125131
window.scroll_up();
@@ -133,15 +139,17 @@ pub fn move_right(ctx: &mut Context) {
133139
let window = tab.tree.focus();
134140
let window = tab.tree.window(window);
135141
let document = editor.document(&window.document);
136-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
142+
let mut cursors = ctx.cursors.write();
143+
let cursor = cursors.get_mut(&window.id).unwrap();
137144
cursor.move_right(document);
138145
}
139146

140147
let mut editor = ctx.editor.write();
141148
let tab = editor.focused_tab_mut();
142149
let window = tab.tree.focus();
143150
let window = tab.tree.window_mut(window);
144-
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
151+
let mut cursors = ctx.cursors.write();
152+
let cursor = cursors.get_mut(&window.id).unwrap();
145153

146154
if cursor.x() - window.scroll().0 >= window.area.width.into() {
147155
window.scroll_right();

glyph-core/src/document.rs

+10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ impl Default for DocumentId {
1313
}
1414
}
1515

16+
impl From<DocumentId> for usize {
17+
fn from(value: DocumentId) -> Self {
18+
value.0.into()
19+
}
20+
}
21+
1622
impl DocumentId {
23+
pub fn new(document: usize) -> Option<DocumentId> {
24+
Some(DocumentId(NonZeroUsize::new(document)?))
25+
}
26+
1727
pub fn next(&self) -> DocumentId {
1828
// Safety: will always be non-zero and less than usize::max + 1
1929
DocumentId(unsafe { NonZeroUsize::new_unchecked(self.0.get().saturating_add(1)) })

glyph-core/src/highlights.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pub struct HighlightGroup {
55
pub fg: Color,
66
pub bg: Color,
77
pub bold: bool,
8+
pub italic: bool,
89
}

glyph-core/src/rect.rs

+20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ impl From<&Cursor> for Point {
2424
}
2525
}
2626

27+
impl From<(u16, u16)> for Point {
28+
fn from((x, y): (u16, u16)) -> Self {
29+
Point { x, y }
30+
}
31+
}
32+
33+
impl From<Rect> for Point {
34+
fn from(rect: Rect) -> Self {
35+
Point { x: rect.x, y: rect.y }
36+
}
37+
}
38+
2739
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
2840
pub struct Rect {
2941
pub x: u16,
@@ -106,4 +118,12 @@ impl Rect {
106118

107119
Rect::new(self.x, self.y, self.width, height)
108120
}
121+
122+
pub fn bottom(&self) -> u16 {
123+
self.y + self.height
124+
}
125+
126+
pub fn right(&self) -> u16 {
127+
self.x + self.width
128+
}
109129
}

glyph-core/src/tree.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use slotmap::HopSlotMap;
1+
use std::collections::BTreeMap;
22

33
use crate::rect::Rect;
44
use crate::window::{Window, WindowId};
@@ -11,10 +11,11 @@ pub enum Layout {
1111

1212
#[derive(Debug)]
1313
pub struct Tree {
14+
area: Rect,
1415
root: WindowId,
1516
focus: WindowId,
16-
area: Rect,
17-
nodes: HopSlotMap<WindowId, Node>,
17+
next_window: WindowId,
18+
nodes: BTreeMap<WindowId, Node>,
1819
}
1920

2021
#[derive(Debug)]
@@ -67,7 +68,8 @@ impl Tree {
6768
Tree {
6869
root: WindowId::default(),
6970
focus: WindowId::default(),
70-
nodes: HopSlotMap::with_key(),
71+
nodes: BTreeMap::default(),
72+
next_window: WindowId::default(),
7173
area,
7274
}
7375
}
@@ -76,12 +78,16 @@ impl Tree {
7678
self.focus
7779
}
7880

81+
pub fn nodes(&self) -> &BTreeMap<WindowId, Node> {
82+
&self.nodes
83+
}
84+
7985
pub fn windows(&self) -> impl Iterator<Item = (&Window, bool)> {
8086
self.nodes.iter().filter_map(|(key, node)| match node {
8187
Node {
8288
value: NodeValue::Window(window),
8389
..
84-
} => Some((window.as_ref(), self.focus == key)),
90+
} => Some((window.as_ref(), &self.focus == key)),
8591
_ => None,
8692
})
8793
}
@@ -92,7 +98,7 @@ impl Tree {
9298
}
9399

94100
pub fn get_window(&self, id: WindowId) -> Option<&Window> {
95-
match self.nodes.get(id) {
101+
match self.nodes.get(&id) {
96102
Some(Node {
97103
value: NodeValue::Window(window),
98104
..
@@ -102,7 +108,7 @@ impl Tree {
102108
}
103109

104110
pub fn get_window_mut(&mut self, id: WindowId) -> Option<&mut Window> {
105-
match self.nodes.get_mut(id) {
111+
match self.nodes.get_mut(&id) {
106112
Some(Node {
107113
value: NodeValue::Window(window),
108114
..
@@ -124,14 +130,16 @@ impl Tree {
124130
window.area = self.area.with_height(self.area.height - 2);
125131

126132
let node = Node::window(window);
127-
let node = self.nodes.insert(node);
133+
let id = self.next_window;
134+
self.next_window = self.next_window.next();
135+
self.nodes.insert(id, node);
128136

129-
self.window_mut(node).id = node;
137+
self.window_mut(id).id = id;
130138
// root is its own parent
131-
self.nodes[node].parent = node;
132-
self.focus = node;
133-
self.root = node;
134-
return node;
139+
self.nodes.get_mut(&id).unwrap().parent = id;
140+
self.focus = id;
141+
self.root = id;
142+
return id;
135143
}
136144

137145
todo!();

glyph-core/src/window.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1+
use std::num::NonZeroUsize;
2+
13
use crate::document::DocumentId;
24
use crate::rect::Rect;
35

4-
slotmap::new_key_type! {
5-
pub struct WindowId;
6+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
7+
pub struct WindowId(NonZeroUsize);
8+
9+
impl WindowId {
10+
pub fn new(window: usize) -> Option<WindowId> {
11+
Some(WindowId(NonZeroUsize::new(window)?))
12+
}
13+
}
14+
15+
impl Default for WindowId {
16+
fn default() -> Self {
17+
// Safety: 1 is non-zero
18+
WindowId(unsafe { NonZeroUsize::new_unchecked(1) })
19+
}
20+
}
21+
22+
impl WindowId {
23+
pub fn next(&self) -> WindowId {
24+
// Safety: will always be non-zero and less than usize::max + 1
25+
WindowId(unsafe { NonZeroUsize::new_unchecked(self.0.get().saturating_add(1)) })
26+
}
27+
}
28+
29+
impl From<WindowId> for usize {
30+
fn from(value: WindowId) -> Self {
31+
value.0.into()
32+
}
633
}
734

835
#[derive(Debug, Clone)]

glyph-runtime/src/colors.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ impl TryFrom<LuaHighlightGroup> for HighlightGroup {
1717
let fg = group.fg.try_into()?;
1818
let bg = group.bg.try_into()?;
1919
let bold = group.bold;
20+
let italic = group.italic;
2021

21-
Ok(HighlightGroup { fg, bg, bold })
22+
Ok(HighlightGroup { fg, bg, bold, italic })
2223
}
2324
}
2425

@@ -30,6 +31,8 @@ pub struct LuaHighlightGroup {
3031
bg: String,
3132
#[serde(default)]
3233
bold: bool,
34+
#[serde(default)]
35+
italic: bool,
3336
}
3437

3538
pub fn setup_colors_api(

0 commit comments

Comments
 (0)