aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/main_loop/pending_requests.rs
blob: 73b33e41942c65505f44d2c2e934665e2aacb7f9 (plain)
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
//! Data structures that keep track of inflight requests.

use std::time::{Duration, Instant};

use lsp_server::RequestId;
use rustc_hash::FxHashMap;

#[derive(Debug)]
pub struct CompletedRequest {
    pub id: RequestId,
    pub method: String,
    pub duration: Duration,
}

#[derive(Debug)]
pub(crate) struct PendingRequest {
    pub(crate) id: RequestId,
    pub(crate) method: String,
    pub(crate) received: Instant,
}

impl From<PendingRequest> for CompletedRequest {
    fn from(pending: PendingRequest) -> CompletedRequest {
        CompletedRequest {
            id: pending.id,
            method: pending.method,
            duration: pending.received.elapsed(),
        }
    }
}

#[derive(Debug, Default)]
pub(crate) struct PendingRequests {
    map: FxHashMap<RequestId, PendingRequest>,
}

impl PendingRequests {
    pub(crate) fn start(&mut self, request: PendingRequest) {
        let id = request.id.clone();
        let prev = self.map.insert(id.clone(), request);
        assert!(prev.is_none(), "duplicate request with id {}", id);
    }
    pub(crate) fn cancel(&mut self, id: &RequestId) -> bool {
        self.map.remove(id).is_some()
    }
    pub(crate) fn finish(&mut self, id: &RequestId) -> Option<CompletedRequest> {
        self.map.remove(id).map(CompletedRequest::from)
    }
}

const N_COMPLETED_REQUESTS: usize = 10;

#[derive(Debug, Default)]
pub struct LatestRequests {
    // hand-rolling VecDeque here to print things in a nicer way
    buf: [Option<CompletedRequest>; N_COMPLETED_REQUESTS],
    idx: usize,
}

impl LatestRequests {
    pub(crate) fn record(&mut self, request: CompletedRequest) {
        // special case: don't track status request itself
        if request.method == "rust-analyzer/analyzerStatus" {
            return;
        }
        let idx = self.idx;
        self.buf[idx] = Some(request);
        self.idx = (idx + 1) % N_COMPLETED_REQUESTS;
    }

    pub(crate) fn iter(&self) -> impl Iterator<Item = (bool, &CompletedRequest)> {
        let idx = self.idx;
        self.buf.iter().enumerate().filter_map(move |(i, req)| Some((i == idx, req.as_ref()?)))
    }
}