@@ -4,11 +4,16 @@ use graphite_desktop_wrapper::messages::{Document, DocumentId};
4
4
pub ( crate ) struct PersistentData {
5
5
documents : DocumentStore ,
6
6
current_document : Option < DocumentId > ,
7
+ #[ serde( skip) ]
8
+ document_order : Option < Vec < DocumentId > > ,
7
9
}
8
10
9
11
impl PersistentData {
10
12
pub ( crate ) fn write_document ( & mut self , id : DocumentId , document : Document ) {
11
13
self . documents . write ( id, document) ;
14
+ if let Some ( order) = & self . document_order {
15
+ self . documents . force_order ( order. clone ( ) ) ;
16
+ }
12
17
self . flush ( ) ;
13
18
}
14
19
@@ -20,13 +25,20 @@ impl PersistentData {
20
25
self . flush ( ) ;
21
26
}
22
27
28
+ pub ( crate ) fn get_current_document_id ( & self ) -> Option < DocumentId > {
29
+ match self . current_document {
30
+ Some ( id) => Some ( id) ,
31
+ None => Some ( * self . documents . document_ids ( ) . first ( ) ?) ,
32
+ }
33
+ }
34
+
23
35
pub ( crate ) fn get_current_document ( & self ) -> Option < ( DocumentId , Document ) > {
24
- let current_id = self . current_document ( ) ?;
36
+ let current_id = self . get_current_document_id ( ) ?;
25
37
Some ( ( current_id, self . documents . read ( & current_id) ?) )
26
38
}
27
39
28
40
pub ( crate ) fn get_documents_before_current ( & self ) -> Vec < ( DocumentId , Document ) > {
29
- let Some ( current_id) = self . current_document ( ) else {
41
+ let Some ( current_id) = self . get_current_document_id ( ) else {
30
42
return Vec :: new ( ) ;
31
43
} ;
32
44
self . documents
@@ -38,7 +50,7 @@ impl PersistentData {
38
50
}
39
51
40
52
pub ( crate ) fn get_documents_after_current ( & self ) -> Vec < ( DocumentId , Document ) > {
41
- let Some ( current_id) = self . current_document ( ) else {
53
+ let Some ( current_id) = self . get_current_document_id ( ) else {
42
54
return Vec :: new ( ) ;
43
55
} ;
44
56
self . documents
@@ -55,18 +67,11 @@ impl PersistentData {
55
67
self . flush ( ) ;
56
68
}
57
69
58
- pub ( crate ) fn force_document_order ( & mut self , order : Vec < DocumentId > ) {
59
- self . documents . force_order ( order) ;
70
+ pub ( crate ) fn set_document_order ( & mut self , order : Vec < DocumentId > ) {
71
+ self . document_order = Some ( order) ;
60
72
self . flush ( ) ;
61
73
}
62
74
63
- fn current_document ( & self ) -> Option < DocumentId > {
64
- match self . current_document {
65
- Some ( id) => Some ( id) ,
66
- None => Some ( * self . documents . document_ids ( ) . first ( ) ?) ,
67
- }
68
- }
69
-
70
75
fn flush ( & self ) {
71
76
let data = match ron:: to_string ( self ) {
72
77
Ok ( d) => d,
@@ -143,8 +148,18 @@ impl DocumentStore {
143
148
} )
144
149
}
145
150
146
- fn force_order ( & mut self , order : Vec < DocumentId > ) {
147
- self . 0 . sort_by_key ( |meta| order. iter ( ) . position ( |id| * id == meta. id ) . unwrap_or ( usize:: MAX ) ) ;
151
+ fn force_order ( & mut self , desired_order : Vec < DocumentId > ) {
152
+ let mut ordered_prefix_len = 0 ;
153
+ for id in desired_order {
154
+ if let Some ( offset) = self . 0 [ ordered_prefix_len..] . iter ( ) . position ( |meta| meta. id == id) {
155
+ let found_index = ordered_prefix_len + offset;
156
+ if found_index != ordered_prefix_len {
157
+ self . 0 [ ordered_prefix_len..=found_index] . rotate_right ( 1 ) ;
158
+ }
159
+ ordered_prefix_len += 1 ;
160
+ }
161
+ }
162
+ self . 0 . truncate ( ordered_prefix_len) ;
148
163
}
149
164
150
165
fn document_ids ( & self ) -> Vec < DocumentId > {
0 commit comments