aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/main_loop
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/main_loop')
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs8
-rw-r--r--crates/ra_lsp_server/src/main_loop/pending_requests.rs72
-rw-r--r--crates/ra_lsp_server/src/main_loop/subscriptions.rs12
3 files changed, 81 insertions, 11 deletions
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 0ebfd641d..8cfb6a192 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -31,10 +31,10 @@ use crate::{
31pub fn handle_analyzer_status(world: ServerWorld, _: ()) -> Result<String> { 31pub fn handle_analyzer_status(world: ServerWorld, _: ()) -> Result<String> {
32 let mut buf = world.status(); 32 let mut buf = world.status();
33 writeln!(buf, "\n\nrequests:").unwrap(); 33 writeln!(buf, "\n\nrequests:").unwrap();
34 let requests = world.latest_completed_requests.read(); 34 let requests = world.latest_requests.read();
35 for (idx, r) in requests.iter().enumerate() { 35 for (is_last, r) in requests.iter() {
36 let current = if idx == world.request_idx { "*" } else { " " }; 36 let mark = if is_last { "*" } else { " " };
37 writeln!(buf, "{:4}{}{:<36}{}ms", r.id, current, r.method, r.duration.as_millis()).unwrap(); 37 writeln!(buf, "{}{:4} {:<36}{}ms", mark, r.id, r.method, r.duration.as_millis()).unwrap();
38 } 38 }
39 Ok(buf) 39 Ok(buf)
40} 40}
diff --git a/crates/ra_lsp_server/src/main_loop/pending_requests.rs b/crates/ra_lsp_server/src/main_loop/pending_requests.rs
new file mode 100644
index 000000000..741770e45
--- /dev/null
+++ b/crates/ra_lsp_server/src/main_loop/pending_requests.rs
@@ -0,0 +1,72 @@
1use std::time::{Duration, Instant};
2
3use rustc_hash::FxHashMap;
4
5#[derive(Debug)]
6pub struct CompletedRequest {
7 pub id: u64,
8 pub method: String,
9 pub duration: Duration,
10}
11
12#[derive(Debug)]
13pub(crate) struct PendingRequest {
14 pub(crate) id: u64,
15 pub(crate) method: String,
16 pub(crate) received: Instant,
17}
18
19impl From<PendingRequest> for CompletedRequest {
20 fn from(pending: PendingRequest) -> CompletedRequest {
21 CompletedRequest {
22 id: pending.id,
23 method: pending.method,
24 duration: pending.received.elapsed(),
25 }
26 }
27}
28
29#[derive(Debug, Default)]
30pub(crate) struct PendingRequests {
31 map: FxHashMap<u64, PendingRequest>,
32}
33
34impl PendingRequests {
35 pub(crate) fn start(&mut self, request: PendingRequest) {
36 let id = request.id;
37 let prev = self.map.insert(id, request);
38 assert!(prev.is_none(), "duplicate request with id {}", id);
39 }
40 pub(crate) fn cancel(&mut self, id: u64) -> bool {
41 self.map.remove(&id).is_some()
42 }
43 pub(crate) fn finish(&mut self, id: u64) -> Option<CompletedRequest> {
44 self.map.remove(&id).map(CompletedRequest::from)
45 }
46}
47
48const N_COMPLETED_REQUESTS: usize = 10;
49
50#[derive(Debug, Default)]
51pub struct LatestRequests {
52 // hand-rolling VecDeque here to print things in a nicer way
53 buf: [Option<CompletedRequest>; N_COMPLETED_REQUESTS],
54 idx: usize,
55}
56
57impl LatestRequests {
58 pub(crate) fn record(&mut self, request: CompletedRequest) {
59 // special case: don't track status request itself
60 if request.method == "rust-analyzer/analyzerStatus" {
61 return;
62 }
63 let idx = self.idx;
64 self.buf[idx] = Some(request);
65 self.idx = (idx + 1) % N_COMPLETED_REQUESTS;
66 }
67
68 pub(crate) fn iter(&self) -> impl Iterator<Item = (bool, &CompletedRequest)> {
69 let idx = self.idx;
70 self.buf.iter().enumerate().filter_map(move |(i, req)| Some((i == idx, req.as_ref()?)))
71 }
72}
diff --git a/crates/ra_lsp_server/src/main_loop/subscriptions.rs b/crates/ra_lsp_server/src/main_loop/subscriptions.rs
index 11bd952d9..470bc1205 100644
--- a/crates/ra_lsp_server/src/main_loop/subscriptions.rs
+++ b/crates/ra_lsp_server/src/main_loop/subscriptions.rs
@@ -1,21 +1,19 @@
1use ra_ide_api::FileId; 1use ra_ide_api::FileId;
2use rustc_hash::FxHashSet; 2use rustc_hash::FxHashSet;
3 3
4pub struct Subscriptions { 4#[derive(Default)]
5pub(crate) struct Subscriptions {
5 subs: FxHashSet<FileId>, 6 subs: FxHashSet<FileId>,
6} 7}
7 8
8impl Subscriptions { 9impl Subscriptions {
9 pub fn new() -> Subscriptions { 10 pub(crate) fn add_sub(&mut self, file_id: FileId) {
10 Subscriptions { subs: FxHashSet::default() }
11 }
12 pub fn add_sub(&mut self, file_id: FileId) {
13 self.subs.insert(file_id); 11 self.subs.insert(file_id);
14 } 12 }
15 pub fn remove_sub(&mut self, file_id: FileId) { 13 pub(crate) fn remove_sub(&mut self, file_id: FileId) {
16 self.subs.remove(&file_id); 14 self.subs.remove(&file_id);
17 } 15 }
18 pub fn subscriptions(&self) -> Vec<FileId> { 16 pub(crate) fn subscriptions(&self) -> Vec<FileId> {
19 self.subs.iter().cloned().collect() 17 self.subs.iter().cloned().collect()
20 } 18 }
21} 19}