2020-02-18 05:25:26 -06:00
|
|
|
//! The context or environment in which the language server functions. In our
|
|
|
|
//! server implementation this is know as the `WorldState`.
|
2019-12-21 14:27:38 -06:00
|
|
|
//!
|
|
|
|
//! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2023-05-02 09:12:22 -05:00
|
|
|
use std::time::Instant;
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2020-06-25 10:14:11 -05:00
|
|
|
use crossbeam_channel::{unbounded, Receiver, Sender};
|
2020-06-25 16:44:58 -05:00
|
|
|
use flycheck::FlycheckHandle;
|
2021-05-17 12:07:10 -05:00
|
|
|
use ide::{Analysis, AnalysisHost, Cancellable, Change, FileId};
|
2023-03-26 01:39:28 -05:00
|
|
|
use ide_db::base_db::{CrateId, FileLoader, ProcMacroPaths, SourceDatabase};
|
2020-07-24 16:55:17 -05:00
|
|
|
use lsp_types::{SemanticTokens, Url};
|
2023-05-04 18:28:15 -05:00
|
|
|
use nohash_hasher::IntMap;
|
2020-08-05 20:35:35 -05:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2021-08-31 07:44:43 -05:00
|
|
|
use proc_macro_api::ProcMacroServer;
|
2021-08-22 05:32:00 -05:00
|
|
|
use project_model::{CargoWorkspace, ProjectWorkspace, Target, WorkspaceBuildScripts};
|
2020-07-21 13:07:42 -05:00
|
|
|
use rustc_hash::FxHashMap;
|
2023-05-02 09:12:22 -05:00
|
|
|
use triomphe::Arc;
|
2020-12-09 10:01:15 -06:00
|
|
|
use vfs::AnchoredPathBuf;
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2018-10-15 12:15:53 -05:00
|
|
|
use crate::{
|
2023-05-26 08:21:00 -05:00
|
|
|
config::{Config, ConfigError},
|
2020-06-13 04:00:06 -05:00
|
|
|
diagnostics::{CheckFixes, DiagnosticCollection},
|
2020-06-11 04:04:09 -05:00
|
|
|
from_proto,
|
2021-02-12 16:28:48 -06:00
|
|
|
line_index::{LineEndings, LineIndex},
|
2021-04-06 06:16:35 -05:00
|
|
|
lsp_ext,
|
2020-06-26 04:21:21 -05:00
|
|
|
main_loop::Task,
|
2021-07-26 12:16:47 -05:00
|
|
|
mem_docs::MemDocs,
|
2021-01-10 09:02:02 -06:00
|
|
|
op_queue::OpQueue,
|
2021-09-13 12:58:09 -05:00
|
|
|
reload::{self, SourceRootConfig},
|
2022-05-14 16:08:16 -05:00
|
|
|
task_pool::TaskPool,
|
2020-06-13 04:00:06 -05:00
|
|
|
to_proto::url_from_abs_path,
|
2020-06-11 04:04:09 -05:00
|
|
|
Result,
|
2018-08-17 11:54:08 -05:00
|
|
|
};
|
|
|
|
|
2020-06-25 17:27:39 -05:00
|
|
|
// Enforces drop order
|
|
|
|
pub(crate) struct Handle<H, C> {
|
|
|
|
pub(crate) handle: H,
|
|
|
|
pub(crate) receiver: C,
|
|
|
|
}
|
|
|
|
|
2020-06-26 04:21:21 -05:00
|
|
|
pub(crate) type ReqHandler = fn(&mut GlobalState, lsp_server::Response);
|
|
|
|
pub(crate) type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>;
|
|
|
|
|
2020-06-03 04:16:08 -05:00
|
|
|
/// `GlobalState` is the primary mutable state of the language server
|
2019-06-01 02:31:40 -05:00
|
|
|
///
|
|
|
|
/// The most interesting components are `vfs`, which stores a consistent
|
|
|
|
/// snapshot of the file systems, and `analysis_host`, which stores our
|
|
|
|
/// incremental salsa database.
|
2020-06-25 17:27:39 -05:00
|
|
|
///
|
2022-02-14 05:30:21 -06:00
|
|
|
/// Note that this struct has more than one impl in various modules!
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) struct GlobalState {
|
2020-06-25 10:14:11 -05:00
|
|
|
sender: Sender<lsp_server::Message>,
|
2020-06-26 10:07:14 -05:00
|
|
|
req_queue: ReqQueue,
|
2023-05-26 08:09:19 -05:00
|
|
|
|
2020-06-25 17:27:39 -05:00
|
|
|
pub(crate) task_pool: Handle<TaskPool<Task>, Receiver<Task>>,
|
2023-05-26 08:09:19 -05:00
|
|
|
|
2021-01-06 06:46:31 -06:00
|
|
|
pub(crate) config: Arc<Config>,
|
2023-05-26 08:21:00 -05:00
|
|
|
pub(crate) config_errors: Option<ConfigError>,
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) analysis_host: AnalysisHost,
|
|
|
|
pub(crate) diagnostics: DiagnosticCollection,
|
2021-07-26 12:16:47 -05:00
|
|
|
pub(crate) mem_docs: MemDocs,
|
2023-05-26 08:09:19 -05:00
|
|
|
pub(crate) source_root_config: SourceRootConfig,
|
2020-07-24 16:55:17 -05:00
|
|
|
pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
|
2023-05-26 08:09:19 -05:00
|
|
|
|
|
|
|
// status
|
2020-08-09 15:27:48 -05:00
|
|
|
pub(crate) shutdown_requested: bool,
|
2021-04-06 06:16:35 -05:00
|
|
|
pub(crate) last_reported_status: Option<lsp_ext::ServerStatusParams>,
|
2023-03-25 12:06:06 -05:00
|
|
|
|
2023-05-26 08:09:19 -05:00
|
|
|
// proc macros
|
2023-03-25 12:06:06 -05:00
|
|
|
pub(crate) proc_macro_changed: bool,
|
2023-04-26 01:06:15 -05:00
|
|
|
pub(crate) proc_macro_clients: Arc<[anyhow::Result<ProcMacroServer>]>,
|
2021-04-05 12:49:00 -05:00
|
|
|
|
2023-05-26 08:09:19 -05:00
|
|
|
// Flycheck
|
2022-10-20 12:28:28 -05:00
|
|
|
pub(crate) flycheck: Arc<[FlycheckHandle]>,
|
2021-04-06 05:23:09 -05:00
|
|
|
pub(crate) flycheck_sender: Sender<flycheck::Message>,
|
|
|
|
pub(crate) flycheck_receiver: Receiver<flycheck::Message>,
|
|
|
|
|
2023-05-26 08:09:19 -05:00
|
|
|
// VFS
|
|
|
|
pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>,
|
2023-05-04 18:28:15 -05:00
|
|
|
pub(crate) vfs: Arc<RwLock<(vfs::Vfs, IntMap<FileId, LineEndings>)>>,
|
2021-04-06 05:23:09 -05:00
|
|
|
pub(crate) vfs_config_version: u32,
|
2021-04-06 06:16:35 -05:00
|
|
|
pub(crate) vfs_progress_config_version: u32,
|
2021-04-06 05:23:09 -05:00
|
|
|
pub(crate) vfs_progress_n_total: usize,
|
|
|
|
pub(crate) vfs_progress_n_done: usize,
|
|
|
|
|
2021-07-18 05:13:03 -05:00
|
|
|
/// `workspaces` field stores the data we actually use, while the `OpQueue`
|
|
|
|
/// stores the result of the last fetch.
|
2021-04-05 14:30:19 -05:00
|
|
|
///
|
2021-07-18 05:13:03 -05:00
|
|
|
/// If the fetch (partially) fails, we do not update the current value.
|
2021-07-18 03:29:22 -05:00
|
|
|
///
|
2021-07-18 05:13:03 -05:00
|
|
|
/// The handling of build data is subtle. We fetch workspace in two phases:
|
|
|
|
///
|
|
|
|
/// *First*, we run `cargo metadata`, which gives us fast results for
|
|
|
|
/// initial analysis.
|
|
|
|
///
|
|
|
|
/// *Second*, we run `cargo check` which runs build scripts and compiles
|
|
|
|
/// proc macros.
|
|
|
|
///
|
|
|
|
/// We need both for the precise analysis, but we want rust-analyzer to be
|
|
|
|
/// at least partially available just after the first phase. That's because
|
|
|
|
/// first phase is much faster, and is much less likely to fail.
|
|
|
|
///
|
|
|
|
/// This creates a complication -- by the time the second phase completes,
|
2023-03-28 04:32:51 -05:00
|
|
|
/// the results of the first phase could be invalid. That is, while we run
|
2021-07-18 05:13:03 -05:00
|
|
|
/// `cargo check`, the user edits `Cargo.toml`, we notice this, and the new
|
|
|
|
/// `cargo metadata` completes before `cargo check`.
|
|
|
|
///
|
|
|
|
/// An additional complication is that we want to avoid needless work. When
|
|
|
|
/// the user just adds comments or whitespace to Cargo.toml, we do not want
|
|
|
|
/// to invalidate any salsa caches.
|
2020-06-25 16:44:58 -05:00
|
|
|
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
|
2023-05-26 08:09:19 -05:00
|
|
|
|
|
|
|
// op queues
|
2023-03-26 01:39:28 -05:00
|
|
|
pub(crate) fetch_workspaces_queue: OpQueue<(), Option<Vec<anyhow::Result<ProjectWorkspace>>>>,
|
2021-07-18 05:13:03 -05:00
|
|
|
pub(crate) fetch_build_data_queue:
|
2023-03-26 01:39:28 -05:00
|
|
|
OpQueue<(), (Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>)>,
|
|
|
|
pub(crate) fetch_proc_macros_queue: OpQueue<Vec<ProcMacroPaths>, bool>,
|
|
|
|
pub(crate) prime_caches_queue: OpQueue,
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2019-06-01 02:31:40 -05:00
|
|
|
/// An immutable snapshot of the world's state at a point in time.
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) struct GlobalStateSnapshot {
|
2021-01-06 06:46:31 -06:00
|
|
|
pub(crate) config: Arc<Config>,
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) analysis: Analysis,
|
|
|
|
pub(crate) check_fixes: CheckFixes,
|
2021-07-26 12:16:47 -05:00
|
|
|
mem_docs: MemDocs,
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
|
2023-05-04 18:28:15 -05:00
|
|
|
vfs: Arc<RwLock<(vfs::Vfs, IntMap<FileId, LineEndings>)>>,
|
2020-06-26 04:44:46 -05:00
|
|
|
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
|
2023-03-26 01:39:28 -05:00
|
|
|
// used to signal semantic highlighting to fall back to syntax based highlighting until proc-macros have been loaded
|
2022-08-28 05:31:31 -05:00
|
|
|
pub(crate) proc_macros_loaded: bool,
|
2022-10-20 12:28:28 -05:00
|
|
|
pub(crate) flycheck: Arc<[FlycheckHandle]>,
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2021-08-22 09:41:09 -05:00
|
|
|
impl std::panic::UnwindSafe for GlobalStateSnapshot {}
|
|
|
|
|
2020-06-03 04:16:08 -05:00
|
|
|
impl GlobalState {
|
2020-06-25 17:54:41 -05:00
|
|
|
pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
|
2020-06-25 16:11:59 -05:00
|
|
|
let loader = {
|
2020-06-25 17:27:39 -05:00
|
|
|
let (sender, receiver) = unbounded::<vfs::loader::Message>();
|
2020-06-26 04:39:27 -05:00
|
|
|
let handle: vfs_notify::NotifyHandle =
|
|
|
|
vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
|
2020-06-25 17:27:39 -05:00
|
|
|
let handle = Box::new(handle) as Box<dyn vfs::loader::Handle>;
|
|
|
|
Handle { handle, receiver }
|
2020-06-11 04:04:09 -05:00
|
|
|
};
|
2020-06-25 08:35:42 -05:00
|
|
|
|
|
|
|
let task_pool = {
|
|
|
|
let (sender, receiver) = unbounded();
|
2022-12-09 07:11:46 -06:00
|
|
|
let handle = TaskPool::new_with_threads(sender, config.main_loop_num_threads());
|
2020-06-25 17:27:39 -05:00
|
|
|
Handle { handle, receiver }
|
2020-06-25 08:35:42 -05:00
|
|
|
};
|
|
|
|
|
2023-03-25 17:03:22 -05:00
|
|
|
let mut analysis_host = AnalysisHost::new(config.lru_parse_query_capacity());
|
|
|
|
if let Some(capacities) = config.lru_query_capacities() {
|
|
|
|
analysis_host.update_lru_capacities(capacities);
|
|
|
|
}
|
2020-07-15 07:37:44 -05:00
|
|
|
let (flycheck_sender, flycheck_receiver) = unbounded();
|
2021-06-03 09:11:20 -05:00
|
|
|
let mut this = GlobalState {
|
2020-06-25 10:14:11 -05:00
|
|
|
sender,
|
2020-06-26 10:07:14 -05:00
|
|
|
req_queue: ReqQueue::default(),
|
2020-06-25 08:35:42 -05:00
|
|
|
task_pool,
|
2020-06-11 04:04:09 -05:00
|
|
|
loader,
|
2021-06-03 09:11:20 -05:00
|
|
|
config: Arc::new(config.clone()),
|
2020-06-25 17:54:41 -05:00
|
|
|
analysis_host,
|
2020-01-31 12:23:25 -06:00
|
|
|
diagnostics: Default::default(),
|
2021-07-26 12:16:47 -05:00
|
|
|
mem_docs: MemDocs::default(),
|
2020-07-24 16:55:17 -05:00
|
|
|
semantic_tokens_cache: Arc::new(Default::default()),
|
2020-08-09 15:27:48 -05:00
|
|
|
shutdown_requested: false,
|
2021-04-06 06:16:35 -05:00
|
|
|
last_reported_status: None,
|
2020-06-25 16:11:59 -05:00
|
|
|
source_root_config: SourceRootConfig::default(),
|
2023-05-26 08:21:00 -05:00
|
|
|
config_errors: Default::default(),
|
2023-03-25 12:06:06 -05:00
|
|
|
|
|
|
|
proc_macro_changed: false,
|
2023-05-02 09:12:22 -05:00
|
|
|
// FIXME: use `Arc::from_iter` when it becomes available
|
|
|
|
proc_macro_clients: Arc::from(Vec::new()),
|
2021-04-05 14:30:19 -05:00
|
|
|
|
2023-05-02 09:12:22 -05:00
|
|
|
// FIXME: use `Arc::from_iter` when it becomes available
|
|
|
|
flycheck: Arc::from(Vec::new()),
|
2021-04-06 05:23:09 -05:00
|
|
|
flycheck_sender,
|
|
|
|
flycheck_receiver,
|
|
|
|
|
2023-05-04 18:28:15 -05:00
|
|
|
vfs: Arc::new(RwLock::new((vfs::Vfs::default(), IntMap::default()))),
|
2021-04-06 05:23:09 -05:00
|
|
|
vfs_config_version: 0,
|
2021-04-06 06:16:35 -05:00
|
|
|
vfs_progress_config_version: 0,
|
2021-04-06 05:23:09 -05:00
|
|
|
vfs_progress_n_total: 0,
|
|
|
|
vfs_progress_n_done: 0,
|
|
|
|
|
2020-06-25 16:11:59 -05:00
|
|
|
workspaces: Arc::new(Vec::new()),
|
2021-01-10 09:02:02 -06:00
|
|
|
fetch_workspaces_queue: OpQueue::default(),
|
2021-04-06 06:16:35 -05:00
|
|
|
fetch_build_data_queue: OpQueue::default(),
|
2023-03-26 01:39:28 -05:00
|
|
|
fetch_proc_macros_queue: OpQueue::default(),
|
|
|
|
|
|
|
|
prime_caches_queue: OpQueue::default(),
|
2021-06-03 09:11:20 -05:00
|
|
|
};
|
|
|
|
// Apply any required database inputs from the config.
|
|
|
|
this.update_configuration(config);
|
|
|
|
this
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:35:22 -05:00
|
|
|
pub(crate) fn process_changes(&mut self) -> bool {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("GlobalState::process_changes");
|
2022-08-16 12:13:10 -05:00
|
|
|
let mut workspace_structure_change = None;
|
2020-07-10 11:48:39 -05:00
|
|
|
|
2023-01-26 09:19:35 -06:00
|
|
|
let mut file_changes = FxHashMap::default();
|
2022-04-15 13:02:15 -05:00
|
|
|
let (change, changed_files) = {
|
2020-10-02 08:45:09 -05:00
|
|
|
let mut change = Change::new();
|
2020-06-11 04:04:09 -05:00
|
|
|
let (vfs, line_endings_map) = &mut *self.vfs.write();
|
2023-05-13 14:21:03 -05:00
|
|
|
let changed_files = vfs.take_changes();
|
2020-06-11 04:04:09 -05:00
|
|
|
if changed_files.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-26 09:19:35 -06:00
|
|
|
// We need to fix up the changed events a bit. If we have a create or modify for a file
|
|
|
|
// id that is followed by a delete we actually skip observing the file text from the
|
|
|
|
// earlier event, to avoid problems later on.
|
2023-05-13 14:21:03 -05:00
|
|
|
for changed_file in changed_files {
|
2022-09-15 14:19:57 -05:00
|
|
|
use vfs::ChangeKind::*;
|
|
|
|
|
2023-01-26 09:19:35 -06:00
|
|
|
file_changes
|
|
|
|
.entry(changed_file.file_id)
|
|
|
|
.and_modify(|(change, just_created)| {
|
|
|
|
// None -> Delete => keep
|
|
|
|
// Create -> Delete => collapse
|
|
|
|
//
|
|
|
|
match (change, just_created, changed_file.change_kind) {
|
|
|
|
// latter `Delete` wins
|
|
|
|
(change, _, Delete) => *change = Delete,
|
|
|
|
// merge `Create` with `Create` or `Modify`
|
|
|
|
(Create, _, Create | Modify) => {}
|
|
|
|
// collapse identical `Modify`es
|
|
|
|
(Modify, _, Modify) => {}
|
|
|
|
// equivalent to `Modify`
|
|
|
|
(change @ Delete, just_created, Create) => {
|
|
|
|
*change = Modify;
|
|
|
|
*just_created = true;
|
|
|
|
}
|
|
|
|
// shouldn't occur, but collapse into `Create`
|
|
|
|
(change @ Delete, just_created, Modify) => {
|
|
|
|
*change = Create;
|
|
|
|
*just_created = true;
|
|
|
|
}
|
|
|
|
// shouldn't occur, but collapse into `Modify`
|
|
|
|
(Modify, _, Create) => {}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.or_insert((
|
|
|
|
changed_file.change_kind,
|
|
|
|
matches!(changed_file.change_kind, Create),
|
|
|
|
));
|
2023-01-25 07:46:06 -06:00
|
|
|
}
|
2023-01-26 09:19:35 -06:00
|
|
|
|
2023-05-13 14:21:03 -05:00
|
|
|
let changed_files: Vec<_> = file_changes
|
|
|
|
.into_iter()
|
|
|
|
.filter(|(_, (change_kind, just_created))| {
|
|
|
|
!matches!((change_kind, just_created), (vfs::ChangeKind::Delete, true))
|
|
|
|
})
|
|
|
|
.map(|(file_id, (change_kind, _))| vfs::ChangedFile { file_id, change_kind })
|
|
|
|
.collect();
|
2023-01-26 09:19:35 -06:00
|
|
|
|
|
|
|
// A file was added or deleted
|
|
|
|
let mut has_structure_changes = false;
|
2022-04-15 13:02:15 -05:00
|
|
|
for file in &changed_files {
|
2021-09-10 11:34:47 -05:00
|
|
|
if let Some(path) = vfs.file_path(file.file_id).as_path() {
|
2021-09-13 12:58:09 -05:00
|
|
|
let path = path.to_path_buf();
|
|
|
|
if reload::should_refresh_for_change(&path, file.change_kind) {
|
2022-08-16 12:13:10 -05:00
|
|
|
workspace_structure_change = Some(path);
|
2021-09-13 12:58:09 -05:00
|
|
|
}
|
2021-09-10 11:34:47 -05:00
|
|
|
if file.is_created_or_deleted() {
|
2021-09-13 12:58:09 -05:00
|
|
|
has_structure_changes = true;
|
2020-07-10 11:48:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 04:49:36 -05:00
|
|
|
// Clear native diagnostics when their file gets deleted
|
2022-05-25 08:47:41 -05:00
|
|
|
if !file.exists() {
|
|
|
|
self.diagnostics.clear_native_for(file.file_id);
|
|
|
|
}
|
|
|
|
|
2020-06-11 04:04:09 -05:00
|
|
|
let text = if file.exists() {
|
|
|
|
let bytes = vfs.file_contents(file.file_id).to_vec();
|
2022-04-15 13:02:15 -05:00
|
|
|
String::from_utf8(bytes).ok().and_then(|text| {
|
|
|
|
let (text, line_endings) = LineEndings::normalize(text);
|
|
|
|
line_endings_map.insert(file.file_id, line_endings);
|
2023-04-22 02:48:37 -05:00
|
|
|
Some(Arc::from(text))
|
2022-04-15 13:02:15 -05:00
|
|
|
})
|
2020-06-11 04:04:09 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
change.change_file(file.file_id, text);
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
2021-09-13 12:58:09 -05:00
|
|
|
if has_structure_changes {
|
2021-06-12 22:54:16 -05:00
|
|
|
let roots = self.source_root_config.partition(vfs);
|
2020-07-10 11:48:39 -05:00
|
|
|
change.set_roots(roots);
|
|
|
|
}
|
2022-04-15 13:02:15 -05:00
|
|
|
(change, changed_files)
|
2020-06-11 04:04:09 -05:00
|
|
|
};
|
|
|
|
|
2018-10-25 02:57:55 -05:00
|
|
|
self.analysis_host.apply_change(change);
|
2022-04-15 13:02:15 -05:00
|
|
|
|
2022-08-05 05:06:31 -05:00
|
|
|
{
|
|
|
|
let raw_database = self.analysis_host.raw_database();
|
2022-08-16 12:13:10 -05:00
|
|
|
// FIXME: ideally we should only trigger a workspace fetch for non-library changes
|
2023-04-13 17:35:00 -05:00
|
|
|
// but something's going wrong with the source root business when we add a new local
|
2022-08-16 12:13:10 -05:00
|
|
|
// crate see https://github.com/rust-lang/rust-analyzer/issues/13029
|
|
|
|
if let Some(path) = workspace_structure_change {
|
2022-08-05 05:06:31 -05:00
|
|
|
self.fetch_workspaces_queue
|
2023-03-26 01:39:28 -05:00
|
|
|
.request_op(format!("workspace vfs file change: {}", path.display()), ());
|
2022-08-05 05:06:31 -05:00
|
|
|
}
|
|
|
|
self.proc_macro_changed =
|
|
|
|
changed_files.iter().filter(|file| !file.is_created_or_deleted()).any(|file| {
|
|
|
|
let crates = raw_database.relevant_crates(file.file_id);
|
|
|
|
let crate_graph = raw_database.crate_graph();
|
|
|
|
|
|
|
|
crates.iter().any(|&krate| crate_graph[krate].is_proc_macro)
|
|
|
|
});
|
|
|
|
}
|
2022-04-15 13:02:15 -05:00
|
|
|
|
2020-06-18 01:29:34 -05:00
|
|
|
true
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:35:22 -05:00
|
|
|
pub(crate) fn snapshot(&self) -> GlobalStateSnapshot {
|
2020-06-03 04:16:08 -05:00
|
|
|
GlobalStateSnapshot {
|
2021-01-06 06:46:31 -06:00
|
|
|
config: Arc::clone(&self.config),
|
2018-09-02 06:46:15 -05:00
|
|
|
workspaces: Arc::clone(&self.workspaces),
|
2018-09-10 04:57:40 -05:00
|
|
|
analysis: self.analysis_host.analysis(),
|
2018-12-19 06:04:15 -06:00
|
|
|
vfs: Arc::clone(&self.vfs),
|
2020-01-31 12:23:25 -06:00
|
|
|
check_fixes: Arc::clone(&self.diagnostics.check_fixes),
|
2020-07-21 13:07:42 -05:00
|
|
|
mem_docs: self.mem_docs.clone(),
|
2020-07-24 16:55:17 -05:00
|
|
|
semantic_tokens_cache: Arc::clone(&self.semantic_tokens_cache),
|
2023-03-26 01:39:28 -05:00
|
|
|
proc_macros_loaded: !self.config.expand_proc_macros()
|
|
|
|
|| *self.fetch_proc_macros_queue.last_op_result(),
|
2022-10-20 12:28:28 -05:00
|
|
|
flycheck: self.flycheck.clone(),
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 10:11:58 -06:00
|
|
|
|
2020-06-26 10:07:14 -05:00
|
|
|
pub(crate) fn send_request<R: lsp_types::request::Request>(
|
|
|
|
&mut self,
|
|
|
|
params: R::Params,
|
|
|
|
handler: ReqHandler,
|
|
|
|
) {
|
2020-06-26 10:43:57 -05:00
|
|
|
let request = self.req_queue.outgoing.register(R::METHOD.to_string(), params, handler);
|
2020-06-26 10:07:14 -05:00
|
|
|
self.send(request.into());
|
|
|
|
}
|
2022-04-11 07:38:30 -05:00
|
|
|
|
2020-06-26 10:07:14 -05:00
|
|
|
pub(crate) fn complete_request(&mut self, response: lsp_server::Response) {
|
2022-04-08 17:13:47 -05:00
|
|
|
let handler = self
|
|
|
|
.req_queue
|
|
|
|
.outgoing
|
|
|
|
.complete(response.id.clone())
|
|
|
|
.expect("received response for unknown request");
|
2020-06-26 10:07:14 -05:00
|
|
|
handler(self, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn send_notification<N: lsp_types::notification::Notification>(
|
2023-03-16 10:26:19 -05:00
|
|
|
&self,
|
2020-06-26 10:07:14 -05:00
|
|
|
params: N::Params,
|
|
|
|
) {
|
2020-06-26 10:17:38 -05:00
|
|
|
let not = lsp_server::Notification::new(N::METHOD.to_string(), params);
|
2020-06-26 10:07:14 -05:00
|
|
|
self.send(not.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn register_request(
|
|
|
|
&mut self,
|
|
|
|
request: &lsp_server::Request,
|
|
|
|
request_received: Instant,
|
|
|
|
) {
|
|
|
|
self.req_queue
|
|
|
|
.incoming
|
|
|
|
.register(request.id.clone(), (request.method.clone(), request_received));
|
2020-06-25 10:14:11 -05:00
|
|
|
}
|
2022-04-11 07:38:30 -05:00
|
|
|
|
2020-06-25 12:23:52 -05:00
|
|
|
pub(crate) fn respond(&mut self, response: lsp_server::Response) {
|
|
|
|
if let Some((method, start)) = self.req_queue.incoming.complete(response.id.clone()) {
|
2021-08-22 09:41:09 -05:00
|
|
|
if let Some(err) = &response.error {
|
|
|
|
if err.message.starts_with("server panicked") {
|
|
|
|
self.poke_rust_analyzer_developer(format!("{}, check the log", err.message))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:23:52 -05:00
|
|
|
let duration = start.elapsed();
|
2022-04-28 08:17:44 -05:00
|
|
|
tracing::debug!("handled {} - ({}) in {:0.2?}", method, response.id, duration);
|
2020-06-25 12:23:52 -05:00
|
|
|
self.send(response.into());
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 07:38:30 -05:00
|
|
|
|
2020-06-26 10:07:14 -05:00
|
|
|
pub(crate) fn cancel(&mut self, request_id: lsp_server::RequestId) {
|
|
|
|
if let Some(response) = self.req_queue.incoming.cancel(request_id) {
|
|
|
|
self.send(response.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:52:55 -05:00
|
|
|
pub(crate) fn is_completed(&self, request: &lsp_server::Request) -> bool {
|
|
|
|
self.req_queue.incoming.is_completed(&request.id)
|
|
|
|
}
|
|
|
|
|
2023-03-16 10:26:19 -05:00
|
|
|
fn send(&self, message: lsp_server::Message) {
|
2020-06-26 10:07:14 -05:00
|
|
|
self.sender.send(message).unwrap()
|
|
|
|
}
|
2020-06-25 10:14:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for GlobalState {
|
|
|
|
fn drop(&mut self) {
|
2022-04-11 07:38:30 -05:00
|
|
|
self.analysis_host.request_cancellation();
|
2020-06-25 10:14:11 -05:00
|
|
|
}
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2020-06-03 04:16:08 -05:00
|
|
|
impl GlobalStateSnapshot {
|
2020-06-11 04:04:09 -05:00
|
|
|
pub(crate) fn url_to_file_id(&self, url: &Url) -> Result<FileId> {
|
2020-06-26 05:02:59 -05:00
|
|
|
url_to_file_id(&self.vfs.read().0, url)
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 04:04:09 -05:00
|
|
|
pub(crate) fn file_id_to_url(&self, id: FileId) -> Url {
|
|
|
|
file_id_to_url(&self.vfs.read().0, id)
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
2018-12-21 03:18:14 -06:00
|
|
|
|
2021-05-17 12:07:10 -05:00
|
|
|
pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancellable<LineIndex> {
|
2021-02-12 15:44:28 -06:00
|
|
|
let endings = self.vfs.read().1[&file_id];
|
|
|
|
let index = self.analysis.file_line_index(file_id)?;
|
2022-10-25 06:43:26 -05:00
|
|
|
let res = LineIndex { index, endings, encoding: self.config.position_encoding() };
|
2021-02-12 15:44:28 -06:00
|
|
|
Ok(res)
|
2019-08-20 10:53:59 -05:00
|
|
|
}
|
|
|
|
|
2020-11-16 14:10:13 -06:00
|
|
|
pub(crate) fn url_file_version(&self, url: &Url) -> Option<i32> {
|
2021-06-12 22:54:16 -05:00
|
|
|
let path = from_proto::vfs_path(url).ok()?;
|
2020-11-16 14:10:13 -06:00
|
|
|
Some(self.mem_docs.get(&path)?.version)
|
2020-07-21 13:07:42 -05:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:01:15 -06:00
|
|
|
pub(crate) fn anchored_path(&self, path: &AnchoredPathBuf) -> Url {
|
|
|
|
let mut base = self.vfs.read().0.file_path(path.anchor);
|
2020-06-16 11:45:58 -05:00
|
|
|
base.pop();
|
2020-12-09 10:01:15 -06:00
|
|
|
let path = base.join(&path.path).unwrap();
|
2020-06-11 04:04:09 -05:00
|
|
|
let path = path.as_path().unwrap();
|
2021-06-12 22:54:16 -05:00
|
|
|
url_from_abs_path(path)
|
2018-12-21 03:18:14 -06:00
|
|
|
}
|
2019-01-22 15:15:03 -06:00
|
|
|
|
2022-10-20 12:28:28 -05:00
|
|
|
pub(crate) fn file_id_to_file_path(&self, file_id: FileId) -> vfs::VfsPath {
|
|
|
|
self.vfs.read().0.file_path(file_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn cargo_target_for_crate_root(
|
2020-06-17 10:51:46 -05:00
|
|
|
&self,
|
2022-10-20 12:28:28 -05:00
|
|
|
crate_id: CrateId,
|
2020-06-17 10:51:46 -05:00
|
|
|
) -> Option<(&CargoWorkspace, Target)> {
|
2022-10-20 12:28:28 -05:00
|
|
|
let file_id = self.analysis.crate_root(crate_id).ok()?;
|
2020-06-11 04:04:09 -05:00
|
|
|
let path = self.vfs.read().0.file_path(file_id);
|
|
|
|
let path = path.as_path()?;
|
2020-06-17 10:51:46 -05:00
|
|
|
self.workspaces.iter().find_map(|ws| match ws {
|
|
|
|
ProjectWorkspace::Cargo { cargo, .. } => {
|
2021-06-12 22:54:16 -05:00
|
|
|
cargo.target_by_root(path).map(|it| (cargo, it))
|
2020-06-17 10:51:46 -05:00
|
|
|
}
|
|
|
|
ProjectWorkspace::Json { .. } => None,
|
2021-05-23 12:56:54 -05:00
|
|
|
ProjectWorkspace::DetachedFiles { .. } => None,
|
2020-06-17 10:51:46 -05:00
|
|
|
})
|
|
|
|
}
|
2023-04-22 02:57:48 -05:00
|
|
|
|
|
|
|
pub(crate) fn vfs_memory_usage(&self) -> usize {
|
|
|
|
self.vfs.read().0.memory_usage()
|
|
|
|
}
|
2020-06-11 04:04:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {
|
|
|
|
let path = vfs.file_path(id);
|
|
|
|
let path = path.as_path().unwrap();
|
2021-06-12 22:54:16 -05:00
|
|
|
url_from_abs_path(path)
|
2020-06-11 04:04:09 -05:00
|
|
|
}
|
2020-06-26 05:02:59 -05:00
|
|
|
|
|
|
|
pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> Result<FileId> {
|
|
|
|
let path = from_proto::vfs_path(url)?;
|
2022-12-23 12:42:58 -06:00
|
|
|
let res = vfs.file_id(&path).ok_or_else(|| format!("file not found: {path}"))?;
|
2020-06-26 05:02:59 -05:00
|
|
|
Ok(res)
|
|
|
|
}
|