-
Notifications
You must be signed in to change notification settings - Fork 42
/
documents.rs
1368 lines (1213 loc) · 45.4 KB
/
documents.rs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::{
errors::attempt,
formats::{Format, FORMATS},
graphs::{Relation, Resource},
methods::{
compile::compile,
decode::decode,
encode::{self, encode},
},
pubsub::publish,
utils::{
hash::{file_sha256_hex, str_sha256_hex},
schemas, uuids,
},
};
use defaults::Defaults;
use eyre::{bail, Result};
use notify::DebouncedEvent;
use once_cell::sync::Lazy;
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::ser::SerializeMap;
use serde::Serialize;
use serde_with::skip_serializing_none;
use std::time::{Duration, Instant};
use std::{
collections::{hash_map::Entry, HashMap},
env, fs,
path::{Path, PathBuf},
sync::Arc,
};
use stencila_schema::Node;
use strum::ToString;
use tokio::{sync::Mutex, task::JoinHandle};
#[derive(Debug, JsonSchema, Serialize, ToString)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
enum DocumentEventType {
Deleted,
Renamed,
Modified,
Encoded,
}
#[skip_serializing_none]
#[derive(Debug, JsonSchema, Serialize)]
#[schemars(deny_unknown_fields)]
struct DocumentEvent {
/// The type of event
#[serde(rename = "type")]
type_: DocumentEventType,
/// The document associated with the event
#[schemars(schema_with = "DocumentEvent::schema_document")]
document: Document,
/// The content associated with the event, only provided for, `modified`
/// and `encoded` events.
content: Option<String>,
/// The format of the document, only provided for `modified` (the format
/// of the document) and `encoded` events (the format of the encoding).
#[schemars(schema_with = "DocumentEvent::schema_format")]
format: Option<Format>,
}
impl DocumentEvent {
/// Generate the JSON Schema for the `document` property to avoid nesting
fn schema_document(_generator: &mut SchemaGenerator) -> Schema {
schemas::typescript("Document", true)
}
/// Generate the JSON Schema for the `format` property to avoid nesting
fn schema_format(_generator: &mut schemars::gen::SchemaGenerator) -> Schema {
schemas::typescript("Format", false)
}
}
/// The status of a document with respect to on-disk synchronization
#[derive(Debug, Clone, JsonSchema, Serialize, ToString)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
enum DocumentStatus {
/// The document `content` is the same as on disk at its `path`.
Synced,
/// The document `content` has modifications that have not yet
/// been written to its `path`.
Unwritten,
/// The document `path` has modifications that have not yet
/// been read into its `content`.
Unread,
/// The document `path` no longer exists and is now set to `None`.
/// The user will need to choose a new path for the document if they
/// want to save it.
Deleted,
}
/// An in-memory representation of a document
#[derive(Debug, Clone, JsonSchema, Defaults, Serialize)]
#[schemars(deny_unknown_fields)]
pub struct Document {
/// The document identifier
pub id: String,
/// The absolute path of the document's file.
pub path: PathBuf,
/// The project directory for this document.
///
/// Used to restrict file links (e.g. image paths) to within
/// the project for both security and reproducibility reasons.
/// For documents opened from within a project, this will be project directory.
/// For "orphan" documents (opened by themselves) this will be the
/// parent directory of the document. When the document is compiled,
/// an error will be returned if a file link is outside of the root.
project: PathBuf,
/// Whether or not the document's file is in the temporary
/// directory.
temporary: bool,
/// The synchronization status of the document.
/// This is orthogonal to `temporary` because a document's
/// `content` can be synced or un-synced with the file system
/// regardless of whether or not its `path` is temporary..
#[def = "DocumentStatus::Unread"]
status: DocumentStatus,
/// The last time that the document was written to disk.
/// Used to ignore subsequent file modification events.
#[serde(skip)]
last_write: Option<Instant>,
/// The name of the document
///
/// Usually the filename from the `path` but "Untitled"
/// for temporary documents.
name: String,
/// The format of the document.
///
/// On initialization, this is inferred, if possible, from the file name extension
/// of the document's `path`. However, it may change whilst the document is
/// open in memory (e.g. if the `load` function sets a different format).
#[def = "Format::unknown(\"unknown\")"]
#[schemars(schema_with = "Document::schema_format")]
format: Format,
/// Whether a HTML preview of the document is supported
///
/// This is determined by the type of the `root` node of the document.
/// Will be `true` if the `root` is a type for which HTML previews are
/// implemented e.g. `Article`, `ImageObject` and `false` if the `root`
/// is `None`, or of some other type e.g. `Entity`.
///
/// This flag is intended for dynamically determining whether to open
/// a preview panel for a document by default. Regardless of its value,
/// a user should be able to open a preview panel, in HTML or some other
/// format, for any document.
#[def = "false"]
previewable: bool,
/// The current UTF8 string content of the document.
///
/// When a document is `read()` from a file the `content` is the content
/// of the file. The `content` may subsequently be changed using
/// the `load()` function. A call to `write()` will write the content
/// back to `path`.
#[serde(skip)]
content: String,
/// The root Stencila Schema node of the document
#[serde(skip)]
pub root: Option<Node>,
/// The set of relations between nodes in this document and other
/// resources.
///
/// Relations may be external (e.g. this document links to
/// another file) or internal (e.g. the second code chunk uses a variable
/// defined in the first code chunk).
#[schemars(schema_with = "Document::schema_relations")]
#[serde(skip_deserializing, serialize_with = "Document::serialize_relations")]
pub relations: HashMap<Resource, Vec<(Relation, Resource)>>,
/// The set of unique subscriptions to this document
///
/// Keeps track of the number of subscribers to each of the document's
/// topic channels. Events will only be published on channels that
/// have at least one subscriber.
///
/// Valid subscription topics are the names of the `DocumentEvent` types:
///
/// - `removed`: published when document file is deleted
/// - `renamed`: published when document file is renamed
/// - `modified`: published when document file is modified
/// - `encoded:<format>` published when a document's content
/// is changed internally or externally and conversions have been
/// completed e.g. `encoded:html`
subscriptions: HashMap<String, u32>,
}
#[allow(unused)]
impl Document {
/// Generate the JSON Schema for the `format` property to avoid duplicated
/// inline type.
fn schema_format(_generator: &mut schemars::gen::SchemaGenerator) -> Schema {
schemas::typescript("Format", true)
}
/// Generate the JSON Schema for the `relations` property to avoid duplicated
/// inline types and allow for custom serialization.
fn schema_relations(_generator: &mut schemars::gen::SchemaGenerator) -> Schema {
schemas::typescript("Record<string, [Relation, Resource]>", false)
}
/// Serialize the `relations` property such that the `Resource` keys are represented as
/// a string suitable for encoding as JSON.
fn serialize_relations<S>(
relations: &HashMap<Resource, Vec<(Relation, Resource)>>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut map = serializer.serialize_map(Some(relations.len()))?;
for (resource, pairs) in relations {
let key = match resource {
Resource::Node(node) => [&node.kind, "@", &node.id].concat(),
_ => unreachable!(),
};
map.serialize_entry(&key, pairs);
}
map.end()
}
/// Create a new empty document.
///
/// # Arguments
///
/// - `path`: The path of the document; defaults to a temporary path.
/// - `format`: The format of the document; defaults to plain text.
///
/// This function is intended to be used by editors when creating
/// a new document. If the `path` is not specified, the created document
/// will be `temporary: true` and have a temporary file path.
fn new(path: Option<PathBuf>, format: Option<String>) -> Document {
let id = uuids::generate(uuids::Family::Document);
let format = if let Some(format) = format {
FORMATS.match_path(&format)
} else if let Some(path) = path.as_ref() {
FORMATS.match_path(path)
} else {
FORMATS.match_name("txt")
};
let previewable = format.preview;
let (path, name, temporary) = match path {
Some(path) => {
let name = path
.file_name()
.map(|os_str| os_str.to_string_lossy())
.unwrap_or_else(|| "Untitled".into())
.into();
(path, name, false)
}
None => {
let path = env::temp_dir().join(
[
uuids::generate(uuids::Family::File),
format.extensions.first().cloned().unwrap_or_default(),
]
.concat(),
);
// Ensure that the file exists
if !path.exists() {
fs::write(path.clone(), "").expect("Unable to write temporary file");
}
let name = "Untitled".into();
(path, name, true)
}
};
let project = path
.parent()
.expect("Unable to get path parent")
.to_path_buf();
Document {
id,
path,
project,
temporary,
status: DocumentStatus::Synced,
name,
format,
previewable,
..Default::default()
}
}
/// Open a document from an existing file.
///
/// # Arguments
///
/// - `path`: The path of the file to create the document from
///
/// - `format`: The format of the document. If `None` will be inferred from
/// the path's file extension.
/// TODO: add project: Option<PathBuf> so that project can be explictly set
pub async fn open<P: AsRef<Path>>(path: P, format: Option<String>) -> Result<Document> {
let path = path.as_ref().canonicalize()?;
// Create a new document with unique id
let mut document = Document {
id: uuids::generate(uuids::Family::Document),
..Default::default()
};
// Apply path and format arguments
document.alter(Some(path), format).await?;
// Attempt to read the document from the file
attempt(document.read().await);
Ok(document)
}
/// Alter properties of the document
///
/// # Arguments
///
/// - `path`: The path of document's file
///
/// - `format`: The format of the document. If `None` will be inferred from
/// the path's file extension.
pub async fn alter<P: AsRef<Path>>(
&mut self,
path: Option<P>,
format: Option<String>,
) -> Result<()> {
if let Some(path) = &path {
let path = path.as_ref();
if path.is_dir() {
bail!("Can not open a folder as a document; maybe try opening it as a project instead.")
}
self.project = path
.parent()
.expect("Unable to get path parent")
.to_path_buf();
self.name = path
.file_name()
.map(|os_str| os_str.to_string_lossy())
.unwrap_or_else(|| "Untitled".into())
.into();
self.path = path.to_path_buf();
self.temporary = false;
self.status = DocumentStatus::Unwritten;
}
if let Some(format) = format {
self.format = FORMATS.match_path(&format);
} else if let Some(path) = path {
self.format = FORMATS.match_path(&path);
};
self.previewable = self.format.preview;
// Given that the `format` may have changed, it is necessary
// to update the `root` of the document
self.update(true).await?;
Ok(())
}
/// Read the document from the file system, update it and return its content.
///
/// Sets `status` to `Synced`. For binary files, does not actually read the content
/// but will update the document nonetheless (possibly delegating the actual read
/// to a binary or plugin)
pub async fn read(&mut self) -> Result<String> {
let content = if !self.format.binary {
let content = fs::read_to_string(&self.path)?;
self.load(content.clone(), None).await?;
content
} else {
self.update(true).await?;
"".to_string()
};
self.status = DocumentStatus::Synced;
Ok(content)
}
/// Write the document to the file system, optionally load new `content`
/// and set `format` before doing so.
///
/// # Arguments
///
/// - `content`: the content to load into the document
/// - `format`: the format of the content; if not supplied assumed to be
/// the document's existing format.
///
/// Sets `status` to `Synced`.
pub async fn write(&mut self, content: Option<String>, format: Option<String>) -> Result<()> {
if let Some(content) = content {
self.load(content, format.clone()).await?;
}
let content_to_write = if let Some(input_format) = format.as_ref() {
let input_format = FORMATS.match_path(&input_format);
if input_format.name != self.format.name {
self.dump(None).await?
} else {
self.content.clone()
}
} else {
self.content.clone()
};
fs::write(&self.path, content_to_write.as_bytes())?;
self.status = DocumentStatus::Synced;
self.last_write = Some(Instant::now());
Ok(())
}
/// Write the document to the file system, as an another file, possibly in
/// another format.
///
/// # Arguments
///
/// - `path`: the path for the new file.
/// - `format`: the format to dump the content as; if not supplied assumed to be
/// the document's existing format.
/// - `theme`: theme to apply to the new document (HTML and PDF only).
///
/// Note: this does not change the `path`, `format` or `status` of the current
/// document.
pub async fn write_as<P: AsRef<Path>>(
&self,
path: P,
format: Option<String>,
theme: Option<String>,
) -> Result<()> {
let path = path.as_ref();
let format = format.unwrap_or_else(|| {
path.extension().map_or_else(
|| self.format.name.clone(),
|ext| ext.to_string_lossy().to_string(),
)
});
let output = ["file://", &path.display().to_string()].concat();
let content = if let Some(root) = &self.root {
let mut options = encode::Options::default();
options.standalone = true;
if let Some(theme) = theme {
options.theme = theme
}
encode(root, &output, &format, Some(options)).await?
} else {
tracing::warn!("Document has no root node");
"".to_string()
};
if !content.starts_with("file://") {
fs::write(path, content)?;
}
Ok(())
}
/// Dump the document's content to a string in its current, or
/// alternative, format.
///
/// # Arguments
///
/// - `format`: the format to dump the content as; if not supplied assumed to be
/// the document's existing format.
pub async fn dump(&self, format: Option<String>) -> Result<String> {
let format = match format {
Some(format) => format,
None => return Ok(self.content.clone()),
};
if let Some(root) = &self.root {
encode(root, "string://", &format, None).await
} else {
tracing::warn!("Document has no root node");
Ok(String::new())
}
}
/// Load content into the document
///
/// If the format of the new content is different to the document's format
/// then the content will be converted to the document's format.
///
/// # Arguments
///
/// - `content`: the content to load into the document
/// - `format`: the format of the content; if not supplied assumed to be
/// the document's existing format.
pub async fn load(&mut self, content: String, format: Option<String>) -> Result<()> {
let mut decode_content = true;
if let Some(format) = format {
let other_format = FORMATS.match_path(&format);
if other_format.name != self.format.name {
let node = decode(&content, &other_format.name).await?;
if !self.format.binary {
self.content = encode(&node, "string://", &self.format.name, None).await?;
}
self.root = Some(node);
decode_content = false;
} else {
self.content = content;
}
} else {
self.content = content;
};
self.status = DocumentStatus::Unwritten;
self.update(decode_content).await
}
/// Get the SHA-256 of the document
///
/// For text-based documents, returns the SHA-256 of the document's `content`.
/// For binary documents, returns the SHA-256 of the document's file.
pub fn sha256(&self) -> Result<String> {
match self.format.binary {
true => Ok(str_sha256_hex(&self.content)),
false => file_sha256_hex(&self.path),
}
}
/// Update the `root` (and associated properties) of the document and publish updated encodings
///
/// Publishes `encoded:` events for each of the formats subscribed to.
/// Error results from this function (e.g. compile errors)
/// should generally not be bubbled up.
///
/// # Arguments
///
/// - `decode_content`: Should the current content of the be decoded?. This
/// is an optimization for situations where the `root` has
/// just been decoded from the current `content`.
async fn update(&mut self, decode_content: bool) -> Result<()> {
tracing::debug!(
"Updating document '{}' at '{}'",
self.id,
self.path.display()
);
// Decode the binary file or, in-memory content into the `root` node
// of the document
let format = &self.format.name;
let mut root = if self.format.binary {
if self.path.exists() {
let path = self.path.display().to_string();
let input = ["file://", &path].concat();
decode(&input, format).await?
} else {
match self.root.as_ref() {
Some(root) => root.clone(),
None => return Ok(()),
}
}
} else if !self.content.is_empty() {
if decode_content {
decode(&self.content, format).await?
} else {
match self.root.as_ref() {
Some(root) => root.clone(),
None => return Ok(()),
}
}
} else {
self.root = None;
return Ok(());
};
#[cfg(feature = "reshape")]
{
// Reshape the `root` according to preferences
use crate::methods::reshape::{self, reshape};
reshape(&mut root, reshape::Options::default())?;
}
// Compile the `root` and update document intra- and inter- dependencies
let compilation = compile(&mut root, &self.path, &self.project).await?;
self.relations = compilation.relations.into_iter().collect();
// Encode the `root` into each of the formats for which there are subscriptions
for subscription in self.subscriptions.keys() {
if let Some(format) = subscription.strip_prefix("encoded:") {
tracing::debug!("Encoding document '{}' to '{}'", self.id, format);
match encode(&root, "string://", format, None).await {
Ok(content) => {
self.publish(
DocumentEventType::Encoded,
Some(content),
Some(format.into()),
);
}
Err(error) => {
tracing::warn!("Unable to encode to format \"{}\": {}", format, error)
}
}
}
}
// Determine if the document is preview-able, based on the type of the root
// This list of types should be updated as HTML encoding is implemented for each.
self.previewable = matches!(
root,
Node::Article(..)
| Node::ImageObject(..)
| Node::AudioObject(..)
| Node::VideoObject(..)
);
// Now that we're done borrowing the root node for encoding to
// different formats, store it.
self.root = Some(root);
Ok(())
}
/// Query the document
///
/// Returns a JSON value. Returns `null` if the query does not select anything.
#[allow(unreachable_code)]
pub fn query(&self, query: &str, lang: &str) -> Result<serde_json::Value> {
let result = match lang {
#[cfg(feature = "query-jmespath")]
"jmespath" => {
let expr = jmespatch::compile(query)?;
let result = expr.search(&self.root)?;
serde_json::to_value(result)?
}
#[cfg(feature = "query-jsonptr")]
"jsonptr" => {
let data = serde_json::to_value(&self.root)?;
let result = data.pointer(query);
match result {
Some(value) => value.clone(),
None => serde_json::Value::Null,
}
}
_ => bail!("Unknown query language '{}'", lang),
};
Ok(result)
}
/// Add a subscriber to one of the document's topics
#[allow(clippy::unnecessary_wraps)]
pub fn subscribe(&mut self, topic: &str) -> Result<()> {
match self.subscriptions.entry(topic.into()) {
Entry::Occupied(mut entry) => {
let subscribers = entry.get_mut();
*subscribers += 1;
}
Entry::Vacant(entry) => {
entry.insert(1);
}
}
Ok(())
}
/// Remove a subscriber to one of the document's topics
#[allow(clippy::unnecessary_wraps)]
pub fn unsubscribe(&mut self, topic: &str) -> Result<()> {
match self.subscriptions.entry(topic.into()) {
Entry::Occupied(mut entry) => {
let subscribers = entry.get_mut();
*subscribers -= 1;
if *subscribers == 0 {
entry.remove();
}
}
Entry::Vacant(_) => {}
}
Ok(())
}
/// Publish a `DocumentEvent` for this document
fn publish(&self, type_: DocumentEventType, content: Option<String>, format: Option<String>) {
let format = format.map(|name| FORMATS.match_name(&name));
let topic = match type_ {
DocumentEventType::Encoded => format!(
"encoded:{}",
format
.clone()
.map_or_else(|| "undef".to_string(), |format| format.name)
),
_ => type_.to_string(),
};
let topic = format!("documents:{}:{}", self.id, topic);
publish(
&topic,
&DocumentEvent {
type_,
document: self.clone(),
content,
format,
},
)
}
/// Called when the file is removed from the file system
///
/// Sets `status` to `Deleted` and publishes a `Deleted` event so that,
/// for example, a document's tab can be updated to indicate it is deleted.
fn deleted(&mut self, path: PathBuf) {
tracing::debug!("Document removed: {}", path.display());
self.status = DocumentStatus::Deleted;
self.publish(DocumentEventType::Deleted, None, None)
}
/// Called when the file is renamed
///
/// Changes the `path` and publishes a `Renamed` event so that, for example,
/// a document's tab can be updated with the new file name.
#[allow(dead_code)]
fn renamed(&mut self, from: PathBuf, to: PathBuf) {
tracing::debug!("Document renamed: {} to {}", from.display(), to.display());
// If the document has been moved out of its project then we need to reassign `project`
// (to ensure that files in the old project can not be linked to).
if to.strip_prefix(&self.project).is_err() {
self.project = match to.parent() {
Some(path) => path.to_path_buf(),
None => to.clone(),
}
}
self.path = to;
self.publish(DocumentEventType::Renamed, None, None)
}
const LAST_WRITE_MUTE_MILLIS: u64 = 300;
/// Called when the file is modified
///
/// Reads the file into `content` and emits a `Modified` event so that the user
/// can be asked if they want to load the new content into editor, or overwrite with
/// existing editor content.
///
/// Will ignore any events within a small duration of `write()` being called.
async fn modified(&mut self, path: PathBuf) {
if let Some(last_write) = self.last_write {
if last_write.elapsed() < Duration::from_millis(Document::LAST_WRITE_MUTE_MILLIS) {
return;
}
}
tracing::debug!("Document modified: {}", path.display());
self.status = DocumentStatus::Unread;
match self.read().await {
Ok(content) => self.publish(
DocumentEventType::Modified,
Some(content),
Some(self.format.name.clone()),
),
Err(error) => tracing::error!("While attempting to read modified file: {}", error),
}
}
}
#[derive(Debug)]
pub struct DocumentHandler {
/// The document being handled.
document: Arc<Mutex<Document>>,
/// The watcher thread's channel sender.
///
/// Held so that when this handler is dropped, the
/// watcher thread is ended.
watcher: Option<crossbeam_channel::Sender<()>>,
/// The event handler thread's join handle.
///
/// Held so that when this handler is dropped, the
/// event handler thread is aborted.
handler: Option<JoinHandle<()>>,
}
impl Clone for DocumentHandler {
fn clone(&self) -> Self {
DocumentHandler {
document: self.document.clone(),
watcher: None,
handler: None,
}
}
}
impl Drop for DocumentHandler {
fn drop(&mut self) {
match &self.handler {
Some(handler) => handler.abort(),
None => {}
}
}
}
impl DocumentHandler {
/// Create a new document handler.
///
/// # Arguments
///
/// - `document`: The document that this handler is for.
/// - `watch`: Whether to watch the document (e.g. not for temporary, new files)
fn new(document: Document, watch: bool) -> DocumentHandler {
let id = document.id.clone();
let path = document.path.clone();
let document = Arc::new(Mutex::new(document));
let (watcher, handler) = if watch {
let (watcher, handler) = DocumentHandler::watch(id, path, Arc::clone(&document));
(Some(watcher), Some(handler))
} else {
(None, None)
};
DocumentHandler {
document,
watcher,
handler,
}
}
const WATCHER_DELAY_MILLIS: u64 = 100;
/// Watch the document.
///
/// It is necessary to have a file watcher that is separate from a project directory watcher
/// for documents that are opened independent of a project (a.k.a. orphan documents).
///
/// It is also necessary for this watcher to be on the parent folder of the document
/// (which, for some documents may be concurrent with the watcher for the project) and to filter
/// events related to the file. That is necessary because some events are otherwise
/// not captured e.g. file renames (delete and then create) and file writes by some software
/// (e.g. LibreOffice deletes and then creates a file instead of just writing it).
fn watch(
id: String,
path: PathBuf,
document: Arc<Mutex<Document>>,
) -> (crossbeam_channel::Sender<()>, JoinHandle<()>) {
let (thread_sender, thread_receiver) = crossbeam_channel::bounded(1);
let (async_sender, mut async_receiver) = tokio::sync::mpsc::channel(100);
let path_cloned = path.clone();
// Standard thread to run blocking sync file watcher
std::thread::spawn(move || -> Result<()> {
use notify::{watcher, RecursiveMode, Watcher};
let (watcher_sender, watcher_receiver) = std::sync::mpsc::channel();
let mut watcher = watcher(
watcher_sender,
Duration::from_millis(DocumentHandler::WATCHER_DELAY_MILLIS),
)?;
let parent = path.parent().unwrap_or(&path);
watcher.watch(&parent, RecursiveMode::NonRecursive)?;
// Event checking timeout. Can be quite long since only want to check
// whether we can end this thread.
let timeout = Duration::from_millis(100);
let path_string = path.display().to_string();
let span = tracing::info_span!("document_watch", path = path_string.as_str());
let _enter = span.enter();
tracing::debug!(
"Starting document watcher for '{}' at '{}'",
id,
path_string
);
loop {
// Check for an event. Use `recv_timeout` so we don't
// get stuck here and will do following check that ends this
// thread if the owning `DocumentHandler` is dropped
if let Ok(event) = watcher_receiver.recv_timeout(timeout) {
tracing::debug!(
"Event for document '{}' at '{}': {:?}",
id,
path_string,
event
);
if async_sender.blocking_send(event).is_err() {
break;
}
}
// Check to see if this thread should be ended
if let Err(crossbeam_channel::TryRecvError::Disconnected) =
thread_receiver.try_recv()
{
break;
}
}
tracing::debug!("Ending document watcher for '{}' at '{}'", id, path_string);
// Drop the sync send so that the event handling thread also ends
drop(async_sender);
Ok(())
});
// Async task to handle events
let handler = tokio::spawn(async move {
let mut document_path = path_cloned;
tracing::debug!("Starting document handler");
while let Some(event) = async_receiver.recv().await {
match event {
DebouncedEvent::Create(path) | DebouncedEvent::Write(path) => {
if path == document_path {
document.lock().await.modified(path).await
}
}
DebouncedEvent::Remove(path) => {
if path == document_path {
document.lock().await.deleted(path)
}
}
DebouncedEvent::Rename(from, to) => {
if from == document_path {
document_path = to.clone();
document.lock().await.renamed(from, to)
}
}
_ => {}
}
}
// Because we abort this thread, this entry may never get
// printed (only if the `async_sender` is dropped before this is aborted)
tracing::debug!("Ending document handler");
});
(thread_sender, handler)
}
}
/// An in-memory store of documents
#[derive(Debug, Default)]
pub struct Documents {
/// A mapping of file paths to open documents
registry: Mutex<HashMap<String, DocumentHandler>>,
}
impl Documents {
/// Create a new documents store
pub fn new() -> Self {
Self::default()
}
/// List documents that are currently open
///
/// Returns a vector of document paths (relative to the current working directory)
pub async fn list(&self) -> Result<Vec<String>> {
let cwd = std::env::current_dir()?;
let mut paths = Vec::new();
for document in self.registry.lock().await.values() {
let path = &document.document.lock().await.path;
let path = match pathdiff::diff_paths(path, &cwd) {
Some(path) => path,
None => path.clone(),
};
let path = path.display().to_string();
paths.push(path);
}
Ok(paths)
}
/// Create a new empty document
pub async fn create(&self, path: Option<String>, format: Option<String>) -> Result<Document> {
let path = path.map(PathBuf::from);
let document = Document::new(path, format);
let handler = DocumentHandler::new(document.clone(), false);
self.registry
.lock()
.await
.insert(document.id.clone(), handler);