mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
move completed requests to a separate file
This commit is contained in:
parent
838915c9a2
commit
e1bda6aeda
5 changed files with 114 additions and 80 deletions
|
@ -31,10 +31,10 @@ use crate::{
|
|||
pub fn handle_analyzer_status(world: ServerWorld, _: ()) -> Result<String> {
|
||||
let mut buf = world.status();
|
||||
writeln!(buf, "\n\nrequests:").unwrap();
|
||||
let requests = world.latest_completed_requests.read();
|
||||
for (idx, r) in requests.iter().enumerate() {
|
||||
let current = if idx == world.request_idx { "*" } else { " " };
|
||||
writeln!(buf, "{:4}{}{:<36}{}ms", r.id, current, r.method, r.duration.as_millis()).unwrap();
|
||||
let requests = world.latest_requests.read();
|
||||
for (is_last, r) in requests.iter() {
|
||||
let mark = if is_last { "*" } else { " " };
|
||||
writeln!(buf, "{}{:4} {:<36}{}ms", mark, r.id, r.method, r.duration.as_millis()).unwrap();
|
||||
}
|
||||
Ok(buf)
|
||||
}
|
||||
|
|
72
crates/ra_lsp_server/src/main_loop/pending_requests.rs
Normal file
72
crates/ra_lsp_server/src/main_loop/pending_requests.rs
Normal file
|
@ -0,0 +1,72 @@
|
|||
use std::time::{Duration, Instant};
|
||||
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CompletedRequest {
|
||||
pub id: u64,
|
||||
pub method: String,
|
||||
pub duration: Duration,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PendingRequest {
|
||||
pub(crate) id: u64,
|
||||
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<u64, PendingRequest>,
|
||||
}
|
||||
|
||||
impl PendingRequests {
|
||||
pub(crate) fn start(&mut self, request: PendingRequest) {
|
||||
let id = request.id;
|
||||
let prev = self.map.insert(id, request);
|
||||
assert!(prev.is_none(), "duplicate request with id {}", id);
|
||||
}
|
||||
pub(crate) fn cancel(&mut self, id: u64) -> bool {
|
||||
self.map.remove(&id).is_some()
|
||||
}
|
||||
pub(crate) fn finish(&mut self, id: u64) -> 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()?)))
|
||||
}
|
||||
}
|
|
@ -1,21 +1,19 @@
|
|||
use ra_ide_api::FileId;
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
pub struct Subscriptions {
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Subscriptions {
|
||||
subs: FxHashSet<FileId>,
|
||||
}
|
||||
|
||||
impl Subscriptions {
|
||||
pub fn new() -> Subscriptions {
|
||||
Subscriptions { subs: FxHashSet::default() }
|
||||
}
|
||||
pub fn add_sub(&mut self, file_id: FileId) {
|
||||
pub(crate) fn add_sub(&mut self, file_id: FileId) {
|
||||
self.subs.insert(file_id);
|
||||
}
|
||||
pub fn remove_sub(&mut self, file_id: FileId) {
|
||||
pub(crate) fn remove_sub(&mut self, file_id: FileId) {
|
||||
self.subs.remove(&file_id);
|
||||
}
|
||||
pub fn subscriptions(&self) -> Vec<FileId> {
|
||||
pub(crate) fn subscriptions(&self) -> Vec<FileId> {
|
||||
self.subs.iter().cloned().collect()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue