Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
; ----------
; Primitives
; ----------

[
  (line_comment)
  (block_comment)
] @comment

(
  ((identifier) @constant.builtin)
  (#match? @constant.builtin "^(nothing|missing|undef)$"))

[
  (true)
  (false)
] @constant.builtin.boolean

(integer_literal) @constant.numeric.integer
(float_literal) @constant.numeric.float

(
  ((identifier) @constant.numeric.float)
  (#match? @constant.numeric.float "^((Inf|NaN)(16|32|64)?)$"))

(character_literal) @constant.character
(escape_sequence) @constant.character.escape

(string_literal) @string

(prefixed_string_literal
  prefix: (identifier) @function.macro) @string

(quote_expression
  (identifier) @string.special.symbol) 
  
; -------------------
; Modules and Imports
; -------------------

(module_definition
  name: (identifier) @namespace)
  
(import_statement
  (identifier) @namespace)
  
(selected_import
  . (identifier) @namespace)

(scoped_identifier
  (identifier) @namespace)

; -----
; Types
; -----

(abstract_definition
  name: (identifier) @type)
  
(primitive_definition
  name: (identifier) @type)

(struct_definition
  name: (identifier) @type)

(struct_definition
  . (_)
    (identifier) @variable.other.member)

(struct_definition
  . (_)
  (typed_expression
    . (identifier) @variable.other.member))
    
(type_parameter_list
  (identifier) @type)

(constrained_type_parameter
  (identifier) @type)
  
(subtype_clause
  (identifier) @type)

(typed_expression
  (identifier) @type . )

(parameterized_identifier
  (identifier) @type)
  
(type_argument_list
  (identifier) @type)

(where_clause
  (identifier) @type)

; -------------------
; Function definition
; -------------------

(
  (function_definition
    name: [
      (identifier) @function
      (scoped_identifier
        (identifier) @namespace
        (identifier) @function)
    ])
  ; prevent constructors (PascalCase) to be highlighted as functions
  (#match? @function "^[^A-Z]"))

(
  (short_function_definition
    name: [
      (identifier) @function
      (scoped_identifier
        (identifier) @namespace
        (identifier) @function)
    ])
  ; prevent constructors (PascalCase) to be highlighted as functions
  (#match? @function "^[^A-Z]"))

(parameter_list
  (identifier) @variable.parameter)

(typed_parameter
  (identifier) @variable.parameter
  (identifier)? @type)

(optional_parameter
  . (identifier) @variable.parameter)

(slurp_parameter
  (identifier) @variable.parameter)

(function_expression
  . (identifier) @variable.parameter)

; ---------------
; Functions calls
; ---------------

(
  (call_expression
    (identifier) @function)
  ; prevent constructors (PascalCase) to be highlighted as functions
  (#match? @function "^[^A-Z]"))

(
  (broadcast_call_expression
    (identifier) @function)
  (#match? @function "^[^A-Z]"))

(
  (call_expression
    (field_expression (identifier) @function .))
  (#match? @function "^[^A-Z]"))

(
  (broadcast_call_expression
    (field_expression (identifier) @function .))
  (#match? @function "^[^A-Z]"))

; ------
; Macros
; ------

(macro_definition
  name: (identifier) @function.macro)

(macro_identifier
  "@" @function.macro
  (identifier) @function.macro)

; --------
; Keywords
; --------

(function_definition 
  ["function" "end"] @keyword.function)

(if_statement
  ["if" "end"] @keyword.control.conditional)
(elseif_clause
  ["elseif"] @keyword.control.conditional)
(else_clause
  ["else"] @keyword.control.conditional)
(ternary_expression
  ["?" ":"] @keyword.control.conditional)

(for_statement
  ["for" "end"] @keyword.control.repeat)
(while_statement
  ["while" "end"] @keyword.control.repeat)
(break_statement) @keyword.control.repeat
(continue_statement) @keyword.control.repeat
(for_binding
  "in" @keyword.control.repeat)
(for_clause
  "for" @keyword.control.repeat)

(try_statement
  ["try" "end" ] @keyword.control.exception)
(finally_clause
  "finally" @keyword.control.exception)
(catch_clause
  "catch" @keyword.control.exception)

[
  "export"
  "import"
  "using"
] @keyword.control.import

[
  "abstract"
  "baremodule"
  "begin"
  "const"
  "do"
  "end"
  "let"
  "macro"
  "module"
  "mutable"
  "primitive"
  "quote"
  "return"
  "struct"
  "type"
  "where"
] @keyword

; TODO: fix this
((identifier) @keyword (match? @keyword "global|local"))

; ---------
; Operators
; ---------

[
  (operator)
  "::"
  "<:"
  ":"
  "=>"
  "..."
  "$"
] @operator

; ------------
; Punctuations
; ------------

[
  "."
  "," 
  ";"
] @punctuation.delimiter

[
  "["
  "]"
  "("
  ")" 
  "{" 
  "}"
] @punctuation.bracket

; ---------------------
; Remaining identifiers
; ---------------------

(const_statement
  (variable_declaration
    . (identifier) @constant))

; SCREAMING_SNAKE_CASE
(
  (identifier) @constant
  (match? @constant "^[A-Z][A-Z0-9_]*$"))

; remaining identifiers that start with capital letters should be types (PascalCase)
(
  (identifier) @type
  (match? @type "^[A-Z]"))

; Field expressions are either module content or struct fields.
; Module types and constants should already be captured, so this
; assumes the remaining identifiers to be struct fields.
(field_expression
  (_)
  (identifier) @variable.other.member)

(identifier) @variable
{ fn default() -> Self { Self::new(Layout::Vertical) } } impl Tree { pub fn new(area: Rect) -> Self { let root = Node::container(Layout::Vertical); let mut nodes = HopSlotMap::with_key(); let root = nodes.insert(root); // root is it's own parent nodes[root].parent = root; Self { root, focus: root, // fullscreen: false, area, nodes, stack: Vec::new(), } } pub fn insert(&mut self, view: View) -> ViewId { let focus = self.focus; let parent = self.nodes[focus].parent; let mut node = Node::view(view); node.parent = parent; let node = self.nodes.insert(node); self.get_mut(node).id = node; let container = match &mut self.nodes[parent] { Node { content: Content::Container(container), .. } => container, _ => unreachable!(), }; // insert node after the current item if there is children already let pos = if container.children.is_empty() { 0 } else { let pos = container .children .iter() .position(|&child| child == focus) .unwrap(); pos + 1 }; container.children.insert(pos, node); // focus the new node self.focus = node; // recalculate all the sizes self.recalculate(); node } pub fn split(&mut self, view: View, layout: Layout) -> ViewId { let focus = self.focus; let parent = self.nodes[focus].parent; let node = Node::view(view); let node = self.nodes.insert(node); self.get_mut(node).id = node; let container = match &mut self.nodes[parent] { Node { content: Content::Container(container), .. } => container, _ => unreachable!(), }; if container.layout == layout { // insert node after the current item if there is children already let pos = if container.children.is_empty() { 0 } else { let pos = container .children .iter() .position(|&child| child == focus) .unwrap(); pos + 1 }; container.children.insert(pos, node); self.nodes[node].parent = parent; } else { let mut split = Node::container(layout); split.parent = parent; let split = self.nodes.insert(split); let container = match &mut self.nodes[split] { Node { content: Content::Container(container), .. } => container, _ => unreachable!(), }; container.children.push(focus); container.children.push(node); self.nodes[focus].parent = split; self.nodes[node].parent = split; let container = match &mut self.nodes[parent] { Node { content: Content::Container(container), .. } => container, _ => unreachable!(), }; let pos = container .children .iter() .position(|&child| child == focus) .unwrap(); // replace focus on parent with split container.children[pos] = split; } // focus the new node self.focus = node; // recalculate all the sizes self.recalculate(); node } pub fn remove(&mut self, index: ViewId) { let mut stack = Vec::new(); if self.focus == index { // focus on something else self.focus_next(); } stack.push(index); while let Some(index) = stack.pop() { let parent_id = self.nodes[index].parent; if let Node { content: Content::Container(container), .. } = &mut self.nodes[parent_id] { if let Some(pos) = container.children.iter().position(|&child| child == index) { container.children.remove(pos); // TODO: if container now only has one child, remove it and place child in parent if container.children.is_empty() && parent_id != self.root { // if container now empty, remove it stack.push(parent_id); } } } self.nodes.remove(index); } self.recalculate() } pub fn views(&self) -> impl Iterator<Item = (&View, bool)> { let focus = self.focus; self.nodes.iter().filter_map(move |(key, node)| match node { Node { content: Content::View(view), .. } => Some((view.as_ref(), focus == key)), _ => None, }) } pub fn views_mut(&mut self) -> impl Iterator<Item = (&mut View, bool)> { let focus = self.focus; self.nodes .iter_mut() .filter_map(move |(key, node)| match node { Node { content: Content::View(view), .. } => Some((view.as_mut(), focus == key)), _ => None, }) } pub fn get(&self, index: ViewId) -> &View { match &self.nodes[index] { Node { content: Content::View(view), .. } => view, _ => unreachable!(), } } pub fn get_mut(&mut self, index: ViewId) -> &mut View { match &mut self.nodes[index] { Node { content: Content::View(view), .. } => view, _ => unreachable!(), } } pub fn is_empty(&self) -> bool { match &self.nodes[self.root] { Node { content: Content::Container(container), .. } => container.children.is_empty(), _ => unreachable!(), } } pub fn resize(&mut self, area: Rect) { self.area = area; self.recalculate(); } pub fn recalculate(&mut self) { if self.is_empty() { return; } self.stack.push((self.root, self.area)); // take the area // fetch the node // a) node is view, give it whole area // b) node is container, calculate areas for each child and push them on the stack while let Some((key, area)) = self.stack.pop() { let node = &mut self.nodes[key]; match &mut node.content { Content::View(view) => { // debug!!("setting view area {:?}", area); view.area = area; } // TODO: call f() Content::Container(container) => { // debug!!("setting container area {:?}", area); container.area = area; match container.layout { Layout::Horizontal => { let len = container.children.len(); let height = area.height / len as u16; let mut child_y = area.y; for (i, child) in container.children.iter().enumerate() { let mut area = Rect::new( container.area.x, child_y, container.area.width, height, ); child_y += height; // last child takes the remaining width because we can get uneven // space from rounding if i == len - 1 { area.height = container.area.y + container.area.height - area.y; } self.stack.push((*child, area)); } } Layout::Vertical => { let len = container.children.len(); let width = area.width / len as u16; let inner_gap = 1u16; // let total_gap = inner_gap * (len as u16 - 1); let mut child_x = area.x; for (i, child) in container.children.iter().enumerate() { let mut area = Rect::new( child_x, container.area.y, width, container.area.height, ); child_x += width + inner_gap; // last child takes the remaining width because we can get uneven // space from rounding if i == len - 1 { area.width = container.area.x + container.area.width - area.x; } self.stack.push((*child, area)); } } } } } } } pub fn traverse(&self) -> Traverse { Traverse::new(self) } pub fn focus_next(&mut self) { // This function is very dumb, but that's because we don't store any parent links. // (we'd be able to go parent.next_sibling() recursively until we find something) // For now that's okay though, since it's unlikely you'll be able to open a large enough // number of splits to notice. // current = focus // let found = loop do { // node = focus.parent; // let found = node.next_sibling_of(current) // if some { // break found; // } // // else // if node == root { // return first child of root; // }; // current = parent; // } // } // // use found next sibling // loop do { // if found = view -> focus = found, return // if found = container -> found = first child // } let iter = self.traverse(); let mut iter = iter.skip_while(|&(key, _view)| key != self.focus); iter.next(); // take the focused value if let Some((key, _)) = iter.next() { self.focus = key; } else { // extremely crude, take the first item again let (key, _) = self.traverse().next().unwrap(); self.focus = key; } } } pub struct Traverse<'a> { tree: &'a Tree, stack: Vec<ViewId>, // TODO: reuse the one we use on update } impl<'a> Traverse<'a> { fn new(tree: &'a Tree) -> Self { Self { tree, stack: vec![tree.root], } } } impl<'a> Iterator for Traverse<'a> { type Item = (ViewId, &'a View); fn next(&mut self) -> Option<Self::Item> { loop { let key = self.stack.pop()?; let node = &self.tree.nodes[key]; match &node.content { Content::View(view) => return Some((key, view)), Content::Container(container) => { self.stack.extend(container.children.iter().rev()); } } } } }