From 195111d7698c39fb4d653da3a39a8cb52c9260e4 Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Wed, 5 Aug 2020 21:35:35 -0400 Subject: [PATCH] Address PR comments --- crates/rust-analyzer/src/global_state.rs | 7 ++----- crates/rust-analyzer/src/handlers.rs | 7 ++----- crates/rust-analyzer/src/main_loop.rs | 2 +- crates/rust-analyzer/src/to_proto.rs | 14 +++++++------- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 4b34c3ec5f7..0e592ac1bea 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -3,15 +3,12 @@ //! //! Each tick provides an immutable snapshot of the state as `WorldSnapshot`. -use std::{ - sync::{Arc, Mutex}, - time::Instant, -}; +use std::{sync::Arc, time::Instant}; use crossbeam_channel::{unbounded, Receiver, Sender}; use flycheck::FlycheckHandle; use lsp_types::{SemanticTokens, Url}; -use parking_lot::RwLock; +use parking_lot::{Mutex, RwLock}; use ra_db::{CrateId, VfsPath}; use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FileId}; use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target}; diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 0b0ea23fdc3..52dc72aad15 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1187,10 +1187,7 @@ pub(crate) fn handle_semantic_tokens( let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights); // Unconditionally cache the tokens - snap.semantic_tokens_cache - .lock() - .unwrap() - .insert(params.text_document.uri, semantic_tokens.clone()); + snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone()); Ok(Some(semantic_tokens.into())) } @@ -1209,7 +1206,7 @@ pub(crate) fn handle_semantic_tokens_edits( let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights); - let mut cache = snap.semantic_tokens_cache.lock().unwrap(); + let mut cache = snap.semantic_tokens_cache.lock(); let cached_tokens = cache.entry(params.text_document.uri).or_default(); if let Some(prev_id) = &cached_tokens.result_id { diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index eb2a86972e7..075abf45cca 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -452,7 +452,7 @@ fn on_notification(&mut self, not: Notification) -> Result<()> { None => log::error!("orphan DidCloseTextDocument: {}", path), } - this.semantic_tokens_cache.lock().unwrap().remove(¶ms.text_document.uri); + this.semantic_tokens_cache.lock().remove(¶ms.text_document.uri); if let Some(path) = path.as_path() { this.loader.handle.invalidate(path.to_path_buf()); diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 8da883ae435..5eba1f15553 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -1,6 +1,8 @@ //! Conversion of rust-analyzer specific types to lsp_types equivalents. -use std::path::{self, Path}; -use std::time::SystemTime; +use std::{ + path::{self, Path}, + sync::atomic::{AtomicU32, Ordering}, +}; use itertools::Itertools; use ra_db::{FileId, FileRange}; @@ -304,16 +306,14 @@ pub(crate) fn inlay_int(line_index: &LineIndex, inlay_hint: InlayHint) -> lsp_ex } } +static TOKEN_RESULT_COUNTER: AtomicU32 = AtomicU32::new(1); + pub(crate) fn semantic_tokens( text: &str, line_index: &LineIndex, highlights: Vec, ) -> lsp_types::SemanticTokens { - let id = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { - Ok(d) => d.as_millis().to_string(), - Err(_) => String::new(), - }; - + let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string(); let mut builder = semantic_tokens::SemanticTokensBuilder::new(id); for highlight_range in highlights {