aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/status.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-28 07:19:14 +0000
committerSeivan Heidari <[email protected]>2019-11-28 07:19:14 +0000
commit18a0937585b836ec5ed054b9ae48e0156ab6d9ef (patch)
tree9de2c0267ddcc00df717f90034d0843d751a851b /crates/ra_ide/src/status.rs
parenta7394b44c870f585eacfeb3036a33471aff49ff8 (diff)
parent484acc8a61d599662ed63a4cbda091d38a982551 (diff)
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_ide/src/status.rs')
-rw-r--r--crates/ra_ide/src/status.rs136
1 files changed, 136 insertions, 0 deletions
diff --git a/crates/ra_ide/src/status.rs b/crates/ra_ide/src/status.rs
new file mode 100644
index 000000000..1bb27eb85
--- /dev/null
+++ b/crates/ra_ide/src/status.rs
@@ -0,0 +1,136 @@
1//! FIXME: write short doc here
2
3use std::{fmt, iter::FromIterator, sync::Arc};
4
5use hir::MacroFile;
6use ra_db::{
7 salsa::{
8 debug::{DebugQueryTable, TableEntry},
9 Database,
10 },
11 FileTextQuery, SourceRootId,
12};
13use ra_prof::{memory_usage, Bytes};
14use ra_syntax::{ast, Parse, SyntaxNode};
15
16use crate::{
17 db::RootDatabase,
18 symbol_index::{LibrarySymbolsQuery, SymbolIndex},
19 FileId,
20};
21
22fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
23 db.query(ra_db::ParseQuery).entries::<SyntaxTreeStats>()
24}
25fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
26 db.query(hir::db::ParseMacroQuery).entries::<SyntaxTreeStats>()
27}
28
29pub(crate) fn status(db: &RootDatabase) -> String {
30 let files_stats = db.query(FileTextQuery).entries::<FilesStats>();
31 let syntax_tree_stats = syntax_tree_stats(db);
32 let macro_syntax_tree_stats = macro_syntax_tree_stats(db);
33 let symbols_stats = db.query(LibrarySymbolsQuery).entries::<LibrarySymbolsStats>();
34 format!(
35 "{}\n{}\n{}\n{} (macros)\n\n\nmemory:\n{}\ngc {:?} seconds ago",
36 files_stats,
37 symbols_stats,
38 syntax_tree_stats,
39 macro_syntax_tree_stats,
40 memory_usage(),
41 db.last_gc.elapsed().as_secs(),
42 )
43}
44
45#[derive(Default)]
46struct FilesStats {
47 total: usize,
48 size: Bytes,
49}
50
51impl fmt::Display for FilesStats {
52 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
53 write!(fmt, "{} ({}) files", self.total, self.size)
54 }
55}
56
57impl FromIterator<TableEntry<FileId, Arc<String>>> for FilesStats {
58 fn from_iter<T>(iter: T) -> FilesStats
59 where
60 T: IntoIterator<Item = TableEntry<FileId, Arc<String>>>,
61 {
62 let mut res = FilesStats::default();
63 for entry in iter {
64 res.total += 1;
65 res.size += entry.value.unwrap().len();
66 }
67 res
68 }
69}
70
71#[derive(Default)]
72pub(crate) struct SyntaxTreeStats {
73 total: usize,
74 pub(crate) retained: usize,
75}
76
77impl fmt::Display for SyntaxTreeStats {
78 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
79 write!(fmt, "{} trees, {} retained", self.total, self.retained)
80 }
81}
82
83impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStats {
84 fn from_iter<T>(iter: T) -> SyntaxTreeStats
85 where
86 T: IntoIterator<Item = TableEntry<FileId, Parse<ast::SourceFile>>>,
87 {
88 let mut res = SyntaxTreeStats::default();
89 for entry in iter {
90 res.total += 1;
91 res.retained += entry.value.is_some() as usize;
92 }
93 res
94 }
95}
96
97impl<M> FromIterator<TableEntry<MacroFile, Option<(Parse<SyntaxNode>, M)>>> for SyntaxTreeStats {
98 fn from_iter<T>(iter: T) -> SyntaxTreeStats
99 where
100 T: IntoIterator<Item = TableEntry<MacroFile, Option<(Parse<SyntaxNode>, M)>>>,
101 {
102 let mut res = SyntaxTreeStats::default();
103 for entry in iter {
104 res.total += 1;
105 res.retained += entry.value.is_some() as usize;
106 }
107 res
108 }
109}
110
111#[derive(Default)]
112struct LibrarySymbolsStats {
113 total: usize,
114 size: Bytes,
115}
116
117impl fmt::Display for LibrarySymbolsStats {
118 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
119 write!(fmt, "{} ({}) symbols", self.total, self.size,)
120 }
121}
122
123impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbolsStats {
124 fn from_iter<T>(iter: T) -> LibrarySymbolsStats
125 where
126 T: IntoIterator<Item = TableEntry<SourceRootId, Arc<SymbolIndex>>>,
127 {
128 let mut res = LibrarySymbolsStats::default();
129 for entry in iter {
130 let value = entry.value.unwrap();
131 res.total += value.len();
132 res.size += value.memory_size();
133 }
134 res
135 }
136}